2019-05-21 18:54:53 +00:00
|
|
|
// Copyright 2019 Intel Corporation. All Rights Reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2020-03-23 12:47:55 +00:00
|
|
|
use super::vu_common_ctrl::{reset_vhost_user, setup_vhost_user, update_mem_table};
|
2019-12-02 20:08:53 +00:00
|
|
|
use super::Error as DeviceError;
|
2019-08-30 17:40:33 +00:00
|
|
|
use super::{Error, Result};
|
2019-08-30 18:47:24 +00:00
|
|
|
use crate::vhost_user::handler::{VhostUserEpollConfig, VhostUserEpollHandler};
|
2019-08-05 20:04:24 +00:00
|
|
|
use crate::{
|
2019-08-30 17:40:33 +00:00
|
|
|
ActivateError, ActivateResult, Queue, VirtioDevice, VirtioDeviceType, VirtioInterrupt,
|
2019-08-30 18:47:24 +00:00
|
|
|
VirtioSharedMemoryList, VIRTIO_F_VERSION_1,
|
2019-08-05 20:04:24 +00:00
|
|
|
};
|
2020-03-14 05:35:31 +00:00
|
|
|
use libc::{self, c_void, off64_t, pread64, pwrite64, EFD_NONBLOCK};
|
2019-05-21 18:54:53 +00:00
|
|
|
use std::cmp;
|
|
|
|
use std::io;
|
|
|
|
use std::io::Write;
|
2019-08-30 18:47:24 +00:00
|
|
|
use std::os::unix::io::RawFd;
|
2019-12-02 20:08:53 +00:00
|
|
|
use std::result;
|
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2019-12-31 10:49:11 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2019-05-21 18:54:53 +00:00
|
|
|
use std::thread;
|
2019-08-06 01:28:59 +00:00
|
|
|
use vhost_rs::vhost_user::message::{
|
2020-03-14 05:35:31 +00:00
|
|
|
VhostUserFSSlaveMsg, VhostUserFSSlaveMsgFlags, VhostUserProtocolFeatures,
|
|
|
|
VhostUserVirtioFeatures, VHOST_USER_FS_SLAVE_ENTRIES,
|
2019-08-06 01:28:59 +00:00
|
|
|
};
|
|
|
|
use vhost_rs::vhost_user::{
|
|
|
|
HandlerResult, Master, MasterReqHandler, VhostUserMaster, VhostUserMasterReqHandler,
|
|
|
|
};
|
2019-08-30 17:40:33 +00:00
|
|
|
use vhost_rs::VhostBackend;
|
2019-12-02 20:08:53 +00:00
|
|
|
use vm_device::{Migratable, MigratableError, Pausable, Snapshotable};
|
2020-03-14 05:35:31 +00:00
|
|
|
use vm_memory::{
|
|
|
|
Address, ByteValued, GuestAddress, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap,
|
|
|
|
};
|
2019-08-02 14:23:52 +00:00
|
|
|
use vmm_sys_util::eventfd::EventFd;
|
2019-05-21 18:54:53 +00:00
|
|
|
|
|
|
|
const NUM_QUEUE_OFFSET: usize = 1;
|
|
|
|
|
2019-08-06 01:28:59 +00:00
|
|
|
struct SlaveReqHandler {
|
2020-03-14 05:35:31 +00:00
|
|
|
cache_offset: GuestAddress,
|
2019-08-06 01:28:59 +00:00
|
|
|
cache_size: u64,
|
|
|
|
mmap_cache_addr: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VhostUserMasterReqHandler for SlaveReqHandler {
|
2020-03-24 11:54:17 +00:00
|
|
|
fn handle_config_change(&mut self) -> HandlerResult<u64> {
|
2019-08-06 01:28:59 +00:00
|
|
|
debug!("handle_config_change");
|
2020-03-24 11:54:17 +00:00
|
|
|
Ok(0)
|
2019-08-06 01:28:59 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 11:54:17 +00:00
|
|
|
fn fs_slave_map(&mut self, fs: &VhostUserFSSlaveMsg, fd: RawFd) -> HandlerResult<u64> {
|
2019-08-06 01:28:59 +00:00
|
|
|
debug!("fs_slave_map");
|
|
|
|
|
2020-01-07 16:18:02 +00:00
|
|
|
for i in 0..VHOST_USER_FS_SLAVE_ENTRIES {
|
|
|
|
// Ignore if the length is 0.
|
|
|
|
if fs.len[i] == 0 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if fs.cache_offset[i] > self.cache_size {
|
|
|
|
return Err(io::Error::new(io::ErrorKind::Other, "Wrong offset"));
|
|
|
|
}
|
|
|
|
|
|
|
|
let addr = self.mmap_cache_addr + fs.cache_offset[i];
|
|
|
|
let ret = unsafe {
|
|
|
|
libc::mmap(
|
|
|
|
addr as *mut libc::c_void,
|
|
|
|
fs.len[i] as usize,
|
|
|
|
fs.flags[i].bits() as i32,
|
|
|
|
libc::MAP_SHARED | libc::MAP_FIXED,
|
|
|
|
fd,
|
|
|
|
fs.fd_offset[i] as libc::off_t,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
if ret == libc::MAP_FAILED {
|
|
|
|
return Err(io::Error::last_os_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
let ret = unsafe { libc::close(fd) };
|
|
|
|
if ret == -1 {
|
|
|
|
return Err(io::Error::last_os_error());
|
|
|
|
}
|
2019-08-06 01:28:59 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 11:54:17 +00:00
|
|
|
Ok(0)
|
2019-08-06 01:28:59 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 11:54:17 +00:00
|
|
|
fn fs_slave_unmap(&mut self, fs: &VhostUserFSSlaveMsg) -> HandlerResult<u64> {
|
2019-08-06 01:28:59 +00:00
|
|
|
debug!("fs_slave_unmap");
|
|
|
|
|
2020-01-07 16:18:02 +00:00
|
|
|
for i in 0..VHOST_USER_FS_SLAVE_ENTRIES {
|
|
|
|
let mut len = fs.len[i];
|
|
|
|
|
|
|
|
// Ignore if the length is 0.
|
|
|
|
if len == 0 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to handle a special case where the slave ask for the unmapping
|
|
|
|
// of the entire mapping.
|
|
|
|
if len == 0xffff_ffff_ffff_ffff {
|
|
|
|
len = self.cache_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if fs.cache_offset[i] > self.cache_size {
|
|
|
|
return Err(io::Error::new(io::ErrorKind::Other, "Wrong offset"));
|
|
|
|
}
|
|
|
|
|
|
|
|
let addr = self.mmap_cache_addr + fs.cache_offset[i];
|
|
|
|
let ret = unsafe {
|
|
|
|
libc::mmap(
|
|
|
|
addr as *mut libc::c_void,
|
|
|
|
len as usize,
|
|
|
|
libc::PROT_NONE,
|
|
|
|
libc::MAP_ANONYMOUS | libc::MAP_PRIVATE | libc::MAP_FIXED,
|
|
|
|
-1,
|
|
|
|
0 as libc::off_t,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
if ret == libc::MAP_FAILED {
|
|
|
|
return Err(io::Error::last_os_error());
|
|
|
|
}
|
2019-08-06 01:28:59 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 11:54:17 +00:00
|
|
|
Ok(0)
|
2019-08-06 01:28:59 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 11:54:17 +00:00
|
|
|
fn fs_slave_sync(&mut self, fs: &VhostUserFSSlaveMsg) -> HandlerResult<u64> {
|
2019-08-06 01:28:59 +00:00
|
|
|
debug!("fs_slave_sync");
|
|
|
|
|
2020-01-07 16:18:02 +00:00
|
|
|
for i in 0..VHOST_USER_FS_SLAVE_ENTRIES {
|
|
|
|
// Ignore if the length is 0.
|
|
|
|
if fs.len[i] == 0 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if fs.cache_offset[i] > self.cache_size {
|
|
|
|
return Err(io::Error::new(io::ErrorKind::Other, "Wrong offset"));
|
|
|
|
}
|
|
|
|
|
|
|
|
let addr = self.mmap_cache_addr + fs.cache_offset[i];
|
|
|
|
let ret = unsafe {
|
|
|
|
libc::msync(addr as *mut libc::c_void, fs.len[i] as usize, libc::MS_SYNC)
|
|
|
|
};
|
|
|
|
if ret == -1 {
|
|
|
|
return Err(io::Error::last_os_error());
|
|
|
|
}
|
2019-08-06 01:28:59 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 11:54:17 +00:00
|
|
|
Ok(0)
|
2019-08-06 01:28:59 +00:00
|
|
|
}
|
2020-03-14 05:35:31 +00:00
|
|
|
|
2020-03-24 11:54:17 +00:00
|
|
|
fn fs_slave_io(&mut self, fs: &VhostUserFSSlaveMsg, fd: RawFd) -> HandlerResult<u64> {
|
2020-03-14 05:35:31 +00:00
|
|
|
debug!("fs_slave_io");
|
|
|
|
|
2020-03-24 11:54:17 +00:00
|
|
|
let mut done: u64 = 0;
|
2020-03-14 05:35:31 +00:00
|
|
|
for i in 0..VHOST_USER_FS_SLAVE_ENTRIES {
|
|
|
|
// Ignore if the length is 0.
|
|
|
|
if fs.len[i] == 0 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut foffset = fs.fd_offset[i];
|
|
|
|
let mut len = fs.len[i] as usize;
|
|
|
|
let gpa = fs.cache_offset[i];
|
|
|
|
if gpa < self.cache_offset.raw_value()
|
|
|
|
|| gpa >= self.cache_offset.raw_value() + self.cache_size
|
|
|
|
{
|
|
|
|
return Err(io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
"gpa is out of cache range",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let offset = gpa
|
|
|
|
.checked_sub(self.cache_offset.raw_value())
|
|
|
|
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "gpa is out of cache range"))?;
|
|
|
|
let mut ptr = self.mmap_cache_addr + offset;
|
|
|
|
|
|
|
|
while len > 0 {
|
|
|
|
let ret = if (fs.flags[i] & VhostUserFSSlaveMsgFlags::MAP_W)
|
|
|
|
== VhostUserFSSlaveMsgFlags::MAP_W
|
|
|
|
{
|
|
|
|
debug!("write: foffset={}, len={}", foffset, len);
|
|
|
|
unsafe { pwrite64(fd, ptr as *const c_void, len as usize, foffset as off64_t) }
|
|
|
|
} else {
|
|
|
|
debug!("read: foffset={}, len={}", foffset, len);
|
|
|
|
unsafe { pread64(fd, ptr as *mut c_void, len as usize, foffset as off64_t) }
|
|
|
|
};
|
|
|
|
|
|
|
|
if ret < 0 {
|
|
|
|
return Err(io::Error::last_os_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ret == 0 {
|
|
|
|
// EOF
|
|
|
|
return Err(io::Error::new(
|
|
|
|
io::ErrorKind::UnexpectedEof,
|
|
|
|
"failed to access whole buffer",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
len -= ret as usize;
|
|
|
|
foffset += ret as u64;
|
|
|
|
ptr += ret as u64;
|
2020-03-24 11:54:17 +00:00
|
|
|
done += ret as u64;
|
2020-03-14 05:35:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let ret = unsafe { libc::close(fd) };
|
|
|
|
if ret == -1 {
|
|
|
|
return Err(io::Error::last_os_error());
|
|
|
|
}
|
|
|
|
|
2020-03-24 11:54:17 +00:00
|
|
|
Ok(done)
|
2020-03-14 05:35:31 +00:00
|
|
|
}
|
2019-08-06 01:28:59 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 18:06:42 +00:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
#[repr(C, packed)]
|
|
|
|
struct VirtioFsConfig {
|
|
|
|
tag: [u8; 36],
|
|
|
|
num_request_queues: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for VirtioFsConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
VirtioFsConfig {
|
|
|
|
tag: [0; 36],
|
|
|
|
num_request_queues: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl ByteValued for VirtioFsConfig {}
|
|
|
|
|
2019-05-21 18:54:53 +00:00
|
|
|
pub struct Fs {
|
|
|
|
vu: Master,
|
|
|
|
queue_sizes: Vec<u16>,
|
|
|
|
avail_features: u64,
|
|
|
|
acked_features: u64,
|
2020-01-27 18:06:42 +00:00
|
|
|
config: VirtioFsConfig,
|
2019-05-21 18:54:53 +00:00
|
|
|
kill_evt: Option<EventFd>,
|
2019-12-02 20:08:53 +00:00
|
|
|
pause_evt: Option<EventFd>,
|
2019-08-06 01:28:59 +00:00
|
|
|
cache: Option<(VirtioSharedMemoryList, u64)>,
|
|
|
|
slave_req_support: bool,
|
2019-10-04 17:04:42 +00:00
|
|
|
queue_evts: Option<Vec<EventFd>>,
|
2020-01-13 17:52:19 +00:00
|
|
|
interrupt_cb: Option<Arc<dyn VirtioInterrupt>>,
|
2020-01-27 13:14:56 +00:00
|
|
|
epoll_threads: Option<Vec<thread::JoinHandle<result::Result<(), DeviceError>>>>,
|
2019-12-02 20:08:53 +00:00
|
|
|
paused: Arc<AtomicBool>,
|
2019-05-21 18:54:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Fs {
|
|
|
|
/// Create a new virtio-fs device.
|
2019-08-05 20:04:24 +00:00
|
|
|
pub fn new(
|
|
|
|
path: &str,
|
|
|
|
tag: &str,
|
|
|
|
req_num_queues: usize,
|
|
|
|
queue_size: u16,
|
2019-08-06 01:28:59 +00:00
|
|
|
cache: Option<(VirtioSharedMemoryList, u64)>,
|
2019-08-05 20:04:24 +00:00
|
|
|
) -> Result<Fs> {
|
2019-08-06 01:28:59 +00:00
|
|
|
let mut slave_req_support = false;
|
2019-08-30 17:40:33 +00:00
|
|
|
|
2019-05-21 18:54:53 +00:00
|
|
|
// Calculate the actual number of queues needed.
|
|
|
|
let num_queues = NUM_QUEUE_OFFSET + req_num_queues;
|
2019-08-30 17:40:33 +00:00
|
|
|
|
2019-05-21 18:54:53 +00:00
|
|
|
// Connect to the vhost-user socket.
|
|
|
|
let mut master =
|
2019-08-30 17:40:33 +00:00
|
|
|
Master::connect(path, num_queues as u64).map_err(Error::VhostUserCreateMaster)?;
|
|
|
|
|
|
|
|
// Filling device and vring features VMM supports.
|
|
|
|
let mut avail_features =
|
|
|
|
1 << VIRTIO_F_VERSION_1 | VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits();
|
|
|
|
|
|
|
|
// Set vhost-user owner.
|
|
|
|
master.set_owner().map_err(Error::VhostUserSetOwner)?;
|
|
|
|
|
|
|
|
// Get features from backend, do negotiation to get a feature collection which
|
|
|
|
// both VMM and backend support.
|
|
|
|
let backend_features = master.get_features().map_err(Error::VhostUserGetFeatures)?;
|
|
|
|
avail_features &= backend_features;
|
|
|
|
// Set features back is required by the vhost crate mechanism, since the
|
|
|
|
// later vhost call will check if features is filled in master before execution.
|
2019-05-21 18:54:53 +00:00
|
|
|
master
|
|
|
|
.set_features(avail_features)
|
|
|
|
.map_err(Error::VhostUserSetFeatures)?;
|
2019-08-30 17:40:33 +00:00
|
|
|
|
2019-05-21 18:54:53 +00:00
|
|
|
// Identify if protocol features are supported by the slave.
|
2019-08-30 17:40:33 +00:00
|
|
|
let mut acked_features = 0;
|
|
|
|
if avail_features & VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits() != 0 {
|
|
|
|
acked_features |= VhostUserVirtioFeatures::PROTOCOL_FEATURES.bits();
|
|
|
|
|
2019-05-21 18:54:53 +00:00
|
|
|
let mut protocol_features = master
|
|
|
|
.get_protocol_features()
|
|
|
|
.map_err(Error::VhostUserGetProtocolFeatures)?;
|
2019-08-06 01:28:59 +00:00
|
|
|
|
|
|
|
if cache.is_some() {
|
|
|
|
protocol_features &= VhostUserProtocolFeatures::MQ
|
|
|
|
| VhostUserProtocolFeatures::REPLY_ACK
|
|
|
|
| VhostUserProtocolFeatures::SLAVE_REQ
|
|
|
|
| VhostUserProtocolFeatures::SLAVE_SEND_FD;
|
|
|
|
} else {
|
|
|
|
protocol_features &=
|
|
|
|
VhostUserProtocolFeatures::MQ | VhostUserProtocolFeatures::REPLY_ACK;
|
|
|
|
}
|
|
|
|
|
2019-05-21 18:54:53 +00:00
|
|
|
master
|
|
|
|
.set_protocol_features(protocol_features)
|
|
|
|
.map_err(Error::VhostUserSetProtocolFeatures)?;
|
2019-08-06 01:28:59 +00:00
|
|
|
|
|
|
|
slave_req_support = true;
|
2019-05-21 18:54:53 +00:00
|
|
|
}
|
2019-08-30 17:40:33 +00:00
|
|
|
|
2020-01-27 18:06:42 +00:00
|
|
|
// Create virtio-fs device configuration.
|
|
|
|
let mut config = VirtioFsConfig::default();
|
|
|
|
let tag_bytes_vec = tag.to_string().into_bytes();
|
|
|
|
config.tag[..tag_bytes_vec.len()].copy_from_slice(tag_bytes_vec.as_slice());
|
|
|
|
config.num_request_queues = req_num_queues as u32;
|
2019-05-21 18:54:53 +00:00
|
|
|
|
|
|
|
Ok(Fs {
|
|
|
|
vu: master,
|
|
|
|
queue_sizes: vec![queue_size; num_queues],
|
|
|
|
avail_features,
|
2019-08-30 17:40:33 +00:00
|
|
|
acked_features,
|
2020-01-27 18:06:42 +00:00
|
|
|
config,
|
2019-05-21 18:54:53 +00:00
|
|
|
kill_evt: None,
|
2019-12-02 20:08:53 +00:00
|
|
|
pause_evt: None,
|
2019-08-06 01:28:59 +00:00
|
|
|
cache,
|
|
|
|
slave_req_support,
|
2019-10-04 17:04:42 +00:00
|
|
|
queue_evts: None,
|
|
|
|
interrupt_cb: None,
|
2020-01-27 13:14:56 +00:00
|
|
|
epoll_threads: None,
|
2019-12-02 20:08:53 +00:00
|
|
|
paused: Arc::new(AtomicBool::new(false)),
|
2019-05-21 18:54:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Fs {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if let Some(kill_evt) = self.kill_evt.take() {
|
|
|
|
// Ignore the result because there is nothing we can do about it.
|
|
|
|
let _ = kill_evt.write(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VirtioDevice for Fs {
|
|
|
|
fn device_type(&self) -> u32 {
|
|
|
|
VirtioDeviceType::TYPE_FS as u32
|
|
|
|
}
|
|
|
|
|
|
|
|
fn queue_max_sizes(&self) -> &[u16] {
|
|
|
|
&self.queue_sizes.as_slice()
|
|
|
|
}
|
|
|
|
|
2020-01-23 10:14:38 +00:00
|
|
|
fn features(&self) -> u64 {
|
|
|
|
self.avail_features
|
2019-05-21 18:54:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 10:14:38 +00:00
|
|
|
fn ack_features(&mut self, value: u64) {
|
|
|
|
let mut v = value;
|
2019-05-21 18:54:53 +00:00
|
|
|
// Check if the guest is ACK'ing a feature that we didn't claim to have.
|
|
|
|
let unrequested_features = v & !self.avail_features;
|
|
|
|
if unrequested_features != 0 {
|
|
|
|
warn!("fs: virtio-fs got unknown feature ack: {:x}", v);
|
|
|
|
|
|
|
|
// Don't count these features as acked.
|
|
|
|
v &= !unrequested_features;
|
|
|
|
}
|
|
|
|
self.acked_features |= v;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_config(&self, offset: u64, mut data: &mut [u8]) {
|
2020-01-27 18:06:42 +00:00
|
|
|
let config_slice = self.config.as_slice();
|
|
|
|
let config_len = config_slice.len() as u64;
|
2019-05-21 18:54:53 +00:00
|
|
|
if offset >= config_len {
|
|
|
|
error!("Failed to read config space");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if let Some(end) = offset.checked_add(data.len() as u64) {
|
|
|
|
// This write can't fail, offset and end are checked against config_len.
|
2020-01-27 18:06:42 +00:00
|
|
|
data.write_all(&config_slice[offset as usize..cmp::min(end, config_len) as usize])
|
2019-05-21 18:54:53 +00:00
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_config(&mut self, offset: u64, data: &[u8]) {
|
2020-01-27 18:06:42 +00:00
|
|
|
let config_slice = self.config.as_mut_slice();
|
2019-05-21 18:54:53 +00:00
|
|
|
let data_len = data.len() as u64;
|
2020-01-27 18:06:42 +00:00
|
|
|
let config_len = config_slice.len() as u64;
|
2019-05-21 18:54:53 +00:00
|
|
|
if offset + data_len > config_len {
|
|
|
|
error!("Failed to write config space");
|
|
|
|
return;
|
|
|
|
}
|
2020-01-27 18:06:42 +00:00
|
|
|
let (_, right) = config_slice.split_at_mut(offset as usize);
|
2019-05-21 18:54:53 +00:00
|
|
|
right.copy_from_slice(&data[..]);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn activate(
|
|
|
|
&mut self,
|
2020-02-11 16:22:40 +00:00
|
|
|
mem: GuestMemoryAtomic<GuestMemoryMmap>,
|
2020-01-13 17:52:19 +00:00
|
|
|
interrupt_cb: Arc<dyn VirtioInterrupt>,
|
2019-05-21 18:54:53 +00:00
|
|
|
queues: Vec<Queue>,
|
|
|
|
queue_evts: Vec<EventFd>,
|
|
|
|
) -> ActivateResult {
|
|
|
|
if queues.len() != self.queue_sizes.len() || queue_evts.len() != self.queue_sizes.len() {
|
|
|
|
error!(
|
|
|
|
"Cannot perform activate. Expected {} queue(s), got {}",
|
|
|
|
self.queue_sizes.len(),
|
|
|
|
queues.len()
|
|
|
|
);
|
|
|
|
return Err(ActivateError::BadActivate);
|
|
|
|
}
|
|
|
|
|
2019-12-02 20:08:53 +00:00
|
|
|
let (self_kill_evt, kill_evt) = EventFd::new(EFD_NONBLOCK)
|
|
|
|
.and_then(|e| Ok((e.try_clone()?, e)))
|
|
|
|
.map_err(|e| {
|
|
|
|
error!("failed creating kill EventFd pair: {}", e);
|
|
|
|
ActivateError::BadActivate
|
|
|
|
})?;
|
2019-05-21 18:54:53 +00:00
|
|
|
self.kill_evt = Some(self_kill_evt);
|
|
|
|
|
2019-12-02 20:08:53 +00:00
|
|
|
let (self_pause_evt, pause_evt) = EventFd::new(EFD_NONBLOCK)
|
|
|
|
.and_then(|e| Ok((e.try_clone()?, e)))
|
|
|
|
.map_err(|e| {
|
|
|
|
error!("failed creating pause EventFd pair: {}", e);
|
|
|
|
ActivateError::BadActivate
|
|
|
|
})?;
|
|
|
|
self.pause_evt = Some(self_pause_evt);
|
|
|
|
|
2019-10-04 17:04:42 +00:00
|
|
|
// Save the interrupt EventFD as we need to return it on reset
|
|
|
|
// but clone it to pass into the thread.
|
|
|
|
self.interrupt_cb = Some(interrupt_cb.clone());
|
|
|
|
|
|
|
|
let mut tmp_queue_evts: Vec<EventFd> = Vec::new();
|
|
|
|
for queue_evt in queue_evts.iter() {
|
|
|
|
// Save the queue EventFD as we need to return it on reset
|
|
|
|
// but clone it to pass into the thread.
|
|
|
|
tmp_queue_evts.push(queue_evt.try_clone().map_err(|e| {
|
|
|
|
error!("failed to clone queue EventFd: {}", e);
|
|
|
|
ActivateError::BadActivate
|
|
|
|
})?);
|
|
|
|
}
|
|
|
|
self.queue_evts = Some(tmp_queue_evts);
|
|
|
|
|
2019-08-30 17:40:33 +00:00
|
|
|
let vu_call_evt_queue_list = setup_vhost_user(
|
|
|
|
&mut self.vu,
|
2020-02-11 16:22:40 +00:00
|
|
|
&mem.memory(),
|
2019-08-30 17:40:33 +00:00
|
|
|
queues,
|
|
|
|
queue_evts,
|
2020-01-14 07:18:35 +00:00
|
|
|
&interrupt_cb,
|
2019-08-30 17:40:33 +00:00
|
|
|
self.acked_features,
|
|
|
|
)
|
|
|
|
.map_err(ActivateError::VhostUserSetup)?;
|
2019-05-21 18:54:53 +00:00
|
|
|
|
2019-08-06 01:28:59 +00:00
|
|
|
// Initialize slave communication.
|
|
|
|
let slave_req_handler = if self.slave_req_support {
|
|
|
|
if let Some(cache) = self.cache.clone() {
|
|
|
|
let vu_master_req_handler = Arc::new(Mutex::new(SlaveReqHandler {
|
2020-03-14 05:35:31 +00:00
|
|
|
cache_offset: cache.0.addr,
|
2019-08-06 01:28:59 +00:00
|
|
|
cache_size: cache.0.len,
|
|
|
|
mmap_cache_addr: cache.1,
|
|
|
|
}));
|
|
|
|
|
|
|
|
let req_handler = MasterReqHandler::new(vu_master_req_handler).map_err(|e| {
|
|
|
|
ActivateError::VhostUserSetup(Error::MasterReqHandlerCreation(e))
|
|
|
|
})?;
|
|
|
|
self.vu
|
|
|
|
.set_slave_request_fd(req_handler.get_tx_raw_fd())
|
|
|
|
.map_err(|e| {
|
|
|
|
ActivateError::VhostUserSetup(Error::VhostUserSetSlaveRequestFd(e))
|
|
|
|
})?;
|
|
|
|
Some(req_handler)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2019-08-30 18:47:24 +00:00
|
|
|
let mut handler = VhostUserEpollHandler::new(VhostUserEpollConfig {
|
|
|
|
vu_interrupt_list: vu_call_evt_queue_list,
|
2019-05-21 18:54:53 +00:00
|
|
|
interrupt_cb,
|
|
|
|
kill_evt,
|
2019-12-02 20:08:53 +00:00
|
|
|
pause_evt,
|
2019-08-06 01:28:59 +00:00
|
|
|
slave_req_handler,
|
2019-08-30 18:47:24 +00:00
|
|
|
});
|
2019-05-21 18:54:53 +00:00
|
|
|
|
2019-12-02 20:08:53 +00:00
|
|
|
let paused = self.paused.clone();
|
2020-01-27 12:56:05 +00:00
|
|
|
let mut epoll_threads = Vec::new();
|
2019-12-02 20:08:53 +00:00
|
|
|
thread::Builder::new()
|
2019-05-21 18:54:53 +00:00
|
|
|
.name("virtio_fs".to_string())
|
2019-12-02 20:08:53 +00:00
|
|
|
.spawn(move || handler.run(paused))
|
2020-01-27 12:56:05 +00:00
|
|
|
.map(|thread| epoll_threads.push(thread))
|
2019-12-02 20:08:53 +00:00
|
|
|
.map_err(|e| {
|
|
|
|
error!("failed to clone queue EventFd: {}", e);
|
|
|
|
ActivateError::BadActivate
|
|
|
|
})?;
|
2019-05-21 18:54:53 +00:00
|
|
|
|
2020-01-27 13:14:56 +00:00
|
|
|
self.epoll_threads = Some(epoll_threads);
|
2020-01-27 12:56:05 +00:00
|
|
|
|
2019-05-21 18:54:53 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-08-06 01:28:59 +00:00
|
|
|
|
2020-01-13 17:52:19 +00:00
|
|
|
fn reset(&mut self) -> Option<(Arc<dyn VirtioInterrupt>, Vec<EventFd>)> {
|
2019-12-02 20:08:53 +00:00
|
|
|
// We first must resume the virtio thread if it was paused.
|
|
|
|
if self.pause_evt.take().is_some() {
|
|
|
|
self.resume().ok()?;
|
|
|
|
}
|
|
|
|
|
2019-10-04 17:04:42 +00:00
|
|
|
if let Err(e) = reset_vhost_user(&mut self.vu, self.queue_sizes.len()) {
|
|
|
|
error!("Failed to reset vhost-user daemon: {:?}", e);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(kill_evt) = self.kill_evt.take() {
|
|
|
|
// Ignore the result because there is nothing we can do about it.
|
|
|
|
let _ = kill_evt.write(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the interrupt and queue EventFDs
|
|
|
|
Some((
|
|
|
|
self.interrupt_cb.take().unwrap(),
|
|
|
|
self.queue_evts.take().unwrap(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2019-08-06 01:28:59 +00:00
|
|
|
fn get_shm_regions(&self) -> Option<VirtioSharedMemoryList> {
|
|
|
|
if let Some(cache) = self.cache.clone() {
|
|
|
|
Some(cache.0)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2020-03-23 12:47:55 +00:00
|
|
|
|
|
|
|
fn update_memory(&mut self, mem: &GuestMemoryMmap) -> std::result::Result<(), crate::Error> {
|
|
|
|
update_mem_table(&mut self.vu, mem).map_err(crate::Error::VhostUserUpdateMemory)
|
|
|
|
}
|
2019-05-21 18:54:53 +00:00
|
|
|
}
|
2019-12-02 20:08:53 +00:00
|
|
|
|
|
|
|
virtio_pausable!(Fs);
|
|
|
|
impl Snapshotable for Fs {}
|
|
|
|
impl Migratable for Fs {}
|