fix: 修复网卡顺序的bug (#996)

* fix: 修复网卡顺序的bug

* make fmt

* feat: 添加默认网卡字段

* make fmt

* 删掉奇怪加入的文件
This commit is contained in:
Cai Junyuan 2024-10-20 20:29:24 +08:00 committed by GitHub
parent bb23a0424a
commit acfceaa42e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 40 additions and 20 deletions

View File

@ -202,7 +202,7 @@ impl E1000EInterface {
let result = Arc::new(E1000EInterface { let result = Arc::new(E1000EInterface {
driver: E1000EDriverWrapper(UnsafeCell::new(driver)), driver: E1000EDriverWrapper(UnsafeCell::new(driver)),
common: IfaceCommon::new(iface_id, iface), common: IfaceCommon::new(iface_id, false, iface),
name: format!("eth{}", iface_id), name: format!("eth{}", iface_id),
inner: SpinLock::new(InnerE1000EInterface { inner: SpinLock::new(InnerE1000EInterface {
netdevice_common: NetDeviceCommonData::default(), netdevice_common: NetDeviceCommonData::default(),

View File

@ -8,7 +8,7 @@ use crate::driver::base::kobject::{
}; };
use crate::driver::base::kset::KSet; use crate::driver::base::kset::KSet;
use crate::filesystem::kernfs::KernFSInode; use crate::filesystem::kernfs::KernFSInode;
// use crate::init::initcall::INITCALL_DEVICE; use crate::init::initcall::INITCALL_DEVICE;
use crate::libs::rwlock::{RwLockReadGuard, RwLockWriteGuard}; use crate::libs::rwlock::{RwLockReadGuard, RwLockWriteGuard};
use crate::libs::spinlock::{SpinLock, SpinLockGuard}; use crate::libs::spinlock::{SpinLock, SpinLockGuard};
use crate::net::{generate_iface_id, NET_DEVICES}; use crate::net::{generate_iface_id, NET_DEVICES};
@ -26,7 +26,7 @@ use smoltcp::{
wire::{IpAddress, IpCidr}, wire::{IpAddress, IpCidr},
}; };
use system_error::SystemError; use system_error::SystemError;
// use unified_init::macros::unified_init; use unified_init::macros::unified_init;
use super::{register_netdevice, NetDeivceState, NetDeviceCommonData, Operstate}; use super::{register_netdevice, NetDeivceState, NetDeviceCommonData, Operstate};
@ -278,7 +278,7 @@ impl LoopbackInterface {
pub fn new(mut driver: LoopbackDriver) -> Arc<Self> { pub fn new(mut driver: LoopbackDriver) -> Arc<Self> {
let iface_id = generate_iface_id(); let iface_id = generate_iface_id();
let mut iface_config = smoltcp::iface::Config::new(HardwareAddress::Ethernet( let mut iface_config = smoltcp::iface::Config::new(HardwareAddress::Ethernet(
smoltcp::wire::EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]), smoltcp::wire::EthernetAddress([0x00, 0x00, 0x00, 0x00, 0x00, 0x00]),
)); ));
iface_config.random_seed = rand() as u64; iface_config.random_seed = rand() as u64;
@ -293,7 +293,7 @@ impl LoopbackInterface {
Arc::new(LoopbackInterface { Arc::new(LoopbackInterface {
driver: LoopbackDriverWapper(UnsafeCell::new(driver)), driver: LoopbackDriverWapper(UnsafeCell::new(driver)),
common: IfaceCommon::new(iface_id, iface), common: IfaceCommon::new(iface_id, false, iface),
inner: SpinLock::new(InnerLoopbackInterface { inner: SpinLock::new(InnerLoopbackInterface {
netdevice_common: NetDeviceCommonData::default(), netdevice_common: NetDeviceCommonData::default(),
device_common: DeviceCommonData::default(), device_common: DeviceCommonData::default(),
@ -500,8 +500,7 @@ pub fn loopback_driver_init() {
} }
/// ## lo网卡设备的注册函数 /// ## lo网卡设备的注册函数
//TODO: 现在先不用初始化宏进行注册使virtonet排在网卡列表头待网络子系统重构后再使用初始化宏并修复该bug #[unified_init(INITCALL_DEVICE)]
// #[unified_init(INITCALL_DEVICE)]
pub fn loopback_init() -> Result<(), SystemError> { pub fn loopback_init() -> Result<(), SystemError> {
loopback_probe(); loopback_probe();
log::debug!("Successfully init loopback device"); log::debug!("Successfully init loopback device");

View File

@ -170,6 +170,9 @@ pub struct IfaceCommon {
port_manager: PortManager, port_manager: PortManager,
/// 下次轮询的时间 /// 下次轮询的时间
poll_at_ms: core::sync::atomic::AtomicU64, poll_at_ms: core::sync::atomic::AtomicU64,
/// 默认网卡标识
/// TODO: 此字段设置目的是解决对bind unspecified地址的分包问题需要在inet实现多网卡监听或路由子系统实现后移除
default_iface: bool,
} }
impl fmt::Debug for IfaceCommon { impl fmt::Debug for IfaceCommon {
@ -185,7 +188,7 @@ impl fmt::Debug for IfaceCommon {
} }
impl IfaceCommon { impl IfaceCommon {
pub fn new(iface_id: usize, iface: smoltcp::iface::Interface) -> Self { pub fn new(iface_id: usize, default_iface: bool, iface: smoltcp::iface::Interface) -> Self {
IfaceCommon { IfaceCommon {
iface_id, iface_id,
smol_iface: SpinLock::new(iface), smol_iface: SpinLock::new(iface),
@ -193,6 +196,7 @@ impl IfaceCommon {
bounds: RwLock::new(Vec::new()), bounds: RwLock::new(Vec::new()),
port_manager: PortManager::new(), port_manager: PortManager::new(),
poll_at_ms: core::sync::atomic::AtomicU64::new(0), poll_at_ms: core::sync::atomic::AtomicU64::new(0),
default_iface,
} }
} }
@ -278,4 +282,9 @@ impl IfaceCommon {
pub fn bind_socket(&self, socket: Arc<dyn InetSocket>) { pub fn bind_socket(&self, socket: Arc<dyn InetSocket>) {
self.bounds.write().push(socket); self.bounds.write().push(socket);
} }
// TODO: 需要在inet实现多网卡监听或路由子系统实现后移除
pub fn is_default_iface(&self) -> bool {
self.default_iface
}
} }

View File

@ -399,7 +399,7 @@ impl VirtioInterface {
device_inner: VirtIONicDeviceInnerWrapper(UnsafeCell::new(device_inner)), device_inner: VirtIONicDeviceInnerWrapper(UnsafeCell::new(device_inner)),
locked_kobj_state: LockedKObjectState::default(), locked_kobj_state: LockedKObjectState::default(),
iface_name: format!("eth{}", iface_id), iface_name: format!("eth{}", iface_id),
iface_common: super::IfaceCommon::new(iface_id, iface), iface_common: super::IfaceCommon::new(iface_id, true, iface),
inner: SpinLock::new(InnerVirtIOInterface { inner: SpinLock::new(InnerVirtIOInterface {
kobj_common: KObjectCommonData::default(), kobj_common: KObjectCommonData::default(),
device_common: DeviceCommonData::default(), device_common: DeviceCommonData::default(),

View File

@ -8,10 +8,7 @@ use system_error::SystemError;
use crate::{ use crate::{
arch::{interrupt::TrapFrame, process::arch_switch_to_user}, arch::{interrupt::TrapFrame, process::arch_switch_to_user},
driver::{ driver::{net::e1000e::e1000e::e1000e_init, virtio::virtio::virtio_probe},
net::{e1000e::e1000e::e1000e_init, loopback::loopback_init},
virtio::virtio::virtio_probe,
},
filesystem::vfs::core::mount_root_fs, filesystem::vfs::core::mount_root_fs,
net::net_core::net_init, net::net_core::net_init,
process::{ process::{
@ -48,7 +45,6 @@ fn kernel_init() -> Result<(), SystemError> {
net_init().unwrap_or_else(|err| { net_init().unwrap_or_else(|err| {
error!("Failed to initialize network: {:?}", err); error!("Failed to initialize network: {:?}", err);
}); });
loopback_init()?;
debug!("initial kernel thread done."); debug!("initial kernel thread done.");

View File

@ -40,8 +40,8 @@ fn dhcp_query() -> Result<(), SystemError> {
let binding = NET_DEVICES.write_irqsave(); let binding = NET_DEVICES.write_irqsave();
log::debug!("binding: {:?}", *binding); log::debug!("binding: {:?}", *binding);
//由于现在os未实现在用户态为网卡动态分配内存而lo网卡的id最先分配且ip固定不能被分配 //由于现在os未实现在用户态为网卡动态分配内存而lo网卡的id最先分配且ip固定不能被分配
//所以特判取用id为0的网卡也就是virto_net //所以特判取用id为1的网卡也就是virtio_net
let net_face = binding.get(&0).ok_or(SystemError::ENODEV)?.clone(); let net_face = binding.get(&1).ok_or(SystemError::ENODEV)?.clone();
drop(binding); drop(binding);

View File

@ -1,5 +1,6 @@
use crate::net::{Iface, NET_DEVICES}; use crate::net::{Iface, NET_DEVICES};
use alloc::sync::Arc; use alloc::sync::Arc;
use alloc::vec::Vec;
use system_error::SystemError::{self, *}; use system_error::SystemError::{self, *};
pub mod port; pub mod port;
@ -47,11 +48,26 @@ impl BoundInner {
// } // }
// 强绑VirtualIO // 强绑VirtualIO
// log::debug!("Not bind to any iface, bind to virtIO"); // log::debug!("Not bind to any iface, bind to virtIO");
let iface = NET_DEVICES
let ifaces: Vec<Arc<dyn Iface>> = NET_DEVICES
.read_irqsave() .read_irqsave()
.get(&0) .iter()
.expect("??bind without virtIO, serious?") .filter_map(|(_, v)| {
.clone(); if v.common().is_default_iface() {
Some(v.clone())
} else {
None
}
})
.collect();
// let iface = NET_DEVICES
// .read_irqsave()
// .get(&0)
// .expect("??bind without virtIO, serious?")
// .clone();
let iface = ifaces[0].clone();
let handle = iface.sockets().lock_no_preempt().add(socket); let handle = iface.sockets().lock_no_preempt().add(socket);
return Ok(Self { handle, iface }); return Ok(Self { handle, iface });
} else { } else {