2019-06-18 21:40:57 +00:00
|
|
|
// Copyright 2019 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 file.
|
|
|
|
//
|
|
|
|
// Copyright © 2019 Intel Corporation
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
|
|
|
|
|
2019-12-31 10:49:11 +00:00
|
|
|
use super::Error as DeviceError;
|
|
|
|
use super::{
|
2020-08-04 11:16:44 +00:00
|
|
|
ActivateError, ActivateResult, DescriptorChain, EpollHelper, EpollHelperError,
|
2020-09-03 09:37:36 +00:00
|
|
|
EpollHelperHandler, Queue, UserspaceMapping, VirtioCommon, VirtioDevice, VirtioDeviceType,
|
2020-08-04 11:16:44 +00:00
|
|
|
EPOLL_HELPER_EVENT_LAST, VIRTIO_F_IOMMU_PLATFORM, VIRTIO_F_VERSION_1,
|
2019-12-31 10:49:11 +00:00
|
|
|
};
|
2020-08-04 19:25:06 +00:00
|
|
|
use crate::seccomp_filters::{get_seccomp_filter, Thread};
|
2021-06-02 19:08:04 +00:00
|
|
|
use crate::{GuestMemoryMmap, MmapRegion};
|
2019-12-31 10:49:11 +00:00
|
|
|
use crate::{VirtioInterrupt, VirtioInterruptType};
|
2021-08-17 03:40:11 +00:00
|
|
|
use seccompiler::{apply_filter, SeccompAction};
|
2019-06-18 21:40:57 +00:00
|
|
|
use std::fmt::{self, Display};
|
|
|
|
use std::fs::File;
|
2020-07-16 09:34:51 +00:00
|
|
|
use std::io;
|
2019-06-18 21:40:57 +00:00
|
|
|
use std::mem::size_of;
|
2020-08-04 11:16:44 +00:00
|
|
|
use std::os::unix::io::AsRawFd;
|
2019-06-18 21:40:57 +00:00
|
|
|
use std::result;
|
2020-09-04 08:37:37 +00:00
|
|
|
use std::sync::atomic::AtomicBool;
|
2020-08-11 14:05:06 +00:00
|
|
|
use std::sync::{Arc, Barrier};
|
2019-06-18 21:40:57 +00:00
|
|
|
use std::thread;
|
2021-05-06 13:34:31 +00:00
|
|
|
use versionize::{VersionMap, Versionize, VersionizeResult};
|
|
|
|
use versionize_derive::Versionize;
|
2019-06-18 21:40:57 +00:00
|
|
|
use vm_memory::{
|
2020-02-11 16:22:40 +00:00
|
|
|
Address, ByteValued, Bytes, GuestAddress, GuestAddressSpace, GuestMemoryAtomic,
|
2021-06-02 19:08:04 +00:00
|
|
|
GuestMemoryError,
|
2019-06-18 21:40:57 +00:00
|
|
|
};
|
2021-05-06 13:34:31 +00:00
|
|
|
use vm_migration::VersionMapped;
|
2021-04-08 09:20:10 +00:00
|
|
|
use vm_migration::{Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable};
|
2019-08-02 14:23:52 +00:00
|
|
|
use vmm_sys_util::eventfd::EventFd;
|
2019-06-18 21:40:57 +00:00
|
|
|
|
|
|
|
const QUEUE_SIZE: u16 = 256;
|
|
|
|
const QUEUE_SIZES: &[u16] = &[QUEUE_SIZE];
|
|
|
|
|
|
|
|
const VIRTIO_PMEM_REQ_TYPE_FLUSH: u32 = 0;
|
|
|
|
const VIRTIO_PMEM_RESP_TYPE_OK: u32 = 0;
|
|
|
|
const VIRTIO_PMEM_RESP_TYPE_EIO: u32 = 1;
|
|
|
|
|
|
|
|
// New descriptors are pending on the virtio queue.
|
2020-08-04 11:16:44 +00:00
|
|
|
const QUEUE_AVAIL_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 1;
|
2019-06-18 21:40:57 +00:00
|
|
|
|
2021-05-11 14:02:43 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Default, Versionize)]
|
2020-10-07 10:20:30 +00:00
|
|
|
#[repr(C)]
|
2019-06-18 21:40:57 +00:00
|
|
|
struct VirtioPmemConfig {
|
|
|
|
start: u64,
|
|
|
|
size: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Safe because it only has data and has no implicit padding.
|
|
|
|
unsafe impl ByteValued for VirtioPmemConfig {}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, Default)]
|
|
|
|
#[repr(C)]
|
|
|
|
struct VirtioPmemReq {
|
|
|
|
type_: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Safe because it only has data and has no implicit padding.
|
|
|
|
unsafe impl ByteValued for VirtioPmemReq {}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, Default)]
|
|
|
|
#[repr(C)]
|
|
|
|
struct VirtioPmemResp {
|
|
|
|
ret: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Safe because it only has data and has no implicit padding.
|
|
|
|
unsafe impl ByteValued for VirtioPmemResp {}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum Error {
|
|
|
|
/// Guest gave us bad memory addresses.
|
|
|
|
GuestMemory(GuestMemoryError),
|
|
|
|
/// 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 buffer that was too short to use.
|
|
|
|
BufferLengthTooSmall,
|
|
|
|
/// Guest sent us invalid request.
|
|
|
|
InvalidRequest,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
use self::Error::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
BufferLengthTooSmall => write!(f, "buffer length too small"),
|
|
|
|
DescriptorChainTooShort => write!(f, "descriptor chain too short"),
|
|
|
|
GuestMemory(e) => write!(f, "bad guest memory address: {}", e),
|
|
|
|
InvalidRequest => write!(f, "invalid request"),
|
|
|
|
UnexpectedReadOnlyDescriptor => write!(f, "unexpected read-only descriptor"),
|
|
|
|
UnexpectedWriteOnlyDescriptor => write!(f, "unexpected write-only descriptor"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
enum RequestType {
|
|
|
|
Flush,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Request {
|
|
|
|
type_: RequestType,
|
|
|
|
status_addr: GuestAddress,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Request {
|
|
|
|
fn parse(
|
|
|
|
avail_desc: &DescriptorChain,
|
|
|
|
mem: &GuestMemoryMmap,
|
|
|
|
) -> result::Result<Request, Error> {
|
|
|
|
// The head contains the request type which MUST be readable.
|
|
|
|
if avail_desc.is_write_only() {
|
|
|
|
return Err(Error::UnexpectedWriteOnlyDescriptor);
|
|
|
|
}
|
|
|
|
|
|
|
|
if avail_desc.len as usize != size_of::<VirtioPmemReq>() {
|
|
|
|
return Err(Error::InvalidRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
let request: VirtioPmemReq = mem.read_obj(avail_desc.addr).map_err(Error::GuestMemory)?;
|
|
|
|
|
|
|
|
let request_type = match request.type_ {
|
|
|
|
VIRTIO_PMEM_REQ_TYPE_FLUSH => RequestType::Flush,
|
|
|
|
_ => return Err(Error::InvalidRequest),
|
|
|
|
};
|
|
|
|
|
|
|
|
let status_desc = avail_desc
|
|
|
|
.next_descriptor()
|
|
|
|
.ok_or(Error::DescriptorChainTooShort)?;
|
|
|
|
|
|
|
|
// The status MUST always be writable
|
|
|
|
if !status_desc.is_write_only() {
|
|
|
|
return Err(Error::UnexpectedReadOnlyDescriptor);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status_desc.len as usize) < size_of::<VirtioPmemResp>() {
|
|
|
|
return Err(Error::BufferLengthTooSmall);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Request {
|
|
|
|
type_: request_type,
|
|
|
|
status_addr: status_desc.addr,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PmemEpollHandler {
|
|
|
|
queue: Queue,
|
2020-02-11 16:22:40 +00:00
|
|
|
mem: GuestMemoryAtomic<GuestMemoryMmap>,
|
2019-06-18 21:40:57 +00:00
|
|
|
disk: File,
|
2020-01-13 17:52:19 +00:00
|
|
|
interrupt_cb: Arc<dyn VirtioInterrupt>,
|
2019-06-18 21:40:57 +00:00
|
|
|
queue_evt: EventFd,
|
|
|
|
kill_evt: EventFd,
|
2019-11-19 00:42:31 +00:00
|
|
|
pause_evt: EventFd,
|
2019-06-18 21:40:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PmemEpollHandler {
|
|
|
|
fn process_queue(&mut self) -> bool {
|
|
|
|
let mut used_desc_heads = [(0, 0); QUEUE_SIZE as usize];
|
|
|
|
let mut used_count = 0;
|
2020-02-11 16:22:40 +00:00
|
|
|
let mem = self.mem.memory();
|
2019-08-20 22:43:23 +00:00
|
|
|
for avail_desc in self.queue.iter(&mem) {
|
|
|
|
let len = match Request::parse(&avail_desc, &mem) {
|
2019-06-18 21:40:57 +00:00
|
|
|
Ok(ref req) if (req.type_ == RequestType::Flush) => {
|
|
|
|
let status_code = match self.disk.sync_all() {
|
|
|
|
Ok(()) => VIRTIO_PMEM_RESP_TYPE_OK,
|
|
|
|
Err(e) => {
|
|
|
|
error!("failed flushing disk image: {}", e);
|
|
|
|
VIRTIO_PMEM_RESP_TYPE_EIO
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let resp = VirtioPmemResp { ret: status_code };
|
2019-08-20 22:43:23 +00:00
|
|
|
match mem.write_obj(resp, req.status_addr) {
|
2019-06-18 21:40:57 +00:00
|
|
|
Ok(_) => size_of::<VirtioPmemResp>() as u32,
|
|
|
|
Err(e) => {
|
|
|
|
error!("bad guest memory address: {}", e);
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(ref req) => {
|
|
|
|
// Currently, there is only one virtio-pmem request, FLUSH.
|
|
|
|
error!("Invalid virtio request type {:?}", req.type_);
|
|
|
|
0
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("Failed to parse available descriptor chain: {:?}", e);
|
|
|
|
0
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
used_desc_heads[used_count] = (avail_desc.index, len);
|
|
|
|
used_count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for &(desc_index, len) in &used_desc_heads[..used_count] {
|
2019-08-20 22:43:23 +00:00
|
|
|
self.queue.add_used(&mem, desc_index, len);
|
2019-06-18 21:40:57 +00:00
|
|
|
}
|
|
|
|
used_count > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signal_used_queue(&self) -> result::Result<(), DeviceError> {
|
2020-01-13 17:52:19 +00:00
|
|
|
self.interrupt_cb
|
|
|
|
.trigger(&VirtioInterruptType::Queue, Some(&self.queue))
|
|
|
|
.map_err(|e| {
|
|
|
|
error!("Failed to signal used queue: {:?}", e);
|
|
|
|
DeviceError::FailedSignalingUsedQueue(e)
|
|
|
|
})
|
2019-06-18 21:40:57 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 14:05:06 +00:00
|
|
|
fn run(
|
|
|
|
&mut self,
|
|
|
|
paused: Arc<AtomicBool>,
|
|
|
|
paused_sync: Arc<Barrier>,
|
|
|
|
) -> result::Result<(), EpollHelperError> {
|
2020-08-04 11:16:44 +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)?;
|
2020-08-11 14:05:06 +00:00
|
|
|
helper.run(paused, paused_sync, self)?;
|
2020-06-22 14:00:02 +00:00
|
|
|
|
2020-08-04 11:16:44 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2020-06-25 08:08:05 +00:00
|
|
|
|
2020-08-04 11:16:44 +00:00
|
|
|
impl EpollHelperHandler for PmemEpollHandler {
|
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-08-04 11:16:44 +00:00
|
|
|
QUEUE_AVAIL_EVENT => {
|
|
|
|
if let Err(e) = self.queue_evt.read() {
|
|
|
|
error!("Failed to get queue event: {:?}", e);
|
|
|
|
return true;
|
|
|
|
} else if self.process_queue() {
|
|
|
|
if let Err(e) = self.signal_used_queue() {
|
|
|
|
error!("Failed to signal used queue: {:?}", e);
|
|
|
|
return true;
|
2019-06-18 21:40:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-04 11:16:44 +00:00
|
|
|
_ => {
|
2020-08-11 17:12:02 +00:00
|
|
|
error!("Unexpected event: {}", ev_type);
|
2020-08-04 11:16:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-06-18 21:40:57 +00:00
|
|
|
}
|
2020-08-04 11:16:44 +00:00
|
|
|
false
|
2019-06-18 21:40:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Pmem {
|
2020-09-03 09:37:36 +00:00
|
|
|
common: VirtioCommon,
|
2020-04-27 11:36:41 +00:00
|
|
|
id: String,
|
2019-06-18 21:40:57 +00:00
|
|
|
disk: Option<File>,
|
|
|
|
config: VirtioPmemConfig,
|
2020-04-20 14:30:36 +00:00
|
|
|
mapping: UserspaceMapping,
|
2020-08-04 19:25:06 +00:00
|
|
|
seccomp_action: SeccompAction,
|
2020-04-14 14:47:11 +00:00
|
|
|
|
|
|
|
// Hold ownership of the memory that is allocated for the device
|
|
|
|
// which will be automatically dropped when the device is dropped
|
|
|
|
_region: MmapRegion,
|
2019-06-18 21:40:57 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 14:02:43 +00:00
|
|
|
#[derive(Versionize)]
|
2020-04-09 06:25:33 +00:00
|
|
|
pub struct PmemState {
|
|
|
|
avail_features: u64,
|
|
|
|
acked_features: u64,
|
|
|
|
config: VirtioPmemConfig,
|
|
|
|
}
|
|
|
|
|
2021-05-06 13:34:31 +00:00
|
|
|
impl VersionMapped for PmemState {}
|
|
|
|
|
2019-06-18 21:40:57 +00:00
|
|
|
impl Pmem {
|
2020-04-14 14:47:11 +00:00
|
|
|
pub fn new(
|
2020-04-27 11:36:41 +00:00
|
|
|
id: String,
|
2020-04-14 14:47:11 +00:00
|
|
|
disk: File,
|
|
|
|
addr: GuestAddress,
|
2020-04-20 14:30:36 +00:00
|
|
|
mapping: UserspaceMapping,
|
2020-04-14 14:47:11 +00:00
|
|
|
_region: MmapRegion,
|
|
|
|
iommu: bool,
|
2020-08-04 19:25:06 +00:00
|
|
|
seccomp_action: SeccompAction,
|
2020-04-14 14:47:11 +00:00
|
|
|
) -> io::Result<Pmem> {
|
2019-06-18 21:40:57 +00:00
|
|
|
let config = VirtioPmemConfig {
|
|
|
|
start: addr.raw_value().to_le(),
|
2020-04-14 14:47:11 +00:00
|
|
|
size: (_region.size() as u64).to_le(),
|
2019-06-18 21:40:57 +00:00
|
|
|
};
|
|
|
|
|
2019-10-04 17:36:47 +00:00
|
|
|
let mut avail_features = 1u64 << VIRTIO_F_VERSION_1;
|
|
|
|
|
|
|
|
if iommu {
|
|
|
|
avail_features |= 1u64 << VIRTIO_F_IOMMU_PLATFORM;
|
|
|
|
}
|
|
|
|
|
2019-06-18 21:40:57 +00:00
|
|
|
Ok(Pmem {
|
2020-09-03 09:37:36 +00:00
|
|
|
common: VirtioCommon {
|
2021-03-25 16:54:09 +00:00
|
|
|
device_type: VirtioDeviceType::Pmem as u32,
|
2020-09-04 08:37:37 +00:00
|
|
|
queue_sizes: QUEUE_SIZES.to_vec(),
|
|
|
|
paused_sync: Some(Arc::new(Barrier::new(2))),
|
2020-09-03 09:37:36 +00:00
|
|
|
avail_features,
|
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-04-27 11:36:41 +00:00
|
|
|
id,
|
2019-06-18 21:40:57 +00:00
|
|
|
disk: Some(disk),
|
|
|
|
config,
|
2020-04-20 14:30:36 +00:00
|
|
|
mapping,
|
2020-08-04 19:25:06 +00:00
|
|
|
seccomp_action,
|
2020-04-14 14:47:11 +00:00
|
|
|
_region,
|
2019-06-18 21:40:57 +00:00
|
|
|
})
|
|
|
|
}
|
2020-04-09 06:25:33 +00:00
|
|
|
|
|
|
|
fn state(&self) -> PmemState {
|
|
|
|
PmemState {
|
2020-09-03 09:37:36 +00:00
|
|
|
avail_features: self.common.avail_features,
|
|
|
|
acked_features: self.common.acked_features,
|
2020-04-09 06:25:33 +00:00
|
|
|
config: self.config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 16:00:53 +00:00
|
|
|
fn set_state(&mut self, state: &PmemState) {
|
2020-09-03 09:37:36 +00:00
|
|
|
self.common.avail_features = state.avail_features;
|
|
|
|
self.common.acked_features = state.acked_features;
|
2020-04-09 06:25:33 +00:00
|
|
|
self.config = state.config;
|
|
|
|
}
|
2019-06-18 21:40:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Pmem {
|
|
|
|
fn drop(&mut self) {
|
2020-09-04 08:37:37 +00:00
|
|
|
if let Some(kill_evt) = self.common.kill_evt.take() {
|
2019-06-18 21:40:57 +00:00
|
|
|
// Ignore the result because there is nothing we can do about it.
|
|
|
|
let _ = kill_evt.write(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VirtioDevice for Pmem {
|
|
|
|
fn device_type(&self) -> u32 {
|
2020-09-04 08:37:37 +00:00
|
|
|
self.common.device_type
|
2019-06-18 21:40:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn queue_max_sizes(&self) -> &[u16] {
|
2020-09-04 08:37:37 +00:00
|
|
|
&self.common.queue_sizes
|
2019-06-18 21:40:57 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 10:14:38 +00:00
|
|
|
fn features(&self) -> u64 {
|
2020-09-03 09:37:36 +00:00
|
|
|
self.common.avail_features
|
2019-06-18 21:40:57 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 10:14:38 +00:00
|
|
|
fn ack_features(&mut self, value: u64) {
|
2020-09-03 09:37:36 +00:00
|
|
|
self.common.ack_features(value)
|
2019-06-18 21:40:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 09:34:51 +00:00
|
|
|
fn read_config(&self, offset: u64, data: &mut [u8]) {
|
|
|
|
self.read_config_from_slice(self.config.as_slice(), offset, data);
|
2019-06-18 21:40:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-06-18 21:40:57 +00:00
|
|
|
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)?;
|
2021-06-02 18:08:06 +00:00
|
|
|
let (kill_evt, pause_evt) = self.common.dup_eventfds();
|
2019-10-04 00:04:02 +00:00
|
|
|
if let Some(disk) = self.disk.as_ref() {
|
|
|
|
let disk = disk.try_clone().map_err(|e| {
|
|
|
|
error!("failed cloning pmem disk: {}", e);
|
|
|
|
ActivateError::BadActivate
|
|
|
|
})?;
|
2019-06-18 21:40:57 +00:00
|
|
|
let mut handler = PmemEpollHandler {
|
|
|
|
queue: queues.remove(0),
|
|
|
|
mem,
|
|
|
|
disk,
|
|
|
|
interrupt_cb,
|
|
|
|
queue_evt: queue_evts.remove(0),
|
|
|
|
kill_evt,
|
2019-11-19 00:42:31 +00:00
|
|
|
pause_evt,
|
2019-06-18 21:40:57 +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-01-27 12:56:05 +00:00
|
|
|
let mut epoll_threads = Vec::new();
|
2020-08-04 19:25:06 +00:00
|
|
|
// Retrieve seccomp filter for virtio_pmem thread
|
|
|
|
let virtio_pmem_seccomp_filter =
|
|
|
|
get_seccomp_filter(&self.seccomp_action, Thread::VirtioPmem)
|
|
|
|
.map_err(ActivateError::CreateSeccompFilter)?;
|
2019-11-19 00:42:31 +00:00
|
|
|
thread::Builder::new()
|
2021-01-13 13:10:36 +00:00
|
|
|
.name(self.id.clone())
|
2020-08-04 19:25:06 +00:00
|
|
|
.spawn(move || {
|
2021-08-17 00:20:11 +00:00
|
|
|
if !virtio_pmem_seccomp_filter.is_empty() {
|
|
|
|
if let Err(e) = apply_filter(&virtio_pmem_seccomp_filter) {
|
|
|
|
error!("Error applying seccomp filter: {:?}", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Err(e) = handler.run(paused, paused_sync.unwrap()) {
|
2020-08-05 08:06:05 +00:00
|
|
|
error!("Error running worker: {:?}", e);
|
|
|
|
}
|
2020-08-04 19:25:06 +00:00
|
|
|
})
|
2020-01-27 12:56:05 +00:00
|
|
|
.map(|thread| epoll_threads.push(thread))
|
2019-11-19 00:42:31 +00:00
|
|
|
.map_err(|e| {
|
|
|
|
error!("failed to clone virtio-pmem epoll thread: {}", e);
|
|
|
|
ActivateError::BadActivate
|
|
|
|
})?;
|
2019-06-18 21:40:57 +00:00
|
|
|
|
2020-09-04 08:37:37 +00:00
|
|
|
self.common.epoll_threads = Some(epoll_threads);
|
2020-01-27 12:56:05 +00:00
|
|
|
|
2021-02-18 15:10:51 +00:00
|
|
|
event!("virtio-device", "activated", "id", &self.id);
|
2019-06-18 21:40:57 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
Err(ActivateError::BadActivate)
|
|
|
|
}
|
2019-10-04 00:04:02 +00:00
|
|
|
|
2021-01-18 12:38:08 +00:00
|
|
|
fn reset(&mut self) -> Option<Arc<dyn VirtioInterrupt>> {
|
2021-02-18 15:10:51 +00:00
|
|
|
let result = self.common.reset();
|
|
|
|
event!("virtio-device", "reset", "id", &self.id);
|
|
|
|
result
|
2019-10-04 00:04:02 +00:00
|
|
|
}
|
2020-04-20 14:30:36 +00:00
|
|
|
|
|
|
|
fn userspace_mappings(&self) -> Vec<UserspaceMapping> {
|
|
|
|
vec![self.mapping.clone()]
|
|
|
|
}
|
2019-06-18 21:40:57 +00:00
|
|
|
}
|
2019-11-19 00:42:31 +00:00
|
|
|
|
2020-09-04 08:37:37 +00:00
|
|
|
impl Pausable for Pmem {
|
|
|
|
fn pause(&mut self) -> result::Result<(), MigratableError> {
|
|
|
|
self.common.pause()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resume(&mut self) -> result::Result<(), MigratableError> {
|
|
|
|
self.common.resume()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 06:25:33 +00:00
|
|
|
impl Snapshottable for Pmem {
|
|
|
|
fn id(&self) -> String {
|
2020-04-27 11:36:41 +00:00
|
|
|
self.id.clone()
|
2020-04-09 06:25:33 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 12:31:58 +00:00
|
|
|
fn snapshot(&mut self) -> std::result::Result<Snapshot, MigratableError> {
|
2021-05-06 13:34:31 +00:00
|
|
|
Snapshot::new_from_versioned_state(&self.id, &self.state())
|
2020-04-09 06:25:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> {
|
2021-05-06 13:34:31 +00:00
|
|
|
self.set_state(&snapshot.to_versioned_state(&self.id)?);
|
2021-04-08 09:20:10 +00:00
|
|
|
Ok(())
|
2020-04-09 06:25:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 16:59:51 +00:00
|
|
|
impl Transportable for Pmem {}
|
2019-11-19 00:42:31 +00:00
|
|
|
impl Migratable for Pmem {}
|