pub trait NicDevice<H: IxgbeHal> {
// Required methods
fn get_driver_name(&self) -> &str;
fn get_mac_addr(&self) -> [u8; 6];
fn reset_stats(&mut self);
fn get_link_speed(&self) -> u16;
fn recycle_tx_buffers(&mut self, queue_id: u16) -> IxgbeResult;
fn receive_packets<F>(
&mut self,
queue_id: u16,
packet_nums: usize,
f: F,
) -> IxgbeResult<usize>
where F: FnMut(IxgbeNetBuf);
fn send(&mut self, queue_id: u16, tx_buf: IxgbeNetBuf) -> IxgbeResult;
fn can_receive(&self, queue_id: u16) -> IxgbeResult<bool>;
fn can_send(&self, queue_id: u16) -> IxgbeResult<bool>;
}Expand description
Generic network device interface.
This trait provides a common interface for network device drivers, allowing protocol stacks to be implemented independently of the specific NIC hardware. It is inspired by the ixy driver approach and provides methods for:
- Device identification and configuration
- Packet transmission and reception
- Queue management
- Statistics tracking
§Example
use ixgbe_driver::{IxgbeDevice, NicDevice};
let mut device = IxgbeDevice::<MyHal, 512>::init(...)?;
// Check device info
println!("Driver: {}", device.get_driver_name());
println!("MAC: {:02x?}", device.get_mac_addr());
println!("Speed: {} Mbit/s", device.get_link_speed());
// Send and receive
while device.can_receive(0)? {
device.receive_packets(0, 32, |packet| {
// Process packet
})?;
}Required Methods§
Sourcefn get_driver_name(&self) -> &str
fn get_driver_name(&self) -> &str
Returns the driver’s name.
This is a simple string identifier such as “ixgbe” or “virtio”.
Sourcefn get_mac_addr(&self) -> [u8; 6]
fn get_mac_addr(&self) -> [u8; 6]
Returns the MAC (Ethernet) address of this device.
Returns a 6-byte array representing the layer 2 address.
Sourcefn reset_stats(&mut self)
fn reset_stats(&mut self)
Resets the network card’s statistics registers.
Clears all packet and byte counters maintained by the hardware. This affects the values returned by subsequent statistics reads.
Sourcefn get_link_speed(&self) -> u16
fn get_link_speed(&self) -> u16
Returns the link speed of the network card.
Returns the current link speed in Mbit/s (e.g., 10000 for 10GbE). Returns 0 if the link is down.
Sourcefn recycle_tx_buffers(&mut self, queue_id: u16) -> IxgbeResult
fn recycle_tx_buffers(&mut self, queue_id: u16) -> IxgbeResult
Polls the transmit queue for completed packets and frees their buffers.
This method reclaims transmit descriptors that have been completed by the hardware and returns their packet buffers to the memory pool.
§Arguments
queue_id- The transmit queue ID to clean
§Errors
Returns IxgbeError::InvalidQueue if queue_id is out of range.
Sourcefn receive_packets<F>(
&mut self,
queue_id: u16,
packet_nums: usize,
f: F,
) -> IxgbeResult<usize>where
F: FnMut(IxgbeNetBuf),
fn receive_packets<F>(
&mut self,
queue_id: u16,
packet_nums: usize,
f: F,
) -> IxgbeResult<usize>where
F: FnMut(IxgbeNetBuf),
Receives up to packet_nums packets from the network.
This method receives packets from the specified queue and invokes the
closure f for each received packet. Using a closure avoids dynamic
memory allocation and allows the caller to handle packets efficiently.
§Arguments
queue_id- The receive queue ID to read frompacket_nums- Maximum number of packets to receivef- Closure called for each received packet with theIxgbeNetBuf
§Returns
Returns the number of packets actually received.
§Errors
IxgbeError::NotReady- No packets are currently availableIxgbeError::InvalidQueue-queue_idis out of range
§Example
let count = device.receive_packets(0, 32, |packet| {
let data = packet.packet();
// Handle packet data
})?;
println!("Received {} packets", count);Sourcefn send(&mut self, queue_id: u16, tx_buf: IxgbeNetBuf) -> IxgbeResult
fn send(&mut self, queue_id: u16, tx_buf: IxgbeNetBuf) -> IxgbeResult
Sends a packet to the network.
Queues the packet for transmission on the specified queue. The actual transmission happens asynchronously in the hardware.
§Arguments
queue_id- The transmit queue ID to send ontx_buf- The packet buffer to send
§Errors
IxgbeError::QueueFull- The transmit queue has no available descriptorsIxgbeError::InvalidQueue-queue_idis out of range
Sourcefn can_receive(&self, queue_id: u16) -> IxgbeResult<bool>
fn can_receive(&self, queue_id: u16) -> IxgbeResult<bool>
Checks whether a packet can be received from the specified queue.
Returns true if at least one packet is available in the receive queue.
§Errors
Returns IxgbeError::InvalidQueue if queue_id is out of range.
Sourcefn can_send(&self, queue_id: u16) -> IxgbeResult<bool>
fn can_send(&self, queue_id: u16) -> IxgbeResult<bool>
Checks whether a packet can be sent on the specified queue.
Returns true if the transmit queue has at least one free descriptor.
§Errors
Returns IxgbeError::InvalidQueue if queue_id is out of range.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.