feat(driver): 把virtio添加到sysfs (#752)

This commit is contained in:
LoGin
2024-04-22 15:11:47 +08:00
committed by GitHub
parent a17651b14b
commit e32effb150
21 changed files with 1131 additions and 279 deletions

View File

@ -5,7 +5,7 @@ use core::{
use alloc::{collections::BTreeMap, sync::Arc};
use crate::{driver::net::NetDriver, libs::rwlock::RwLock};
use crate::{driver::net::NetDevice, libs::rwlock::RwLock};
use smoltcp::wire::IpEndpoint;
use self::socket::SocketInode;
@ -18,7 +18,7 @@ pub mod syscall;
lazy_static! {
/// # 所有网络接口的列表
/// 这个列表在中断上下文会使用到因此需要irqsave
pub static ref NET_DRIVERS: RwLock<BTreeMap<usize, Arc<dyn NetDriver>>> = RwLock::new(BTreeMap::new());
pub static ref NET_DEVICES: RwLock<BTreeMap<usize, Arc<dyn NetDevice>>> = RwLock::new(BTreeMap::new());
}
/// 生成网络接口的id (全局自增)

View File

@ -3,10 +3,10 @@ use smoltcp::{socket::dhcpv4, wire};
use system_error::SystemError;
use crate::{
driver::net::NetDriver,
driver::net::NetDevice,
kdebug, kinfo, kwarn,
libs::rwlock::RwLockReadGuard,
net::{socket::SocketPollMethod, NET_DRIVERS},
net::{socket::SocketPollMethod, NET_DEVICES},
time::timer::{next_n_ms_timer_jiffies, Timer, TimerFunction},
};
@ -41,7 +41,7 @@ pub fn net_init() -> Result<(), SystemError> {
}
fn dhcp_query() -> Result<(), SystemError> {
let binding = NET_DRIVERS.write_irqsave();
let binding = NET_DEVICES.write_irqsave();
let net_face = binding.get(&0).ok_or(SystemError::ENODEV)?.clone();
@ -119,7 +119,7 @@ fn dhcp_query() -> Result<(), SystemError> {
}
pub fn poll_ifaces() {
let guard: RwLockReadGuard<BTreeMap<usize, Arc<dyn NetDriver>>> = NET_DRIVERS.read_irqsave();
let guard: RwLockReadGuard<BTreeMap<usize, Arc<dyn NetDevice>>> = NET_DEVICES.read_irqsave();
if guard.len() == 0 {
kwarn!("poll_ifaces: No net driver found!");
return;
@ -139,8 +139,8 @@ pub fn poll_ifaces() {
pub fn poll_ifaces_try_lock(times: u16) -> Result<(), SystemError> {
let mut i = 0;
while i < times {
let guard: RwLockReadGuard<BTreeMap<usize, Arc<dyn NetDriver>>> =
NET_DRIVERS.read_irqsave();
let guard: RwLockReadGuard<BTreeMap<usize, Arc<dyn NetDevice>>> =
NET_DEVICES.read_irqsave();
if guard.len() == 0 {
kwarn!("poll_ifaces: No net driver found!");
// 没有网卡,返回错误
@ -170,7 +170,7 @@ pub fn poll_ifaces_try_lock(times: u16) -> Result<(), SystemError> {
/// @return 加锁超时返回SystemError::EAGAIN_OR_EWOULDBLOCK
/// @return 没有网卡返回SystemError::ENODEV
pub fn poll_ifaces_try_lock_onetime() -> Result<(), SystemError> {
let guard: RwLockReadGuard<BTreeMap<usize, Arc<dyn NetDriver>>> = NET_DRIVERS.read_irqsave();
let guard: RwLockReadGuard<BTreeMap<usize, Arc<dyn NetDevice>>> = NET_DEVICES.read_irqsave();
if guard.len() == 0 {
kwarn!("poll_ifaces: No net driver found!");
// 没有网卡,返回错误

View File

@ -7,12 +7,12 @@ use system_error::SystemError;
use crate::{
arch::rand::rand,
driver::net::NetDriver,
driver::net::NetDevice,
kerror, kwarn,
libs::rwlock::RwLock,
net::{
event_poll::EPollEventType, net_core::poll_ifaces, Endpoint, Protocol, ShutdownType,
NET_DRIVERS,
NET_DEVICES,
},
};
@ -151,7 +151,7 @@ impl Socket for RawSocket {
socket_set_guard.get_mut::<raw::Socket>(self.handle.smoltcp_handle().unwrap());
// 暴力解决方案只考虑0号网卡。 TODO考虑多网卡的情况
let iface = NET_DRIVERS.read_irqsave().get(&0).unwrap().clone();
let iface = NET_DEVICES.read_irqsave().get(&0).unwrap().clone();
// 构造IP头
let ipv4_src_addr: Option<wire::Ipv4Address> =
@ -700,7 +700,7 @@ impl Socket for TcpSocket {
PORT_MANAGER.bind_port(self.metadata.socket_type, temp_port, self.clone())?;
// kdebug!("temp_port: {}", temp_port);
let iface: Arc<dyn NetDriver> = NET_DRIVERS.write_irqsave().get(&0).unwrap().clone();
let iface: Arc<dyn NetDevice> = NET_DEVICES.write_irqsave().get(&0).unwrap().clone();
let mut inner_iface = iface.inner_iface().lock();
// kdebug!("to connect: {ip:?}");