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>(self, f: F) -> R
where F: FnOnce(& [u8]) -> R
{
// TODO: receive packet into buffer
let result = f(&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§
- Checksum
Capabilities - A description of checksum behavior for every supported protocol.
- Device
Capabilities - A description of device capabilities.
- Fault
Injector - A fault injector device.
- Fuzz
Injector - A fuzz injector device.
- Loopback
- A loopback device.
- Packet
Meta - Metadata associated to a packet.
- Pcap
Writer - A packet capture writer device.
- Tracer
- A tracer device.
Enums§
- Checksum
- A description of checksum behavior for a particular protocol.
- Medium
- Type of medium of a device.
- Pcap
Link Type - Captured packet header type.
- Pcap
Mode - Packet capture mode.