pub struct AsyncDevice(/* private fields */);
async_io
or async_tokio
only.Expand description
An async Tun/Tap device wrapper around a Tun/Tap device.
This type does not provide a split method, because this functionality can be achieved by instead wrapping the socket in an Arc.
§Streams
If you need to produce a Stream
, you can look at DeviceFramed
.
Note: DeviceFramed
is only available when the async_framed
feature is enabled.
Implementations§
Source§impl AsyncDevice
impl AsyncDevice
Sourcepub fn poll_readable(&self, cx: &mut Context<'_>) -> Poll<Result<()>>
pub fn poll_readable(&self, cx: &mut Context<'_>) -> Poll<Result<()>>
Polls the I/O handle for readability.
§Caveats
Note that on multiple calls to a poll_*
method in the recv
direction, only the
Waker
from the Context
passed to the most recent call will be scheduled to
receive a wakeup.
§Return value
The function returns:
Poll::Pending
if the device is not ready for reading.Poll::Ready(Ok(()))
if the device is ready for reading.Poll::Ready(Err(e))
if an error is encountered.
§Errors
This function may encounter any standard I/O error except WouldBlock
.
Sourcepub fn poll_recv(
&self,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<Result<usize>>
pub fn poll_recv( &self, cx: &mut Context<'_>, buf: &mut [u8], ) -> Poll<Result<usize>>
Attempts to receive a single packet from the device
§Caveats
Note that on multiple calls to a poll_*
method in the recv
direction, only the
Waker
from the Context
passed to the most recent call will be scheduled to
receive a wakeup.
§Return value
The function returns:
Poll::Pending
if the device is not ready to readPoll::Ready(Ok(()))
reads databuf
if the device is readyPoll::Ready(Err(e))
if an error is encountered.
§Errors
This function may encounter any standard I/O error except WouldBlock
.
Sourcepub fn poll_writable(&self, cx: &mut Context<'_>) -> Poll<Result<()>>
pub fn poll_writable(&self, cx: &mut Context<'_>) -> Poll<Result<()>>
Polls the I/O handle for writability.
§Caveats
Note that on multiple calls to a poll_*
method in the send direction,
only the Waker
from the Context
passed to the most recent call will
be scheduled to receive a wakeup.
§Return value
The function returns:
Poll::Pending
if the device is not ready for writing.Poll::Ready(Ok(()))
if the device is ready for writing.Poll::Ready(Err(e))
if an error is encountered.
§Errors
This function may encounter any standard I/O error except WouldBlock
.
Sourcepub fn poll_send(&self, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>>
pub fn poll_send(&self, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>>
Attempts to send packet to the device
§Caveats
Note that on multiple calls to a poll_*
method in the send direction,
only the Waker
from the Context
passed to the most recent call will
be scheduled to receive a wakeup.
§Return value
The function returns:
Poll::Pending
if the device is not available to writePoll::Ready(Ok(n))
n
is the number of bytes sentPoll::Ready(Err(e))
if an error is encountered.
§Errors
This function may encounter any standard I/O error except WouldBlock
.
Source§impl AsyncDevice
impl AsyncDevice
pub fn new(device: SyncDevice) -> Result<AsyncDevice>
Sourcepub unsafe fn from_fd(fd: RawFd) -> Result<AsyncDevice>
pub unsafe fn from_fd(fd: RawFd) -> Result<AsyncDevice>
§Safety
This method is safe if the provided fd is valid Construct a AsyncDevice from an existing file descriptor
pub fn into_fd(self) -> Result<RawFd>
Sourcepub async fn readable(&self) -> Result<()>
pub async fn readable(&self) -> Result<()>
Waits for the device to become readable.
This function is usually paired with try_recv()
.
The function may complete without the device being readable. This is a
false-positive and attempting a try_recv()
will return with
io::ErrorKind::WouldBlock
.
§Cancel safety
This method is cancel safe. Once a readiness event occurs, the method
will continue to return immediately until the readiness event is
consumed by an attempt to read that fails with WouldBlock
or
Poll::Pending
.
Sourcepub async fn writable(&self) -> Result<()>
pub async fn writable(&self) -> Result<()>
Waits for the device to become writable.
This function is usually paired with try_send()
.
The function may complete without the device being writable. This is a
false-positive and attempting a try_send()
will return with
io::ErrorKind::WouldBlock
.
§Cancel safety
This method is cancel safe. Once a readiness event occurs, the method
will continue to return immediately until the readiness event is
consumed by an attempt to write that fails with WouldBlock
or
Poll::Pending
.
Sourcepub async fn recv(&self, buf: &mut [u8]) -> Result<usize>
pub async fn recv(&self, buf: &mut [u8]) -> Result<usize>
Receives a single packet from the device. On success, returns the number of bytes read.
The function must be called with valid byte array buf
of sufficient
size to hold the message bytes. If a message is too long to fit in the
supplied buffer, excess bytes may be discarded.
Sourcepub fn try_recv(&self, buf: &mut [u8]) -> Result<usize>
pub fn try_recv(&self, buf: &mut [u8]) -> Result<usize>
Tries to receive a single packet from the device. On success, returns the number of bytes read.
This method must be called with valid byte array buf
of sufficient size
to hold the message bytes. If a message is too long to fit in the
supplied buffer, excess bytes may be discarded.
When there is no pending data, Err(io::ErrorKind::WouldBlock)
is
returned. This function is usually paired with readable()
.
Sourcepub async fn send(&self, buf: &[u8]) -> Result<usize>
pub async fn send(&self, buf: &[u8]) -> Result<usize>
Send a packet to the device
§Return
On success, the number of bytes sent is returned, otherwise, the encountered error is returned.
Sourcepub fn try_send(&self, buf: &[u8]) -> Result<usize>
pub fn try_send(&self, buf: &[u8]) -> Result<usize>
Tries to send packet to the device.
When the device buffer is full, Err(io::ErrorKind::WouldBlock)
is
returned. This function is usually paired with writable()
.
§Returns
If successful, Ok(n)
is returned, where n
is the number of bytes
sent. If the device is not ready to send data,
Err(ErrorKind::WouldBlock)
is returned.
Sourcepub async fn recv_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
pub async fn recv_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
Receives a packet into multiple buffers (scatter read). Processes single packet per call.
Sourcepub fn try_recv_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
pub fn try_recv_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
Non-blocking version of recv_vectored
.
Sourcepub async fn send_vectored(&self, bufs: &[IoSlice<'_>]) -> Result<usize>
pub async fn send_vectored(&self, bufs: &[IoSlice<'_>]) -> Result<usize>
Sends multiple buffers as a single packet (gather write).
Sourcepub fn try_send_vectored(&self, bufs: &[IoSlice<'_>]) -> Result<usize>
pub fn try_send_vectored(&self, bufs: &[IoSlice<'_>]) -> Result<usize>
Non-blocking version of send_vectored
.
Source§impl AsyncDevice
impl AsyncDevice
Sourcepub async fn recv_multiple<B: AsRef<[u8]> + AsMut<[u8]>>(
&self,
original_buffer: &mut [u8],
bufs: &mut [B],
sizes: &mut [usize],
offset: usize,
) -> Result<usize>
pub async fn recv_multiple<B: AsRef<[u8]> + AsMut<[u8]>>( &self, original_buffer: &mut [u8], bufs: &mut [B], sizes: &mut [usize], offset: usize, ) -> Result<usize>
Recv a packet from the device. If offload is enabled. This method can be used to obtain processed data.
original_buffer is used to store raw data, including the VirtioNetHdr and the unsplit IP packet. The recommended size is 10 + 65535. bufs and sizes are used to store the segmented IP packets. bufs.len == sizes.len > 65535/MTU offset: Starting position
Sourcepub async fn send_multiple<B: ExpandBuffer>(
&self,
gro_table: &mut GROTable,
bufs: &mut [B],
offset: usize,
) -> Result<usize>
pub async fn send_multiple<B: ExpandBuffer>( &self, gro_table: &mut GROTable, bufs: &mut [B], offset: usize, ) -> Result<usize>
send multiple fragmented data packets. GROTable can be reused, as it is used to assist in data merging. Offset is the starting position of the data. Need to meet offset>10.
Methods from Deref<Target = DeviceImpl>§
Sourcepub fn if_index(&self) -> Result<u32>
pub fn if_index(&self) -> Result<u32>
Retrieves the interface index for the network interface.
This function converts the interface name (obtained via self.name()
) into a
C-compatible string (CString) and then calls the libc function if_nametoindex
to retrieve the corresponding interface index.
Sourcepub fn addresses(&self) -> Result<Vec<IpAddr>>
pub fn addresses(&self) -> Result<Vec<IpAddr>>
Retrieves all IP addresses associated with the network interface.
This function calls getifaddrs
with the interface name,
then iterates over the returned list of interface addresses, extracting and collecting
the IP addresses into a vector.
Sourcepub fn udp_gso(&self) -> bool
pub fn udp_gso(&self) -> bool
Returns whether UDP Generic Segmentation Offload (GSO) is enabled.
This is determined by the udp_gso
flag in the device.
Sourcepub fn tcp_gso(&self) -> bool
pub fn tcp_gso(&self) -> bool
Returns whether TCP Generic Segmentation Offload (GSO) is enabled.
In this implementation, this is represented by the vnet_hdr
flag.
Sourcepub fn set_tx_queue_len(&self, tx_queue_len: u32) -> Result<()>
pub fn set_tx_queue_len(&self, tx_queue_len: u32) -> Result<()>
Sets the transmit queue length for the network interface.
This method constructs an interface request (ifreq
) structure,
assigns the desired transmit queue length to the ifru_metric
field,
and calls the change_tx_queue_len
function using the control file descriptor.
If the underlying operation fails, an I/O error is returned.
Sourcepub fn tx_queue_len(&self) -> Result<u32>
pub fn tx_queue_len(&self) -> Result<u32>
Retrieves the current transmit queue length for the network interface.
This function constructs an interface request structure and calls tx_queue_len
to populate it with the current transmit queue length. The value is then returned.
Sourcepub fn send_multiple<B: ExpandBuffer>(
&self,
gro_table: &mut GROTable,
bufs: &mut [B],
offset: usize,
) -> Result<usize>
pub fn send_multiple<B: ExpandBuffer>( &self, gro_table: &mut GROTable, bufs: &mut [B], offset: usize, ) -> Result<usize>
send multiple fragmented data packets. GROTable can be reused, as it is used to assist in data merging. Offset is the starting position of the data. Need to meet offset>=10.
Sourcepub fn recv_multiple<B: AsRef<[u8]> + AsMut<[u8]>>(
&self,
original_buffer: &mut [u8],
bufs: &mut [B],
sizes: &mut [usize],
offset: usize,
) -> Result<usize>
pub fn recv_multiple<B: AsRef<[u8]> + AsMut<[u8]>>( &self, original_buffer: &mut [u8], bufs: &mut [B], sizes: &mut [usize], offset: usize, ) -> Result<usize>
Recv a packet from tun device. If offload is enabled. This method can be used to obtain processed data.
original_buffer is used to store raw data, including the VirtioNetHdr and the unsplit IP packet. The recommended size is 10 + 65535. bufs and sizes are used to store the segmented IP packets. bufs.len == sizes.len > 65535/MTU offset: Starting position
pub fn remove_address_v6(&self, addr: Ipv6Addr, prefix: u8) -> Result<()>
Sourcepub fn set_name(&self, value: &str) -> Result<()>
pub fn set_name(&self, value: &str) -> Result<()>
Sets a new name for the network interface.
This function converts the provided name into a C-compatible string,
checks that its length does not exceed the maximum allowed (IFNAMSIZ),
and then copies it into an interface request structure. It then uses a system call
(via siocsifname
) to apply the new name.
Sourcepub fn is_running(&self) -> Result<bool>
pub fn is_running(&self) -> Result<bool>
Checks whether the network interface is currently running.
The interface is considered running if both the IFF_UP and IFF_RUNNING flags are set.
Sourcepub fn enabled(&self, value: bool) -> Result<()>
pub fn enabled(&self, value: bool) -> Result<()>
Enables or disables the network interface.
If value
is true, the interface is enabled by setting the IFF_UP and IFF_RUNNING flags.
If false, the IFF_UP flag is cleared. The change is applied using a system call.
Sourcepub fn broadcast(&self) -> Result<IpAddr>
pub fn broadcast(&self) -> Result<IpAddr>
Retrieves the broadcast address of the network interface.
This function populates an interface request with the broadcast address via a system call, converts it into a sockaddr structure, and then extracts the IP address.
Sourcepub fn set_broadcast(&self, value: IpAddr) -> Result<()>
pub fn set_broadcast(&self, value: IpAddr) -> Result<()>
Sets the broadcast address of the network interface.
This function converts the given IP address into a sockaddr structure (with a specified overwrite size) and then applies it to the interface via a system call.
Sourcepub fn set_network_address<IPv4: ToIpv4Address, Netmask: ToIpv4Netmask>(
&self,
address: IPv4,
netmask: Netmask,
destination: Option<IPv4>,
) -> Result<()>
pub fn set_network_address<IPv4: ToIpv4Address, Netmask: ToIpv4Netmask>( &self, address: IPv4, netmask: Netmask, destination: Option<IPv4>, ) -> Result<()>
Sets the IPv4 network address, netmask, and an optional destination address.
This function sets the interface’s address, netmask, and if provided, the destination address.
It calls the helper methods set_address_v4
, set_netmask
, and set_destination
respectively.
Sourcepub fn remove_address(&self, addr: IpAddr) -> Result<()>
pub fn remove_address(&self, addr: IpAddr) -> Result<()>
Removes an IP address from the interface.
For IPv4 addresses, it iterates over the current addresses and if a match is found,
resets the address to 0.0.0.0
(unspecified).
For IPv6 addresses, it retrieves the interface addresses by name and removes the matching address,
taking into account its prefix length.
Sourcepub fn add_address_v6<IPv6: ToIpv6Address, Netmask: ToIpv6Netmask>(
&self,
addr: IPv6,
netmask: Netmask,
) -> Result<()>
pub fn add_address_v6<IPv6: ToIpv6Address, Netmask: ToIpv6Netmask>( &self, addr: IPv6, netmask: Netmask, ) -> Result<()>
Adds an IPv6 address to the interface.
This function creates an in6_ifreq
structure, fills in the interface index,
prefix length, and IPv6 address (converted into a sockaddr structure),
and then applies it using a system call.
Sourcepub fn mtu(&self) -> Result<u16>
pub fn mtu(&self) -> Result<u16>
Retrieves the current MTU (Maximum Transmission Unit) for the interface.
This function constructs an interface request and uses a system call (via siocgifmtu
)
to obtain the MTU. The result is then converted to a u16.
Sourcepub fn set_mtu(&self, value: u16) -> Result<()>
pub fn set_mtu(&self, value: u16) -> Result<()>
Sets the MTU (Maximum Transmission Unit) for the interface.
This function creates an interface request, sets the ifru_mtu
field to the new value,
and then applies it via a system call.
Sourcepub fn set_mac_address(&self, eth_addr: [u8; 6]) -> Result<()>
pub fn set_mac_address(&self, eth_addr: [u8; 6]) -> Result<()>
Sets the MAC (hardware) address for the interface.
This function constructs an interface request and copies the provided MAC address into the hardware address field. It then applies the change via a system call. This operation is typically supported only for TAP devices.
Sourcepub fn mac_address(&self) -> Result<[u8; 6]>
pub fn mac_address(&self) -> Result<[u8; 6]>
Retrieves the MAC (hardware) address of the interface.
This function queries the MAC address by the interface name using a helper function. An error is returned if the MAC address cannot be found.
Trait Implementations§
Source§impl AsRawFd for AsyncDevice
impl AsRawFd for AsyncDevice
Source§impl Deref for AsyncDevice
impl Deref for AsyncDevice
Source§impl FromRawFd for AsyncDevice
impl FromRawFd for AsyncDevice
Source§unsafe fn from_raw_fd(fd: RawFd) -> Self
unsafe fn from_raw_fd(fd: RawFd) -> Self
Self
from the given raw file
descriptor. Read more