Module smoltcp::phy
[−]
[src]
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 TapInterface, 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 std::slice; use smoltcp::{Error, Result}; use smoltcp::phy::{DeviceLimits, Device}; const TX_BUFFERS: [*mut u8; 2] = [0x10000000 as *mut u8, 0x10001000 as *mut u8]; const RX_BUFFERS: [*mut u8; 2] = [0x10002000 as *mut u8, 0x10003000 as *mut u8]; fn rx_full() -> bool { /* platform-specific code to check if an incoming packet has arrived */ false } fn rx_setup(_buf: *mut u8, _length: &mut usize) { /* platform-specific code to receive a packet into a buffer */ } fn tx_empty() -> bool { /* platform-specific code to check if an outgoing packet can be sent */ false } fn tx_setup(_buf: *const u8, _length: usize) { /* platform-specific code to send a buffer with a packet */ } pub struct EthernetDevice { tx_next: usize, rx_next: usize } impl Device for EthernetDevice { type RxBuffer = &'static [u8]; type TxBuffer = EthernetTxBuffer; fn limits(&self) -> DeviceLimits { let mut limits = DeviceLimits::default(); limits.max_transmission_unit = 1536; limits.max_burst_size = Some(2); limits } fn receive(&mut self, _timestamp: u64) -> Result<Self::RxBuffer> { if rx_full() { let index = self.rx_next; self.rx_next = (self.rx_next + 1) % RX_BUFFERS.len(); let mut length = 0; rx_setup(RX_BUFFERS[self.rx_next], &mut length); Ok(unsafe { slice::from_raw_parts(RX_BUFFERS[index], length) }) } else { Err(Error::Exhausted) } } fn transmit(&mut self, _timestamp: u64, length: usize) -> Result<Self::TxBuffer> { if tx_empty() { let index = self.tx_next; self.tx_next = (self.tx_next + 1) % TX_BUFFERS.len(); Ok(EthernetTxBuffer(unsafe { slice::from_raw_parts_mut(TX_BUFFERS[index], length) })) } else { Err(Error::Exhausted) } } } pub struct EthernetTxBuffer(&'static mut [u8]); impl AsRef<[u8]> for EthernetTxBuffer { fn as_ref(&self) -> &[u8] { self.0 } } impl AsMut<[u8]> for EthernetTxBuffer { fn as_mut(&mut self) -> &mut [u8] { self.0 } } impl Drop for EthernetTxBuffer { fn drop(&mut self) { tx_setup(self.0.as_ptr(), self.0.len()) } }
Structs
DeviceLimits |
A description of device limitations. |
FaultInjector |
A fault injector device. |
Loopback |
A loopback device. |
PcapWriter |
A packet capture writer device. |
RawSocket |
A socket that captures or transmits the complete frame. |
Tracer |
A tracer device. |
Enums
PcapLinkType |
Captured packet header type. |
PcapMode |
Packet capture mode. |
Traits
Device |
An interface for sending and receiving raw network frames. |
PcapSink |
A packet capture sink. |
Functions
wait |
Wait until given file descriptor becomes readable, but no longer than given timeout. |
Type Definitions
EthernetTracer |
A tracer device for Ethernet frames. |