Skip to content

Commit

Permalink
Add network config devices (#700)
Browse files Browse the repository at this point in the history
* Add net-gw device

* Add net-ip device

* Add net-mac device

* Add net-usage device

* Use filesystem instead of net command

* Add deprecation warnings
  • Loading branch information
vinc authored Nov 8, 2024
1 parent 7e33159 commit 92055d5
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 17 deletions.
8 changes: 6 additions & 2 deletions src/api/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,12 @@ fn device_type(name: &str) -> Result<DeviceType, ()> {
"clk-boot" => Ok(DeviceType::BootTime),
"clk-epoch" => Ok(DeviceType::EpochTime),
"clk-rtc" => Ok(DeviceType::RTC),
"tcp" => Ok(DeviceType::TcpSocket),
"udp" => Ok(DeviceType::UdpSocket),
"net-tcp" => Ok(DeviceType::TcpSocket),
"net-udp" => Ok(DeviceType::UdpSocket),
"net-gw" => Ok(DeviceType::NetGw),
"net-ip" => Ok(DeviceType::NetIp),
"net-mac" => Ok(DeviceType::NetMac),
"net-usage" => Ok(DeviceType::NetUsage),
"vga-buffer" => Ok(DeviceType::VgaBuffer),
"vga-font" => Ok(DeviceType::VgaFont),
"vga-mode" => Ok(DeviceType::VgaMode),
Expand Down
42 changes: 41 additions & 1 deletion src/sys/fs/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ use super::{dirname, filename, realpath, FileIO, IO};
use crate::sys::ata::Drive;
use crate::sys::clk::{RTC, EpochTime, BootTime};
use crate::sys::console::Console;
use crate::sys::net::gw::NetGw;
use crate::sys::net::ip::NetIp;
use crate::sys::net::mac::NetMac;
use crate::sys::net::usage::NetUsage;
use crate::sys::net::socket::tcp::TcpSocket;
use crate::sys::net::socket::udp::UdpSocket;
use crate::sys::rng::Random;
Expand Down Expand Up @@ -35,6 +39,10 @@ pub enum DeviceType {
VgaMode = 12,
VgaPalette = 13,
Speaker = 14,
NetGw = 15,
NetIp = 16,
NetMac = 17,
NetUsage = 18,
}

impl TryFrom<&[u8]> for DeviceType {
Expand All @@ -57,6 +65,10 @@ impl TryFrom<&[u8]> for DeviceType {
12 => Ok(DeviceType::VgaMode),
13 => Ok(DeviceType::VgaPalette),
14 => Ok(DeviceType::Speaker),
15 => Ok(DeviceType::NetGw),
16 => Ok(DeviceType::NetIp),
17 => Ok(DeviceType::NetMac),
18 => Ok(DeviceType::NetUsage),
_ => Err(()),
}
}
Expand All @@ -78,6 +90,10 @@ impl DeviceType {
DeviceType::VgaBuffer => VgaBuffer::size(),
DeviceType::VgaMode => VgaMode::size(),
DeviceType::VgaPalette => VgaPalette::size(),
DeviceType::NetGw => NetGw::size(),
DeviceType::NetIp => NetIp::size(),
DeviceType::NetMac => NetMac::size(),
DeviceType::NetUsage => NetUsage::size(),
_ => 1,
};
let mut res = vec![0; len];
Expand All @@ -97,12 +113,16 @@ pub enum Device {
RTC(RTC),
TcpSocket(TcpSocket),
UdpSocket(UdpSocket),
Drive(Drive),
VgaBuffer(VgaBuffer),
VgaFont(VgaFont),
VgaMode(VgaMode),
VgaPalette(VgaPalette),
Speaker(Speaker),
Drive(Drive),
NetGw(NetGw),
NetIp(NetIp),
NetMac(NetMac),
NetUsage(NetUsage),
}

impl TryFrom<&[u8]> for Device {
Expand All @@ -124,6 +144,10 @@ impl TryFrom<&[u8]> for Device {
DeviceType::VgaMode => Ok(Device::VgaMode(VgaMode::new())),
DeviceType::VgaPalette => Ok(Device::VgaPalette(VgaPalette::new())),
DeviceType::Speaker => Ok(Device::Speaker(Speaker::new())),
DeviceType::NetGw => Ok(Device::NetGw(NetGw::new())),
DeviceType::NetIp => Ok(Device::NetIp(NetIp::new())),
DeviceType::NetMac => Ok(Device::NetMac(NetMac::new())),
DeviceType::NetUsage => Ok(Device::NetUsage(NetUsage::new())),
DeviceType::Drive if buf.len() > 2 => {
let bus = buf[1];
let dsk = buf[2];
Expand Down Expand Up @@ -188,6 +212,10 @@ impl FileIO for Device {
Device::VgaPalette(io) => io.read(buf),
Device::Speaker(io) => io.read(buf),
Device::Drive(io) => io.read(buf),
Device::NetGw(io) => io.read(buf),
Device::NetIp(io) => io.read(buf),
Device::NetMac(io) => io.read(buf),
Device::NetUsage(io) => io.read(buf),
}
}

Expand All @@ -208,6 +236,10 @@ impl FileIO for Device {
Device::VgaPalette(io) => io.write(buf),
Device::Speaker(io) => io.write(buf),
Device::Drive(io) => io.write(buf),
Device::NetGw(io) => io.write(buf),
Device::NetIp(io) => io.write(buf),
Device::NetMac(io) => io.write(buf),
Device::NetUsage(io) => io.write(buf),
}
}

Expand All @@ -228,6 +260,10 @@ impl FileIO for Device {
Device::VgaPalette(io) => io.close(),
Device::Speaker(io) => io.close(),
Device::Drive(io) => io.close(),
Device::NetGw(io) => io.close(),
Device::NetIp(io) => io.close(),
Device::NetMac(io) => io.close(),
Device::NetUsage(io) => io.close(),
}
}

Expand All @@ -248,6 +284,10 @@ impl FileIO for Device {
Device::VgaPalette(io) => io.poll(event),
Device::Speaker(io) => io.poll(event),
Device::Drive(io) => io.poll(event),
Device::NetGw(io) => io.poll(event),
Device::NetIp(io) => io.poll(event),
Device::NetMac(io) => io.poll(event),
Device::NetUsage(io) => io.poll(event),
}
}
}
63 changes: 63 additions & 0 deletions src/sys/net/gw.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use crate::api::fs::{FileIO, IO};

use alloc::string::{String, ToString};
use core::str::FromStr;
use smoltcp::wire::Ipv4Address;

#[derive(Debug, Clone)]
pub struct NetGw;

impl NetGw {
pub fn new() -> Self {
Self
}

pub fn size() -> usize {
16
}
}

impl FileIO for NetGw {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()> {
if let Some((ref mut iface, _)) = *super::NET.lock() {
let mut n = 0;
iface.routes_mut().update(|storage| {
if let Some(route) = storage.iter().next() {
let s = route.via_router.to_string();
n = s.len();
buf[0..n].copy_from_slice(s.as_bytes());
}
});
if n > 0 {
return Ok(n);
}
}
Err(())
}

fn write(&mut self, buf: &[u8]) -> Result<usize, ()> {
if let Some((ref mut iface, _)) = *super::NET.lock() {
if let Ok(s) = String::from_utf8(buf.to_vec()) {
if s == "0.0.0.0" {
iface.routes_mut().remove_default_ipv4_route();
return Ok(s.len());
} else if let Ok(ip) = Ipv4Address::from_str(&s) {
iface.routes_mut().add_default_ipv4_route(ip).unwrap();
log!("NET GW {}", s);
return Ok(s.len());
}
}
}
Err(())
}

fn close(&mut self) {}

fn poll(&mut self, event: IO) -> bool {
match event {
IO::Read => true,
IO::Write => true,
}
}
}

59 changes: 59 additions & 0 deletions src/sys/net/ip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use crate::api::fs::{FileIO, IO};

use alloc::format;
use alloc::string::String;
use core::str::FromStr;
use smoltcp::wire::IpCidr;

#[derive(Debug, Clone)]
pub struct NetIp;

impl NetIp {
pub fn new() -> Self {
Self
}

pub fn size() -> usize {
16 + 1 + 3
}
}

impl FileIO for NetIp {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()> {
if let Some((ref mut iface, _)) = *super::NET.lock() {
if let Some(ip) = iface.ip_addrs().iter().next() {
let s = format!("{}/{}", ip.address(), ip.prefix_len());
let n = s.len();
buf[0..n].copy_from_slice(s.as_bytes());
return Ok(n);
}
}
Err(())
}

fn write(&mut self, buf: &[u8]) -> Result<usize, ()> {
if let Ok(s) = String::from_utf8(buf.to_vec()) {
if let Ok(addr) = IpCidr::from_str(&s) {
if let Some((ref mut iface, _)) = *super::NET.lock() {
iface.update_ip_addrs(|addrs| {
addrs.clear();
addrs.push(addr).unwrap();
log!("NET IP {}", s);
});
return Ok(buf.len());
}
}
}
Err(())
}

fn close(&mut self) {}

fn poll(&mut self, event: IO) -> bool {
match event {
IO::Read => true,
IO::Write => true,
}
}
}

42 changes: 42 additions & 0 deletions src/sys/net/mac.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::api::fs::{FileIO, IO};

use alloc::string::ToString;

#[derive(Debug, Clone)]
pub struct NetMac;

impl NetMac {
pub fn new() -> Self {
Self
}

pub fn size() -> usize {
17
}
}

impl FileIO for NetMac {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()> {
if let Some((ref mut iface, _)) = *super::NET.lock() {
let s = iface.hardware_addr().to_string();
let n = s.len();
buf[0..n].copy_from_slice(s.as_bytes());
return Ok(n);
}
Err(())
}

fn write(&mut self, _buf: &[u8]) -> Result<usize, ()> {
Err(())
}

fn close(&mut self) {}

fn poll(&mut self, event: IO) -> bool {
match event {
IO::Read => true,
IO::Write => false,
}
}
}

4 changes: 4 additions & 0 deletions src/sys/net/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
mod nic;
pub mod gw;
pub mod ip;
pub mod mac;
pub mod usage;
pub mod socket;

use crate::{sys, usr};
Expand Down
50 changes: 50 additions & 0 deletions src/sys/net/usage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use crate::api::fs::{FileIO, IO};
use crate::sys::net::EthernetDeviceIO;

use alloc::format;

#[derive(Debug, Clone)]
pub struct NetUsage;

impl NetUsage {
pub fn new() -> Self {
Self
}

pub fn size() -> usize {
83
}
}

impl FileIO for NetUsage {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()> {
if let Some((_, ref mut device)) = *super::NET.lock() {
let stats = device.stats();
let s = format!(
"{} {} {} {}",
stats.rx_packets_count(),
stats.rx_bytes_count(),
stats.tx_packets_count(),
stats.tx_bytes_count(),
);
let n = s.len();
buf[0..n].copy_from_slice(s.as_bytes());
return Ok(n);
}
Err(())
}

fn write(&mut self, _buf: &[u8]) -> Result<usize, ()> {
Err(())
}

fn close(&mut self) {}

fn poll(&mut self, event: IO) -> bool {
match event {
IO::Read => true,
IO::Write => false,
}
}
}

Loading

0 comments on commit 92055d5

Please sign in to comment.