generated from rust-vmm/crate-template
-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] Block device proposal #32
Closed
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,18 @@ | ||
[package] | ||
name = "devices" | ||
version = "0.1.0" | ||
authors = ["rust-vmm AWS maintainers <[email protected]>"] | ||
edition = "2018" | ||
license = "Apache-2.0 OR BSD-3-Clause" | ||
|
||
[dependencies] | ||
event-manager = { version = "0.2.0", features = ["remote_endpoint"] } | ||
kvm-ioctls = "0.5.0" | ||
libc = "0.2.76" | ||
log = "0.4.6" | ||
vm-memory = "0.3.0" | ||
vmm-sys-util = "0.6.1" | ||
|
||
vm-device = { git = "https://github.com/rust-vmm/vm-device.git" } | ||
vm-virtio = { git = "https://github.com/rust-vmm/vm-virtio.git" } | ||
|
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,8 @@ | ||
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause | ||
|
||
// This crate holds devices used by the VMM. They are reusing rust-vmm generic components, and | ||
// we are striving to turn as much of the local code as possible into reusable building blocks | ||
// going forward. | ||
|
||
pub mod virtio; |
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,168 @@ | ||
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause | ||
|
||
use std::sync::Arc; | ||
|
||
use event_manager::{MutEventSubscriber, RemoteEndpoint, Result as EvmgrResult, SubscriberId}; | ||
use kvm_ioctls::{IoEventAddress, VmFd}; | ||
use vm_device::bus::MmioAddress; | ||
use vm_device::MutDeviceMmio; | ||
use vm_memory::GuestAddressSpace; | ||
use vm_virtio::devices::{VirtioConfig, VirtioMmioDevice, WithDeviceOps, WithVirtioConfig}; | ||
use vm_virtio::Queue; | ||
use vmm_sys_util::eventfd::{EventFd, EFD_NONBLOCK}; | ||
|
||
use crate::virtio::block::request::DiskProperties; | ||
use crate::virtio::block::BLOCK_DEVICE_ID; | ||
use crate::virtio::features::{VIRTIO_F_IN_ORDER, VIRTIO_F_RING_EVENT_IDX, VIRTIO_F_VERSION_1}; | ||
use crate::virtio::{ | ||
MmioConfig, SingleFdSignalQueue, QUEUE_MAX_SIZE, VIRTIO_MMIO_QUEUE_NOTIFY_OFFSET, | ||
}; | ||
|
||
use super::handler::QueueHandler; | ||
use super::simple_handler::SimpleHandler; | ||
use super::{build_config_space, BlockArgs, Error, Result, VIRTIO_BLK_F_FLUSH}; | ||
|
||
// This Block device can only use the MMIO transport for now, but we plan to reuse large parts of | ||
// the functionality when we implement virtio PCI as well, for example by having a base generic | ||
// type, and then separate concrete instantiations for `MmioConfig` and `PciConfig`. | ||
pub struct Block<M: GuestAddressSpace> { | ||
virtio_cfg: VirtioConfig<M>, | ||
mmio_cfg: MmioConfig, | ||
endpoint: RemoteEndpoint<Box<dyn MutEventSubscriber + Send>>, | ||
vm_fd: Arc<VmFd>, | ||
irqfd: Arc<EventFd>, | ||
file_path: String, | ||
} | ||
|
||
impl<M: GuestAddressSpace + Clone> Block<M> { | ||
pub fn new(args: BlockArgs<M>) -> Result<Self> { | ||
// The queue handling logic for this device uses the buffers in order, so we enable the | ||
// corresponding feature as well. | ||
let device_features = | ||
VIRTIO_F_VERSION_1 | VIRTIO_F_IN_ORDER | VIRTIO_F_RING_EVENT_IDX | VIRTIO_BLK_F_FLUSH; | ||
|
||
// A block device has a single queue. | ||
let queues = vec![Queue::new(args.mem, QUEUE_MAX_SIZE)]; | ||
let config_space = build_config_space(&args.file_path)?; | ||
let virtio_cfg = VirtioConfig::new(device_features, queues, config_space); | ||
|
||
let irqfd = EventFd::new(EFD_NONBLOCK).map_err(Error::EventFd)?; | ||
args.vm_fd | ||
.register_irqfd(&irqfd, args.mmio_cfg.gsi) | ||
.map_err(Error::RegisterIrqfd)?; | ||
|
||
Ok(Block { | ||
virtio_cfg, | ||
mmio_cfg: args.mmio_cfg, | ||
endpoint: args.endpoint, | ||
vm_fd: args.vm_fd, | ||
irqfd: Arc::new(irqfd), | ||
file_path: args.file_path, | ||
}) | ||
} | ||
} | ||
|
||
// We now implement `WithVirtioConfig` and `WithDeviceOps` to get the automatic implementation | ||
// for `VirtioDevice`. | ||
impl<M: GuestAddressSpace + Clone + Send + 'static> WithVirtioConfig<M> for Block<M> { | ||
fn device_type(&self) -> u32 { | ||
BLOCK_DEVICE_ID | ||
} | ||
|
||
fn virtio_config(&self) -> &VirtioConfig<M> { | ||
&self.virtio_cfg | ||
} | ||
|
||
fn virtio_config_mut(&mut self) -> &mut VirtioConfig<M> { | ||
&mut self.virtio_cfg | ||
} | ||
} | ||
|
||
impl<M: GuestAddressSpace + Clone + Send + 'static> WithDeviceOps for Block<M> { | ||
type E = Error; | ||
|
||
fn activate(&mut self) -> Result<()> { | ||
if self.virtio_cfg.device_activated { | ||
return Err(Error::AlreadyActivated); | ||
} | ||
|
||
if !self.queues_valid() { | ||
return Err(Error::QueuesNotValid); | ||
} | ||
|
||
// We do not support legacy drivers. | ||
if self.virtio_cfg.driver_features & VIRTIO_F_VERSION_1 == 0 { | ||
return Err(Error::BadFeatures(self.virtio_cfg.driver_features)); | ||
} | ||
|
||
// Set the appropriate queue configuration flag if the `EVENT_IDX` features has been | ||
// negotiated. | ||
if self.virtio_cfg.driver_features & VIRTIO_F_RING_EVENT_IDX != 0 { | ||
self.virtio_cfg.queues[0].set_event_idx(true); | ||
} | ||
|
||
let ioeventfd = EventFd::new(EFD_NONBLOCK).map_err(Error::EventFd)?; | ||
|
||
// Register the queue event fd. | ||
self.vm_fd | ||
.register_ioevent( | ||
&ioeventfd, | ||
&IoEventAddress::Mmio( | ||
self.mmio_cfg.range.base().0 + VIRTIO_MMIO_QUEUE_NOTIFY_OFFSET, | ||
), | ||
0u32, | ||
) | ||
.map_err(Error::RegisterIoevent)?; | ||
|
||
let disk = DiskProperties::new(self.file_path.clone(), false).map_err(Error::Backend)?; | ||
|
||
let driver_notify = SingleFdSignalQueue { | ||
irqfd: self.irqfd.clone(), | ||
interrupt_status: self.virtio_cfg.interrupt_status.clone(), | ||
}; | ||
|
||
let inner = SimpleHandler { | ||
driver_notify, | ||
queue: self.virtio_cfg.queues[0].clone(), | ||
disk, | ||
}; | ||
|
||
let handler = QueueHandler { inner, ioeventfd }; | ||
|
||
// We use the model where the queue handler we've instantiated is passed into the | ||
// ownership of the event manager below. We can also experiment to see if there's any | ||
// noticeable difference between this approach and the one with an `Arc<Mutex<dyn ...>` | ||
// wrapper, and then pick the more convenient one. | ||
|
||
// We could record the `sub_id` for further interaction (i.e. to retrieve state at a | ||
// later time). | ||
let _sub_id = self | ||
.endpoint | ||
.call_blocking(move |mgr| -> EvmgrResult<SubscriberId> { | ||
Ok(mgr.add_subscriber(Box::new(handler))) | ||
}) | ||
.map_err(Error::Endpoint)?; | ||
|
||
self.virtio_cfg.device_activated = true; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn reset(&mut self) -> Result<()> { | ||
// Not implemented for now. | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<M: GuestAddressSpace + Clone + Send + 'static> VirtioMmioDevice<M> for Block<M> {} | ||
|
||
impl<M: GuestAddressSpace + Clone + Send + 'static> MutDeviceMmio for Block<M> { | ||
fn mmio_read(&mut self, _base: MmioAddress, offset: u64, data: &mut [u8]) { | ||
self.read(offset, data); | ||
} | ||
|
||
fn mmio_write(&mut self, _base: MmioAddress, offset: u64, data: &[u8]) { | ||
self.write(offset, data); | ||
} | ||
} |
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,50 @@ | ||
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause | ||
|
||
use event_manager::{EventOps, Events, MutEventSubscriber}; | ||
use log::error; | ||
use vm_memory::GuestAddressSpace; | ||
use vmm_sys_util::epoll::EventSet; | ||
use vmm_sys_util::eventfd::EventFd; | ||
|
||
use crate::virtio::block::simple_handler::SimpleHandler; | ||
use crate::virtio::SingleFdSignalQueue; | ||
|
||
const IOEVENT_DATA: u32 = 0; | ||
|
||
// This object simply combines the more generic `SimpleHandler` with a concrete queue signalling | ||
// implementation, and then also implements `MutEventSubscriber` to interact with the event | ||
// manager. `ioeventfd` is the `EventFd` connected to queue notifications coming from the driver. | ||
pub(crate) struct QueueHandler<M: GuestAddressSpace> { | ||
pub inner: SimpleHandler<M, SingleFdSignalQueue>, | ||
pub ioeventfd: EventFd, | ||
} | ||
|
||
impl<M: GuestAddressSpace> MutEventSubscriber for QueueHandler<M> { | ||
fn process(&mut self, events: Events, _ops: &mut EventOps) { | ||
if events.event_set() != EventSet::IN { | ||
error!("unexpected event_set"); | ||
} | ||
|
||
if events.data() != IOEVENT_DATA { | ||
error!("unexpected events data {}", events.data()); | ||
} | ||
|
||
if self.ioeventfd.read().is_err() { | ||
error!("ioeventfd read error") | ||
} | ||
|
||
if let Err(e) = self.inner.process_queue() { | ||
error!("error processing block queue {:?}", e); | ||
} | ||
} | ||
|
||
fn init(&mut self, ops: &mut EventOps) { | ||
ops.add(Events::with_data( | ||
&self.ioeventfd, | ||
IOEVENT_DATA, | ||
EventSet::IN, | ||
)) | ||
.expect("Failed to init block queue handler"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the spirit of #14 I'd like to see this done elsewhere (
run()
?). The emptyBlock
can hang around inert without aVmFd
until it really needs to become a live device.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would really like to try out this approach because it helps delegate all the setup logic to the device, as long as the necessary "resources" (such as the
VmFd
for this particular implementation) are provided by the environment.