2020-07-30 10:40:09 +00:00
|
|
|
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE-BSD-3-Clause file.
|
|
|
|
//
|
|
|
|
// Copyright © 2020 Intel Corporation
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
|
|
|
|
|
|
|
|
use super::Error as DeviceError;
|
|
|
|
use super::{
|
|
|
|
ActivateError, ActivateResult, EpollHelper, EpollHelperError, EpollHelperHandler, Queue,
|
2020-09-03 09:37:36 +00:00
|
|
|
VirtioCommon, VirtioDevice, VirtioDeviceType, VirtioInterruptType, EPOLL_HELPER_EVENT_LAST,
|
2020-07-30 10:40:09 +00:00
|
|
|
};
|
2020-08-18 00:10:03 +00:00
|
|
|
use crate::seccomp_filters::{get_seccomp_filter, Thread};
|
2020-07-30 10:40:09 +00:00
|
|
|
use crate::VirtioInterrupt;
|
|
|
|
use anyhow::anyhow;
|
|
|
|
use block_util::{build_disk_image_id, Request, RequestType, VirtioBlockConfig};
|
|
|
|
use io_uring::IoUring;
|
|
|
|
use libc::EFD_NONBLOCK;
|
2020-08-18 00:10:03 +00:00
|
|
|
use seccomp::{SeccompAction, SeccompFilter};
|
2020-07-30 10:40:09 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{self, Seek, SeekFrom};
|
|
|
|
use std::num::Wrapping;
|
|
|
|
use std::os::unix::io::{AsRawFd, RawFd};
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::result;
|
|
|
|
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
2020-08-11 14:05:06 +00:00
|
|
|
use std::sync::{Arc, Barrier};
|
2020-07-30 10:40:09 +00:00
|
|
|
use std::thread;
|
|
|
|
use virtio_bindings::bindings::virtio_blk::*;
|
|
|
|
use vm_memory::{
|
|
|
|
ByteValued, Bytes, GuestAddress, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryError,
|
|
|
|
GuestMemoryMmap,
|
|
|
|
};
|
|
|
|
use vm_migration::{
|
|
|
|
Migratable, MigratableError, Pausable, Snapshot, SnapshotDataSection, Snapshottable,
|
|
|
|
Transportable,
|
|
|
|
};
|
|
|
|
use vmm_sys_util::eventfd::EventFd;
|
|
|
|
|
|
|
|
const SECTOR_SHIFT: u8 = 9;
|
2021-01-02 19:55:08 +00:00
|
|
|
pub const SECTOR_SIZE: u64 = 0x01 << SECTOR_SHIFT;
|
2020-07-30 10:40:09 +00:00
|
|
|
|
|
|
|
// New descriptors are pending on the virtio queue.
|
|
|
|
const QUEUE_AVAIL_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 1;
|
|
|
|
// New completed tasks are pending on the completion ring.
|
|
|
|
const IO_URING_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 2;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
/// Guest gave us bad memory addresses.
|
|
|
|
GuestMemory(GuestMemoryError),
|
|
|
|
/// Guest gave us offsets that would have overflowed a usize.
|
|
|
|
CheckedOffset(GuestAddress, usize),
|
|
|
|
/// Guest gave us a write only descriptor that protocol says to read from.
|
|
|
|
UnexpectedWriteOnlyDescriptor,
|
|
|
|
/// Guest gave us a read only descriptor that protocol says to write to.
|
|
|
|
UnexpectedReadOnlyDescriptor,
|
|
|
|
/// Guest gave us too few descriptors in a descriptor chain.
|
|
|
|
DescriptorChainTooShort,
|
|
|
|
/// Guest gave us a descriptor that was too short to use.
|
|
|
|
DescriptorLengthTooSmall,
|
|
|
|
/// Getting a block's metadata fails for any reason.
|
|
|
|
GetFileMetadata,
|
|
|
|
/// The requested operation would cause a seek beyond disk end.
|
|
|
|
InvalidOffset,
|
|
|
|
/// Unsupported operation on the disk.
|
|
|
|
Unsupported(u32),
|
2020-07-30 09:58:29 +00:00
|
|
|
/// Failed to parse the request.
|
|
|
|
RequestParsing(block_util::Error),
|
|
|
|
/// Failed to execute the request.
|
|
|
|
RequestExecuting(block_util::ExecuteError),
|
|
|
|
/// Missing the expected entry in the list of requests.
|
|
|
|
MissingEntryRequestList,
|
|
|
|
/// The asynchronous request returned with failure.
|
|
|
|
AsyncRequestFailure,
|
2020-07-30 10:40:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 09:58:29 +00:00
|
|
|
pub type Result<T> = result::Result<T, Error>;
|
|
|
|
|
2020-07-30 10:40:09 +00:00
|
|
|
#[derive(Default, Clone)]
|
|
|
|
pub struct BlockCounters {
|
|
|
|
read_bytes: Arc<AtomicU64>,
|
|
|
|
read_ops: Arc<AtomicU64>,
|
|
|
|
write_bytes: Arc<AtomicU64>,
|
|
|
|
write_ops: Arc<AtomicU64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct BlockIoUringEpollHandler {
|
|
|
|
queue: Queue,
|
|
|
|
mem: GuestMemoryAtomic<GuestMemoryMmap>,
|
|
|
|
disk_image_fd: RawFd,
|
|
|
|
disk_nsectors: u64,
|
|
|
|
interrupt_cb: Arc<dyn VirtioInterrupt>,
|
|
|
|
disk_image_id: Vec<u8>,
|
|
|
|
kill_evt: EventFd,
|
|
|
|
pause_evt: EventFd,
|
|
|
|
writeback: Arc<AtomicBool>,
|
|
|
|
counters: BlockCounters,
|
|
|
|
queue_evt: EventFd,
|
|
|
|
io_uring: IoUring,
|
|
|
|
io_uring_evt: EventFd,
|
|
|
|
request_list: HashMap<u16, Request>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BlockIoUringEpollHandler {
|
2020-07-30 09:58:29 +00:00
|
|
|
fn process_queue_submit(&mut self) -> Result<bool> {
|
2020-07-30 10:40:09 +00:00
|
|
|
let queue = &mut self.queue;
|
|
|
|
let mem = self.mem.memory();
|
|
|
|
|
|
|
|
let mut used_desc_heads = Vec::new();
|
|
|
|
let mut used_count = 0;
|
|
|
|
|
|
|
|
for avail_desc in queue.iter(&mem) {
|
2020-07-30 09:58:29 +00:00
|
|
|
let mut request = Request::parse(&avail_desc, &mem).map_err(Error::RequestParsing)?;
|
2020-12-01 16:15:26 +00:00
|
|
|
request.set_writeback(self.writeback.load(Ordering::Acquire));
|
2020-07-30 09:58:29 +00:00
|
|
|
if request
|
|
|
|
.execute_io_uring(
|
|
|
|
&mem,
|
|
|
|
&mut self.io_uring,
|
|
|
|
self.disk_nsectors,
|
|
|
|
self.disk_image_fd,
|
|
|
|
&self.disk_image_id,
|
|
|
|
avail_desc.index as u64,
|
|
|
|
)
|
|
|
|
.map_err(Error::RequestExecuting)?
|
|
|
|
{
|
|
|
|
self.request_list.insert(avail_desc.index, request);
|
|
|
|
} else {
|
|
|
|
// We use unwrap because the request parsing process already
|
|
|
|
// checked that the status_addr was valid.
|
|
|
|
mem.write_obj(VIRTIO_BLK_S_OK, request.status_addr).unwrap();
|
2020-07-30 10:40:09 +00:00
|
|
|
|
2020-07-30 09:58:29 +00:00
|
|
|
// If no asynchronous operation has been submitted, we can
|
|
|
|
// simply return the used descriptor.
|
|
|
|
used_desc_heads.push((avail_desc.index, 0));
|
|
|
|
used_count += 1;
|
2020-07-30 10:40:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for &(desc_index, len) in used_desc_heads.iter() {
|
|
|
|
queue.add_used(&mem, desc_index, len);
|
|
|
|
}
|
|
|
|
|
2020-07-30 09:58:29 +00:00
|
|
|
Ok(used_count > 0)
|
2020-07-30 10:40:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 09:58:29 +00:00
|
|
|
fn process_queue_complete(&mut self) -> Result<bool> {
|
2020-07-30 10:40:09 +00:00
|
|
|
let queue = &mut self.queue;
|
|
|
|
|
|
|
|
let mut used_desc_heads = Vec::new();
|
|
|
|
let mut used_count = 0;
|
|
|
|
let mem = self.mem.memory();
|
|
|
|
let mut read_bytes = Wrapping(0);
|
|
|
|
let mut write_bytes = Wrapping(0);
|
|
|
|
let mut read_ops = Wrapping(0);
|
|
|
|
let mut write_ops = Wrapping(0);
|
|
|
|
|
|
|
|
let cq = self.io_uring.completion();
|
|
|
|
for cq_entry in cq.available() {
|
|
|
|
let result = cq_entry.result();
|
|
|
|
let desc_index = cq_entry.user_data() as u16;
|
2020-07-30 09:58:29 +00:00
|
|
|
let request = self
|
|
|
|
.request_list
|
|
|
|
.remove(&desc_index)
|
|
|
|
.ok_or(Error::MissingEntryRequestList)?;
|
2020-07-30 10:40:09 +00:00
|
|
|
|
|
|
|
let (status, len) = if result >= 0 {
|
|
|
|
match request.request_type {
|
|
|
|
RequestType::In => {
|
2020-09-14 13:02:04 +00:00
|
|
|
for (_, data_len) in &request.data_descriptors {
|
|
|
|
read_bytes += Wrapping(*data_len as u64);
|
|
|
|
}
|
2020-07-30 10:40:09 +00:00
|
|
|
read_ops += Wrapping(1);
|
|
|
|
}
|
|
|
|
RequestType::Out => {
|
|
|
|
if !request.writeback {
|
|
|
|
unsafe { libc::fsync(self.disk_image_fd) };
|
|
|
|
}
|
2020-09-14 13:02:04 +00:00
|
|
|
for (_, data_len) in &request.data_descriptors {
|
|
|
|
write_bytes += Wrapping(*data_len as u64);
|
|
|
|
}
|
2020-07-30 10:40:09 +00:00
|
|
|
write_ops += Wrapping(1);
|
|
|
|
}
|
|
|
|
_ => {}
|
2020-07-30 09:58:29 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 10:40:09 +00:00
|
|
|
(VIRTIO_BLK_S_OK, result as u32)
|
|
|
|
} else {
|
|
|
|
error!(
|
|
|
|
"Request failed: {:?}",
|
|
|
|
io::Error::from_raw_os_error(-result)
|
|
|
|
);
|
2020-07-30 09:58:29 +00:00
|
|
|
return Err(Error::AsyncRequestFailure);
|
2020-07-30 10:40:09 +00:00
|
|
|
};
|
2020-07-30 09:58:29 +00:00
|
|
|
|
2020-07-30 10:40:09 +00:00
|
|
|
// We use unwrap because the request parsing process already
|
|
|
|
// checked that the status_addr was valid.
|
|
|
|
mem.write_obj(status, request.status_addr).unwrap();
|
|
|
|
|
|
|
|
used_desc_heads.push((desc_index, len));
|
|
|
|
used_count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for &(desc_index, len) in used_desc_heads.iter() {
|
|
|
|
queue.add_used(&mem, desc_index, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.counters
|
|
|
|
.write_bytes
|
|
|
|
.fetch_add(write_bytes.0, Ordering::AcqRel);
|
|
|
|
self.counters
|
|
|
|
.write_ops
|
|
|
|
.fetch_add(write_ops.0, Ordering::AcqRel);
|
|
|
|
|
|
|
|
self.counters
|
|
|
|
.read_bytes
|
|
|
|
.fetch_add(read_bytes.0, Ordering::AcqRel);
|
|
|
|
self.counters
|
|
|
|
.read_ops
|
|
|
|
.fetch_add(read_ops.0, Ordering::AcqRel);
|
|
|
|
|
2020-07-30 09:58:29 +00:00
|
|
|
Ok(used_count > 0)
|
2020-07-30 10:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn signal_used_queue(&self) -> result::Result<(), DeviceError> {
|
|
|
|
self.interrupt_cb
|
|
|
|
.trigger(&VirtioInterruptType::Queue, Some(&self.queue))
|
|
|
|
.map_err(|e| {
|
|
|
|
error!("Failed to signal used queue: {:?}", e);
|
|
|
|
DeviceError::FailedSignalingUsedQueue(e)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-11 14:05:06 +00:00
|
|
|
fn run(
|
|
|
|
&mut self,
|
|
|
|
paused: Arc<AtomicBool>,
|
|
|
|
paused_sync: Arc<Barrier>,
|
|
|
|
) -> result::Result<(), EpollHelperError> {
|
2020-07-30 10:40:09 +00:00
|
|
|
let mut helper = EpollHelper::new(&self.kill_evt, &self.pause_evt)?;
|
|
|
|
helper.add_event(self.queue_evt.as_raw_fd(), QUEUE_AVAIL_EVENT)?;
|
|
|
|
helper.add_event(self.io_uring_evt.as_raw_fd(), IO_URING_EVENT)?;
|
2020-08-11 14:05:06 +00:00
|
|
|
helper.run(paused, paused_sync, self)?;
|
2020-07-30 10:40:09 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EpollHelperHandler for BlockIoUringEpollHandler {
|
2020-08-11 17:12:02 +00:00
|
|
|
fn handle_event(&mut self, _helper: &mut EpollHelper, event: &epoll::Event) -> bool {
|
|
|
|
let ev_type = event.data as u16;
|
|
|
|
match ev_type {
|
2020-07-30 10:40:09 +00:00
|
|
|
QUEUE_AVAIL_EVENT => {
|
|
|
|
if let Err(e) = self.queue_evt.read() {
|
|
|
|
error!("Failed to get queue event: {:?}", e);
|
|
|
|
return true;
|
2020-07-30 09:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
match self.process_queue_submit() {
|
|
|
|
Ok(needs_notification) => {
|
|
|
|
if needs_notification {
|
|
|
|
if let Err(e) = self.signal_used_queue() {
|
|
|
|
error!("Failed to signal used queue: {:?}", e);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("Failed to process queue (submit): {:?}", e);
|
2020-07-30 10:40:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
IO_URING_EVENT => {
|
|
|
|
if let Err(e) = self.io_uring_evt.read() {
|
|
|
|
error!("Failed to get queue event: {:?}", e);
|
|
|
|
return true;
|
2020-07-30 09:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
match self.process_queue_complete() {
|
|
|
|
Ok(needs_notification) => {
|
|
|
|
if needs_notification {
|
|
|
|
if let Err(e) = self.signal_used_queue() {
|
|
|
|
error!("Failed to signal used queue: {:?}", e);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("Failed to process queue (complete): {:?}", e);
|
2020-07-30 10:40:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2020-08-11 17:12:02 +00:00
|
|
|
error!("Unexpected event: {}", ev_type);
|
2020-07-30 10:40:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Virtio device for exposing block level read/write operations on a host file.
|
|
|
|
pub struct BlockIoUring {
|
2020-09-03 09:37:36 +00:00
|
|
|
common: VirtioCommon,
|
2020-07-30 10:40:09 +00:00
|
|
|
id: String,
|
|
|
|
disk_image: File,
|
|
|
|
disk_path: PathBuf,
|
|
|
|
disk_nsectors: u64,
|
|
|
|
config: VirtioBlockConfig,
|
|
|
|
writeback: Arc<AtomicBool>,
|
|
|
|
counters: BlockCounters,
|
2020-08-18 00:10:03 +00:00
|
|
|
seccomp_action: SeccompAction,
|
2020-07-30 10:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct BlockState {
|
|
|
|
pub disk_path: PathBuf,
|
|
|
|
pub disk_nsectors: u64,
|
|
|
|
pub avail_features: u64,
|
|
|
|
pub acked_features: u64,
|
|
|
|
pub config: VirtioBlockConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BlockIoUring {
|
|
|
|
/// Create a new virtio block device that operates on the given file.
|
2020-08-18 00:10:03 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2020-07-30 10:40:09 +00:00
|
|
|
pub fn new(
|
|
|
|
id: String,
|
|
|
|
mut disk_image: File,
|
|
|
|
disk_path: PathBuf,
|
|
|
|
is_disk_read_only: bool,
|
|
|
|
iommu: bool,
|
|
|
|
num_queues: usize,
|
|
|
|
queue_size: u16,
|
2020-08-18 00:10:03 +00:00
|
|
|
seccomp_action: SeccompAction,
|
2020-07-30 10:40:09 +00:00
|
|
|
) -> io::Result<Self> {
|
|
|
|
let disk_size = disk_image.seek(SeekFrom::End(0))? as u64;
|
|
|
|
if disk_size % SECTOR_SIZE != 0 {
|
|
|
|
warn!(
|
|
|
|
"Disk size {} is not a multiple of sector size {}; \
|
|
|
|
the remainder will not be visible to the guest.",
|
|
|
|
disk_size, SECTOR_SIZE
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut avail_features = (1u64 << VIRTIO_F_VERSION_1)
|
|
|
|
| (1u64 << VIRTIO_BLK_F_FLUSH)
|
|
|
|
| (1u64 << VIRTIO_BLK_F_CONFIG_WCE);
|
|
|
|
|
|
|
|
if iommu {
|
|
|
|
avail_features |= 1u64 << VIRTIO_F_IOMMU_PLATFORM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_disk_read_only {
|
|
|
|
avail_features |= 1u64 << VIRTIO_BLK_F_RO;
|
|
|
|
}
|
|
|
|
|
|
|
|
let disk_nsectors = disk_size / SECTOR_SIZE;
|
|
|
|
let mut config = VirtioBlockConfig {
|
|
|
|
capacity: disk_nsectors,
|
|
|
|
writeback: 1,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
if num_queues > 1 {
|
|
|
|
avail_features |= 1u64 << VIRTIO_BLK_F_MQ;
|
|
|
|
config.num_queues = num_queues as u16;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(BlockIoUring {
|
2020-09-03 09:37:36 +00:00
|
|
|
common: VirtioCommon {
|
2020-09-04 08:37:37 +00:00
|
|
|
device_type: VirtioDeviceType::TYPE_BLOCK as u32,
|
2020-09-03 09:37:36 +00:00
|
|
|
avail_features,
|
2020-09-04 08:37:37 +00:00
|
|
|
paused_sync: Some(Arc::new(Barrier::new(num_queues + 1))),
|
|
|
|
queue_sizes: vec![queue_size; num_queues],
|
2021-01-19 06:11:07 +00:00
|
|
|
min_queues: 1,
|
2020-09-03 15:56:32 +00:00
|
|
|
..Default::default()
|
2020-09-03 09:37:36 +00:00
|
|
|
},
|
2020-07-30 10:40:09 +00:00
|
|
|
id,
|
|
|
|
disk_image,
|
|
|
|
disk_path,
|
|
|
|
disk_nsectors,
|
|
|
|
config,
|
|
|
|
writeback: Arc::new(AtomicBool::new(true)),
|
|
|
|
counters: BlockCounters::default(),
|
2020-08-18 00:10:03 +00:00
|
|
|
seccomp_action,
|
2020-07-30 10:40:09 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn state(&self) -> BlockState {
|
|
|
|
BlockState {
|
|
|
|
disk_path: self.disk_path.clone(),
|
|
|
|
disk_nsectors: self.disk_nsectors,
|
2020-09-03 09:37:36 +00:00
|
|
|
avail_features: self.common.avail_features,
|
|
|
|
acked_features: self.common.acked_features,
|
2020-07-30 10:40:09 +00:00
|
|
|
config: self.config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_state(&mut self, state: &BlockState) -> io::Result<()> {
|
|
|
|
self.disk_path = state.disk_path.clone();
|
|
|
|
self.disk_nsectors = state.disk_nsectors;
|
2020-09-03 09:37:36 +00:00
|
|
|
self.common.avail_features = state.avail_features;
|
|
|
|
self.common.acked_features = state.acked_features;
|
2020-07-30 10:40:09 +00:00
|
|
|
self.config = state.config;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_writeback(&mut self) {
|
|
|
|
// Use writeback from config if VIRTIO_BLK_F_CONFIG_WCE
|
2020-09-03 09:37:36 +00:00
|
|
|
let writeback = if self.common.feature_acked(VIRTIO_BLK_F_CONFIG_WCE.into()) {
|
|
|
|
self.config.writeback == 1
|
|
|
|
} else {
|
|
|
|
// Else check if VIRTIO_BLK_F_FLUSH negotiated
|
|
|
|
self.common.feature_acked(VIRTIO_BLK_F_FLUSH.into())
|
|
|
|
};
|
2020-07-30 10:40:09 +00:00
|
|
|
|
|
|
|
info!(
|
|
|
|
"Changing cache mode to {}",
|
|
|
|
if writeback {
|
|
|
|
"writeback"
|
|
|
|
} else {
|
|
|
|
"writethrough"
|
|
|
|
}
|
|
|
|
);
|
2020-12-01 16:15:26 +00:00
|
|
|
self.writeback.store(writeback, Ordering::Release);
|
2020-07-30 10:40:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for BlockIoUring {
|
|
|
|
fn drop(&mut self) {
|
2020-09-04 08:37:37 +00:00
|
|
|
if let Some(kill_evt) = self.common.kill_evt.take() {
|
2020-07-30 10:40:09 +00:00
|
|
|
// Ignore the result because there is nothing we can do about it.
|
|
|
|
let _ = kill_evt.write(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VirtioDevice for BlockIoUring {
|
|
|
|
fn device_type(&self) -> u32 {
|
2020-09-04 08:37:37 +00:00
|
|
|
self.common.device_type
|
2020-07-30 10:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn queue_max_sizes(&self) -> &[u16] {
|
2020-09-04 08:37:37 +00:00
|
|
|
&self.common.queue_sizes
|
2020-07-30 10:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn features(&self) -> u64 {
|
2020-09-03 09:37:36 +00:00
|
|
|
self.common.avail_features
|
2020-07-30 10:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ack_features(&mut self, value: u64) {
|
2020-09-03 09:37:36 +00:00
|
|
|
self.common.ack_features(value)
|
2020-07-30 10:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn read_config(&self, offset: u64, data: &mut [u8]) {
|
|
|
|
self.read_config_from_slice(self.config.as_slice(), offset, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_config(&mut self, offset: u64, data: &[u8]) {
|
|
|
|
// The "writeback" field is the only mutable field
|
|
|
|
let writeback_offset =
|
|
|
|
(&self.config.writeback as *const _ as u64) - (&self.config as *const _ as u64);
|
|
|
|
if offset != writeback_offset || data.len() != std::mem::size_of_val(&self.config.writeback)
|
|
|
|
{
|
|
|
|
error!(
|
|
|
|
"Attempt to write to read-only field: offset {:x} length {}",
|
|
|
|
offset,
|
|
|
|
data.len()
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.config.writeback = data[0];
|
|
|
|
self.update_writeback();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn activate(
|
|
|
|
&mut self,
|
|
|
|
mem: GuestMemoryAtomic<GuestMemoryMmap>,
|
|
|
|
interrupt_cb: Arc<dyn VirtioInterrupt>,
|
|
|
|
mut queues: Vec<Queue>,
|
|
|
|
mut queue_evts: Vec<EventFd>,
|
|
|
|
) -> ActivateResult {
|
2020-09-04 08:37:37 +00:00
|
|
|
self.common.activate(&queues, &queue_evts, &interrupt_cb)?;
|
2020-07-30 10:40:09 +00:00
|
|
|
|
|
|
|
let disk_image_id = build_disk_image_id(&self.disk_path);
|
|
|
|
self.update_writeback();
|
|
|
|
|
|
|
|
let mut epoll_threads = Vec::new();
|
2021-01-18 13:55:37 +00:00
|
|
|
for i in 0..queues.len() {
|
2020-07-30 10:40:09 +00:00
|
|
|
let queue_evt = queue_evts.remove(0);
|
2021-01-18 13:55:37 +00:00
|
|
|
let queue = queues.remove(0);
|
|
|
|
let queue_size = queue.size;
|
2020-07-30 10:40:09 +00:00
|
|
|
let io_uring = IoUring::new(queue_size as u32).map_err(|e| {
|
|
|
|
error!("failed to create io_uring instance: {}", e);
|
|
|
|
ActivateError::BadActivate
|
|
|
|
})?;
|
2020-09-04 08:37:37 +00:00
|
|
|
let kill_evt = self
|
|
|
|
.common
|
|
|
|
.kill_evt
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.try_clone()
|
|
|
|
.map_err(|e| {
|
|
|
|
error!("failed to clone kill_evt eventfd: {}", e);
|
|
|
|
ActivateError::BadActivate
|
|
|
|
})?;
|
|
|
|
let pause_evt = self
|
|
|
|
.common
|
|
|
|
.pause_evt
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.try_clone()
|
|
|
|
.map_err(|e| {
|
|
|
|
error!("failed to clone pause_evt eventfd: {}", e);
|
|
|
|
ActivateError::BadActivate
|
|
|
|
})?;
|
|
|
|
|
2020-07-30 10:40:09 +00:00
|
|
|
let mut handler = BlockIoUringEpollHandler {
|
2021-01-18 13:55:37 +00:00
|
|
|
queue,
|
2020-07-30 10:40:09 +00:00
|
|
|
mem: mem.clone(),
|
|
|
|
disk_image_fd: self.disk_image.as_raw_fd(),
|
|
|
|
disk_nsectors: self.disk_nsectors,
|
|
|
|
interrupt_cb: interrupt_cb.clone(),
|
|
|
|
disk_image_id: disk_image_id.clone(),
|
2020-09-04 08:37:37 +00:00
|
|
|
kill_evt,
|
|
|
|
pause_evt,
|
2020-07-30 10:40:09 +00:00
|
|
|
writeback: self.writeback.clone(),
|
|
|
|
counters: self.counters.clone(),
|
|
|
|
queue_evt,
|
|
|
|
io_uring,
|
|
|
|
io_uring_evt: EventFd::new(EFD_NONBLOCK).map_err(|e| {
|
|
|
|
error!("failed to create io_uring eventfd: {}", e);
|
|
|
|
ActivateError::BadActivate
|
|
|
|
})?,
|
2021-01-18 13:55:37 +00:00
|
|
|
request_list: HashMap::with_capacity(queue_size.into()),
|
2020-07-30 10:40:09 +00:00
|
|
|
};
|
|
|
|
|
2020-09-04 08:37:37 +00:00
|
|
|
let paused = self.common.paused.clone();
|
|
|
|
let paused_sync = self.common.paused_sync.clone();
|
2020-07-30 10:40:09 +00:00
|
|
|
|
|
|
|
// Register the io_uring eventfd that will notify the epoll loop
|
|
|
|
// when something in the completion queue is ready.
|
|
|
|
handler
|
|
|
|
.io_uring
|
|
|
|
.submitter()
|
|
|
|
.register_eventfd(handler.io_uring_evt.as_raw_fd())
|
|
|
|
.map_err(|e| {
|
|
|
|
error!("failed to register eventfd for io_uring: {}", e);
|
|
|
|
ActivateError::BadActivate
|
|
|
|
})?;
|
|
|
|
|
2020-08-18 00:10:03 +00:00
|
|
|
// Retrieve seccomp filter for virtio_blk_io_uring thread
|
|
|
|
let virtio_blk_io_uring_seccomp_filter =
|
|
|
|
get_seccomp_filter(&self.seccomp_action, Thread::VirtioBlkIoUring)
|
|
|
|
.map_err(ActivateError::CreateSeccompFilter)?;
|
|
|
|
|
2020-07-30 10:40:09 +00:00
|
|
|
thread::Builder::new()
|
2021-01-13 13:10:36 +00:00
|
|
|
.name(format!("{}_q{}", self.id.clone(), i))
|
2020-08-17 19:45:17 +00:00
|
|
|
.spawn(move || {
|
2020-08-18 00:10:03 +00:00
|
|
|
if let Err(e) = SeccompFilter::apply(virtio_blk_io_uring_seccomp_filter) {
|
|
|
|
error!("Error applying seccomp filter: {:?}", e);
|
|
|
|
} else if let Err(e) = handler.run(paused, paused_sync.unwrap()) {
|
2020-08-17 19:45:17 +00:00
|
|
|
error!("Error running worker: {:?}", e);
|
|
|
|
}
|
|
|
|
})
|
2020-07-30 10:40:09 +00:00
|
|
|
.map(|thread| epoll_threads.push(thread))
|
|
|
|
.map_err(|e| {
|
|
|
|
error!("failed to clone the virtio-blk epoll thread: {}", e);
|
|
|
|
ActivateError::BadActivate
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
|
2020-09-04 08:37:37 +00:00
|
|
|
self.common.epoll_threads = Some(epoll_threads);
|
2020-07-30 10:40:09 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-01-18 12:38:08 +00:00
|
|
|
fn reset(&mut self) -> Option<Arc<dyn VirtioInterrupt>> {
|
2020-09-04 08:37:37 +00:00
|
|
|
self.common.reset()
|
2020-07-30 10:40:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn counters(&self) -> Option<HashMap<&'static str, Wrapping<u64>>> {
|
|
|
|
let mut counters = HashMap::new();
|
|
|
|
|
|
|
|
counters.insert(
|
|
|
|
"read_bytes",
|
|
|
|
Wrapping(self.counters.read_bytes.load(Ordering::Acquire)),
|
|
|
|
);
|
|
|
|
counters.insert(
|
|
|
|
"write_bytes",
|
|
|
|
Wrapping(self.counters.write_bytes.load(Ordering::Acquire)),
|
|
|
|
);
|
|
|
|
counters.insert(
|
|
|
|
"read_ops",
|
|
|
|
Wrapping(self.counters.read_ops.load(Ordering::Acquire)),
|
|
|
|
);
|
|
|
|
counters.insert(
|
|
|
|
"write_ops",
|
|
|
|
Wrapping(self.counters.write_ops.load(Ordering::Acquire)),
|
|
|
|
);
|
|
|
|
|
|
|
|
Some(counters)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-04 08:37:37 +00:00
|
|
|
impl Pausable for BlockIoUring {
|
|
|
|
fn pause(&mut self) -> result::Result<(), MigratableError> {
|
|
|
|
self.common.pause()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resume(&mut self) -> result::Result<(), MigratableError> {
|
|
|
|
self.common.resume()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 10:40:09 +00:00
|
|
|
impl Snapshottable for BlockIoUring {
|
|
|
|
fn id(&self) -> String {
|
|
|
|
self.id.clone()
|
|
|
|
}
|
|
|
|
|
2020-08-21 12:31:58 +00:00
|
|
|
fn snapshot(&mut self) -> std::result::Result<Snapshot, MigratableError> {
|
2020-07-30 10:40:09 +00:00
|
|
|
let snapshot =
|
|
|
|
serde_json::to_vec(&self.state()).map_err(|e| MigratableError::Snapshot(e.into()))?;
|
|
|
|
|
|
|
|
let mut block_snapshot = Snapshot::new(self.id.as_str());
|
|
|
|
block_snapshot.add_data_section(SnapshotDataSection {
|
|
|
|
id: format!("{}-section", self.id),
|
|
|
|
snapshot,
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(block_snapshot)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> {
|
|
|
|
if let Some(block_section) = snapshot.snapshot_data.get(&format!("{}-section", self.id)) {
|
|
|
|
let block_state = match serde_json::from_slice(&block_section.snapshot) {
|
|
|
|
Ok(state) => state,
|
|
|
|
Err(error) => {
|
|
|
|
return Err(MigratableError::Restore(anyhow!(
|
|
|
|
"Could not deserialize BLOCK {}",
|
|
|
|
error
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return self.set_state(&block_state).map_err(|e| {
|
|
|
|
MigratableError::Restore(anyhow!("Could not restore BLOCK state {:?}", e))
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(MigratableError::Restore(anyhow!(
|
|
|
|
"Could not find BLOCK snapshot section"
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Transportable for BlockIoUring {}
|
|
|
|
impl Migratable for BlockIoUring {}
|