Skip to main content

net_core/
lib.rs

1#![no_std]
2
3pub const MTU: usize = 1514;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum NetError {
7    NoPacket,
8    TxQueueFull,
9    BufferTooSmall,
10    NotReady,
11    LinkDown,
12    DeviceNotFound,
13}
14
15impl core::fmt::Display for NetError {
16    /// Performs the fmt operation.
17    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
18        match self {
19            Self::NoPacket => f.write_str("no packet available"),
20            Self::TxQueueFull => f.write_str("transmit queue full"),
21            Self::BufferTooSmall => f.write_str("buffer too small"),
22            Self::NotReady => f.write_str("device not ready"),
23            Self::LinkDown => f.write_str("link down"),
24            Self::DeviceNotFound => f.write_str("device not found"),
25        }
26    }
27}
28
29/// Unified network device interface.
30///
31/// Kernel-resident drivers wrap their hardware-specific struct in a
32/// `SpinLock` and implement this trait with interior mutability.
33/// Future silo-hosted drivers expose the same interface via IPC.
34pub trait NetworkDevice: Send + Sync {
35    /// Performs the name operation.
36    fn name(&self) -> &str;
37    /// Performs the receive operation.
38    fn receive(&self, buf: &mut [u8]) -> Result<usize, NetError>;
39    /// Performs the transmit operation.
40    fn transmit(&self, buf: &[u8]) -> Result<(), NetError>;
41    /// Performs the mac address operation.
42    fn mac_address(&self) -> [u8; 6];
43    /// Performs the link up operation.
44    fn link_up(&self) -> bool;
45    /// Handles interrupt.
46    fn handle_interrupt(&self) {}
47}