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::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 phy::Device for StmPhy {
type RxToken<'a> = StmPhyRxToken<'a> where Self: 'a;
type TxToken<'a> = StmPhyTxToken<'a> where Self: 'a;
fn receive(&mut self, _timestamp: Instant) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
Some((StmPhyRxToken(&mut self.rx_buffer[..]),
StmPhyTxToken(&mut self.tx_buffer[..])))
}
fn transmit(&mut self, _timestamp: Instant) -> 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, f: F) -> R
where F: FnOnce(&mut [u8]) -> 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, len: usize, f: F) -> R
where F: FnOnce(&mut [u8]) -> 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.
- Metadata associated to a packet.
- A packet capture writer device.
- A tracer device.
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.