Expand description
Access to networking hardware.
The phy
module deals with the network devices. It provides a trait
for transmitting and receiving frames, Device
and implementations of it:
- the loopback, for zero dependency testing;
- middleware Tracer and FaultInjector, to facilitate debugging;
- adapters RawSocket and TunTapInterface, to transmit and receive frames on the host OS.
Examples
An implementation of the Device trait for a simple hardware Ethernet controller could look as follows:
use smoltcp::Result;
use smoltcp::phy::{self, DeviceCapabilities, Device, Medium};
use smoltcp::time::Instant;
struct StmPhy {
rx_buffer: [u8; 1536],
tx_buffer: [u8; 1536],
}
impl<'a> StmPhy {
fn new() -> StmPhy {
StmPhy {
rx_buffer: [0; 1536],
tx_buffer: [0; 1536],
}
}
}
impl<'a> phy::Device<'a> for StmPhy {
type RxToken = StmPhyRxToken<'a>;
type TxToken = StmPhyTxToken<'a>;
fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> {
Some((StmPhyRxToken(&mut self.rx_buffer[..]),
StmPhyTxToken(&mut self.tx_buffer[..])))
}
fn transmit(&'a mut self) -> Option<Self::TxToken> {
Some(StmPhyTxToken(&mut self.tx_buffer[..]))
}
fn capabilities(&self) -> DeviceCapabilities {
let mut caps = DeviceCapabilities::default();
caps.max_transmission_unit = 1536;
caps.max_burst_size = Some(1);
caps.medium = Medium::Ethernet;
caps
}
}
struct StmPhyRxToken<'a>(&'a mut [u8]);
impl<'a> phy::RxToken for StmPhyRxToken<'a> {
fn consume<R, F>(mut self, _timestamp: Instant, f: F) -> Result<R>
where F: FnOnce(&mut [u8]) -> Result<R>
{
// TODO: receive packet into buffer
let result = f(&mut self.0);
println!("rx called");
result
}
}
struct StmPhyTxToken<'a>(&'a mut [u8]);
impl<'a> phy::TxToken for StmPhyTxToken<'a> {
fn consume<R, F>(self, _timestamp: Instant, len: usize, f: F) -> Result<R>
where F: FnOnce(&mut [u8]) -> Result<R>
{
let result = f(&mut self.0[..len]);
println!("tx called {}", len);
// TODO: send packet out
result
}
}
Structs
A description of checksum behavior for every supported protocol.
A description of device capabilities.
A fault injector device.
A fuzz injector device.
A loopback device.
A packet capture writer device.
A socket that captures or transmits the complete frame.
A tracer device.
A virtual TUN (IP) or TAP (Ethernet) interface.
Enums
A description of checksum behavior for a particular protocol.
Type of medium of a device.
Captured packet header type.
Packet capture mode.
Traits
An interface for sending and receiving raw network frames.
Represents a fuzzer. It is expected to replace bytes in the packet with fuzzed data.
A packet capture sink.
A token to receive a single network packet.
A token to transmit a single network packet.
Functions
Wait until given file descriptor becomes readable, but no longer than given timeout.