Module phy

Source
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:

§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§

ChecksumCapabilities
A description of checksum behavior for every supported protocol.
DeviceCapabilities
A description of device capabilities.
FaultInjector
A fault injector device.
FuzzInjector
A fuzz injector device.
Loopback
A loopback device.
PacketMeta
Metadata associated to a packet.
PcapWriter
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.
PcapLinkType
Captured packet header type.
PcapMode
Packet capture mode.

Traits§

Device
An interface for sending and receiving raw network frames.
Fuzzer
Represents a fuzzer. It is expected to replace bytes in the packet with fuzzed data.
PcapSink
A packet capture sink.
RxToken
A token to receive a single network packet.
TxToken
A token to transmit a single network packet.