| 1 |
use std::{ |
| 2 |
io::{self, IoSliceMut}, |
| 3 |
sync::Mutex, |
| 4 |
time::Instant, |
| 5 |
}; |
| 6 |
|
| 7 |
use super::{IO_ERROR_LOG_INTERVAL, RecvMeta, Transmit, UdpSockRef, log_sendmsg_error}; |
| 8 |
|
| 9 |
/// Fallback UDP socket interface that stubs out all special functionality |
| 10 |
/// |
| 11 |
/// Used when a better implementation is not available for a particular target, at the cost of |
| 12 |
/// reduced performance compared to that enabled by some target-specific interfaces. |
| 13 |
#[derive(Debug)] |
| 14 |
pub struct UdpSocketState { |
| 15 |
last_send_error: Mutex<Instant>, |
| 16 |
} |
| 17 |
|
| 18 |
impl UdpSocketState { |
| 19 |
pub fn new(socket: UdpSockRef<'_>) -> io::Result<Self> { |
| 20 |
socket.0.set_nonblocking(true)?; |
| 21 |
let now = Instant::now(); |
| 22 |
Ok(Self { |
| 23 |
last_send_error: Mutex::new(now.checked_sub(2 * IO_ERROR_LOG_INTERVAL).unwrap_or(now)), |
| 24 |
}) |
| 25 |
} |
| 26 |
|
| 27 |
/// Sends a [`Transmit`] on the given socket. |
| 28 |
/// |
| 29 |
/// This function will only ever return errors of kind [`io::ErrorKind::WouldBlock`]. |
| 30 |
/// All other errors will be logged and converted to `Ok`. |
| 31 |
/// |
| 32 |
/// UDP transmission errors are considered non-fatal because higher-level protocols must |
| 33 |
/// employ retransmits and timeouts anyway in order to deal with UDP's unreliable nature. |
| 34 |
/// Thus, logging is most likely the only thing you can do with these errors. |
| 35 |
/// |
| 36 |
/// If you would like to handle these errors yourself, use [`UdpSocketState::try_send`] |
| 37 |
/// instead. |
| 38 |
pub fn send(&self, socket: UdpSockRef<'_>, transmit: &Transmit<'_>) -> io::Result<()> { |
| 39 |
match send(socket, transmit) { |
| 40 |
Ok(()) => Ok(()), |
| 41 |
Err(e) if e.kind() == io::ErrorKind::WouldBlock => Err(e), |
| 42 |
Err(e) => { |
| 43 |
log_sendmsg_error(&self.last_send_error, e, transmit); |
| 44 |
|
| 45 |
Ok(()) |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/// Sends a [`Transmit`] on the given socket without any additional error handling. |
| 51 |
pub fn try_send(&self, socket: UdpSockRef<'_>, transmit: &Transmit<'_>) -> io::Result<()> { |
| 52 |
send(socket, transmit) |
| 53 |
} |
| 54 |
|
| 55 |
pub fn recv( |
| 56 |
&self, |
| 57 |
socket: UdpSockRef<'_>, |
| 58 |
bufs: &mut [IoSliceMut<'_>], |
| 59 |
meta: &mut [RecvMeta], |
| 60 |
) -> io::Result<usize> { |
| 61 |
// Safety: both `IoSliceMut` and `MaybeUninitSlice` promise to have the |
| 62 |
// same layout, that of `iovec`/`WSABUF`. Furthermore `recv_vectored` |
| 63 |
// promises to not write unitialised bytes to the `bufs` and pass it |
| 64 |
// directly to the `recvmsg` system call, so this is safe. |
| 65 |
let bufs = unsafe { |
| 66 |
&mut *(bufs as *mut [IoSliceMut<'_>] as *mut [socket2::MaybeUninitSlice<'_>]) |
| 67 |
}; |
| 68 |
let (len, _flags, addr) = socket.0.recv_from_vectored(bufs)?; |
| 69 |
meta[0] = RecvMeta { |
| 70 |
len, |
| 71 |
stride: len, |
| 72 |
addr: addr.as_socket().unwrap(), |
| 73 |
ecn: None, |
| 74 |
dst_ip: None, |
| 75 |
}; |
| 76 |
Ok(1) |
| 77 |
} |
| 78 |
|
| 79 |
#[inline] |
| 80 |
pub fn max_gso_segments(&self) -> usize { |
| 81 |
1 |
| 82 |
} |
| 83 |
|
| 84 |
#[inline] |
| 85 |
pub fn gro_segments(&self) -> usize { |
| 86 |
1 |
| 87 |
} |
| 88 |
|
| 89 |
/// Resize the send buffer of `socket` to `bytes` |
| 90 |
#[inline] |
| 91 |
pub fn set_send_buffer_size(&self, socket: UdpSockRef<'_>, bytes: usize) -> io::Result<()> { |
| 92 |
socket.0.set_send_buffer_size(bytes) |
| 93 |
} |
| 94 |
|
| 95 |
/// Resize the receive buffer of `socket` to `bytes` |
| 96 |
#[inline] |
| 97 |
pub fn set_recv_buffer_size(&self, socket: UdpSockRef<'_>, bytes: usize) -> io::Result<()> { |
| 98 |
socket.0.set_recv_buffer_size(bytes) |
| 99 |
} |
| 100 |
|
| 101 |
/// Get the size of the `socket` send buffer |
| 102 |
#[inline] |
| 103 |
pub fn send_buffer_size(&self, socket: UdpSockRef<'_>) -> io::Result<usize> { |
| 104 |
socket.0.send_buffer_size() |
| 105 |
} |
| 106 |
|
| 107 |
/// Get the size of the `socket` receive buffer |
| 108 |
#[inline] |
| 109 |
pub fn recv_buffer_size(&self, socket: UdpSockRef<'_>) -> io::Result<usize> { |
| 110 |
socket.0.recv_buffer_size() |
| 111 |
} |
| 112 |
|
| 113 |
#[inline] |
| 114 |
pub fn may_fragment(&self) -> bool { |
| 115 |
true |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
fn send(socket: UdpSockRef<'_>, transmit: &Transmit<'_>) -> io::Result<()> { |
| 120 |
socket.0.send_to( |
| 121 |
transmit.contents, |
| 122 |
&socket2::SockAddr::from(transmit.destination), |
| 123 |
) |
| 124 |
} |
| 125 |
|
| 126 |
pub(crate) const BATCH_SIZE: usize = 1; |