Skip to content

Commit

Permalink
Merge pull request #1316 from cagatay-y/bufferelem
Browse files Browse the repository at this point in the history
refactor(virtq): use proper types for buffer elements
  • Loading branch information
mkroening authored Jul 31, 2024
2 parents f71d2db + c66c363 commit d0d2078
Show file tree
Hide file tree
Showing 10 changed files with 495 additions and 1,923 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ crossbeam-utils = { version = "0.8", default-features = false }
dyn-clone = "1.0"
fdt = { version = "0.1", features = ["pretty-printing"] }
free-list = "0.3"
fuse-abi = { version = "0.1", optional = true }
fuse-abi = { version = "0.1", features = ["zerocopy"], optional = true }
hashbrown = { version = "0.14", default-features = false }
hermit-entry = { version = "0.10", features = ["kernel"] }
hermit-sync = "0.1"
Expand Down
46 changes: 23 additions & 23 deletions src/drivers/fs/virtio_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use alloc::boxed::Box;
use alloc::rc::Rc;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::mem::MaybeUninit;
use core::str;

use pci_types::InterruptLine;
Expand All @@ -20,9 +19,10 @@ use crate::drivers::virtio::transport::pci::{ComCfg, IsrStatus, NotifCfg};
use crate::drivers::virtio::virtqueue::error::VirtqError;
use crate::drivers::virtio::virtqueue::split::SplitVq;
use crate::drivers::virtio::virtqueue::{
AsSliceU8, BufferToken, BufferType, Virtq, VqIndex, VqSize,
AvailBufferToken, BufferElem, BufferType, Virtq, VqIndex, VqSize,
};
use crate::fs::fuse::{self, FuseInterface, Rsp, RspHeader};
use crate::mm::device_alloc::DeviceAlloc;

/// A wrapper struct for the raw configuration structure.
/// Handling the right access to fields, as some are read-only
Expand Down Expand Up @@ -150,7 +150,7 @@ impl VirtioFsDriver {
}

impl FuseInterface for VirtioFsDriver {
fn send_command<O: fuse::ops::Op>(
fn send_command<O: fuse::ops::Op + 'static>(
&mut self,
cmd: fuse::Cmd<O>,
rsp_payload_len: u32,
Expand All @@ -159,33 +159,33 @@ impl FuseInterface for VirtioFsDriver {
headers: cmd_headers,
payload: cmd_payload_opt,
} = cmd;
let send: &[&[u8]] = if let Some(cmd_payload) = cmd_payload_opt.as_deref() {
&[cmd_headers.as_slice_u8(), cmd_payload]
let send = if let Some(cmd_payload) = cmd_payload_opt {
vec![
BufferElem::Sized(cmd_headers),
BufferElem::Vector(cmd_payload),
]
} else {
&[cmd_headers.as_slice_u8()]
vec![BufferElem::Sized(cmd_headers)]
};

let mut rsp_headers = Box::<RspHeader<O>>::new_uninit();
let mut rsp_payload_opt;
let recv: &[&mut [MaybeUninit<u8>]] = if rsp_payload_len == 0 {
rsp_payload_opt = None;
&[rsp_headers.as_bytes_mut()]
let rsp_headers = Box::<RspHeader<O>, _>::new_uninit_in(DeviceAlloc);
let recv = if rsp_payload_len == 0 {
vec![BufferElem::Sized(rsp_headers)]
} else {
rsp_payload_opt = Some(Box::new_uninit_slice(rsp_payload_len as usize));
&[
rsp_headers.as_bytes_mut(),
rsp_payload_opt.as_mut().unwrap(),
let rsp_payload = Vec::with_capacity_in(rsp_payload_len as usize, DeviceAlloc);
vec![
BufferElem::Sized(rsp_headers),
BufferElem::Vector(rsp_payload),
]
};

let buffer_tkn = BufferToken::from_existing(send, recv).unwrap();
self.vqueues[1].dispatch_blocking(buffer_tkn, BufferType::Direct)?;
Ok(unsafe {
Rsp {
headers: rsp_headers.assume_init(),
payload: rsp_payload_opt.map(|slice| Box::<[MaybeUninit<_>]>::assume_init(slice)),
}
})
let buffer_tkn = AvailBufferToken::new(send, recv).unwrap();
let mut transfer_result =
self.vqueues[1].dispatch_blocking(buffer_tkn, BufferType::Direct)?;

let headers = transfer_result.used_recv_buff.pop_front_downcast().unwrap();
let payload = transfer_result.used_recv_buff.pop_front_vec();
Ok(Rsp { headers, payload })
}

fn get_mount_point(&self) -> String {
Expand Down
6 changes: 4 additions & 2 deletions src/drivers/net/virtio/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ impl VirtioNetDriver {
1514
};

let send_vqs = TxQueues::new(Vec::<Rc<dyn Virtq>>::new(), false, &dev_cfg);
let recv_vqs = RxQueues::new(Vec::<Rc<dyn Virtq>>::new(), false, &dev_cfg);
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),
recv_vqs,
send_vqs,
num_vqs: 0,
irq,
mtu,
Expand Down
Loading

0 comments on commit d0d2078

Please sign in to comment.