From 76302f456deafff73910450ebefb5ab0bc8b983a Mon Sep 17 00:00:00 2001 From: yuyi2439 Date: Wed, 17 Apr 2024 23:08:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86smoltcp=E5=8D=87=E7=BA=A7=E5=88=B00.11?= =?UTF-8?q?.0=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/Cargo.toml | 2 +- kernel/src/driver/net/e1000e/e1000e_driver.rs | 29 ++++----- kernel/src/driver/net/virtio_net.rs | 45 ++++++-------- kernel/src/net/socket/inet.rs | 62 +++++++++---------- kernel/src/time/mod.rs | 5 +- 5 files changed, 66 insertions(+), 77 deletions(-) diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index 9e90a397b..d876b075a 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -44,7 +44,7 @@ linkme = "=0.2" num = { version = "=0.4.0", default-features = false } num-derive = "=0.3" num-traits = { git = "https://git.mirrors.dragonos.org.cn/DragonOS-Community/num-traits.git", rev="1597c1c", default-features = false } -smoltcp = { git = "https://git.mirrors.dragonos.org.cn/DragonOS-Community/smoltcp.git", rev = "9027825", default-features = false, features = ["log", "alloc", "socket-raw", "socket-udp", "socket-tcp", "socket-icmp", "socket-dhcpv4", "socket-dns", "proto-ipv4", "proto-ipv6"]} +smoltcp = { version = "=0.11.0", default-features = false, features = ["log", "alloc", "socket-raw", "socket-udp", "socket-tcp", "socket-icmp", "socket-dhcpv4", "socket-dns", "proto-ipv4", "proto-ipv6"]} system_error = { path = "crates/system_error" } unified-init = { path = "crates/unified-init" } virtio-drivers = { git = "https://git.mirrors.dragonos.org.cn/DragonOS-Community/virtio-drivers.git", rev = "f1d1cbb" } diff --git a/kernel/src/driver/net/e1000e/e1000e_driver.rs b/kernel/src/driver/net/e1000e/e1000e_driver.rs index 91a01f0fd..9bb816249 100644 --- a/kernel/src/driver/net/e1000e/e1000e_driver.rs +++ b/kernel/src/driver/net/e1000e/e1000e_driver.rs @@ -1,6 +1,7 @@ //这个文件的绝大部分内容是copy virtio_net.rs的,考虑到所有的驱动都要用操作系统提供的协议栈,我觉得可以把这些内容抽象出来 use crate::{ + arch::rand::rand, driver::{ base::{ device::{bus::Bus, driver::Driver, Device, IdTable}, @@ -22,7 +23,10 @@ use core::{ fmt::Debug, ops::{Deref, DerefMut}, }; -use smoltcp::{phy, wire}; +use smoltcp::{ + phy, + wire::{self, HardwareAddress}, +}; use system_error::SystemError; use super::e1000e::{E1000EBuffer, E1000EDevice}; @@ -102,16 +106,12 @@ impl phy::TxToken for E1000ETxToken { impl E1000EDriver { #[allow(clippy::arc_with_non_send_sync)] pub fn new(device: E1000EDevice) -> Self { - let mut iface_config = smoltcp::iface::Config::new(); - - // todo: 随机设定这个值。 - // 参见 https://docs.rs/smoltcp/latest/smoltcp/iface/struct.Config.html#structfield.random_seed - iface_config.random_seed = 12345; - - iface_config.hardware_addr = Some(wire::HardwareAddress::Ethernet( + let mut iface_config = smoltcp::iface::Config::new(HardwareAddress::Ethernet( smoltcp::wire::EthernetAddress(device.mac_address()), )); + iface_config.random_seed = rand() as u64; + let inner: Arc> = Arc::new(SpinLock::new(device)); let result = E1000EDriver { inner }; return result; @@ -175,16 +175,13 @@ impl phy::Device for E1000EDriver { impl E1000EInterface { pub fn new(mut driver: E1000EDriver) -> Arc { let iface_id = generate_iface_id(); - let mut iface_config = smoltcp::iface::Config::new(); - - // todo: 随机设定这个值。 - // 参见 https://docs.rs/smoltcp/latest/smoltcp/iface/struct.Config.html#structfield.random_seed - iface_config.random_seed = 12345; - - iface_config.hardware_addr = Some(wire::HardwareAddress::Ethernet( + let mut iface_config = smoltcp::iface::Config::new(HardwareAddress::Ethernet( smoltcp::wire::EthernetAddress(driver.inner.lock().mac_address()), )); - let iface = smoltcp::iface::Interface::new(iface_config, &mut driver); + iface_config.random_seed = rand() as u64; + + let iface = + smoltcp::iface::Interface::new(iface_config, &mut driver, Instant::now().into()); let driver: E1000EDriverWrapper = E1000EDriverWrapper(UnsafeCell::new(driver)); let result = Arc::new(E1000EInterface { diff --git a/kernel/src/driver/net/virtio_net.rs b/kernel/src/driver/net/virtio_net.rs index d3321d235..0ff11fbdd 100644 --- a/kernel/src/driver/net/virtio_net.rs +++ b/kernel/src/driver/net/virtio_net.rs @@ -8,11 +8,12 @@ use alloc::{ string::String, sync::{Arc, Weak}, }; -use smoltcp::{phy, wire}; +use smoltcp::{iface, phy, wire}; use virtio_drivers::{device::net::VirtIONet, transport::Transport}; use super::NetDriver; use crate::{ + arch::rand::rand, driver::{ base::{ device::{bus::Bus, driver::Driver, Device, DeviceId, IdTable}, @@ -77,7 +78,7 @@ impl Debug for VirtioNICDriver { pub struct VirtioInterface { driver: VirtioNICDriverWrapper, iface_id: usize, - iface: SpinLock, + iface: SpinLock, name: String, dev_id: Arc, } @@ -96,16 +97,12 @@ impl Debug for VirtioInterface { impl VirtioInterface { pub fn new(mut driver: VirtioNICDriver, dev_id: Arc) -> Arc { let iface_id = generate_iface_id(); - let mut iface_config = smoltcp::iface::Config::new(); - - // todo: 随机设定这个值。 - // 参见 https://docs.rs/smoltcp/latest/smoltcp/iface/struct.Config.html#structfield.random_seed - iface_config.random_seed = 12345; - - iface_config.hardware_addr = Some(wire::HardwareAddress::Ethernet( - smoltcp::wire::EthernetAddress(driver.inner.lock().mac_address()), + let mut iface_config = iface::Config::new(wire::HardwareAddress::Ethernet( + wire::EthernetAddress(driver.inner.lock().mac_address()), )); - let iface = smoltcp::iface::Interface::new(iface_config, &mut driver); + iface_config.random_seed = rand() as u64; + + let iface = iface::Interface::new(iface_config, &mut driver, Instant::now().into()); let driver: VirtioNICDriverWrapper = VirtioNICDriverWrapper(UnsafeCell::new(driver)); let result = Arc::new(VirtioInterface { @@ -140,16 +137,12 @@ impl Drop for VirtioInterface { impl VirtioNICDriver { pub fn new(driver_net: VirtIONet) -> Self { - let mut iface_config = smoltcp::iface::Config::new(); - - // todo: 随机设定这个值。 - // 参见 https://docs.rs/smoltcp/latest/smoltcp/iface/struct.Config.html#structfield.random_seed - iface_config.random_seed = 12345; - - iface_config.hardware_addr = Some(wire::HardwareAddress::Ethernet( - smoltcp::wire::EthernetAddress(driver_net.mac_address()), + let mut iface_config = iface::Config::new(wire::HardwareAddress::Ethernet( + wire::EthernetAddress(driver_net.mac_address()), )); + iface_config.random_seed = rand() as u64; + let inner: Arc>> = Arc::new(SpinLock::new(driver_net)); let result = VirtioNICDriver { inner }; return result; @@ -255,7 +248,7 @@ pub fn virtio_net(transport: T, dev_id: Arc) { return; } }; - let mac = smoltcp::wire::EthernetAddress::from_bytes(&driver_net.mac_address()); + let mac = wire::EthernetAddress::from_bytes(&driver_net.mac_address()); let driver: VirtioNICDriver = VirtioNICDriver::new(driver_net); let iface = VirtioInterface::new(driver, dev_id); let name = iface.name.clone(); @@ -301,9 +294,9 @@ impl Driver for VirtioInterface { } impl NetDriver for VirtioInterface { - fn mac(&self) -> smoltcp::wire::EthernetAddress { + fn mac(&self) -> wire::EthernetAddress { let mac: [u8; 6] = self.driver.inner.lock().mac_address(); - return smoltcp::wire::EthernetAddress::from_bytes(&mac); + return wire::EthernetAddress::from_bytes(&mac); } #[inline] @@ -327,13 +320,15 @@ impl NetDriver for VirtioInterface { if let Some(dest) = dest { *dest = ip_addrs[0]; } else { - addrs.push(ip_addrs[0]).expect("Push ipCidr failed: full"); + addrs + .push(ip_addrs[0]) + .expect("Push wire::IpCidr failed: full"); } }); return Ok(()); } - fn poll(&self, sockets: &mut smoltcp::iface::SocketSet) -> Result<(), SystemError> { + fn poll(&self, sockets: &mut iface::SocketSet) -> Result<(), SystemError> { let timestamp: smoltcp::time::Instant = Instant::now().into(); let mut guard = self.iface.lock(); let poll_res = guard.poll(timestamp, self.driver.force_get_mut(), sockets); @@ -346,7 +341,7 @@ impl NetDriver for VirtioInterface { } #[inline(always)] - fn inner_iface(&self) -> &SpinLock { + fn inner_iface(&self) -> &SpinLock { return &self.iface; } // fn as_any_ref(&'static self) -> &'static dyn core::any::Any { diff --git a/kernel/src/net/socket/inet.rs b/kernel/src/net/socket/inet.rs index 2e4e25962..f5e4699e1 100644 --- a/kernel/src/net/socket/inet.rs +++ b/kernel/src/net/socket/inet.rs @@ -112,7 +112,7 @@ impl Socket for RawSocket { })), ); } - Err(raw::RecvError::Exhausted) => { + Err(_) => { if !self.metadata.options.contains(SocketOptions::BLOCK) { // 如果是非阻塞的socket,就返回错误 return (Err(SystemError::EAGAIN_OR_EWOULDBLOCK), Endpoint::Ip(None)); @@ -327,10 +327,10 @@ impl Socket for UdpSocket { // kdebug!("Wait to Read"); if socket.can_recv() { - if let Ok((size, remote_endpoint)) = socket.recv_slice(buf) { + if let Ok((size, metadata)) = socket.recv_slice(buf) { drop(socket_set_guard); poll_ifaces(); - return (Ok(size), Endpoint::Ip(Some(remote_endpoint))); + return (Ok(size), Endpoint::Ip(Some(metadata.endpoint))); } } else { // 如果socket没有连接,则忙等 @@ -592,38 +592,34 @@ impl Socket for TcpSocket { } if socket.may_recv() { - let recv_res = socket.recv_slice(buf); - - if let Ok(size) = recv_res { - if size > 0 { - let endpoint = if let Some(p) = socket.remote_endpoint() { - p - } else { - return (Err(SystemError::ENOTCONN), Endpoint::Ip(None)); - }; - - drop(socket_set_guard); - poll_ifaces(); - return (Ok(size), Endpoint::Ip(Some(endpoint))); - } - } else { - let err = recv_res.unwrap_err(); - match err { - tcp::RecvError::InvalidState => { - kwarn!("Tcp Socket Read Error, InvalidState"); - return (Err(SystemError::ENOTCONN), Endpoint::Ip(None)); - } - tcp::RecvError::Finished => { - // 对端写端已关闭,我们应该关闭读端 - HANDLE_MAP - .write_irqsave() - .get_mut(&self.socket_handle()) - .unwrap() - .shutdown_type_writer() - .insert(ShutdownType::RCV_SHUTDOWN); - return (Err(SystemError::ENOTCONN), Endpoint::Ip(None)); + match socket.recv_slice(buf) { + Ok(size) => { + if size > 0 { + let endpoint = if let Some(p) = socket.remote_endpoint() { + p + } else { + return (Err(SystemError::ENOTCONN), Endpoint::Ip(None)); + }; + + drop(socket_set_guard); + poll_ifaces(); + return (Ok(size), Endpoint::Ip(Some(endpoint))); } } + Err(tcp::RecvError::InvalidState) => { + kwarn!("Tcp Socket Read Error, InvalidState"); + return (Err(SystemError::ENOTCONN), Endpoint::Ip(None)); + } + Err(tcp::RecvError::Finished) => { + // 对端写端已关闭,我们应该关闭读端 + HANDLE_MAP + .write_irqsave() + .get_mut(&self.socket_handle()) + .unwrap() + .shutdown_type_writer() + .insert(ShutdownType::RCV_SHUTDOWN); + return (Err(SystemError::ENOTCONN), Endpoint::Ip(None)); + } } } else { return (Err(SystemError::ENOTCONN), Endpoint::Ip(None)); diff --git a/kernel/src/time/mod.rs b/kernel/src/time/mod.rs index 7a4eb9eb9..ca9ae138a 100644 --- a/kernel/src/time/mod.rs +++ b/kernel/src/time/mod.rs @@ -6,7 +6,7 @@ use core::{ use crate::arch::CurrentTimeArch; -use self::{timekeep::ktime_get_real_ns, timekeeping::getnstimeofday}; +use self::timekeeping::getnstimeofday; pub mod clocksource; pub mod jiffies; @@ -225,7 +225,8 @@ impl Instant { /// Create a new `Instant` from the current time pub fn now() -> Instant { - Self::from_micros(ktime_get_real_ns() / 1000) + let tm = getnstimeofday(); + Self::from_micros(tm.tv_sec * 1000000 + tm.tv_nsec / 1000) } /// The fractional number of milliseconds that have passed