Skip to main content

GROTable

Struct GROTable 

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

Generic Receive Offload (GRO) table for managing packet coalescing.

This structure maintains the state needed to coalesce multiple received packets into larger segments, reducing per-packet processing overhead. It combines both TCP and UDP GRO capabilities.

§Purpose

When receiving many small packets of the same flow, GRO can combine them into fewer, larger packets. This provides significant performance benefits:

  • Reduces the number of packets passed to the application
  • Fewer context switches and system calls
  • Better cache utilization
  • Lower CPU usage per gigabit of traffic

§Usage

Create a GROTable and reuse it across multiple recv_multiple calls:

use tun_rs::{DeviceBuilder, GROTable, IDEAL_BATCH_SIZE, VIRTIO_NET_HDR_LEN};

let dev = DeviceBuilder::new()
    .offload(true)
    .ipv4("10.0.0.1", 24, None)
    .build_sync()?;

let mut gro_table = GROTable::default();
let mut original_buffer = vec![0; VIRTIO_NET_HDR_LEN + 65535];
let mut bufs = vec![vec![0u8; 1500]; IDEAL_BATCH_SIZE];
let mut sizes = vec![0; IDEAL_BATCH_SIZE];

loop {
    let num = dev.recv_multiple(&mut original_buffer, &mut bufs, &mut sizes, 0)?;

    // GRO table is automatically used by recv_multiple
    // to coalesce packets
    for i in 0..num {
        println!("Packet: {} bytes", sizes[i]);
    }
}

§Fields

  • tcp_gro_table: State for TCP packet coalescing
  • udp_gro_table: State for UDP packet coalescing (if supported by kernel)
  • to_write: Internal buffer tracking which packets to emit

§Performance

The GRO table maintains internal state across calls, including:

  • Hash map of active flows (preallocated for IDEAL_BATCH_SIZE flows)
  • Memory pools to reduce allocations
  • Per-flow coalescing state

Typical coalescing ratios:

  • TCP bulk transfers: 5-20 packets coalesced into 1
  • UDP: 2-5 packets coalesced into 1
  • Interactive traffic: minimal coalescing (preserves latency)

§Thread Safety

GROTable is not thread-safe. Use one instance per thread or protect with a mutex.

Implementations§

Source§

impl GROTable

Source

pub fn new() -> GROTable

Available on Linux and non-target_env=ohos only.

Trait Implementations§

Source§

impl Default for GROTable

Available on Linux and non-target_env=ohos only.
Source§

fn default() -> GROTable

Returns the “default value” for a type. Read more

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.