113776c11Slogin use core::{ 2e32effb1SLoGin any::Any, 313776c11Slogin cell::UnsafeCell, 413776c11Slogin fmt::Debug, 513776c11Slogin ops::{Deref, DerefMut}, 613776c11Slogin }; 713776c11Slogin 8c566df45SLoGin use alloc::{ 9e32effb1SLoGin string::{String, ToString}, 10c566df45SLoGin sync::{Arc, Weak}, 11e32effb1SLoGin vec::Vec, 12c566df45SLoGin }; 13*2eab6dd7S曾俊 use log::{debug, error}; 14881ff6f9Syuyi2439 use smoltcp::{iface, phy, wire}; 15e32effb1SLoGin use unified_init::macros::unified_init; 16e32effb1SLoGin use virtio_drivers::device::net::VirtIONet; 1713776c11Slogin 18e32effb1SLoGin use super::NetDevice; 1913776c11Slogin use crate::{ 20881ff6f9Syuyi2439 arch::rand::rand, 21b087521eSChiichen driver::{ 2206d5e247SLoGin base::{ 23e32effb1SLoGin class::Class, 24e32effb1SLoGin device::{ 25e32effb1SLoGin bus::Bus, 26e32effb1SLoGin driver::{Driver, DriverCommonData}, 27e32effb1SLoGin Device, DeviceCommonData, DeviceId, DeviceType, IdTable, 2806d5e247SLoGin }, 29e32effb1SLoGin kobject::{KObjType, KObject, KObjectCommonData, KObjectState, LockedKObjectState}, 30e32effb1SLoGin kset::KSet, 31e32effb1SLoGin }, 32e32effb1SLoGin virtio::{ 33e32effb1SLoGin irq::virtio_irq_manager, 34e32effb1SLoGin sysfs::{virtio_bus, virtio_device_manager, virtio_driver_manager}, 35e32effb1SLoGin transport::VirtIOTransport, 36e32effb1SLoGin virtio_impl::HalImpl, 37e32effb1SLoGin VirtIODevice, VirtIODeviceIndex, VirtIODriver, VIRTIO_VENDOR_ID, 38e32effb1SLoGin }, 39d470019bSLoGin }, 40e2841179SLoGin exception::{irqdesc::IrqReturn, IrqNumber}, 41e32effb1SLoGin filesystem::kernfs::KernFSInode, 42e32effb1SLoGin init::initcall::INITCALL_POSTCORE, 43e32effb1SLoGin libs::{ 44e32effb1SLoGin rwlock::{RwLockReadGuard, RwLockWriteGuard}, 45e32effb1SLoGin spinlock::{SpinLock, SpinLockGuard}, 46e32effb1SLoGin }, 47e32effb1SLoGin net::{generate_iface_id, net_core::poll_ifaces_try_lock_onetime, NET_DEVICES}, 4813776c11Slogin time::Instant, 4913776c11Slogin }; 5091e9d4abSLoGin use system_error::SystemError; 5113776c11Slogin 52e32effb1SLoGin static mut VIRTIO_NET_DRIVER: Option<Arc<VirtIONetDriver>> = None; 53e32effb1SLoGin 54e32effb1SLoGin const DEVICE_NAME: &str = "virtio_net"; 55e32effb1SLoGin 56e32effb1SLoGin #[inline(always)] 57e32effb1SLoGin fn virtio_net_driver() -> Arc<VirtIONetDriver> { 58e32effb1SLoGin unsafe { VIRTIO_NET_DRIVER.as_ref().unwrap().clone() } 5913776c11Slogin } 6013776c11Slogin 61e32effb1SLoGin pub struct VirtIoNetImpl { 62e32effb1SLoGin inner: VirtIONet<HalImpl, VirtIOTransport, 2>, 63e32effb1SLoGin } 64e32effb1SLoGin 65e32effb1SLoGin impl VirtIoNetImpl { 66e32effb1SLoGin const fn new(inner: VirtIONet<HalImpl, VirtIOTransport, 2>) -> Self { 67e32effb1SLoGin Self { inner } 6813776c11Slogin } 6913776c11Slogin } 7013776c11Slogin 71e32effb1SLoGin impl Deref for VirtIoNetImpl { 72e32effb1SLoGin type Target = VirtIONet<HalImpl, VirtIOTransport, 2>; 73e32effb1SLoGin fn deref(&self) -> &Self::Target { 74e32effb1SLoGin &self.inner 75e32effb1SLoGin } 76e32effb1SLoGin } 7713776c11Slogin 78e32effb1SLoGin impl DerefMut for VirtIoNetImpl { 79e32effb1SLoGin fn deref_mut(&mut self) -> &mut Self::Target { 80e32effb1SLoGin &mut self.inner 81e32effb1SLoGin } 82e32effb1SLoGin } 83e32effb1SLoGin 84e32effb1SLoGin unsafe impl Send for VirtIoNetImpl {} 85e32effb1SLoGin unsafe impl Sync for VirtIoNetImpl {} 86e32effb1SLoGin 87e32effb1SLoGin #[derive(Debug)] 88e32effb1SLoGin struct VirtIONicDeviceInnerWrapper(UnsafeCell<VirtIONicDeviceInner>); 89e32effb1SLoGin unsafe impl Send for VirtIONicDeviceInnerWrapper {} 90e32effb1SLoGin unsafe impl Sync for VirtIONicDeviceInnerWrapper {} 91e32effb1SLoGin 92e32effb1SLoGin impl Deref for VirtIONicDeviceInnerWrapper { 93e32effb1SLoGin type Target = VirtIONicDeviceInner; 9413776c11Slogin fn deref(&self) -> &Self::Target { 9513776c11Slogin unsafe { &*self.0.get() } 9613776c11Slogin } 9713776c11Slogin } 98e32effb1SLoGin impl DerefMut for VirtIONicDeviceInnerWrapper { 9913776c11Slogin fn deref_mut(&mut self) -> &mut Self::Target { 10013776c11Slogin unsafe { &mut *self.0.get() } 10113776c11Slogin } 10213776c11Slogin } 10313776c11Slogin 104b5b571e0SLoGin #[allow(clippy::mut_from_ref)] 105e32effb1SLoGin impl VirtIONicDeviceInnerWrapper { 106e32effb1SLoGin fn force_get_mut(&self) -> &mut <VirtIONicDeviceInnerWrapper as Deref>::Target { 10713776c11Slogin unsafe { &mut *self.0.get() } 10813776c11Slogin } 10913776c11Slogin } 11013776c11Slogin 111e32effb1SLoGin /// Virtio网络设备驱动(加锁) 112e32effb1SLoGin pub struct VirtIONicDeviceInner { 113e32effb1SLoGin pub inner: Arc<SpinLock<VirtIoNetImpl>>, 114e32effb1SLoGin } 115e32effb1SLoGin 116e32effb1SLoGin impl Clone for VirtIONicDeviceInner { 117e32effb1SLoGin fn clone(&self) -> Self { 118e32effb1SLoGin return VirtIONicDeviceInner { 119e32effb1SLoGin inner: self.inner.clone(), 120e32effb1SLoGin }; 121e32effb1SLoGin } 122e32effb1SLoGin } 123e32effb1SLoGin 124e32effb1SLoGin impl Debug for VirtIONicDeviceInner { 12513776c11Slogin fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 126e32effb1SLoGin f.debug_struct("VirtIONicDriver").finish() 12713776c11Slogin } 12813776c11Slogin } 12913776c11Slogin 130e32effb1SLoGin #[cast_to([sync] VirtIODevice)] 131731bc2b3SLoGin #[cast_to([sync] Device)] 132e32effb1SLoGin pub struct VirtioInterface { 133e32effb1SLoGin device_inner: VirtIONicDeviceInnerWrapper, 13413776c11Slogin iface_id: usize, 135e32effb1SLoGin iface_name: String, 136e2841179SLoGin dev_id: Arc<DeviceId>, 137e32effb1SLoGin iface: SpinLock<iface::Interface>, 138e32effb1SLoGin inner: SpinLock<InnerVirtIOInterface>, 139e32effb1SLoGin locked_kobj_state: LockedKObjectState, 14013776c11Slogin } 14113776c11Slogin 142e32effb1SLoGin #[derive(Debug)] 143e32effb1SLoGin struct InnerVirtIOInterface { 144e32effb1SLoGin name: Option<String>, 145e32effb1SLoGin virtio_index: Option<VirtIODeviceIndex>, 146e32effb1SLoGin device_common: DeviceCommonData, 147e32effb1SLoGin kobj_common: KObjectCommonData, 148e32effb1SLoGin } 149e32effb1SLoGin 150e32effb1SLoGin impl core::fmt::Debug for VirtioInterface { 15113776c11Slogin fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 15213776c11Slogin f.debug_struct("VirtioInterface") 15313776c11Slogin .field("iface_id", &self.iface_id) 154e32effb1SLoGin .field("iface_name", &self.iface_name) 155e32effb1SLoGin .field("dev_id", &self.dev_id) 156e32effb1SLoGin .field("inner", &self.inner) 157e32effb1SLoGin .field("locked_kobj_state", &self.locked_kobj_state) 15813776c11Slogin .finish() 15913776c11Slogin } 16013776c11Slogin } 16113776c11Slogin 162e32effb1SLoGin impl VirtioInterface { 163e32effb1SLoGin pub fn new(mut device_inner: VirtIONicDeviceInner, dev_id: Arc<DeviceId>) -> Arc<Self> { 16413776c11Slogin let iface_id = generate_iface_id(); 165881ff6f9Syuyi2439 let mut iface_config = iface::Config::new(wire::HardwareAddress::Ethernet( 166e32effb1SLoGin wire::EthernetAddress(device_inner.inner.lock().mac_address()), 16713776c11Slogin )); 168881ff6f9Syuyi2439 iface_config.random_seed = rand() as u64; 169881ff6f9Syuyi2439 170e32effb1SLoGin let iface = iface::Interface::new(iface_config, &mut device_inner, Instant::now().into()); 17113776c11Slogin 17213776c11Slogin let result = Arc::new(VirtioInterface { 173e32effb1SLoGin device_inner: VirtIONicDeviceInnerWrapper(UnsafeCell::new(device_inner)), 17413776c11Slogin iface_id, 175e32effb1SLoGin locked_kobj_state: LockedKObjectState::default(), 17613776c11Slogin iface: SpinLock::new(iface), 177e32effb1SLoGin iface_name: format!("eth{}", iface_id), 178e2841179SLoGin dev_id, 179e32effb1SLoGin inner: SpinLock::new(InnerVirtIOInterface { 180e32effb1SLoGin name: None, 181e32effb1SLoGin virtio_index: None, 182e32effb1SLoGin device_common: DeviceCommonData::default(), 183e32effb1SLoGin kobj_common: KObjectCommonData::default(), 184e32effb1SLoGin }), 18513776c11Slogin }); 18613776c11Slogin 187e32effb1SLoGin result.inner().device_common.driver = 188e32effb1SLoGin Some(Arc::downgrade(&virtio_net_driver()) as Weak<dyn Driver>); 189e32effb1SLoGin 19013776c11Slogin return result; 19113776c11Slogin } 192e32effb1SLoGin 193e32effb1SLoGin fn inner(&self) -> SpinLockGuard<InnerVirtIOInterface> { 194e32effb1SLoGin return self.inner.lock(); 19513776c11Slogin } 19613776c11Slogin 197e32effb1SLoGin /// 获取网卡接口的名称 198e32effb1SLoGin #[allow(dead_code)] 199e32effb1SLoGin pub fn iface_name(&self) -> String { 200e32effb1SLoGin self.iface_name.clone() 201e32effb1SLoGin } 202e32effb1SLoGin } 203e32effb1SLoGin 204e32effb1SLoGin impl VirtIODevice for VirtioInterface { 205e2841179SLoGin fn handle_irq(&self, _irq: IrqNumber) -> Result<IrqReturn, SystemError> { 206e2841179SLoGin poll_ifaces_try_lock_onetime().ok(); 207e2841179SLoGin return Ok(IrqReturn::Handled); 208e2841179SLoGin } 209e2841179SLoGin 2100102d69fSLoGin fn irq(&self) -> Option<IrqNumber> { 2110102d69fSLoGin None 2120102d69fSLoGin } 2130102d69fSLoGin 214e2841179SLoGin fn dev_id(&self) -> &Arc<DeviceId> { 215e2841179SLoGin return &self.dev_id; 216e2841179SLoGin } 217e32effb1SLoGin fn set_virtio_device_index(&self, index: VirtIODeviceIndex) { 218e32effb1SLoGin self.inner().virtio_index = Some(index); 219e2841179SLoGin } 220e2841179SLoGin 221e32effb1SLoGin fn virtio_device_index(&self) -> Option<VirtIODeviceIndex> { 222e32effb1SLoGin return self.inner().virtio_index; 223e32effb1SLoGin } 224e32effb1SLoGin 225e32effb1SLoGin fn set_device_name(&self, name: String) { 226e32effb1SLoGin self.inner().name = Some(name); 227e32effb1SLoGin } 228e32effb1SLoGin 229e32effb1SLoGin fn device_name(&self) -> String { 230e32effb1SLoGin self.inner() 231e32effb1SLoGin .name 232e32effb1SLoGin .clone() 233e32effb1SLoGin .unwrap_or_else(|| "virtio_net".to_string()) 234e32effb1SLoGin } 235e32effb1SLoGin 236e32effb1SLoGin fn device_type_id(&self) -> u32 { 237e32effb1SLoGin virtio_drivers::transport::DeviceType::Network as u32 238e32effb1SLoGin } 239e32effb1SLoGin 240e32effb1SLoGin fn vendor(&self) -> u32 { 241e32effb1SLoGin VIRTIO_VENDOR_ID.into() 242e32effb1SLoGin } 243e32effb1SLoGin } 244e32effb1SLoGin 245e32effb1SLoGin impl Drop for VirtioInterface { 246e2841179SLoGin fn drop(&mut self) { 247e2841179SLoGin // 从全局的网卡接口信息表中删除这个网卡的接口信息 248e32effb1SLoGin NET_DEVICES.write_irqsave().remove(&self.iface_id); 249e2841179SLoGin } 250e2841179SLoGin } 251e2841179SLoGin 252e32effb1SLoGin impl Device for VirtioInterface { 253e32effb1SLoGin fn dev_type(&self) -> DeviceType { 254e32effb1SLoGin DeviceType::Net 255e32effb1SLoGin } 256e32effb1SLoGin 257e32effb1SLoGin fn id_table(&self) -> IdTable { 258e32effb1SLoGin IdTable::new(DEVICE_NAME.to_string(), None) 259e32effb1SLoGin } 260e32effb1SLoGin 261e32effb1SLoGin fn bus(&self) -> Option<Weak<dyn Bus>> { 262e32effb1SLoGin self.inner().device_common.bus.clone() 263e32effb1SLoGin } 264e32effb1SLoGin 265e32effb1SLoGin fn set_bus(&self, bus: Option<Weak<dyn Bus>>) { 266e32effb1SLoGin self.inner().device_common.bus = bus; 267e32effb1SLoGin } 268e32effb1SLoGin 269e32effb1SLoGin fn class(&self) -> Option<Arc<dyn Class>> { 270e32effb1SLoGin let mut guard = self.inner(); 271e32effb1SLoGin let r = guard.device_common.class.clone()?.upgrade(); 272e32effb1SLoGin if r.is_none() { 273e32effb1SLoGin guard.device_common.class = None; 274e32effb1SLoGin } 275e32effb1SLoGin 276e32effb1SLoGin return r; 277e32effb1SLoGin } 278e32effb1SLoGin 279e32effb1SLoGin fn set_class(&self, class: Option<Weak<dyn Class>>) { 280e32effb1SLoGin self.inner().device_common.class = class; 281e32effb1SLoGin } 282e32effb1SLoGin 283e32effb1SLoGin fn driver(&self) -> Option<Arc<dyn Driver>> { 284e32effb1SLoGin let r = self.inner().device_common.driver.clone()?.upgrade(); 285e32effb1SLoGin if r.is_none() { 286e32effb1SLoGin self.inner().device_common.driver = None; 287e32effb1SLoGin } 288e32effb1SLoGin 289e32effb1SLoGin return r; 290e32effb1SLoGin } 291e32effb1SLoGin 292e32effb1SLoGin fn set_driver(&self, driver: Option<Weak<dyn Driver>>) { 293e32effb1SLoGin self.inner().device_common.driver = driver; 294e32effb1SLoGin } 295e32effb1SLoGin 296e32effb1SLoGin fn is_dead(&self) -> bool { 297e32effb1SLoGin false 298e32effb1SLoGin } 299e32effb1SLoGin 300e32effb1SLoGin fn can_match(&self) -> bool { 301731bc2b3SLoGin self.inner().device_common.can_match 302e32effb1SLoGin } 303e32effb1SLoGin 304e32effb1SLoGin fn set_can_match(&self, can_match: bool) { 305e32effb1SLoGin self.inner().device_common.can_match = can_match; 306e32effb1SLoGin } 307e32effb1SLoGin 308e32effb1SLoGin fn state_synced(&self) -> bool { 309e32effb1SLoGin true 310e32effb1SLoGin } 311e32effb1SLoGin } 312e32effb1SLoGin 313e32effb1SLoGin impl VirtIONicDeviceInner { 314e32effb1SLoGin pub fn new(driver_net: VirtIONet<HalImpl, VirtIOTransport, 2>) -> Self { 315881ff6f9Syuyi2439 let mut iface_config = iface::Config::new(wire::HardwareAddress::Ethernet( 316881ff6f9Syuyi2439 wire::EthernetAddress(driver_net.mac_address()), 31713776c11Slogin )); 31813776c11Slogin 319881ff6f9Syuyi2439 iface_config.random_seed = rand() as u64; 320881ff6f9Syuyi2439 321e32effb1SLoGin let inner = Arc::new(SpinLock::new(VirtIoNetImpl::new(driver_net))); 322e32effb1SLoGin let result = VirtIONicDeviceInner { inner }; 32313776c11Slogin return result; 32413776c11Slogin } 32513776c11Slogin } 32613776c11Slogin 327e32effb1SLoGin pub struct VirtioNetToken { 328e32effb1SLoGin driver: VirtIONicDeviceInner, 32913776c11Slogin rx_buffer: Option<virtio_drivers::device::net::RxBuffer>, 33013776c11Slogin } 33113776c11Slogin 332e32effb1SLoGin impl VirtioNetToken { 33313776c11Slogin pub fn new( 334e32effb1SLoGin driver: VirtIONicDeviceInner, 33513776c11Slogin rx_buffer: Option<virtio_drivers::device::net::RxBuffer>, 33613776c11Slogin ) -> Self { 33713776c11Slogin return Self { driver, rx_buffer }; 33813776c11Slogin } 33913776c11Slogin } 34013776c11Slogin 341e32effb1SLoGin impl phy::Device for VirtIONicDeviceInner { 342e32effb1SLoGin type RxToken<'a> = VirtioNetToken where Self: 'a; 343e32effb1SLoGin type TxToken<'a> = VirtioNetToken where Self: 'a; 34413776c11Slogin 34513776c11Slogin fn receive( 34613776c11Slogin &mut self, 34713776c11Slogin _timestamp: smoltcp::time::Instant, 34813776c11Slogin ) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> { 34913776c11Slogin match self.inner.lock().receive() { 35013776c11Slogin Ok(buf) => Some(( 35113776c11Slogin VirtioNetToken::new(self.clone(), Some(buf)), 35213776c11Slogin VirtioNetToken::new(self.clone(), None), 35313776c11Slogin )), 35413776c11Slogin Err(virtio_drivers::Error::NotReady) => None, 35513776c11Slogin Err(err) => panic!("VirtIO receive failed: {}", err), 35613776c11Slogin } 35713776c11Slogin } 35813776c11Slogin 35913776c11Slogin fn transmit(&mut self, _timestamp: smoltcp::time::Instant) -> Option<Self::TxToken<'_>> { 360*2eab6dd7S曾俊 // debug!("VirtioNet: transmit"); 36140609970SGnoCiYeH if self.inner.lock_irqsave().can_send() { 362*2eab6dd7S曾俊 // debug!("VirtioNet: can send"); 36313776c11Slogin return Some(VirtioNetToken::new(self.clone(), None)); 36413776c11Slogin } else { 365*2eab6dd7S曾俊 // debug!("VirtioNet: can not send"); 36613776c11Slogin return None; 36713776c11Slogin } 36813776c11Slogin } 36913776c11Slogin 37013776c11Slogin fn capabilities(&self) -> phy::DeviceCapabilities { 37113776c11Slogin let mut caps = phy::DeviceCapabilities::default(); 37213776c11Slogin // 网卡的最大传输单元. 请与IP层的MTU进行区分。这个值应当是网卡的最大传输单元,而不是IP层的MTU。 37313776c11Slogin caps.max_transmission_unit = 2000; 37413776c11Slogin /* 37513776c11Slogin Maximum burst size, in terms of MTU. 37613776c11Slogin The network device is unable to send or receive bursts large than the value returned by this function. 37713776c11Slogin If None, there is no fixed limit on burst size, e.g. if network buffers are dynamically allocated. 37813776c11Slogin */ 37913776c11Slogin caps.max_burst_size = Some(1); 38013776c11Slogin return caps; 38113776c11Slogin } 38213776c11Slogin } 38313776c11Slogin 384e32effb1SLoGin impl phy::TxToken for VirtioNetToken { 38513776c11Slogin fn consume<R, F>(self, len: usize, f: F) -> R 38613776c11Slogin where 38713776c11Slogin F: FnOnce(&mut [u8]) -> R, 38813776c11Slogin { 38913776c11Slogin // // 为了线程安全,这里需要对VirtioNet进行加【写锁】,以保证对设备的互斥访问。 39013776c11Slogin let mut driver_net = self.driver.inner.lock(); 39113776c11Slogin let mut tx_buf = driver_net.new_tx_buffer(len); 39213776c11Slogin let result = f(tx_buf.packet_mut()); 39313776c11Slogin driver_net.send(tx_buf).expect("virtio_net send failed"); 39413776c11Slogin return result; 39513776c11Slogin } 39613776c11Slogin } 39713776c11Slogin 398e32effb1SLoGin impl phy::RxToken for VirtioNetToken { 39913776c11Slogin fn consume<R, F>(self, f: F) -> R 40013776c11Slogin where 40113776c11Slogin F: FnOnce(&mut [u8]) -> R, 40213776c11Slogin { 40313776c11Slogin // 为了线程安全,这里需要对VirtioNet进行加【写锁】,以保证对设备的互斥访问。 40413776c11Slogin let mut rx_buf = self.rx_buffer.unwrap(); 40513776c11Slogin let result = f(rx_buf.packet_mut()); 40613776c11Slogin self.driver 40713776c11Slogin .inner 40813776c11Slogin .lock() 40913776c11Slogin .recycle_rx_buffer(rx_buf) 41013776c11Slogin .expect("virtio_net recv failed"); 41113776c11Slogin result 41213776c11Slogin } 41313776c11Slogin } 41413776c11Slogin 41513776c11Slogin /// @brief virtio-net 驱动的初始化与测试 416e32effb1SLoGin pub fn virtio_net(transport: VirtIOTransport, dev_id: Arc<DeviceId>) { 417e32effb1SLoGin let driver_net: VirtIONet<HalImpl, VirtIOTransport, 2> = 418e32effb1SLoGin match VirtIONet::<HalImpl, VirtIOTransport, 2>::new(transport, 4096) { 41913776c11Slogin Ok(net) => net, 42013776c11Slogin Err(_) => { 421*2eab6dd7S曾俊 error!("VirtIONet init failed"); 42213776c11Slogin return; 42313776c11Slogin } 42413776c11Slogin }; 425881ff6f9Syuyi2439 let mac = wire::EthernetAddress::from_bytes(&driver_net.mac_address()); 426e32effb1SLoGin let dev_inner = VirtIONicDeviceInner::new(driver_net); 427e32effb1SLoGin let iface = VirtioInterface::new(dev_inner, dev_id); 428*2eab6dd7S曾俊 debug!("To add virtio net: {}, mac: {}", iface.device_name(), mac); 429e32effb1SLoGin virtio_device_manager() 430e32effb1SLoGin .device_add(iface.clone() as Arc<dyn VirtIODevice>) 431e32effb1SLoGin .expect("Add virtio net failed"); 43213776c11Slogin } 43313776c11Slogin 434e32effb1SLoGin impl NetDevice for VirtioInterface { 435881ff6f9Syuyi2439 fn mac(&self) -> wire::EthernetAddress { 436e32effb1SLoGin let mac: [u8; 6] = self.device_inner.inner.lock().mac_address(); 437881ff6f9Syuyi2439 return wire::EthernetAddress::from_bytes(&mac); 43813776c11Slogin } 43913776c11Slogin 44013776c11Slogin #[inline] 44113776c11Slogin fn nic_id(&self) -> usize { 44213776c11Slogin return self.iface_id; 44313776c11Slogin } 44413776c11Slogin 44513776c11Slogin #[inline] 44613776c11Slogin fn name(&self) -> String { 447e32effb1SLoGin return self.iface_name.clone(); 44813776c11Slogin } 44913776c11Slogin 45013776c11Slogin fn update_ip_addrs(&self, ip_addrs: &[wire::IpCidr]) -> Result<(), SystemError> { 45113776c11Slogin if ip_addrs.len() != 1 { 45213776c11Slogin return Err(SystemError::EINVAL); 45313776c11Slogin } 45413776c11Slogin 45513776c11Slogin self.iface.lock().update_ip_addrs(|addrs| { 45613776c11Slogin let dest = addrs.iter_mut().next(); 457b5b571e0SLoGin 458b5b571e0SLoGin if let Some(dest) = dest { 45913776c11Slogin *dest = ip_addrs[0]; 460b5b571e0SLoGin } else { 461881ff6f9Syuyi2439 addrs 462881ff6f9Syuyi2439 .push(ip_addrs[0]) 463881ff6f9Syuyi2439 .expect("Push wire::IpCidr failed: full"); 46413776c11Slogin } 46513776c11Slogin }); 46613776c11Slogin return Ok(()); 46713776c11Slogin } 46813776c11Slogin 469881ff6f9Syuyi2439 fn poll(&self, sockets: &mut iface::SocketSet) -> Result<(), SystemError> { 47013776c11Slogin let timestamp: smoltcp::time::Instant = Instant::now().into(); 47113776c11Slogin let mut guard = self.iface.lock(); 472e32effb1SLoGin let poll_res = guard.poll(timestamp, self.device_inner.force_get_mut(), sockets); 47313776c11Slogin // todo: notify!!! 474*2eab6dd7S曾俊 // debug!("Virtio Interface poll:{poll_res}"); 47513776c11Slogin if poll_res { 47613776c11Slogin return Ok(()); 47713776c11Slogin } 47879a452ceShoumkh return Err(SystemError::EAGAIN_OR_EWOULDBLOCK); 47913776c11Slogin } 48013776c11Slogin 48113776c11Slogin #[inline(always)] 482881ff6f9Syuyi2439 fn inner_iface(&self) -> &SpinLock<iface::Interface> { 48313776c11Slogin return &self.iface; 48413776c11Slogin } 48513776c11Slogin // fn as_any_ref(&'static self) -> &'static dyn core::any::Any { 48613776c11Slogin // return self; 48713776c11Slogin // } 48813776c11Slogin } 48913776c11Slogin 490e32effb1SLoGin impl KObject for VirtioInterface { 49106d5e247SLoGin fn as_any_ref(&self) -> &dyn core::any::Any { 49206d5e247SLoGin self 49306d5e247SLoGin } 49406d5e247SLoGin 495e32effb1SLoGin fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 496e32effb1SLoGin self.inner().kobj_common.kern_inode = inode; 49706d5e247SLoGin } 49806d5e247SLoGin 499e32effb1SLoGin fn inode(&self) -> Option<Arc<KernFSInode>> { 500e32effb1SLoGin self.inner().kobj_common.kern_inode.clone() 50106d5e247SLoGin } 50206d5e247SLoGin 503e32effb1SLoGin fn parent(&self) -> Option<Weak<dyn KObject>> { 504e32effb1SLoGin self.inner().kobj_common.parent.clone() 50506d5e247SLoGin } 50606d5e247SLoGin 507e32effb1SLoGin fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 508e32effb1SLoGin self.inner().kobj_common.parent = parent; 50906d5e247SLoGin } 51006d5e247SLoGin 511e32effb1SLoGin fn kset(&self) -> Option<Arc<KSet>> { 512e32effb1SLoGin self.inner().kobj_common.kset.clone() 51306d5e247SLoGin } 51406d5e247SLoGin 515e32effb1SLoGin fn set_kset(&self, kset: Option<Arc<KSet>>) { 516e32effb1SLoGin self.inner().kobj_common.kset = kset; 51706d5e247SLoGin } 51806d5e247SLoGin 519e32effb1SLoGin fn kobj_type(&self) -> Option<&'static dyn KObjType> { 520e32effb1SLoGin self.inner().kobj_common.kobj_type 52106d5e247SLoGin } 52206d5e247SLoGin 52306d5e247SLoGin fn name(&self) -> String { 524e32effb1SLoGin self.device_name() 52506d5e247SLoGin } 52606d5e247SLoGin 52706d5e247SLoGin fn set_name(&self, _name: String) { 528e32effb1SLoGin // do nothing 52906d5e247SLoGin } 53006d5e247SLoGin 531e32effb1SLoGin fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 532e32effb1SLoGin self.locked_kobj_state.read() 53306d5e247SLoGin } 53406d5e247SLoGin 535e32effb1SLoGin fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 536e32effb1SLoGin self.locked_kobj_state.write() 53706d5e247SLoGin } 53806d5e247SLoGin 539e32effb1SLoGin fn set_kobj_state(&self, state: KObjectState) { 540e32effb1SLoGin *self.locked_kobj_state.write() = state; 541a03c4f9dSLoGin } 542a03c4f9dSLoGin 543e32effb1SLoGin fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 544e32effb1SLoGin self.inner().kobj_common.kobj_type = ktype; 54506d5e247SLoGin } 54606d5e247SLoGin } 54706d5e247SLoGin 548e32effb1SLoGin #[unified_init(INITCALL_POSTCORE)] 549e32effb1SLoGin fn virtio_net_driver_init() -> Result<(), SystemError> { 550e32effb1SLoGin let driver = VirtIONetDriver::new(); 551e32effb1SLoGin virtio_driver_manager() 552e32effb1SLoGin .register(driver.clone() as Arc<dyn VirtIODriver>) 553e32effb1SLoGin .expect("Add virtio net driver failed"); 554e32effb1SLoGin unsafe { 555e32effb1SLoGin VIRTIO_NET_DRIVER = Some(driver); 556e32effb1SLoGin } 557e32effb1SLoGin 558e32effb1SLoGin return Ok(()); 559e32effb1SLoGin } 560731bc2b3SLoGin 561e32effb1SLoGin #[derive(Debug)] 562e32effb1SLoGin #[cast_to([sync] VirtIODriver)] 563731bc2b3SLoGin #[cast_to([sync] Driver)] 564e32effb1SLoGin struct VirtIONetDriver { 565e32effb1SLoGin inner: SpinLock<InnerVirtIODriver>, 566e32effb1SLoGin kobj_state: LockedKObjectState, 567e32effb1SLoGin } 568e32effb1SLoGin 569e32effb1SLoGin impl VirtIONetDriver { 570e32effb1SLoGin pub fn new() -> Arc<Self> { 571e32effb1SLoGin let inner = InnerVirtIODriver { 572e32effb1SLoGin driver_common: DriverCommonData::default(), 573e32effb1SLoGin kobj_common: KObjectCommonData::default(), 574e32effb1SLoGin }; 575e32effb1SLoGin Arc::new(VirtIONetDriver { 576e32effb1SLoGin inner: SpinLock::new(inner), 577e32effb1SLoGin kobj_state: LockedKObjectState::default(), 578e32effb1SLoGin }) 579e32effb1SLoGin } 580e32effb1SLoGin 581e32effb1SLoGin fn inner(&self) -> SpinLockGuard<InnerVirtIODriver> { 582e32effb1SLoGin return self.inner.lock(); 583e32effb1SLoGin } 584e32effb1SLoGin } 585e32effb1SLoGin 586e32effb1SLoGin #[derive(Debug)] 587e32effb1SLoGin struct InnerVirtIODriver { 588e32effb1SLoGin driver_common: DriverCommonData, 589e32effb1SLoGin kobj_common: KObjectCommonData, 590e32effb1SLoGin } 591e32effb1SLoGin 592e32effb1SLoGin impl VirtIODriver for VirtIONetDriver { 593e32effb1SLoGin fn probe(&self, device: &Arc<dyn VirtIODevice>) -> Result<(), SystemError> { 594e32effb1SLoGin let iface = device 595e32effb1SLoGin .clone() 596e32effb1SLoGin .arc_any() 597e32effb1SLoGin .downcast::<VirtioInterface>() 598e32effb1SLoGin .map_err(|_| { 599*2eab6dd7S曾俊 error!( 600e32effb1SLoGin "VirtIONetDriver::probe() failed: device is not a VirtioInterface. Device: '{:?}'", 601e32effb1SLoGin device.name() 602e32effb1SLoGin ); 603e32effb1SLoGin SystemError::EINVAL 604e32effb1SLoGin })?; 605e32effb1SLoGin 606e32effb1SLoGin // 将网卡的接口信息注册到全局的网卡接口信息表中 607e32effb1SLoGin NET_DEVICES 608e32effb1SLoGin .write_irqsave() 609e32effb1SLoGin .insert(iface.nic_id(), iface.clone()); 610e32effb1SLoGin 611e32effb1SLoGin virtio_irq_manager() 612e32effb1SLoGin .register_device(iface.clone()) 613e32effb1SLoGin .expect("Register virtio net failed"); 614e32effb1SLoGin 615e32effb1SLoGin return Ok(()); 616e32effb1SLoGin } 617e32effb1SLoGin } 618e32effb1SLoGin 619e32effb1SLoGin impl Driver for VirtIONetDriver { 620e32effb1SLoGin fn id_table(&self) -> Option<IdTable> { 621e32effb1SLoGin Some(IdTable::new(DEVICE_NAME.to_string(), None)) 622e32effb1SLoGin } 623e32effb1SLoGin 624e32effb1SLoGin fn add_device(&self, device: Arc<dyn Device>) { 625e32effb1SLoGin let iface = device 626e32effb1SLoGin .arc_any() 627e32effb1SLoGin .downcast::<VirtioInterface>() 628e32effb1SLoGin .expect("VirtIONetDriver::add_device() failed: device is not a VirtioInterface"); 629e32effb1SLoGin 630e32effb1SLoGin self.inner() 631e32effb1SLoGin .driver_common 632e32effb1SLoGin .devices 633e32effb1SLoGin .push(iface as Arc<dyn Device>); 634e32effb1SLoGin } 635e32effb1SLoGin 636e32effb1SLoGin fn delete_device(&self, device: &Arc<dyn Device>) { 637e32effb1SLoGin let _iface = device 638e32effb1SLoGin .clone() 639e32effb1SLoGin .arc_any() 640e32effb1SLoGin .downcast::<VirtioInterface>() 641e32effb1SLoGin .expect("VirtIONetDriver::delete_device() failed: device is not a VirtioInterface"); 642e32effb1SLoGin 643e32effb1SLoGin let mut guard = self.inner(); 644e32effb1SLoGin let index = guard 645e32effb1SLoGin .driver_common 646e32effb1SLoGin .devices 647e32effb1SLoGin .iter() 648e32effb1SLoGin .position(|dev| Arc::ptr_eq(device, dev)) 649e32effb1SLoGin .expect("VirtIONetDriver::delete_device() failed: device not found"); 650e32effb1SLoGin 651e32effb1SLoGin guard.driver_common.devices.remove(index); 652e32effb1SLoGin } 653e32effb1SLoGin 654e32effb1SLoGin fn devices(&self) -> Vec<Arc<dyn Device>> { 655e32effb1SLoGin self.inner().driver_common.devices.clone() 656e32effb1SLoGin } 657e32effb1SLoGin 658e32effb1SLoGin fn bus(&self) -> Option<Weak<dyn Bus>> { 659e32effb1SLoGin Some(Arc::downgrade(&virtio_bus()) as Weak<dyn Bus>) 660e32effb1SLoGin } 661e32effb1SLoGin 662e32effb1SLoGin fn set_bus(&self, _bus: Option<Weak<dyn Bus>>) { 663e32effb1SLoGin // do nothing 664e32effb1SLoGin } 665e32effb1SLoGin } 666e32effb1SLoGin 667e32effb1SLoGin impl KObject for VirtIONetDriver { 668e32effb1SLoGin fn as_any_ref(&self) -> &dyn Any { 669e32effb1SLoGin self 670e32effb1SLoGin } 671e32effb1SLoGin 672e32effb1SLoGin fn set_inode(&self, inode: Option<Arc<KernFSInode>>) { 673e32effb1SLoGin self.inner().kobj_common.kern_inode = inode; 674e32effb1SLoGin } 675e32effb1SLoGin 676e32effb1SLoGin fn inode(&self) -> Option<Arc<KernFSInode>> { 677e32effb1SLoGin self.inner().kobj_common.kern_inode.clone() 678e32effb1SLoGin } 679e32effb1SLoGin 680e32effb1SLoGin fn parent(&self) -> Option<Weak<dyn KObject>> { 681e32effb1SLoGin self.inner().kobj_common.parent.clone() 682e32effb1SLoGin } 683e32effb1SLoGin 684e32effb1SLoGin fn set_parent(&self, parent: Option<Weak<dyn KObject>>) { 685e32effb1SLoGin self.inner().kobj_common.parent = parent; 686e32effb1SLoGin } 687e32effb1SLoGin 688e32effb1SLoGin fn kset(&self) -> Option<Arc<KSet>> { 689e32effb1SLoGin self.inner().kobj_common.kset.clone() 690e32effb1SLoGin } 691e32effb1SLoGin 692e32effb1SLoGin fn set_kset(&self, kset: Option<Arc<KSet>>) { 693e32effb1SLoGin self.inner().kobj_common.kset = kset; 694e32effb1SLoGin } 695e32effb1SLoGin 696e32effb1SLoGin fn kobj_type(&self) -> Option<&'static dyn KObjType> { 697e32effb1SLoGin self.inner().kobj_common.kobj_type 698e32effb1SLoGin } 699e32effb1SLoGin 700e32effb1SLoGin fn set_kobj_type(&self, ktype: Option<&'static dyn KObjType>) { 701e32effb1SLoGin self.inner().kobj_common.kobj_type = ktype; 702e32effb1SLoGin } 703e32effb1SLoGin 704e32effb1SLoGin fn name(&self) -> String { 705e32effb1SLoGin DEVICE_NAME.to_string() 706e32effb1SLoGin } 707e32effb1SLoGin 708e32effb1SLoGin fn set_name(&self, _name: String) { 709e32effb1SLoGin // do nothing 710e32effb1SLoGin } 711e32effb1SLoGin 712e32effb1SLoGin fn kobj_state(&self) -> RwLockReadGuard<KObjectState> { 713e32effb1SLoGin self.kobj_state.read() 714e32effb1SLoGin } 715e32effb1SLoGin 716e32effb1SLoGin fn kobj_state_mut(&self) -> RwLockWriteGuard<KObjectState> { 717e32effb1SLoGin self.kobj_state.write() 718e32effb1SLoGin } 719e32effb1SLoGin 720e32effb1SLoGin fn set_kobj_state(&self, state: KObjectState) { 721e32effb1SLoGin *self.kobj_state.write() = state; 722e32effb1SLoGin } 723e32effb1SLoGin } 724