Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

将smoltcp升级到0.11.0版本 #740

Merged
merged 1 commit into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
29 changes: 13 additions & 16 deletions kernel/src/driver/net/e1000e/e1000e_driver.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//这个文件的绝大部分内容是copy virtio_net.rs的,考虑到所有的驱动都要用操作系统提供的协议栈,我觉得可以把这些内容抽象出来

use crate::{
arch::rand::rand,
driver::{
base::{
device::{bus::Bus, driver::Driver, Device, IdTable},
Expand All @@ -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};
Expand Down Expand Up @@ -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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样设置会有问题,http_server read不到信息,但是改成12345就能行了,我不理解

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read不到信息具体指的是什么?卡在哪个地方的代码?

Copy link
Member

@fslongjin fslongjin Apr 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#743

跟这个issue有关,是tcp accept地方有问题


let inner: Arc<SpinLock<E1000EDevice>> = Arc::new(SpinLock::new(device));
let result = E1000EDriver { inner };
return result;
Expand Down Expand Up @@ -175,16 +175,13 @@ impl phy::Device for E1000EDriver {
impl E1000EInterface {
pub fn new(mut driver: E1000EDriver) -> Arc<Self> {
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 {
Expand Down
45 changes: 20 additions & 25 deletions kernel/src/driver/net/virtio_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -77,7 +78,7 @@ impl<T: Transport> Debug for VirtioNICDriver<T> {
pub struct VirtioInterface<T: Transport> {
driver: VirtioNICDriverWrapper<T>,
iface_id: usize,
iface: SpinLock<smoltcp::iface::Interface>,
iface: SpinLock<iface::Interface>,
name: String,
dev_id: Arc<DeviceId>,
}
Expand All @@ -96,16 +97,12 @@ impl<T: Transport> Debug for VirtioInterface<T> {
impl<T: Transport> VirtioInterface<T> {
pub fn new(mut driver: VirtioNICDriver<T>, dev_id: Arc<DeviceId>) -> Arc<Self> {
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<T> = VirtioNICDriverWrapper(UnsafeCell::new(driver));
let result = Arc::new(VirtioInterface {
Expand Down Expand Up @@ -140,16 +137,12 @@ impl<T: Transport> Drop for VirtioInterface<T> {

impl<T: 'static + Transport> VirtioNICDriver<T> {
pub fn new(driver_net: VirtIONet<HalImpl, T, 2>) -> 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<SpinLock<VirtIONet<HalImpl, T, 2>>> = Arc::new(SpinLock::new(driver_net));
let result = VirtioNICDriver { inner };
return result;
Expand Down Expand Up @@ -255,7 +248,7 @@ pub fn virtio_net<T: Transport + 'static>(transport: T, dev_id: Arc<DeviceId>) {
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<T> = VirtioNICDriver::new(driver_net);
let iface = VirtioInterface::new(driver, dev_id);
let name = iface.name.clone();
Expand Down Expand Up @@ -301,9 +294,9 @@ impl<T: Transport + 'static> Driver for VirtioInterface<T> {
}

impl<T: Transport + 'static> NetDriver for VirtioInterface<T> {
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]
Expand All @@ -327,13 +320,15 @@ impl<T: Transport + 'static> NetDriver for VirtioInterface<T> {
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);
Expand All @@ -346,7 +341,7 @@ impl<T: Transport + 'static> NetDriver for VirtioInterface<T> {
}

#[inline(always)]
fn inner_iface(&self) -> &SpinLock<smoltcp::iface::Interface> {
fn inner_iface(&self) -> &SpinLock<iface::Interface> {
return &self.iface;
}
// fn as_any_ref(&'static self) -> &'static dyn core::any::Any {
Expand Down
62 changes: 29 additions & 33 deletions kernel/src/net/socket/inet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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没有连接,则忙等
Expand Down Expand Up @@ -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));
Expand Down
5 changes: 3 additions & 2 deletions kernel/src/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Loading