2020-03-20 03:43:17 +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.
|
|
|
|
|
2022-02-10 13:40:09 +00:00
|
|
|
use crate::{
|
|
|
|
seccomp_filters::Thread, thread_helper::spawn_virtio_thread, ActivateError, ActivateResult,
|
|
|
|
EpollHelper, EpollHelperError, EpollHelperHandler, GuestMemoryMmap, VirtioCommon, VirtioDevice,
|
|
|
|
VirtioDeviceType, VirtioInterrupt, VirtioInterruptType, EPOLL_HELPER_EVENT_LAST,
|
|
|
|
VIRTIO_F_VERSION_1,
|
2020-03-20 03:43:17 +00:00
|
|
|
};
|
|
|
|
use libc::EFD_NONBLOCK;
|
2021-09-03 10:43:30 +00:00
|
|
|
use seccompiler::SeccompAction;
|
2020-07-16 09:34:51 +00:00
|
|
|
use std::io;
|
2020-03-20 03:43:17 +00:00
|
|
|
use std::mem::size_of;
|
2020-08-11 16:12:12 +00:00
|
|
|
use std::os::unix::io::AsRawFd;
|
2020-03-20 03:43:17 +00:00
|
|
|
use std::result;
|
2022-02-10 13:40:09 +00:00
|
|
|
use std::sync::{
|
|
|
|
atomic::{AtomicBool, AtomicU64, Ordering},
|
|
|
|
mpsc, Arc, Barrier, Mutex,
|
|
|
|
};
|
2021-09-21 08:20:01 +00:00
|
|
|
use versionize::{VersionMap, Versionize, VersionizeResult};
|
|
|
|
use versionize_derive::Versionize;
|
2021-10-21 10:41:16 +00:00
|
|
|
use virtio_queue::Queue;
|
2022-02-10 13:40:09 +00:00
|
|
|
use vm_memory::{
|
|
|
|
Address, ByteValued, Bytes, GuestAddress, GuestMemory, GuestMemoryAtomic, GuestMemoryError,
|
|
|
|
GuestMemoryRegion,
|
|
|
|
};
|
|
|
|
use vm_migration::{
|
|
|
|
Migratable, MigratableError, Pausable, Snapshot, Snapshottable, Transportable, VersionMapped,
|
|
|
|
};
|
2020-03-20 03:43:17 +00:00
|
|
|
use vmm_sys_util::eventfd::EventFd;
|
|
|
|
|
|
|
|
const QUEUE_SIZE: u16 = 128;
|
2022-02-10 13:52:35 +00:00
|
|
|
const REPORTING_QUEUE_SIZE: u16 = 32;
|
|
|
|
const MIN_NUM_QUEUES: usize = 2;
|
2020-03-20 03:43:17 +00:00
|
|
|
|
2022-02-10 13:40:09 +00:00
|
|
|
// Resize event.
|
2020-08-11 16:12:12 +00:00
|
|
|
const RESIZE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 1;
|
2022-02-10 13:40:09 +00:00
|
|
|
// Inflate virtio queue event.
|
2020-08-11 16:12:12 +00:00
|
|
|
const INFLATE_QUEUE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 2;
|
2022-02-10 13:40:09 +00:00
|
|
|
// Deflate virtio queue event.
|
2020-08-11 16:12:12 +00:00
|
|
|
const DEFLATE_QUEUE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 3;
|
2022-02-10 13:52:35 +00:00
|
|
|
// Reporting virtio queue event.
|
|
|
|
const REPORTING_QUEUE_EVENT: u16 = EPOLL_HELPER_EVENT_LAST + 4;
|
2020-03-20 03:43:17 +00:00
|
|
|
|
|
|
|
// Size of a PFN in the balloon interface.
|
|
|
|
const VIRTIO_BALLOON_PFN_SHIFT: u64 = 12;
|
|
|
|
|
2021-01-21 11:22:29 +00:00
|
|
|
// Deflate balloon on OOM
|
|
|
|
const VIRTIO_BALLOON_F_DEFLATE_ON_OOM: u64 = 2;
|
2022-02-10 13:52:35 +00:00
|
|
|
// Enable an additional virtqueue to let the guest notify the host about free
|
|
|
|
// pages.
|
|
|
|
const VIRTIO_BALLOON_F_REPORTING: u64 = 5;
|
2021-01-21 11:22:29 +00:00
|
|
|
|
2020-03-20 03:43:17 +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 sent us invalid request.
|
|
|
|
InvalidRequest,
|
2021-01-28 07:56:42 +00:00
|
|
|
// Fallocate fail.
|
|
|
|
FallocateFail(std::io::Error),
|
2020-03-20 03:43:17 +00:00
|
|
|
// Madvise fail.
|
|
|
|
MadviseFail(std::io::Error),
|
|
|
|
// 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
|
|
|
|
ResizeInval(String),
|
2022-02-10 13:40:09 +00:00
|
|
|
// Invalid queue index
|
|
|
|
InvalidQueueIndex(usize),
|
2020-03-20 03:43:17 +00:00
|
|
|
// Fail tp signal
|
|
|
|
FailedSignal(io::Error),
|
2021-10-21 10:41:16 +00:00
|
|
|
/// Descriptor chain is too short
|
|
|
|
DescriptorChainTooShort,
|
|
|
|
/// Failed adding used index
|
|
|
|
QueueAddUsed(virtio_queue::Error),
|
|
|
|
/// Failed creating an iterator over the queue
|
|
|
|
QueueIterator(virtio_queue::Error),
|
2020-03-20 03:43:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Got from include/uapi/linux/virtio_balloon.h
|
2020-08-25 06:44:21 +00:00
|
|
|
#[repr(C)]
|
2021-09-21 08:20:01 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Default, Versionize)]
|
|
|
|
pub struct VirtioBalloonConfig {
|
2020-03-20 03:43:17 +00:00
|
|
|
// Number of pages host wants Guest to give up.
|
|
|
|
num_pages: u32,
|
|
|
|
// Number of pages we've actually got in balloon.
|
|
|
|
actual: u32,
|
|
|
|
}
|
|
|
|
|
2020-08-25 06:44:21 +00:00
|
|
|
const CONFIG_ACTUAL_OFFSET: u64 = 4;
|
|
|
|
const CONFIG_ACTUAL_SIZE: usize = 4;
|
|
|
|
|
2021-11-17 13:39:53 +00:00
|
|
|
// SAFETY: it only has data and has no implicit padding.
|
2020-03-20 03:43:17 +00:00
|
|
|
unsafe impl ByteValued for VirtioBalloonConfig {}
|
|
|
|
|
|
|
|
struct VirtioBalloonResizeReceiver {
|
|
|
|
size: Arc<AtomicU64>,
|
|
|
|
tx: mpsc::Sender<Result<(), Error>>,
|
|
|
|
evt: EventFd,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VirtioBalloonResizeReceiver {
|
|
|
|
fn get_size(&self) -> u64 {
|
2020-12-01 16:15:26 +00:00
|
|
|
self.size.load(Ordering::Acquire)
|
2020-03-20 03:43:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn send(&self, r: Result<(), Error>) -> Result<(), mpsc::SendError<Result<(), Error>>> {
|
|
|
|
self.tx.send(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct VirtioBalloonResize {
|
|
|
|
size: Arc<AtomicU64>,
|
|
|
|
tx: mpsc::Sender<Result<(), Error>>,
|
|
|
|
rx: mpsc::Receiver<Result<(), Error>>,
|
|
|
|
evt: EventFd,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VirtioBalloonResize {
|
2021-09-21 08:20:01 +00:00
|
|
|
pub fn new(size: u64) -> io::Result<Self> {
|
2020-03-20 03:43:17 +00:00
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
|
|
|
Ok(Self {
|
2021-09-21 08:20:01 +00:00
|
|
|
size: Arc::new(AtomicU64::new(size)),
|
2020-03-20 03:43:17 +00:00
|
|
|
tx,
|
|
|
|
rx,
|
|
|
|
evt: EventFd::new(EFD_NONBLOCK)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_receiver(&self) -> Result<VirtioBalloonResizeReceiver, Error> {
|
|
|
|
Ok(VirtioBalloonResizeReceiver {
|
|
|
|
size: self.size.clone(),
|
|
|
|
tx: self.tx.clone(),
|
|
|
|
evt: self.evt.try_clone().map_err(Error::EventFdTryCloneFail)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn work(&self, size: u64) -> Result<(), Error> {
|
2020-12-01 16:15:26 +00:00
|
|
|
self.size.store(size, Ordering::Release);
|
2020-03-20 03:43:17 +00:00
|
|
|
self.evt.write(1).map_err(Error::EventFdWriteFail)?;
|
|
|
|
self.rx.recv().map_err(Error::MpscRecvFail)?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct BalloonEpollHandler {
|
|
|
|
config: Arc<Mutex<VirtioBalloonConfig>>,
|
|
|
|
resize_receiver: VirtioBalloonResizeReceiver,
|
2021-10-21 10:41:16 +00:00
|
|
|
queues: Vec<Queue<GuestMemoryAtomic<GuestMemoryMmap>>>,
|
2020-03-20 03:43:17 +00:00
|
|
|
interrupt_cb: Arc<dyn VirtioInterrupt>,
|
|
|
|
inflate_queue_evt: EventFd,
|
|
|
|
deflate_queue_evt: EventFd,
|
2022-02-10 13:52:35 +00:00
|
|
|
reporting_queue_evt: Option<EventFd>,
|
2020-03-20 03:43:17 +00:00
|
|
|
kill_evt: EventFd,
|
|
|
|
pause_evt: EventFd,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BalloonEpollHandler {
|
2022-01-24 14:30:42 +00:00
|
|
|
fn signal(&self, int_type: VirtioInterruptType) -> result::Result<(), Error> {
|
|
|
|
self.interrupt_cb.trigger(int_type).map_err(|e| {
|
2020-03-20 03:43:17 +00:00
|
|
|
error!("Failed to signal used queue: {:?}", e);
|
|
|
|
Error::FailedSignal(e)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-10 13:40:09 +00:00
|
|
|
fn advise_memory_range(
|
|
|
|
memory: &GuestMemoryMmap,
|
|
|
|
range_base: GuestAddress,
|
|
|
|
range_len: usize,
|
|
|
|
advice: libc::c_int,
|
|
|
|
) -> result::Result<(), Error> {
|
|
|
|
let hva = memory
|
|
|
|
.get_host_address(range_base)
|
|
|
|
.map_err(Error::GuestMemory)?;
|
|
|
|
// Need unsafe to do syscall madvise
|
|
|
|
let res =
|
|
|
|
unsafe { libc::madvise(hva as *mut libc::c_void, range_len as libc::size_t, advice) };
|
|
|
|
if res != 0 {
|
|
|
|
return Err(Error::MadviseFail(io::Error::last_os_error()));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn release_memory_range(
|
|
|
|
memory: &GuestMemoryMmap,
|
|
|
|
range_base: GuestAddress,
|
|
|
|
range_len: usize,
|
|
|
|
) -> result::Result<(), Error> {
|
|
|
|
let region = memory.find_region(range_base).ok_or(Error::GuestMemory(
|
|
|
|
GuestMemoryError::InvalidGuestAddress(range_base),
|
|
|
|
))?;
|
|
|
|
if let Some(f_off) = region.file_offset() {
|
|
|
|
let offset = range_base.0 - region.start_addr().0;
|
|
|
|
let res = unsafe {
|
|
|
|
libc::fallocate64(
|
|
|
|
f_off.file().as_raw_fd(),
|
|
|
|
libc::FALLOC_FL_PUNCH_HOLE | libc::FALLOC_FL_KEEP_SIZE,
|
|
|
|
(offset as u64 + f_off.start()) as libc::off64_t,
|
|
|
|
range_len as libc::off64_t,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
if res != 0 {
|
|
|
|
return Err(Error::FallocateFail(io::Error::last_os_error()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Self::advise_memory_range(memory, range_base, range_len, libc::MADV_DONTNEED)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn notify_queue(
|
|
|
|
&mut self,
|
|
|
|
queue_index: usize,
|
|
|
|
used_descs: Vec<(u16, u32)>,
|
|
|
|
) -> result::Result<(), Error> {
|
|
|
|
for (desc_index, len) in used_descs.iter() {
|
|
|
|
self.queues[queue_index]
|
|
|
|
.add_used(*desc_index, *len)
|
|
|
|
.map_err(Error::QueueAddUsed)?;
|
|
|
|
}
|
2020-03-20 03:43:17 +00:00
|
|
|
|
2022-02-10 13:40:09 +00:00
|
|
|
if !used_descs.is_empty() {
|
|
|
|
self.signal(VirtioInterruptType::Queue(queue_index as u16))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_queue(&mut self, queue_index: usize) -> result::Result<(), Error> {
|
|
|
|
let mut used_descs = Vec::new();
|
2021-10-21 10:41:16 +00:00
|
|
|
for mut desc_chain in self.queues[queue_index]
|
|
|
|
.iter()
|
|
|
|
.map_err(Error::QueueIterator)?
|
|
|
|
{
|
|
|
|
let desc = desc_chain.next().ok_or(Error::DescriptorChainTooShort)?;
|
|
|
|
|
2022-02-10 13:40:09 +00:00
|
|
|
used_descs.push((desc_chain.head_index(), desc.len()));
|
2020-03-20 03:43:17 +00:00
|
|
|
|
2020-06-05 15:14:27 +00:00
|
|
|
let data_chunk_size = size_of::<u32>();
|
|
|
|
|
2020-03-20 03:43:17 +00:00
|
|
|
// The head contains the request type which MUST be readable.
|
2021-10-21 10:41:16 +00:00
|
|
|
if desc.is_write_only() {
|
2020-03-20 03:43:17 +00:00
|
|
|
error!("The head contains the request type is not right");
|
|
|
|
return Err(Error::UnexpectedWriteOnlyDescriptor);
|
|
|
|
}
|
2021-10-21 10:41:16 +00:00
|
|
|
if desc.len() as usize % data_chunk_size != 0 {
|
|
|
|
error!("the request size {} is not right", desc.len());
|
2020-03-20 03:43:17 +00:00
|
|
|
return Err(Error::InvalidRequest);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut offset = 0u64;
|
2021-10-21 10:41:16 +00:00
|
|
|
while offset < desc.len() as u64 {
|
|
|
|
let addr = desc.addr().checked_add(offset).unwrap();
|
|
|
|
let pfn: u32 = desc_chain
|
|
|
|
.memory()
|
|
|
|
.read_obj(addr)
|
|
|
|
.map_err(Error::GuestMemory)?;
|
2020-06-05 15:14:27 +00:00
|
|
|
offset += data_chunk_size as u64;
|
|
|
|
|
2022-02-10 13:40:09 +00:00
|
|
|
let range_base = GuestAddress((pfn as u64) << VIRTIO_BALLOON_PFN_SHIFT);
|
|
|
|
let range_len = 1 << VIRTIO_BALLOON_PFN_SHIFT;
|
|
|
|
|
|
|
|
match queue_index {
|
|
|
|
0 => {
|
|
|
|
Self::release_memory_range(desc_chain.memory(), range_base, range_len)?;
|
|
|
|
}
|
|
|
|
1 => {
|
|
|
|
Self::advise_memory_range(
|
|
|
|
desc_chain.memory(),
|
|
|
|
range_base,
|
|
|
|
range_len,
|
|
|
|
libc::MADV_WILLNEED,
|
|
|
|
)?;
|
2020-03-20 03:43:17 +00:00
|
|
|
}
|
2022-02-10 13:40:09 +00:00
|
|
|
_ => return Err(Error::InvalidQueueIndex(queue_index)),
|
2020-03-20 03:43:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 13:40:09 +00:00
|
|
|
self.notify_queue(queue_index, used_descs)
|
2020-03-20 03:43:17 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 13:52:35 +00:00
|
|
|
fn process_reporting_queue(&mut self, queue_index: usize) -> result::Result<(), Error> {
|
|
|
|
let mut used_descs = Vec::new();
|
|
|
|
|
|
|
|
for mut desc_chain in self.queues[queue_index]
|
|
|
|
.iter()
|
|
|
|
.map_err(Error::QueueIterator)?
|
|
|
|
{
|
|
|
|
let mut descs_len = 0;
|
|
|
|
while let Some(desc) = desc_chain.next() {
|
|
|
|
descs_len += desc.len();
|
|
|
|
Self::release_memory_range(desc_chain.memory(), desc.addr(), desc.len() as usize)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
used_descs.push((desc_chain.head_index(), descs_len));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.notify_queue(queue_index, used_descs)
|
|
|
|
}
|
|
|
|
|
2020-08-11 14:05:06 +00:00
|
|
|
fn run(
|
|
|
|
&mut self,
|
|
|
|
paused: Arc<AtomicBool>,
|
|
|
|
paused_sync: Arc<Barrier>,
|
|
|
|
) -> result::Result<(), EpollHelperError> {
|
2020-08-11 16:12:12 +00:00
|
|
|
let mut helper = EpollHelper::new(&self.kill_evt, &self.pause_evt)?;
|
|
|
|
helper.add_event(self.resize_receiver.evt.as_raw_fd(), RESIZE_EVENT)?;
|
|
|
|
helper.add_event(self.inflate_queue_evt.as_raw_fd(), INFLATE_QUEUE_EVENT)?;
|
|
|
|
helper.add_event(self.deflate_queue_evt.as_raw_fd(), DEFLATE_QUEUE_EVENT)?;
|
2022-02-10 13:52:35 +00:00
|
|
|
if let Some(reporting_queue_evt) = self.reporting_queue_evt.as_ref() {
|
|
|
|
helper.add_event(reporting_queue_evt.as_raw_fd(), REPORTING_QUEUE_EVENT)?;
|
|
|
|
}
|
2020-08-11 14:05:06 +00:00
|
|
|
helper.run(paused, paused_sync, self)?;
|
2020-08-11 16:12:12 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EpollHelperHandler for BalloonEpollHandler {
|
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-11 16:12:12 +00:00
|
|
|
RESIZE_EVENT => {
|
|
|
|
if let Err(e) = self.resize_receiver.evt.read() {
|
|
|
|
error!("Failed to get resize event: {:?}", e);
|
|
|
|
return true;
|
2020-03-20 03:43:17 +00:00
|
|
|
}
|
2020-08-11 16:12:12 +00:00
|
|
|
let mut signal_error = false;
|
|
|
|
let r = {
|
|
|
|
let mut config = self.config.lock().unwrap();
|
2020-08-25 06:44:21 +00:00
|
|
|
config.num_pages =
|
|
|
|
(self.resize_receiver.get_size() >> VIRTIO_BALLOON_PFN_SHIFT) as u32;
|
2022-01-24 14:30:42 +00:00
|
|
|
if let Err(e) = self.signal(VirtioInterruptType::Config) {
|
2020-08-11 16:12:12 +00:00
|
|
|
signal_error = true;
|
|
|
|
Err(e)
|
|
|
|
} else {
|
|
|
|
Ok(())
|
2020-03-20 03:43:17 +00:00
|
|
|
}
|
2020-08-11 16:12:12 +00:00
|
|
|
};
|
|
|
|
if let Err(e) = &r {
|
|
|
|
// This error will send back to resize caller.
|
|
|
|
error!("Handle resize event get error: {:?}", e);
|
|
|
|
}
|
|
|
|
if let Err(e) = self.resize_receiver.send(r) {
|
|
|
|
error!("Sending \"resize\" generated error: {:?}", e);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if signal_error {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
INFLATE_QUEUE_EVENT => {
|
|
|
|
if let Err(e) = self.inflate_queue_evt.read() {
|
|
|
|
error!("Failed to get inflate queue event: {:?}", e);
|
|
|
|
return true;
|
2022-02-10 13:40:09 +00:00
|
|
|
} else if let Err(e) = self.process_queue(0) {
|
2020-08-11 16:12:12 +00:00
|
|
|
error!("Failed to signal used inflate queue: {:?}", e);
|
|
|
|
return true;
|
2020-03-20 03:43:17 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-11 16:12:12 +00:00
|
|
|
DEFLATE_QUEUE_EVENT => {
|
|
|
|
if let Err(e) = self.deflate_queue_evt.read() {
|
|
|
|
error!("Failed to get deflate queue event: {:?}", e);
|
|
|
|
return true;
|
2022-02-10 13:40:09 +00:00
|
|
|
} else if let Err(e) = self.process_queue(1) {
|
2020-08-11 16:12:12 +00:00
|
|
|
error!("Failed to signal used deflate queue: {:?}", e);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2022-02-10 13:52:35 +00:00
|
|
|
REPORTING_QUEUE_EVENT => {
|
|
|
|
if let Some(reporting_queue_evt) = self.reporting_queue_evt.as_ref() {
|
|
|
|
if let Err(e) = reporting_queue_evt.read() {
|
|
|
|
error!("Failed to get reporting queue event: {:?}", e);
|
|
|
|
return true;
|
|
|
|
} else if let Err(e) = self.process_reporting_queue(2) {
|
|
|
|
error!("Failed to signal used inflate queue: {:?}", e);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error!("Invalid reporting queue event as no eventfd registered");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2020-08-11 16:12:12 +00:00
|
|
|
_ => {
|
2020-09-25 04:05:01 +00:00
|
|
|
error!("Unknown event for virtio-balloon");
|
2020-08-11 16:12:12 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-03-20 03:43:17 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 16:12:12 +00:00
|
|
|
false
|
2020-03-20 03:43:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 08:20:01 +00:00
|
|
|
#[derive(Versionize)]
|
|
|
|
pub struct BalloonState {
|
|
|
|
pub avail_features: u64,
|
|
|
|
pub acked_features: u64,
|
|
|
|
pub config: VirtioBalloonConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VersionMapped for BalloonState {}
|
|
|
|
|
2020-03-20 03:43:17 +00:00
|
|
|
// Virtio device for exposing entropy to the guest OS through virtio.
|
|
|
|
pub struct Balloon {
|
2020-09-03 09:37:36 +00:00
|
|
|
common: VirtioCommon,
|
2020-03-20 03:43:17 +00:00
|
|
|
id: String,
|
|
|
|
resize: VirtioBalloonResize,
|
|
|
|
config: Arc<Mutex<VirtioBalloonConfig>>,
|
2020-08-18 02:58:13 +00:00
|
|
|
seccomp_action: SeccompAction,
|
2021-09-07 15:10:48 +00:00
|
|
|
exit_evt: EventFd,
|
2020-03-20 03:43:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Balloon {
|
|
|
|
// Create a new virtio-balloon.
|
2021-01-21 11:22:29 +00:00
|
|
|
pub fn new(
|
|
|
|
id: String,
|
|
|
|
size: u64,
|
|
|
|
deflate_on_oom: bool,
|
2022-02-10 13:52:35 +00:00
|
|
|
free_page_reporting: bool,
|
2021-01-21 11:22:29 +00:00
|
|
|
seccomp_action: SeccompAction,
|
2021-09-07 15:10:48 +00:00
|
|
|
exit_evt: EventFd,
|
2021-01-21 11:22:29 +00:00
|
|
|
) -> io::Result<Self> {
|
2022-02-10 13:52:35 +00:00
|
|
|
let mut queue_sizes = vec![QUEUE_SIZE; MIN_NUM_QUEUES];
|
2021-01-21 11:22:29 +00:00
|
|
|
let mut avail_features = 1u64 << VIRTIO_F_VERSION_1;
|
|
|
|
if deflate_on_oom {
|
|
|
|
avail_features |= 1u64 << VIRTIO_BALLOON_F_DEFLATE_ON_OOM;
|
|
|
|
}
|
2022-02-10 13:52:35 +00:00
|
|
|
if free_page_reporting {
|
|
|
|
avail_features |= 1u64 << VIRTIO_BALLOON_F_REPORTING;
|
|
|
|
queue_sizes.push(REPORTING_QUEUE_SIZE);
|
|
|
|
}
|
2020-03-20 03:43:17 +00:00
|
|
|
|
2021-01-02 20:37:05 +00:00
|
|
|
let config = VirtioBalloonConfig {
|
|
|
|
num_pages: (size >> VIRTIO_BALLOON_PFN_SHIFT) as u32,
|
|
|
|
..Default::default()
|
|
|
|
};
|
2020-03-20 03:43:17 +00:00
|
|
|
|
|
|
|
Ok(Balloon {
|
2020-09-03 09:37:36 +00:00
|
|
|
common: VirtioCommon {
|
2021-03-25 16:54:09 +00:00
|
|
|
device_type: VirtioDeviceType::Balloon 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))),
|
2022-02-10 13:52:35 +00:00
|
|
|
queue_sizes,
|
|
|
|
min_queues: MIN_NUM_QUEUES as u16,
|
2020-09-03 15:56:32 +00:00
|
|
|
..Default::default()
|
2020-09-03 09:37:36 +00:00
|
|
|
},
|
2020-03-20 03:43:17 +00:00
|
|
|
id,
|
2021-09-21 08:20:01 +00:00
|
|
|
resize: VirtioBalloonResize::new(size)?,
|
2020-03-20 03:43:17 +00:00
|
|
|
config: Arc::new(Mutex::new(config)),
|
2020-08-18 02:58:13 +00:00
|
|
|
seccomp_action,
|
2021-09-07 15:10:48 +00:00
|
|
|
exit_evt,
|
2020-03-20 03:43:17 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resize(&self, size: u64) -> Result<(), Error> {
|
|
|
|
self.resize.work(size)
|
|
|
|
}
|
2020-08-25 06:44:21 +00:00
|
|
|
|
|
|
|
// Get the actual size of the virtio-balloon.
|
|
|
|
pub fn get_actual(&self) -> u64 {
|
|
|
|
(self.config.lock().unwrap().actual as u64) << VIRTIO_BALLOON_PFN_SHIFT
|
|
|
|
}
|
2021-09-21 08:20:01 +00:00
|
|
|
|
|
|
|
fn state(&self) -> BalloonState {
|
|
|
|
BalloonState {
|
|
|
|
avail_features: self.common.avail_features,
|
|
|
|
acked_features: self.common.acked_features,
|
|
|
|
config: *(self.config.lock().unwrap()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_state(&mut self, state: &BalloonState) {
|
|
|
|
self.common.avail_features = state.avail_features;
|
|
|
|
self.common.acked_features = state.acked_features;
|
|
|
|
*(self.config.lock().unwrap()) = state.config;
|
|
|
|
}
|
2020-03-20 03:43:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Balloon {
|
|
|
|
fn drop(&mut self) {
|
2020-09-04 08:37:37 +00:00
|
|
|
if let Some(kill_evt) = self.common.kill_evt.take() {
|
2020-03-20 03:43:17 +00:00
|
|
|
// Ignore the result because there is nothing we can do about it.
|
|
|
|
let _ = kill_evt.write(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VirtioDevice for Balloon {
|
|
|
|
fn device_type(&self) -> u32 {
|
2020-09-04 08:37:37 +00:00
|
|
|
self.common.device_type
|
2020-03-20 03:43:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn queue_max_sizes(&self) -> &[u16] {
|
2020-09-04 08:37:37 +00:00
|
|
|
&self.common.queue_sizes
|
2020-03-20 03:43:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn features(&self) -> u64 {
|
2020-09-03 09:37:36 +00:00
|
|
|
self.common.avail_features
|
2020-03-20 03:43:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ack_features(&mut self, value: u64) {
|
2020-09-03 09:37:36 +00:00
|
|
|
self.common.ack_features(value)
|
2020-03-20 03:43:17 +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-20 03:43:17 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 06:44:21 +00:00
|
|
|
fn write_config(&mut self, offset: u64, data: &[u8]) {
|
|
|
|
// The "actual" field is the only mutable field
|
|
|
|
if offset != CONFIG_ACTUAL_OFFSET || data.len() != CONFIG_ACTUAL_SIZE {
|
|
|
|
error!(
|
|
|
|
"Attempt to write to read-only field: offset {:x} length {}",
|
|
|
|
offset,
|
|
|
|
data.len()
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.write_config_helper(self.config.lock().unwrap().as_mut_slice(), offset, data);
|
|
|
|
}
|
|
|
|
|
2020-03-20 03:43:17 +00:00
|
|
|
fn activate(
|
|
|
|
&mut self,
|
2021-10-21 10:41:16 +00:00
|
|
|
_mem: GuestMemoryAtomic<GuestMemoryMmap>,
|
2020-03-20 03:43:17 +00:00
|
|
|
interrupt_cb: Arc<dyn VirtioInterrupt>,
|
2022-07-20 14:45:49 +00:00
|
|
|
mut queues: Vec<(usize, Queue<GuestMemoryAtomic<GuestMemoryMmap>>, EventFd)>,
|
2020-03-20 03:43:17 +00:00
|
|
|
) -> ActivateResult {
|
2022-07-20 14:45:49 +00:00
|
|
|
self.common.activate(&queues, &interrupt_cb)?;
|
2021-06-02 18:08:06 +00:00
|
|
|
let (kill_evt, pause_evt) = self.common.dup_eventfds();
|
2020-03-20 03:43:17 +00:00
|
|
|
|
2022-07-20 14:45:49 +00:00
|
|
|
let mut virtqueues = Vec::new();
|
|
|
|
let (_, queue, queue_evt) = queues.remove(0);
|
|
|
|
virtqueues.push(queue);
|
|
|
|
let inflate_queue_evt = queue_evt;
|
|
|
|
let (_, queue, queue_evt) = queues.remove(0);
|
|
|
|
virtqueues.push(queue);
|
|
|
|
let deflate_queue_evt = queue_evt;
|
2022-02-10 13:52:35 +00:00
|
|
|
let reporting_queue_evt =
|
2022-07-20 14:45:49 +00:00
|
|
|
if self.common.feature_acked(VIRTIO_BALLOON_F_REPORTING) && !queues.is_empty() {
|
|
|
|
let (_, queue, queue_evt) = queues.remove(0);
|
|
|
|
virtqueues.push(queue);
|
|
|
|
Some(queue_evt)
|
2022-02-10 13:52:35 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2022-02-10 13:40:09 +00:00
|
|
|
|
2020-03-20 03:43:17 +00:00
|
|
|
let mut handler = BalloonEpollHandler {
|
|
|
|
config: self.config.clone(),
|
|
|
|
resize_receiver: self.resize.get_receiver().map_err(|e| {
|
|
|
|
error!("failed to clone resize EventFd: {:?}", e);
|
|
|
|
ActivateError::BadActivate
|
|
|
|
})?,
|
2022-07-20 14:45:49 +00:00
|
|
|
queues: virtqueues,
|
2020-03-20 03:43:17 +00:00
|
|
|
interrupt_cb,
|
2022-02-10 13:40:09 +00:00
|
|
|
inflate_queue_evt,
|
|
|
|
deflate_queue_evt,
|
2022-02-10 13:52:35 +00:00
|
|
|
reporting_queue_evt,
|
2020-03-20 03:43:17 +00:00
|
|
|
kill_evt,
|
|
|
|
pause_evt,
|
|
|
|
};
|
|
|
|
|
2020-09-04 08:37:37 +00:00
|
|
|
let paused = self.common.paused.clone();
|
|
|
|
let paused_sync = self.common.paused_sync.clone();
|
2020-03-20 03:43:17 +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::VirtioBalloon,
|
|
|
|
&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-20 03:43:17 +00:00
|
|
|
|
2021-02-18 15:10:51 +00:00
|
|
|
event!("virtio-device", "activated", "id", &self.id);
|
2020-03-20 03:43:17 +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-20 03:43:17 +00:00
|
|
|
|
2020-09-04 08:37:37 +00:00
|
|
|
impl Pausable for Balloon {
|
|
|
|
fn pause(&mut self) -> result::Result<(), MigratableError> {
|
|
|
|
self.common.pause()
|
|
|
|
}
|
2020-03-20 03:43:17 +00:00
|
|
|
|
2020-09-04 08:37:37 +00:00
|
|
|
fn resume(&mut self) -> result::Result<(), MigratableError> {
|
|
|
|
self.common.resume()
|
2020-03-20 03:43:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Snapshottable for Balloon {
|
|
|
|
fn id(&self) -> String {
|
|
|
|
self.id.clone()
|
|
|
|
}
|
2021-09-21 08:20:01 +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-03-20 03:43:17 +00:00
|
|
|
}
|
|
|
|
impl Transportable for Balloon {}
|
|
|
|
impl Migratable for Balloon {}
|