2020-03-04 02:12:22 +00:00
|
|
|
// Copyright (c) 2020 Ant Financial
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
use super::Error as DeviceError;
|
|
|
|
use super::{
|
2021-10-21 10:41:16 +00:00
|
|
|
ActivateError, ActivateResult, EpollHelper, EpollHelperError, EpollHelperHandler, VirtioCommon,
|
|
|
|
VirtioDevice, VirtioDeviceType, EPOLL_HELPER_EVENT_LAST, VIRTIO_F_VERSION_1,
|
2020-03-04 02:12:22 +00:00
|
|
|
};
|
2021-09-03 10:43:30 +00:00
|
|
|
use crate::seccomp_filters::Thread;
|
|
|
|
use crate::thread_helper::spawn_virtio_thread;
|
2021-06-02 19:08:04 +00:00
|
|
|
use crate::{GuestMemoryMmap, GuestRegionMmap};
|
2020-03-04 02:12:22 +00:00
|
|
|
use crate::{VirtioInterrupt, VirtioInterruptType};
|
2021-02-02 10:35:38 +00:00
|
|
|
use anyhow::anyhow;
|
2020-03-04 02:12:22 +00:00
|
|
|
use libc::EFD_NONBLOCK;
|
2021-09-03 10:43:30 +00:00
|
|
|
use seccompiler::SeccompAction;
|
2021-09-21 09:00:22 +00:00
|
|
|
use std::collections::BTreeMap;
|
2020-07-16 09:34:51 +00:00
|
|
|
use std::io;
|
2020-03-04 02:12:22 +00:00
|
|
|
use std::mem::size_of;
|
2020-08-04 11:16:44 +00:00
|
|
|
use std::os::unix::io::{AsRawFd, RawFd};
|
2020-03-04 02:12:22 +00:00
|
|
|
use std::result;
|
|
|
|
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
|
|
|
use std::sync::mpsc;
|
2020-08-11 14:05:06 +00:00
|
|
|
use std::sync::{Arc, Barrier, Mutex};
|
2021-09-22 15:26:30 +00:00
|
|
|
use versionize::{VersionMap, Versionize, VersionizeResult};
|
|
|
|
use versionize_derive::Versionize;
|
2021-10-21 10:41:16 +00:00
|
|
|
use virtio_queue::{DescriptorChain, Queue};
|
2021-03-02 17:47:01 +00:00
|
|
|
use vm_device::dma_mapping::ExternalDmaMapping;
|
2020-03-04 02:12:22 +00:00
|
|
|
use vm_memory::{
|
2021-10-21 10:41:16 +00:00
|
|
|
Address, ByteValued, Bytes, GuestAddress, GuestMemoryAtomic, GuestMemoryError,
|
|
|
|
GuestMemoryRegion,
|
2020-03-04 02:12:22 +00:00
|
|
|
};
|
2021-09-23 08:21:46 +00:00
|
|
|
use vm_migration::protocol::MemoryRangeTable;
|
2021-09-22 15:26:30 +00:00
|
|
|
use vm_migration::{
|
|
|
|
Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable, VersionMapped,
|
|
|
|
};
|
2020-03-04 02:12:22 +00:00
|
|
|
use vmm_sys_util::eventfd::EventFd;
|
|
|
|
|
|
|
|
const QUEUE_SIZE: u16 = 128;
|
|
|
|
const QUEUE_SIZES: &[u16] = &[QUEUE_SIZE];
|
|
|
|
|
2020-09-14 09:36:31 +00:00
|
|
|
// 128MiB is the standard memory block size in Linux. A virtio-mem region must
|
|
|
|
// be aligned on this size, and the region size must be a multiple of it.
|
2021-02-02 10:35:38 +00:00
|
|
|
pub const VIRTIO_MEM_ALIGN_SIZE: u64 = 128 << 20;
|
2020-03-04 02:12:22 +00:00
|
|
|
// Use 2 MiB alignment so transparent hugepages can be used by KVM.
|
2021-02-02 10:35:38 +00:00
|
|
|
const VIRTIO_MEM_DEFAULT_BLOCK_SIZE: u64 = 2 << 20;
|
2020-03-04 02:12:22 +00:00
|
|
|
|
|
|
|
// Request processed successfully, applicable for
|
|
|
|
// - VIRTIO_MEM_REQ_PLUG
|
|
|
|
// - VIRTIO_MEM_REQ_UNPLUG
|
|
|
|
// - VIRTIO_MEM_REQ_UNPLUG_ALL
|
|
|
|
// - VIRTIO_MEM_REQ_STATE
|
|
|
|
const VIRTIO_MEM_RESP_ACK: u16 = 0;
|
|
|
|
|
|
|
|
// Request denied - e.g. trying to plug more than requested, applicable for
|
|
|
|
// - VIRTIO_MEM_REQ_PLUG
|
|
|
|
const VIRTIO_MEM_RESP_NACK: u16 = 1;
|
|
|
|
|
|
|
|
// Request cannot be processed right now, try again later, applicable for
|
|
|
|
// - VIRTIO_MEM_REQ_PLUG
|
|
|
|
// - VIRTIO_MEM_REQ_UNPLUG
|
|
|
|
// - VIRTIO_MEM_REQ_UNPLUG_ALL
|
2021-02-02 10:35:38 +00:00
|
|
|
#[allow(unused)]
|
|
|
|
const VIRTIO_MEM_RESP_BUSY: u16 = 2;
|
2020-03-04 02:12:22 +00:00
|
|
|
|
2020-09-22 11:31:42 +00:00
|
|
|
// Error in request (e.g. addresses/alignment), applicable for
|
2020-03-04 02:12:22 +00:00
|
|
|
// - VIRTIO_MEM_REQ_PLUG
|
|
|
|
// - VIRTIO_MEM_REQ_UNPLUG
|
|
|
|
// - VIRTIO_MEM_REQ_STATE
|
|
|
|
const VIRTIO_MEM_RESP_ERROR: u16 = 3;
|
|
|
|
|
|
|
|
// State of memory blocks is "plugged"
|
|
|
|
const VIRTIO_MEM_STATE_PLUGGED: u16 = 0;
|
|
|
|
// State of memory blocks is "unplugged"
|
|
|
|
const VIRTIO_MEM_STATE_UNPLUGGED: u16 = 1;
|
|
|
|
// State of memory blocks is "mixed"
|
|
|
|
const VIRTIO_MEM_STATE_MIXED: u16 = 2;
|
|
|
|
|
|
|
|
// request to plug memory blocks
|
|
|
|
const VIRTIO_MEM_REQ_PLUG: u16 = 0;
|
|
|
|
// request to unplug memory blocks
|
|
|
|
const VIRTIO_MEM_REQ_UNPLUG: u16 = 1;
|
|
|
|
// request to unplug all blocks and shrink the usable size
|
|
|
|
const VIRTIO_MEM_REQ_UNPLUG_ALL: u16 = 2;
|
|
|
|
// request information about the plugged state of memory blocks
|
|
|
|
const VIRTIO_MEM_REQ_STATE: u16 = 3;
|
|
|
|
|
|
|
|
// Get resize event.
|
2020-08-04 11:16:44 +00:00
|
|
|
const RESIZE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 1;
|
2020-03-04 02:12:22 +00:00
|
|
|
// 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 + 2;
|
2020-03-04 02:12:22 +00:00
|
|
|
|
2020-09-11 13:09:06 +00:00
|
|
|
// Virtio features
|
|
|
|
const VIRTIO_MEM_F_ACPI_PXM: u8 = 0;
|
|
|
|
|
2020-03-04 02:12:22 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub 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,
|
|
|
|
// Failed to EventFd write.
|
|
|
|
EventFdWriteFail(std::io::Error),
|
|
|
|
// Failed to EventFd try_clone.
|
|
|
|
EventFdTryCloneFail(std::io::Error),
|
|
|
|
// Failed to MpscRecv.
|
|
|
|
MpscRecvFail(mpsc::RecvError),
|
|
|
|
// Resize invalid argument
|
2021-02-02 10:35:38 +00:00
|
|
|
ResizeError(anyhow::Error),
|
2020-03-04 02:12:22 +00:00
|
|
|
// Fail to resize trigger
|
|
|
|
ResizeTriggerFail(DeviceError),
|
2021-02-02 10:35:38 +00:00
|
|
|
// Invalid configuration
|
|
|
|
ValidateError(anyhow::Error),
|
2021-02-02 18:11:08 +00:00
|
|
|
// Failed discarding memory range
|
|
|
|
DiscardMemoryRange(std::io::Error),
|
2021-03-02 17:47:01 +00:00
|
|
|
// Failed DMA mapping.
|
|
|
|
DmaMap(std::io::Error),
|
|
|
|
// Failed DMA unmapping.
|
|
|
|
DmaUnmap(std::io::Error),
|
|
|
|
// Invalid DMA mapping handler
|
|
|
|
InvalidDmaMappingHandler,
|
2020-03-04 02:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone, Debug, Default)]
|
|
|
|
struct VirtioMemReq {
|
|
|
|
req_type: u16,
|
|
|
|
padding: [u16; 3],
|
|
|
|
addr: u64,
|
|
|
|
nb_blocks: u16,
|
2021-02-02 10:35:38 +00:00
|
|
|
padding_1: [u16; 3],
|
2020-03-04 02:12:22 +00:00
|
|
|
}
|
|
|
|
|
2021-11-17 13:39:53 +00:00
|
|
|
// SAFETY: it only has data and has no implicit padding.
|
2020-03-04 02:12:22 +00:00
|
|
|
unsafe impl ByteValued for VirtioMemReq {}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone, Debug, Default)]
|
|
|
|
struct VirtioMemResp {
|
|
|
|
resp_type: u16,
|
|
|
|
padding: [u16; 3],
|
2021-02-02 10:35:38 +00:00
|
|
|
state: u16,
|
2020-03-04 02:12:22 +00:00
|
|
|
}
|
|
|
|
|
2021-11-17 13:39:53 +00:00
|
|
|
// SAFETY: it only has data and has no implicit padding.
|
2020-03-04 02:12:22 +00:00
|
|
|
unsafe impl ByteValued for VirtioMemResp {}
|
|
|
|
|
2020-10-07 10:20:30 +00:00
|
|
|
#[repr(C)]
|
2021-09-22 15:26:30 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Default, Versionize)]
|
|
|
|
pub struct VirtioMemConfig {
|
2020-03-04 02:12:22 +00:00
|
|
|
// Block size and alignment. Cannot change.
|
2020-06-12 07:19:34 +00:00
|
|
|
block_size: u64,
|
2020-03-04 02:12:22 +00:00
|
|
|
// Valid with VIRTIO_MEM_F_ACPI_PXM. Cannot change.
|
|
|
|
node_id: u16,
|
2020-06-12 07:19:34 +00:00
|
|
|
padding: [u8; 6],
|
2020-03-04 02:12:22 +00:00
|
|
|
// Start address of the memory region. Cannot change.
|
|
|
|
addr: u64,
|
|
|
|
// Region size (maximum). Cannot change.
|
|
|
|
region_size: u64,
|
|
|
|
// Currently usable region size. Can grow up to region_size. Can
|
|
|
|
// shrink due to VIRTIO_MEM_REQ_UNPLUG_ALL (in which case no config
|
|
|
|
// update will be sent).
|
|
|
|
usable_region_size: u64,
|
|
|
|
// Currently used size. Changes due to plug/unplug requests, but no
|
|
|
|
// config updates will be sent.
|
|
|
|
plugged_size: u64,
|
|
|
|
// Requested size. New plug requests cannot exceed it. Can change.
|
|
|
|
requested_size: u64,
|
|
|
|
}
|
|
|
|
|
2021-11-17 13:39:53 +00:00
|
|
|
// SAFETY: it only has data and has no implicit padding.
|
2020-03-04 02:12:22 +00:00
|
|
|
unsafe impl ByteValued for VirtioMemConfig {}
|
|
|
|
|
2021-02-02 10:35:38 +00:00
|
|
|
impl VirtioMemConfig {
|
|
|
|
fn validate(&self) -> result::Result<(), Error> {
|
|
|
|
if self.addr % self.block_size != 0 {
|
|
|
|
return Err(Error::ValidateError(anyhow!(
|
|
|
|
"addr 0x{:x} is not aligned on block_size 0x{:x}",
|
|
|
|
self.addr,
|
|
|
|
self.block_size
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
if self.region_size % self.block_size != 0 {
|
|
|
|
return Err(Error::ValidateError(anyhow!(
|
|
|
|
"region_size 0x{:x} is not aligned on block_size 0x{:x}",
|
|
|
|
self.region_size,
|
|
|
|
self.block_size
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
if self.usable_region_size % self.block_size != 0 {
|
|
|
|
return Err(Error::ValidateError(anyhow!(
|
|
|
|
"usable_region_size 0x{:x} is not aligned on block_size 0x{:x}",
|
|
|
|
self.usable_region_size,
|
|
|
|
self.block_size
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
if self.plugged_size % self.block_size != 0 {
|
|
|
|
return Err(Error::ValidateError(anyhow!(
|
|
|
|
"plugged_size 0x{:x} is not aligned on block_size 0x{:x}",
|
|
|
|
self.plugged_size,
|
|
|
|
self.block_size
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
if self.requested_size % self.block_size != 0 {
|
|
|
|
return Err(Error::ValidateError(anyhow!(
|
|
|
|
"requested_size 0x{:x} is not aligned on block_size 0x{:x}",
|
|
|
|
self.requested_size,
|
|
|
|
self.block_size
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2020-08-10 10:22:55 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 10:35:38 +00:00
|
|
|
fn resize(&mut self, size: u64) -> result::Result<(), Error> {
|
|
|
|
if self.requested_size == size {
|
|
|
|
return Err(Error::ResizeError(anyhow!(
|
|
|
|
"new size 0x{:x} and requested_size are identical",
|
|
|
|
size
|
|
|
|
)));
|
|
|
|
} else if size > self.region_size {
|
|
|
|
return Err(Error::ResizeError(anyhow!(
|
|
|
|
"new size 0x{:x} is bigger than region_size 0x{:x}",
|
|
|
|
size,
|
|
|
|
self.region_size
|
|
|
|
)));
|
|
|
|
} else if size % (self.block_size as u64) != 0 {
|
|
|
|
return Err(Error::ResizeError(anyhow!(
|
|
|
|
"new size 0x{:x} is not aligned on block_size 0x{:x}",
|
|
|
|
size,
|
|
|
|
self.block_size
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.requested_size = size;
|
2020-08-10 10:22:55 +00:00
|
|
|
|
2021-02-02 10:35:38 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-02-02 18:11:08 +00:00
|
|
|
|
|
|
|
fn is_valid_range(&self, addr: u64, size: u64) -> bool {
|
|
|
|
// Start address must be aligned on block_size, the size must be
|
|
|
|
// greater than 0, and all blocks covered by the request must be
|
|
|
|
// in the usable region.
|
|
|
|
if addr % self.block_size != 0
|
|
|
|
|| size == 0
|
|
|
|
|| (addr < self.addr || addr + size >= self.addr + self.usable_region_size)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
2020-08-10 10:22:55 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 02:12:22 +00:00
|
|
|
struct Request {
|
|
|
|
req: VirtioMemReq,
|
|
|
|
status_addr: GuestAddress,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Request {
|
|
|
|
fn parse(
|
2021-10-21 10:41:16 +00:00
|
|
|
desc_chain: &mut DescriptorChain<GuestMemoryAtomic<GuestMemoryMmap>>,
|
2020-03-04 02:12:22 +00:00
|
|
|
) -> result::Result<Request, Error> {
|
2021-10-21 10:41:16 +00:00
|
|
|
let desc = desc_chain.next().ok_or(Error::DescriptorChainTooShort)?;
|
|
|
|
// The descriptor contains the request type which MUST be readable.
|
|
|
|
if desc.is_write_only() {
|
2020-03-04 02:12:22 +00:00
|
|
|
return Err(Error::UnexpectedWriteOnlyDescriptor);
|
|
|
|
}
|
2021-10-21 10:41:16 +00:00
|
|
|
if desc.len() as usize != size_of::<VirtioMemReq>() {
|
2020-03-04 02:12:22 +00:00
|
|
|
return Err(Error::InvalidRequest);
|
|
|
|
}
|
2021-10-21 10:41:16 +00:00
|
|
|
let req: VirtioMemReq = desc_chain
|
|
|
|
.memory()
|
|
|
|
.read_obj(desc.addr())
|
|
|
|
.map_err(Error::GuestMemory)?;
|
2020-03-04 02:12:22 +00:00
|
|
|
|
2021-10-21 10:41:16 +00:00
|
|
|
let status_desc = desc_chain.next().ok_or(Error::DescriptorChainTooShort)?;
|
2020-03-04 02:12:22 +00:00
|
|
|
|
|
|
|
// The status MUST always be writable
|
|
|
|
if !status_desc.is_write_only() {
|
|
|
|
return Err(Error::UnexpectedReadOnlyDescriptor);
|
|
|
|
}
|
|
|
|
|
2021-10-21 10:41:16 +00:00
|
|
|
if (status_desc.len() as usize) < size_of::<VirtioMemResp>() {
|
2020-03-04 02:12:22 +00:00
|
|
|
return Err(Error::BufferLengthTooSmall);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Request {
|
|
|
|
req,
|
2021-10-21 10:41:16 +00:00
|
|
|
status_addr: status_desc.addr(),
|
2020-03-04 02:12:22 +00:00
|
|
|
})
|
|
|
|
}
|
2021-02-02 18:11:08 +00:00
|
|
|
|
|
|
|
fn send_response(&self, mem: &GuestMemoryMmap, resp_type: u16, state: u16) -> u32 {
|
|
|
|
let resp = VirtioMemResp {
|
|
|
|
resp_type,
|
|
|
|
state,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
match mem.write_obj(resp, self.status_addr) {
|
|
|
|
Ok(_) => size_of::<VirtioMemResp>() as u32,
|
|
|
|
Err(e) => {
|
|
|
|
error!("bad guest memory address: {}", e);
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 02:12:22 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 15:17:35 +00:00
|
|
|
pub struct ResizeSender {
|
2021-09-22 15:26:30 +00:00
|
|
|
hotplugged_size: Arc<AtomicU64>,
|
2021-02-02 15:17:35 +00:00
|
|
|
tx: mpsc::Sender<Result<(), Error>>,
|
|
|
|
evt: EventFd,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ResizeSender {
|
|
|
|
fn size(&self) -> u64 {
|
2021-09-22 15:26:30 +00:00
|
|
|
self.hotplugged_size.load(Ordering::Acquire)
|
2021-02-02 15:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn send(&self, r: Result<(), Error>) -> Result<(), mpsc::SendError<Result<(), Error>>> {
|
|
|
|
self.tx.send(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Clone for ResizeSender {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
ResizeSender {
|
2021-09-22 15:26:30 +00:00
|
|
|
hotplugged_size: self.hotplugged_size.clone(),
|
2021-02-02 15:17:35 +00:00
|
|
|
tx: self.tx.clone(),
|
|
|
|
evt: self
|
|
|
|
.evt
|
|
|
|
.try_clone()
|
|
|
|
.expect("Failed cloning EventFd from ResizeSender"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 02:12:22 +00:00
|
|
|
pub struct Resize {
|
2021-09-22 15:26:30 +00:00
|
|
|
hotplugged_size: Arc<AtomicU64>,
|
2020-03-04 02:12:22 +00:00
|
|
|
tx: mpsc::Sender<Result<(), Error>>,
|
2021-02-02 15:17:35 +00:00
|
|
|
rx: mpsc::Receiver<Result<(), Error>>,
|
2020-03-04 02:12:22 +00:00
|
|
|
evt: EventFd,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resize {
|
2021-09-22 15:26:30 +00:00
|
|
|
pub fn new(hotplugged_size: u64) -> io::Result<Self> {
|
2020-03-04 02:12:22 +00:00
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
|
|
|
Ok(Resize {
|
2021-09-22 15:26:30 +00:00
|
|
|
hotplugged_size: Arc::new(AtomicU64::new(hotplugged_size)),
|
2020-03-04 02:12:22 +00:00
|
|
|
tx,
|
2021-02-02 15:17:35 +00:00
|
|
|
rx,
|
2020-03-04 02:12:22 +00:00
|
|
|
evt: EventFd::new(EFD_NONBLOCK)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-02 15:17:35 +00:00
|
|
|
pub fn new_resize_sender(&self) -> Result<ResizeSender, Error> {
|
|
|
|
Ok(ResizeSender {
|
2021-09-22 15:26:30 +00:00
|
|
|
hotplugged_size: self.hotplugged_size.clone(),
|
2020-03-04 02:12:22 +00:00
|
|
|
tx: self.tx.clone(),
|
|
|
|
evt: self.evt.try_clone().map_err(Error::EventFdTryCloneFail)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-22 15:26:30 +00:00
|
|
|
pub fn work(&self, desired_size: u64) -> Result<(), Error> {
|
|
|
|
self.hotplugged_size.store(desired_size, Ordering::Release);
|
2021-02-02 15:17:35 +00:00
|
|
|
self.evt.write(1).map_err(Error::EventFdWriteFail)?;
|
|
|
|
self.rx.recv().map_err(Error::MpscRecvFail)?
|
2020-03-04 02:12:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 15:26:30 +00:00
|
|
|
#[derive(Clone, Versionize)]
|
|
|
|
pub struct BlocksState {
|
|
|
|
bitmap: Vec<bool>,
|
|
|
|
}
|
2021-02-02 18:11:08 +00:00
|
|
|
|
|
|
|
impl BlocksState {
|
2021-09-23 08:42:00 +00:00
|
|
|
pub fn new(region_size: u64) -> Self {
|
2021-09-22 15:34:59 +00:00
|
|
|
BlocksState {
|
|
|
|
bitmap: vec![false; (region_size / VIRTIO_MEM_DEFAULT_BLOCK_SIZE) as usize],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:11:08 +00:00
|
|
|
fn is_range_state(&self, first_block_index: usize, nb_blocks: u16, plug: bool) -> bool {
|
|
|
|
for state in self
|
2021-09-22 15:26:30 +00:00
|
|
|
.bitmap
|
2021-02-02 18:11:08 +00:00
|
|
|
.iter()
|
|
|
|
.skip(first_block_index)
|
|
|
|
.take(nb_blocks as usize)
|
|
|
|
{
|
|
|
|
if *state != plug {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_range(&mut self, first_block_index: usize, nb_blocks: u16, plug: bool) {
|
|
|
|
for state in self
|
2021-09-22 15:26:30 +00:00
|
|
|
.bitmap
|
2021-02-02 18:11:08 +00:00
|
|
|
.iter_mut()
|
|
|
|
.skip(first_block_index)
|
|
|
|
.take(nb_blocks as usize)
|
|
|
|
{
|
|
|
|
*state = plug;
|
|
|
|
}
|
|
|
|
}
|
2021-03-02 17:47:01 +00:00
|
|
|
|
|
|
|
fn inner(&self) -> &Vec<bool> {
|
2021-09-22 15:26:30 +00:00
|
|
|
&self.bitmap
|
2021-03-02 17:47:01 +00:00
|
|
|
}
|
2021-09-23 08:21:46 +00:00
|
|
|
|
|
|
|
pub fn memory_ranges(&self, start_addr: u64, plugged: bool) -> MemoryRangeTable {
|
|
|
|
let mut bitmap: Vec<u64> = Vec::new();
|
|
|
|
let mut i = 0;
|
|
|
|
for (j, bit) in self.bitmap.iter().enumerate() {
|
|
|
|
if j % 64 == 0 {
|
|
|
|
bitmap.push(0);
|
|
|
|
|
|
|
|
if j != 0 {
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if *bit == plugged {
|
|
|
|
bitmap[i] |= 1 << (j % 64);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryRangeTable::from_bitmap(bitmap, start_addr, VIRTIO_MEM_DEFAULT_BLOCK_SIZE)
|
|
|
|
}
|
2021-02-02 18:11:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 02:12:22 +00:00
|
|
|
struct MemEpollHandler {
|
|
|
|
host_addr: u64,
|
|
|
|
host_fd: Option<RawFd>,
|
2021-03-02 17:47:01 +00:00
|
|
|
blocks_state: Arc<Mutex<BlocksState>>,
|
2020-03-04 02:12:22 +00:00
|
|
|
config: Arc<Mutex<VirtioMemConfig>>,
|
2021-02-02 15:17:35 +00:00
|
|
|
resize: ResizeSender,
|
2021-10-21 10:41:16 +00:00
|
|
|
queue: Queue<GuestMemoryAtomic<GuestMemoryMmap>>,
|
2020-03-04 02:12:22 +00:00
|
|
|
interrupt_cb: Arc<dyn VirtioInterrupt>,
|
|
|
|
queue_evt: EventFd,
|
|
|
|
kill_evt: EventFd,
|
|
|
|
pause_evt: EventFd,
|
2021-02-03 08:46:14 +00:00
|
|
|
hugepages: bool,
|
2021-09-21 09:00:22 +00:00
|
|
|
dma_mapping_handlers: Arc<Mutex<BTreeMap<VirtioMemMappingSource, Arc<dyn ExternalDmaMapping>>>>,
|
2020-03-04 02:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MemEpollHandler {
|
2021-02-02 18:11:08 +00:00
|
|
|
fn discard_memory_range(&self, offset: u64, size: u64) -> Result<(), Error> {
|
2021-02-03 08:46:14 +00:00
|
|
|
// Use fallocate if the memory region is backed by a file.
|
2021-02-02 18:11:08 +00:00
|
|
|
if let Some(fd) = self.host_fd {
|
|
|
|
let res = unsafe {
|
|
|
|
libc::fallocate64(
|
|
|
|
fd,
|
|
|
|
libc::FALLOC_FL_PUNCH_HOLE | libc::FALLOC_FL_KEEP_SIZE,
|
|
|
|
offset as libc::off64_t,
|
|
|
|
size as libc::off64_t,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
if res != 0 {
|
|
|
|
let err = io::Error::last_os_error();
|
|
|
|
error!("Deallocating file space failed: {}", err);
|
|
|
|
return Err(Error::DiscardMemoryRange(err));
|
|
|
|
}
|
2020-03-04 02:12:22 +00:00
|
|
|
}
|
2021-02-03 08:46:14 +00:00
|
|
|
|
|
|
|
// Only use madvise if the memory region is not allocated with
|
|
|
|
// hugepages.
|
|
|
|
if !self.hugepages {
|
|
|
|
let res = unsafe {
|
|
|
|
libc::madvise(
|
|
|
|
(self.host_addr + offset) as *mut libc::c_void,
|
|
|
|
size as libc::size_t,
|
|
|
|
libc::MADV_DONTNEED,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
if res != 0 {
|
|
|
|
let err = io::Error::last_os_error();
|
|
|
|
error!("Advising kernel about pages range failed: {}", err);
|
|
|
|
return Err(Error::DiscardMemoryRange(err));
|
|
|
|
}
|
2020-03-04 02:12:22 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 18:11:08 +00:00
|
|
|
Ok(())
|
2020-03-04 02:12:22 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 18:11:08 +00:00
|
|
|
fn state_change_request(&mut self, addr: u64, nb_blocks: u16, plug: bool) -> u16 {
|
|
|
|
let mut config = self.config.lock().unwrap();
|
|
|
|
let size: u64 = nb_blocks as u64 * config.block_size;
|
2020-03-04 02:12:22 +00:00
|
|
|
|
2021-02-02 18:11:08 +00:00
|
|
|
if plug && (config.plugged_size + size > config.requested_size) {
|
2020-03-04 02:12:22 +00:00
|
|
|
return VIRTIO_MEM_RESP_NACK;
|
|
|
|
}
|
2021-02-02 18:11:08 +00:00
|
|
|
if !config.is_valid_range(addr, size) {
|
2020-03-04 02:12:22 +00:00
|
|
|
return VIRTIO_MEM_RESP_ERROR;
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:11:08 +00:00
|
|
|
let offset = addr - config.addr;
|
2020-03-04 02:12:22 +00:00
|
|
|
|
2021-02-02 18:11:08 +00:00
|
|
|
let first_block_index = (offset / config.block_size) as usize;
|
|
|
|
if !self
|
|
|
|
.blocks_state
|
2021-03-02 17:47:01 +00:00
|
|
|
.lock()
|
|
|
|
.unwrap()
|
2021-02-02 18:11:08 +00:00
|
|
|
.is_range_state(first_block_index, nb_blocks, !plug)
|
|
|
|
{
|
2020-03-04 02:12:22 +00:00
|
|
|
return VIRTIO_MEM_RESP_ERROR;
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:11:08 +00:00
|
|
|
if !plug {
|
|
|
|
if let Err(e) = self.discard_memory_range(offset, size) {
|
|
|
|
error!("failed discarding memory range: {:?}", e);
|
2020-03-04 02:12:22 +00:00
|
|
|
return VIRTIO_MEM_RESP_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:11:08 +00:00
|
|
|
self.blocks_state
|
2021-03-02 17:47:01 +00:00
|
|
|
.lock()
|
|
|
|
.unwrap()
|
2021-02-02 18:11:08 +00:00
|
|
|
.set_range(first_block_index, nb_blocks, plug);
|
|
|
|
|
2021-09-21 09:00:22 +00:00
|
|
|
let handlers = self.dma_mapping_handlers.lock().unwrap();
|
2021-02-02 18:11:08 +00:00
|
|
|
if plug {
|
2021-03-02 17:47:01 +00:00
|
|
|
let mut gpa = addr;
|
|
|
|
for _ in 0..nb_blocks {
|
2021-09-21 09:00:22 +00:00
|
|
|
for (_, handler) in handlers.iter() {
|
2021-03-02 17:47:01 +00:00
|
|
|
if let Err(e) = handler.map(gpa, gpa, config.block_size) {
|
|
|
|
error!(
|
|
|
|
"failed DMA mapping addr 0x{:x} size 0x{:x}: {}",
|
|
|
|
gpa, config.block_size, e
|
|
|
|
);
|
|
|
|
return VIRTIO_MEM_RESP_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gpa += config.block_size;
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:11:08 +00:00
|
|
|
config.plugged_size += size;
|
|
|
|
} else {
|
2021-09-21 09:00:22 +00:00
|
|
|
for (_, handler) in handlers.iter() {
|
2021-03-02 17:47:01 +00:00
|
|
|
if let Err(e) = handler.unmap(addr, size) {
|
|
|
|
error!(
|
|
|
|
"failed DMA unmapping addr 0x{:x} size 0x{:x}: {}",
|
|
|
|
addr, size, e
|
|
|
|
);
|
|
|
|
return VIRTIO_MEM_RESP_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:11:08 +00:00
|
|
|
config.plugged_size -= size;
|
|
|
|
}
|
2020-03-04 02:12:22 +00:00
|
|
|
|
|
|
|
VIRTIO_MEM_RESP_ACK
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:11:08 +00:00
|
|
|
fn unplug_all(&mut self) -> u16 {
|
|
|
|
let mut config = self.config.lock().unwrap();
|
|
|
|
if let Err(e) = self.discard_memory_range(0, config.region_size) {
|
|
|
|
error!("failed discarding memory range: {:?}", e);
|
|
|
|
return VIRTIO_MEM_RESP_ERROR;
|
2020-03-04 02:12:22 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 17:47:01 +00:00
|
|
|
// Remaining plugged blocks are unmapped.
|
|
|
|
if config.plugged_size > 0 {
|
2021-09-21 09:00:22 +00:00
|
|
|
let handlers = self.dma_mapping_handlers.lock().unwrap();
|
2021-03-02 17:47:01 +00:00
|
|
|
for (idx, plugged) in self.blocks_state.lock().unwrap().inner().iter().enumerate() {
|
|
|
|
if *plugged {
|
|
|
|
let gpa = config.addr + (idx as u64 * config.block_size);
|
2021-09-21 09:00:22 +00:00
|
|
|
for (_, handler) in handlers.iter() {
|
2021-03-02 17:47:01 +00:00
|
|
|
if let Err(e) = handler.unmap(gpa, config.block_size) {
|
|
|
|
error!(
|
|
|
|
"failed DMA unmapping addr 0x{:x} size 0x{:x}: {}",
|
|
|
|
gpa, config.block_size, e
|
|
|
|
);
|
|
|
|
return VIRTIO_MEM_RESP_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.blocks_state.lock().unwrap().set_range(
|
|
|
|
0,
|
|
|
|
(config.region_size / config.block_size) as u16,
|
|
|
|
false,
|
|
|
|
);
|
2021-02-02 18:11:08 +00:00
|
|
|
|
|
|
|
config.plugged_size = 0;
|
|
|
|
|
2020-03-04 02:12:22 +00:00
|
|
|
VIRTIO_MEM_RESP_ACK
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:11:08 +00:00
|
|
|
fn state_request(&self, addr: u64, nb_blocks: u16) -> (u16, u16) {
|
|
|
|
let config = self.config.lock().unwrap();
|
|
|
|
let size: u64 = nb_blocks as u64 * config.block_size;
|
|
|
|
|
|
|
|
let resp_type = if config.is_valid_range(addr, size) {
|
2020-03-04 02:12:22 +00:00
|
|
|
VIRTIO_MEM_RESP_ACK
|
|
|
|
} else {
|
|
|
|
VIRTIO_MEM_RESP_ERROR
|
|
|
|
};
|
|
|
|
|
|
|
|
let offset = addr - config.addr;
|
2021-02-02 18:11:08 +00:00
|
|
|
let first_block_index = (offset / config.block_size) as usize;
|
2021-03-02 17:47:01 +00:00
|
|
|
let resp_state =
|
|
|
|
if self
|
|
|
|
.blocks_state
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.is_range_state(first_block_index, nb_blocks, true)
|
|
|
|
{
|
|
|
|
VIRTIO_MEM_STATE_PLUGGED
|
|
|
|
} else if self.blocks_state.lock().unwrap().is_range_state(
|
|
|
|
first_block_index,
|
|
|
|
nb_blocks,
|
|
|
|
false,
|
|
|
|
) {
|
|
|
|
VIRTIO_MEM_STATE_UNPLUGGED
|
|
|
|
} else {
|
|
|
|
VIRTIO_MEM_STATE_MIXED
|
|
|
|
};
|
2020-03-04 02:12:22 +00:00
|
|
|
|
|
|
|
(resp_type, resp_state)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signal(&self, int_type: &VirtioInterruptType) -> result::Result<(), DeviceError> {
|
|
|
|
self.interrupt_cb
|
|
|
|
.trigger(int_type, Some(&self.queue))
|
|
|
|
.map_err(|e| {
|
|
|
|
error!("Failed to signal used queue: {:?}", e);
|
|
|
|
DeviceError::FailedSignalingUsedQueue(e)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_queue(&mut self) -> bool {
|
2021-02-02 18:11:08 +00:00
|
|
|
let mut request_list = Vec::new();
|
2020-03-04 02:12:22 +00:00
|
|
|
let mut used_count = 0;
|
2021-10-21 10:41:16 +00:00
|
|
|
|
|
|
|
for mut desc_chain in self.queue.iter().unwrap() {
|
|
|
|
request_list.push((
|
|
|
|
desc_chain.head_index(),
|
|
|
|
Request::parse(&mut desc_chain),
|
|
|
|
desc_chain.memory().clone(),
|
|
|
|
));
|
2021-02-02 18:11:08 +00:00
|
|
|
}
|
|
|
|
|
2021-10-21 10:41:16 +00:00
|
|
|
for (head_index, request, memory) in request_list {
|
2021-02-02 18:11:08 +00:00
|
|
|
let len = match request {
|
2020-03-04 02:12:22 +00:00
|
|
|
Err(e) => {
|
|
|
|
error!("failed parse VirtioMemReq: {:?}", e);
|
|
|
|
0
|
|
|
|
}
|
2021-02-02 18:11:08 +00:00
|
|
|
Ok(r) => match r.req.req_type {
|
|
|
|
VIRTIO_MEM_REQ_PLUG => {
|
|
|
|
let resp_type =
|
|
|
|
self.state_change_request(r.req.addr, r.req.nb_blocks, true);
|
2021-10-21 10:41:16 +00:00
|
|
|
r.send_response(&memory, resp_type, 0u16)
|
2020-03-04 02:12:22 +00:00
|
|
|
}
|
2021-02-02 18:11:08 +00:00
|
|
|
VIRTIO_MEM_REQ_UNPLUG => {
|
|
|
|
let resp_type =
|
|
|
|
self.state_change_request(r.req.addr, r.req.nb_blocks, false);
|
2021-10-21 10:41:16 +00:00
|
|
|
r.send_response(&memory, resp_type, 0u16)
|
2021-02-02 18:11:08 +00:00
|
|
|
}
|
|
|
|
VIRTIO_MEM_REQ_UNPLUG_ALL => {
|
|
|
|
let resp_type = self.unplug_all();
|
2021-10-21 10:41:16 +00:00
|
|
|
r.send_response(&memory, resp_type, 0u16)
|
2021-02-02 18:11:08 +00:00
|
|
|
}
|
|
|
|
VIRTIO_MEM_REQ_STATE => {
|
|
|
|
let (resp_type, resp_state) =
|
|
|
|
self.state_request(r.req.addr, r.req.nb_blocks);
|
2021-10-21 10:41:16 +00:00
|
|
|
r.send_response(&memory, resp_type, resp_state)
|
2021-02-02 18:11:08 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
error!("VirtioMemReq unknown request type {:?}", r.req.req_type);
|
|
|
|
0
|
|
|
|
}
|
|
|
|
},
|
2020-03-04 02:12:22 +00:00
|
|
|
};
|
2021-02-02 18:11:08 +00:00
|
|
|
|
2021-10-21 10:41:16 +00:00
|
|
|
self.queue.add_used(head_index, len).unwrap();
|
2020-03-04 02:12:22 +00:00
|
|
|
used_count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
used_count > 0
|
|
|
|
}
|
|
|
|
|
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.resize.evt.as_raw_fd(), RESIZE_EVENT)?;
|
|
|
|
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-03-04 02:12:22 +00:00
|
|
|
|
2020-08-04 11:16:44 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 02:12:22 +00:00
|
|
|
|
2020-08-04 11:16:44 +00:00
|
|
|
impl EpollHelperHandler for MemEpollHandler {
|
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
|
|
|
RESIZE_EVENT => {
|
|
|
|
if let Err(e) = self.resize.evt.read() {
|
|
|
|
error!("Failed to get resize event: {:?}", e);
|
|
|
|
return true;
|
|
|
|
} else {
|
2021-02-02 15:17:35 +00:00
|
|
|
let size = self.resize.size();
|
2020-08-04 11:16:44 +00:00
|
|
|
let mut config = self.config.lock().unwrap();
|
|
|
|
let mut signal_error = false;
|
2021-02-02 10:35:38 +00:00
|
|
|
let mut r = config.resize(size);
|
2020-08-10 10:22:55 +00:00
|
|
|
r = match r {
|
|
|
|
Err(e) => Err(e),
|
|
|
|
_ => match self.signal(&VirtioInterruptType::Config) {
|
|
|
|
Err(e) => {
|
|
|
|
signal_error = true;
|
|
|
|
Err(Error::ResizeTriggerFail(e))
|
|
|
|
}
|
|
|
|
_ => Ok(()),
|
|
|
|
},
|
2020-08-04 11:16:44 +00:00
|
|
|
};
|
|
|
|
if let Err(e) = self.resize.send(r) {
|
2020-09-22 11:31:42 +00:00
|
|
|
error!("Sending \"resize\" response: {:?}", e);
|
2020-08-04 11:16:44 +00:00
|
|
|
return true;
|
2020-03-04 02:12:22 +00:00
|
|
|
}
|
2020-08-04 11:16:44 +00:00
|
|
|
if signal_error {
|
|
|
|
return true;
|
2020-03-04 02:12:22 +00:00
|
|
|
}
|
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(&VirtioInterruptType::Queue) {
|
|
|
|
error!("Failed to signal used queue: {:?}", e);
|
|
|
|
return true;
|
2020-03-04 02:12:22 +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;
|
|
|
|
}
|
2020-03-04 02:12:22 +00:00
|
|
|
}
|
2020-08-04 11:16:44 +00:00
|
|
|
false
|
2020-03-04 02:12:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 09:00:22 +00:00
|
|
|
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
pub enum VirtioMemMappingSource {
|
|
|
|
Container,
|
|
|
|
Device(u32),
|
|
|
|
}
|
|
|
|
|
2021-09-22 15:26:30 +00:00
|
|
|
#[derive(Versionize)]
|
|
|
|
pub struct MemState {
|
|
|
|
pub avail_features: u64,
|
|
|
|
pub acked_features: u64,
|
|
|
|
pub config: VirtioMemConfig,
|
|
|
|
pub blocks_state: BlocksState,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VersionMapped for MemState {}
|
|
|
|
|
2020-03-04 02:12:22 +00:00
|
|
|
pub struct Mem {
|
2020-09-03 09:37:36 +00:00
|
|
|
common: VirtioCommon,
|
2020-04-27 11:44:43 +00:00
|
|
|
id: String,
|
2021-02-02 15:17:35 +00:00
|
|
|
resize: ResizeSender,
|
2020-03-04 02:12:22 +00:00
|
|
|
host_addr: u64,
|
|
|
|
host_fd: Option<RawFd>,
|
|
|
|
config: Arc<Mutex<VirtioMemConfig>>,
|
2020-08-18 01:48:52 +00:00
|
|
|
seccomp_action: SeccompAction,
|
2021-02-03 08:46:14 +00:00
|
|
|
hugepages: bool,
|
2021-09-21 09:00:22 +00:00
|
|
|
dma_mapping_handlers: Arc<Mutex<BTreeMap<VirtioMemMappingSource, Arc<dyn ExternalDmaMapping>>>>,
|
2021-03-02 17:47:01 +00:00
|
|
|
blocks_state: Arc<Mutex<BlocksState>>,
|
2021-09-07 15:10:48 +00:00
|
|
|
exit_evt: EventFd,
|
2020-03-04 02:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Mem {
|
|
|
|
// Create a new virtio-mem device.
|
2021-09-07 15:10:48 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2020-08-18 01:48:52 +00:00
|
|
|
pub fn new(
|
|
|
|
id: String,
|
|
|
|
region: &Arc<GuestRegionMmap>,
|
2021-02-02 15:17:35 +00:00
|
|
|
resize: ResizeSender,
|
2020-08-18 01:48:52 +00:00
|
|
|
seccomp_action: SeccompAction,
|
2020-09-11 13:09:06 +00:00
|
|
|
numa_node_id: Option<u16>,
|
2020-08-10 10:22:55 +00:00
|
|
|
initial_size: u64,
|
2021-02-03 08:46:14 +00:00
|
|
|
hugepages: bool,
|
2021-09-07 15:10:48 +00:00
|
|
|
exit_evt: EventFd,
|
2021-09-23 08:42:00 +00:00
|
|
|
blocks_state: Arc<Mutex<BlocksState>>,
|
2020-08-18 01:48:52 +00:00
|
|
|
) -> io::Result<Mem> {
|
2020-03-04 02:12:22 +00:00
|
|
|
let region_len = region.len();
|
|
|
|
|
2020-09-14 09:36:31 +00:00
|
|
|
if region_len != region_len / VIRTIO_MEM_ALIGN_SIZE * VIRTIO_MEM_ALIGN_SIZE {
|
2020-03-04 02:12:22 +00:00
|
|
|
return Err(io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
format!(
|
|
|
|
"Virtio-mem size is not aligned with {}",
|
2020-09-14 09:36:31 +00:00
|
|
|
VIRTIO_MEM_ALIGN_SIZE
|
2020-03-04 02:12:22 +00:00
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2020-09-11 13:09:06 +00:00
|
|
|
let mut avail_features = 1u64 << VIRTIO_F_VERSION_1;
|
2020-03-04 02:12:22 +00:00
|
|
|
|
2021-02-02 10:35:38 +00:00
|
|
|
let mut config = VirtioMemConfig {
|
|
|
|
block_size: VIRTIO_MEM_DEFAULT_BLOCK_SIZE,
|
|
|
|
addr: region.start_addr().raw_value(),
|
|
|
|
region_size: region.len(),
|
|
|
|
usable_region_size: region.len(),
|
|
|
|
plugged_size: 0,
|
|
|
|
requested_size: 0,
|
|
|
|
..Default::default()
|
|
|
|
};
|
2020-03-04 02:12:22 +00:00
|
|
|
|
2020-08-10 10:22:55 +00:00
|
|
|
if initial_size != 0 {
|
2021-02-02 10:35:38 +00:00
|
|
|
config.resize(initial_size).map_err(|e| {
|
2020-08-10 10:22:55 +00:00
|
|
|
io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
2021-02-02 10:35:38 +00:00
|
|
|
format!(
|
|
|
|
"Failed to resize virtio-mem configuration to {}: {:?}",
|
|
|
|
initial_size, e
|
|
|
|
),
|
2020-08-10 10:22:55 +00:00
|
|
|
)
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
|
2020-09-11 13:09:06 +00:00
|
|
|
if let Some(node_id) = numa_node_id {
|
|
|
|
avail_features |= 1u64 << VIRTIO_MEM_F_ACPI_PXM;
|
|
|
|
config.node_id = node_id;
|
|
|
|
}
|
|
|
|
|
2021-02-02 10:35:38 +00:00
|
|
|
// Make sure the virtio-mem configuration complies with the
|
|
|
|
// specification.
|
|
|
|
config.validate().map_err(|e| {
|
|
|
|
io::Error::new(
|
|
|
|
io::ErrorKind::Other,
|
|
|
|
format!("Invalid virtio-mem configuration: {:?}", e),
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let host_fd = region
|
|
|
|
.file_offset()
|
|
|
|
.map(|f_offset| f_offset.file().as_raw_fd());
|
2020-03-04 02:12:22 +00:00
|
|
|
|
|
|
|
Ok(Mem {
|
2020-09-03 09:37:36 +00:00
|
|
|
common: VirtioCommon {
|
2021-03-25 16:54:09 +00:00
|
|
|
device_type: VirtioDeviceType::Mem 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(2))),
|
|
|
|
queue_sizes: QUEUE_SIZES.to_vec(),
|
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:44:43 +00:00
|
|
|
id,
|
2020-03-04 02:12:22 +00:00
|
|
|
resize,
|
|
|
|
host_addr: region.as_ptr() as u64,
|
|
|
|
host_fd,
|
|
|
|
config: Arc::new(Mutex::new(config)),
|
2020-08-18 01:48:52 +00:00
|
|
|
seccomp_action,
|
2021-02-03 08:46:14 +00:00
|
|
|
hugepages,
|
2021-09-21 09:00:22 +00:00
|
|
|
dma_mapping_handlers: Arc::new(Mutex::new(BTreeMap::new())),
|
2021-09-23 08:42:00 +00:00
|
|
|
blocks_state,
|
2021-09-07 15:10:48 +00:00
|
|
|
exit_evt,
|
2020-03-04 02:12:22 +00:00
|
|
|
})
|
|
|
|
}
|
2021-03-02 17:47:01 +00:00
|
|
|
|
|
|
|
pub fn add_dma_mapping_handler(
|
|
|
|
&mut self,
|
2021-09-21 09:00:22 +00:00
|
|
|
source: VirtioMemMappingSource,
|
2021-03-02 17:47:01 +00:00
|
|
|
handler: Arc<dyn ExternalDmaMapping>,
|
|
|
|
) -> result::Result<(), Error> {
|
|
|
|
let config = self.config.lock().unwrap();
|
|
|
|
|
|
|
|
if config.plugged_size > 0 {
|
|
|
|
for (idx, plugged) in self.blocks_state.lock().unwrap().inner().iter().enumerate() {
|
|
|
|
if *plugged {
|
|
|
|
let gpa = config.addr + (idx as u64 * config.block_size);
|
|
|
|
handler
|
|
|
|
.map(gpa, gpa, config.block_size)
|
|
|
|
.map_err(Error::DmaMap)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 09:00:22 +00:00
|
|
|
self.dma_mapping_handlers
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.insert(source, handler);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove_dma_mapping_handler(
|
|
|
|
&mut self,
|
|
|
|
source: VirtioMemMappingSource,
|
|
|
|
) -> result::Result<(), Error> {
|
|
|
|
let handler = self
|
|
|
|
.dma_mapping_handlers
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.remove(&source)
|
|
|
|
.ok_or(Error::InvalidDmaMappingHandler)?;
|
|
|
|
|
|
|
|
let config = self.config.lock().unwrap();
|
|
|
|
|
|
|
|
if config.plugged_size > 0 {
|
|
|
|
for (idx, plugged) in self.blocks_state.lock().unwrap().inner().iter().enumerate() {
|
|
|
|
if *plugged {
|
|
|
|
let gpa = config.addr + (idx as u64 * config.block_size);
|
|
|
|
handler
|
|
|
|
.unmap(gpa, config.block_size)
|
|
|
|
.map_err(Error::DmaUnmap)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-02 17:47:01 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-09-22 15:26:30 +00:00
|
|
|
|
|
|
|
fn state(&self) -> MemState {
|
|
|
|
MemState {
|
|
|
|
avail_features: self.common.avail_features,
|
|
|
|
acked_features: self.common.acked_features,
|
|
|
|
config: *(self.config.lock().unwrap()),
|
|
|
|
blocks_state: self.blocks_state.lock().unwrap().clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_state(&mut self, state: &MemState) {
|
|
|
|
self.common.avail_features = state.avail_features;
|
|
|
|
self.common.acked_features = state.acked_features;
|
|
|
|
*(self.config.lock().unwrap()) = state.config;
|
|
|
|
*(self.blocks_state.lock().unwrap()) = state.blocks_state.clone();
|
|
|
|
}
|
2020-03-04 02:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Mem {
|
|
|
|
fn drop(&mut self) {
|
2020-09-04 08:37:37 +00:00
|
|
|
if let Some(kill_evt) = self.common.kill_evt.take() {
|
2020-03-04 02:12:22 +00:00
|
|
|
// Ignore the result because there is nothing we can do about it.
|
|
|
|
let _ = kill_evt.write(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VirtioDevice for Mem {
|
|
|
|
fn device_type(&self) -> u32 {
|
2020-09-04 08:37:37 +00:00
|
|
|
self.common.device_type
|
2020-03-04 02:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn queue_max_sizes(&self) -> &[u16] {
|
2020-09-04 08:37:37 +00:00
|
|
|
&self.common.queue_sizes
|
2020-03-04 02:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn features(&self) -> u64 {
|
2020-09-03 09:37:36 +00:00
|
|
|
self.common.avail_features
|
2020-03-04 02:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ack_features(&mut self, value: u64) {
|
2020-09-03 09:37:36 +00:00
|
|
|
self.common.ack_features(value)
|
2020-03-04 02:12:22 +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.lock().unwrap().as_slice(), offset, data);
|
2020-03-04 02:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn activate(
|
|
|
|
&mut self,
|
2021-10-21 10:41:16 +00:00
|
|
|
_mem: GuestMemoryAtomic<GuestMemoryMmap>,
|
2020-03-04 02:12:22 +00:00
|
|
|
interrupt_cb: Arc<dyn VirtioInterrupt>,
|
2021-10-21 10:41:16 +00:00
|
|
|
mut queues: Vec<Queue<GuestMemoryAtomic<GuestMemoryMmap>>>,
|
2020-03-04 02:12:22 +00:00
|
|
|
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();
|
2020-03-04 02:12:22 +00:00
|
|
|
let mut handler = MemEpollHandler {
|
|
|
|
host_addr: self.host_addr,
|
|
|
|
host_fd: self.host_fd,
|
2021-03-02 17:47:01 +00:00
|
|
|
blocks_state: Arc::clone(&self.blocks_state),
|
2020-03-04 02:12:22 +00:00
|
|
|
config: self.config.clone(),
|
2021-02-02 15:17:35 +00:00
|
|
|
resize: self.resize.clone(),
|
2020-03-04 02:12:22 +00:00
|
|
|
queue: queues.remove(0),
|
|
|
|
interrupt_cb,
|
|
|
|
queue_evt: queue_evts.remove(0),
|
|
|
|
kill_evt,
|
|
|
|
pause_evt,
|
2021-02-03 08:46:14 +00:00
|
|
|
hugepages: self.hugepages,
|
2021-09-21 09:00:22 +00:00
|
|
|
dma_mapping_handlers: Arc::clone(&self.dma_mapping_handlers),
|
2020-03-04 02:12:22 +00:00
|
|
|
};
|
|
|
|
|
2021-09-24 09:44:40 +00:00
|
|
|
let unplugged_memory_ranges = self.blocks_state.lock().unwrap().memory_ranges(0, false);
|
|
|
|
for range in unplugged_memory_ranges.regions() {
|
|
|
|
handler
|
|
|
|
.discard_memory_range(range.gpa, range.length)
|
|
|
|
.map_err(|e| {
|
|
|
|
error!(
|
|
|
|
"failed discarding memory range [0x{:x}-0x{:x}]: {:?}",
|
|
|
|
range.gpa,
|
|
|
|
range.gpa + range.length - 1,
|
|
|
|
e
|
|
|
|
);
|
|
|
|
ActivateError::BadActivate
|
|
|
|
})?;
|
|
|
|
}
|
2021-02-02 18:11:08 +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-03-04 02:12:22 +00:00
|
|
|
let mut epoll_threads = Vec::new();
|
2021-09-03 10:43:30 +00:00
|
|
|
|
|
|
|
spawn_virtio_thread(
|
|
|
|
&self.id,
|
|
|
|
&self.seccomp_action,
|
|
|
|
Thread::VirtioMem,
|
|
|
|
&mut epoll_threads,
|
2021-09-07 15:10:48 +00:00
|
|
|
&self.exit_evt,
|
2021-09-03 10:43:30 +00:00
|
|
|
move || {
|
2021-08-17 00:20:11 +00:00
|
|
|
if let Err(e) = handler.run(paused, paused_sync.unwrap()) {
|
2020-08-17 19:45:17 +00:00
|
|
|
error!("Error running worker: {:?}", e);
|
|
|
|
}
|
2021-09-03 10:43:30 +00:00
|
|
|
},
|
|
|
|
)?;
|
2020-09-04 08:37:37 +00:00
|
|
|
self.common.epoll_threads = Some(epoll_threads);
|
2020-03-04 02:12:22 +00:00
|
|
|
|
2021-02-18 15:10:51 +00:00
|
|
|
event!("virtio-device", "activated", "id", &self.id);
|
2020-03-04 02:12:22 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
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
|
2020-09-04 08:37:37 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-04 02:12:22 +00:00
|
|
|
|
2020-09-04 08:37:37 +00:00
|
|
|
impl Pausable for Mem {
|
|
|
|
fn pause(&mut self) -> result::Result<(), MigratableError> {
|
|
|
|
self.common.pause()
|
|
|
|
}
|
2020-03-04 02:12:22 +00:00
|
|
|
|
2020-09-04 08:37:37 +00:00
|
|
|
fn resume(&mut self) -> result::Result<(), MigratableError> {
|
|
|
|
self.common.resume()
|
2020-03-04 02:12:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 11:44:43 +00:00
|
|
|
impl Snapshottable for Mem {
|
|
|
|
fn id(&self) -> String {
|
|
|
|
self.id.clone()
|
|
|
|
}
|
2021-09-22 15:26:30 +00:00
|
|
|
|
|
|
|
fn snapshot(&mut self) -> std::result::Result<Snapshot, MigratableError> {
|
|
|
|
Snapshot::new_from_versioned_state(&self.id(), &self.state())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> {
|
|
|
|
self.set_state(&snapshot.to_versioned_state(&self.id)?);
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-27 11:44:43 +00:00
|
|
|
}
|
2019-05-01 16:59:51 +00:00
|
|
|
impl Transportable for Mem {}
|
2020-03-04 02:12:22 +00:00
|
|
|
impl Migratable for Mem {}
|