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 coalescingudp_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_SIZEflows) - 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§
Trait Implementations§
Auto Trait Implementations§
impl Freeze for GROTable
impl RefUnwindSafe for GROTable
impl Send for GROTable
impl Sync for GROTable
impl Unpin for GROTable
impl UnwindSafe for GROTable
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more