Skip to main content

NetBackend

Trait NetBackend 

Source
pub trait NetBackend: Send + Sync {
    // Required methods
    fn send(&mut self, packet: &NetPacket) -> Result<usize, Error>;
    fn recv(&mut self, buf: &mut [u8]) -> Result<usize, Error>;
    fn has_data(&self) -> bool;

    // Provided methods
    fn send_tso(&mut self, packet: &NetPacket) -> Result<usize, Error> { ... }
    fn supports_tso(&self) -> bool { ... }
    fn configure_offload(&mut self, flags: NetOffloadFlags) -> Result<(), Error> { ... }
    fn set_vnet_hdr_sz(&mut self, size: u32) -> Result<(), Error> { ... }
}
Expand description

Network backend trait.

Required Methods§

Source

fn send(&mut self, packet: &NetPacket) -> Result<usize, Error>

Sends a packet.

Source

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

Receives a packet.

Source

fn has_data(&self) -> bool

Returns whether packets are available to receive.

Provided Methods§

Source

fn send_tso(&mut self, packet: &NetPacket) -> Result<usize, Error>

Sends a TSO/GSO packet.

Called when the guest emits a packet with gso_type != GSO_NONE. The packet contains a large payload that the guest expects the device to segment (or relay as-is if the host stack handles it).

The default implementation ignores the GSO header and forwards the packet via send(). Backends that can exploit TSO (e.g. writing directly to a host TcpStream) should override this.

Source

fn supports_tso(&self) -> bool

Returns whether this backend supports TSO offload.

When true, the device advertises GUEST_TSO4/6 and HOST_TSO4/6 features to the guest, and routes TSO packets through send_tso().

Source

fn configure_offload(&mut self, flags: NetOffloadFlags) -> Result<(), Error>

Configure host-side offload to match the features the guest negotiated.

Call from the device’s activate() hook after ack_features. The flags come from the guest’s acknowledged GUEST_* feature bits — the backend is free to translate (e.g. TAP translates to TUN_F_*) or ignore. Default is no-op so non-kernel backends don’t need to care.

Source

fn set_vnet_hdr_sz(&mut self, size: u32) -> Result<(), Error>

Configure the size of the virtio_net_hdr prepended to each frame on the backend’s wire representation.

For TAP, this must match the guest’s view of the header (sizeof(virtio_net_hdr_v1) = 12 bytes when MRG_RXBUF or any of the modern flags are negotiated; 10 bytes on legacy). Default no-op.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§