-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1268 from hermit-os/net-config
refactor(net/virtio): migrate `NetDevCfgRaw` to virtio-spec
- Loading branch information
Showing
15 changed files
with
255 additions
and
302 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
//! A module containing a virtio network driver. | ||
//! | ||
//! The module contains ... | ||
|
||
use alloc::rc::Rc; | ||
use alloc::vec::Vec; | ||
use core::str::FromStr; | ||
|
||
use smoltcp::phy::ChecksumCapabilities; | ||
use virtio_spec::mmio::{DeviceRegisterVolatileFieldAccess, DeviceRegisters}; | ||
use volatile::VolatileRef; | ||
|
||
use crate::drivers::net::virtio::{CtrlQueue, NetDevCfg, RxQueues, TxQueues, VirtioNetDriver}; | ||
use crate::drivers::virtio::error::{VirtioError, VirtioNetError}; | ||
use crate::drivers::virtio::transport::mmio::{ComCfg, IsrStatus, NotifCfg}; | ||
use crate::drivers::virtio::virtqueue::Virtq; | ||
|
||
// Backend-dependent interface for Virtio network driver | ||
impl VirtioNetDriver { | ||
pub fn new( | ||
dev_id: u16, | ||
mut registers: VolatileRef<'static, DeviceRegisters>, | ||
irq: u8, | ||
) -> Result<Self, VirtioNetError> { | ||
let dev_cfg_raw: &'static virtio_spec::net::Config = unsafe { | ||
&*registers | ||
.borrow_mut() | ||
.as_mut_ptr() | ||
.config() | ||
.as_raw_ptr() | ||
.cast::<virtio_spec::net::Config>() | ||
.as_ptr() | ||
}; | ||
let dev_cfg_raw = VolatileRef::from_ref(dev_cfg_raw); | ||
let dev_cfg = NetDevCfg { | ||
raw: dev_cfg_raw, | ||
dev_id, | ||
features: virtio_spec::net::F::empty(), | ||
}; | ||
let isr_stat = IsrStatus::new(registers.borrow_mut()); | ||
let notif_cfg = NotifCfg::new(registers.borrow_mut()); | ||
|
||
let mtu = if let Some(my_mtu) = hermit_var!("HERMIT_MTU") { | ||
u16::from_str(&my_mtu).unwrap() | ||
} else { | ||
// fallback to the default MTU | ||
1514 | ||
}; | ||
|
||
Ok(VirtioNetDriver { | ||
dev_cfg, | ||
com_cfg: ComCfg::new(registers, 1), | ||
isr_stat, | ||
notif_cfg, | ||
ctrl_vq: CtrlQueue::new(None), | ||
recv_vqs: RxQueues::new(Vec::<Rc<dyn Virtq>>::new(), false), | ||
send_vqs: TxQueues::new(Vec::<Rc<dyn Virtq>>::new(), Vec::new(), false), | ||
num_vqs: 0, | ||
irq, | ||
mtu, | ||
checksums: ChecksumCapabilities::default(), | ||
}) | ||
} | ||
|
||
pub fn print_information(&mut self) { | ||
self.com_cfg.print_information(); | ||
if self.dev_status() == virtio_spec::net::S::LINK_UP { | ||
info!("The link of the network device is up!"); | ||
} | ||
} | ||
|
||
/// Initializes virtio network device by mapping configuration layout to | ||
/// respective structs (configuration structs are: | ||
/// | ||
/// Returns a driver instance of | ||
/// [VirtioNetDriver](structs.virtionetdriver.html) or an [VirtioError](enums.virtioerror.html). | ||
pub fn init( | ||
dev_id: u16, | ||
registers: VolatileRef<'static, DeviceRegisters>, | ||
irq_no: u8, | ||
) -> Result<VirtioNetDriver, VirtioError> { | ||
if let Ok(mut drv) = VirtioNetDriver::new(dev_id, registers, irq_no) { | ||
match drv.init_dev() { | ||
Err(error_code) => Err(VirtioError::NetDriver(error_code)), | ||
_ => { | ||
drv.print_information(); | ||
Ok(drv) | ||
} | ||
} | ||
} else { | ||
error!("Unable to create Driver. Aborting!"); | ||
Err(VirtioError::Unknown) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.