net_core/lib.rs
1/*
2 * This file is part of strat9-os.
3 *
4 * strat9-os is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * strat9-os is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with strat9-os. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Website: https://www.strat9-os.org
18 * Repository: https://github.com/strat9-os/strat9-os
19 *
20 * Copyright (C) 2023-26 Guillaume Gielly
21 */
22
23#![no_std]
24
25pub const MTU: usize = 1514;
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub enum NetError {
29 NoPacket,
30 TxQueueFull,
31 BufferTooSmall,
32 NotReady,
33 LinkDown,
34 DeviceNotFound,
35}
36
37impl core::fmt::Display for NetError {
38 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
39 match self {
40 Self::NoPacket => f.write_str("no packet available"),
41 Self::TxQueueFull => f.write_str("transmit queue full"),
42 Self::BufferTooSmall => f.write_str("buffer too small"),
43 Self::NotReady => f.write_str("device not ready"),
44 Self::LinkDown => f.write_str("link down"),
45 Self::DeviceNotFound => f.write_str("device not found"),
46 }
47 }
48}
49
50/// Link-layer media type, as reported by `ifconfig`.
51#[derive(Debug, Clone, Copy, PartialEq, Eq)]
52pub enum NetMedia {
53 /// Ethernet framing (Ethernet II header, 6-byte MAC).
54 Ethernet,
55 /// IEEE 802.3 framing without the Ethernet II type field.
56 Ieee8023,
57 /// Point-to-point serial link.
58 Ppp,
59 /// Fibre channel.
60 FibreChannel,
61 /// Loopback adapter.
62 Loopback,
63 /// Anything a driver cannot classify.
64 Other,
65}
66
67impl NetMedia {
68 /// Short lowercase name used by `ifconfig`.
69 pub const fn as_str(self) -> &'static str {
70 match self {
71 Self::Ethernet => "ethernet",
72 Self::Ieee8023 => "ieee802.3",
73 Self::Ppp => "ppp",
74 Self::FibreChannel => "fibrechannel",
75 Self::Loopback => "loopback",
76 Self::Other => "other",
77 }
78 }
79}
80
81/// Unified network device interface.
82///
83/// Kernel-resident drivers wrap their hardware-specific struct in a
84/// `SpinLock` and implement this trait with interior mutability.
85/// Future silo-hosted drivers expose the same interface via IPC.
86pub trait NetworkDevice: Send + Sync {
87 fn name(&self) -> &str;
88 fn receive(&self, buf: &mut [u8]) -> Result<usize, NetError>;
89 fn transmit(&self, buf: &[u8]) -> Result<(), NetError>;
90 fn mac_address(&self) -> [u8; 6];
91 fn link_up(&self) -> bool;
92 fn handle_interrupt(&self) {}
93
94 /// Link-layer media. Ethernet is the only framing implemented today.
95 fn media(&self) -> NetMedia {
96 NetMedia::Ethernet
97 }
98
99 /// Periodic housekeeping called from the timer path (or interrupt
100 /// context). Default is a no-op; drivers that need a watchdog or
101 /// buffer-reclamation cycle override this.
102 fn poll(&self) {}
103
104 /// Negotiated `strat9_abi::net::features::*` mask.
105 ///
106 /// Drivers return only the features they actually implement; the stack
107 /// must never assume an offload is available. Default is 0 (software
108 /// fallback for everything).
109 fn features(&self) -> u64 {
110 0
111 }
112
113 /// IP MTU (without the Ethernet header).
114 fn mtu(&self) -> usize {
115 MTU - 14
116 }
117
118 /// Number of hardware queues exposed by the device.
119 fn queue_count(&self) -> u16 {
120 1
121 }
122
123 /// Current link speed in Mbps, or 0 when unknown.
124 fn link_speed_mbps(&self) -> u32 {
125 0
126 }
127
128 /// Device generation. Bumped after reset/hotplug; consumers must reject
129 /// operations carrying a stale epoch.
130 fn epoch(&self) -> u32 {
131 1
132 }
133
134 /// Stop DMA and interrupts, drain in-flight work. Default no-op.
135 fn quiesce(&self) -> Result<(), NetError> {
136 Ok(())
137 }
138
139 /// Resume after `quiesce`.
140 fn resume(&self) -> Result<(), NetError> {
141 Ok(())
142 }
143
144 /// Reset the device.
145 fn reset(&self) -> Result<(), NetError> {
146 Ok(())
147 }
148}