pub struct TunTap { /* private fields */ }Expand description
A TUN (L3) or TAP (L2) tunnel device.
Implementations§
Source§impl TunTap
impl TunTap
Sourcepub fn create(
name: &str,
kind: DeviceKind,
want_vnet_hdr: bool,
) -> Result<TunTap, DeviceError>
pub fn create( name: &str, kind: DeviceKind, want_vnet_hdr: bool, ) -> Result<TunTap, DeviceError>
Create a tunnel device of kind named name. Requires CAP_NET_ADMIN.
When want_vnet_hdr is set, first attempts to open with IFF_VNET_HDR
framing and kernel GSO/GRO offload (TUNSETOFFLOAD) enabled. If the
kernel or driver doesn’t support one of the two, this falls back
transparently to a plain device (no vnet_hdr framing) with a fresh fd
— see TunTap::vnet_hdr_len to check which mode was actually
negotiated.
Sourcepub fn create_multi_queue(
name: &str,
kind: DeviceKind,
queue_count: usize,
want_vnet_hdr: bool,
) -> Result<Vec<TunTap>, DeviceError>
pub fn create_multi_queue( name: &str, kind: DeviceKind, queue_count: usize, want_vnet_hdr: bool, ) -> Result<Vec<TunTap>, DeviceError>
Create a multi-queue TUN/TAP device with queue_count independent queue fds.
When queue_count == 1, delegates transparently to TunTap::create.
When queue_count > 1, opens /dev/net/tun queue_count times with
IFF_MULTI_QUEUE set, returning one TunTap per queue.
All returned queue fds are placed in non-blocking mode (O_NONBLOCK).
If want_vnet_hdr is requested, IFF_VNET_HDR and kernel offload
(TUNSETOFFLOAD) are negotiated across all queues, falling back
transparently to plain queues if unsupported.
Sourcepub fn vnet_hdr_len(&self) -> Option<usize>
pub fn vnet_hdr_len(&self) -> Option<usize>
The virtio_net_hdr prefix length (bytes) on every TUN read/write
when kernel GSO/GRO offload is active, or None for a plain device
(see TunTap::create’s want_vnet_hdr parameter).
Sourcepub fn as_raw_fd(&self) -> RawFd
pub fn as_raw_fd(&self) -> RawFd
Return the raw file descriptor for this TUN/TAP device.
The fd is bidirectional: read pulls frames injected by the kernel
(egress path for the tunnel), and write injects frames into the
kernel network stack (ingress path for the tunnel).
The fd is valid as long as this TunTap value lives.
Sourcepub fn set_nonblocking(&self) -> Result<(), DeviceError>
pub fn set_nonblocking(&self) -> Result<(), DeviceError>
Set the TUN/TAP fd non-blocking via fcntl(F_SETFL, O_NONBLOCK).
Required before passing the fd to an epoll-based event loop that
drains with MSG_DONTWAIT / non-blocking read.