Skip to main content

TunTap

Struct TunTap 

Source
pub struct TunTap { /* private fields */ }
Expand description

A TUN (L3) or TAP (L2) tunnel device.

Implementations§

Source§

impl TunTap

Source

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.

Source

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.

Source

pub fn name(&self) -> &str

The interface name.

Source

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).

Source

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.

Source

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.

Source

pub fn set_mtu(&self, mtu: u32) -> Result<(), DeviceError>

Set the MTU of this device. Requires CAP_NET_ADMIN.

Source§

impl TunTap

Source

pub fn split(self) -> Result<(TunReader, TunWriter), DeviceError>

Split into independent reader/writer halves backed by duplicated fds, so one thread can read while another writes the same device.

Trait Implementations§

Source§

impl AsRawFd for TunTap

Source§

fn as_raw_fd(&self) -> RawFd

Extracts the raw file descriptor. Read more
Source§

impl Device for TunTap

Source§

fn kind(&self) -> DeviceKind

Whether this is an L3 (TUN) or L2 (TAP) device.
Source§

fn read_frame(&mut self, buf: &mut [u8]) -> Result<usize>

Read one inner frame into buf, returning its length.
Source§

fn write_frame(&mut self, frame: &[u8]) -> Result<usize>

Write one inner frame, returning the number of bytes written.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.