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    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
17        match self {
18            Self::NoPacket => f.write_str("no packet available"),
19            Self::TxQueueFull => f.write_str("transmit queue full"),
20            Self::BufferTooSmall => f.write_str("buffer too small"),
21            Self::NotReady => f.write_str("device not ready"),
22            Self::LinkDown => f.write_str("link down"),
23            Self::DeviceNotFound => f.write_str("device not found"),
24        }
25    }
26}
27
28/// Unified network device interface.
29///
30/// Kernel-resident drivers wrap their hardware-specific struct in a
31/// `SpinLock` and implement this trait with interior mutability.
32/// Future silo-hosted drivers expose the same interface via IPC.
33pub trait NetworkDevice: Send + Sync {
34    fn name(&self) -> &str;
35    fn receive(&self, buf: &mut [u8]) -> Result<usize, NetError>;
36    fn transmit(&self, buf: &[u8]) -> Result<(), NetError>;
37    fn mac_address(&self) -> [u8; 6];
38    fn link_up(&self) -> bool;
39    fn handle_interrupt(&self) {}
40
41    /// Periodic housekeeping called from the timer path (or interrupt
42    /// context).  Default is a no-op; drivers that need a watchdog or
43    /// buffer-reclamation cycle override this.
44    fn poll(&self) {}
45}