2019-05-09 05:01:48 +00:00
|
|
|
// Copyright 2017 The Chromium OS Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-12-31 10:49:11 +00:00
|
|
|
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_IOMMU_PLATFORM,
|
2020-08-04 11:16:44 +00:00
|
|
|
VIRTIO_F_VERSION_1,
|
2019-12-31 10:49:11 +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;
|
2019-12-31 10:49:11 +00:00
|
|
|
use crate::{VirtioInterrupt, VirtioInterruptType};
|
2021-09-03 10:43:30 +00:00
|
|
|
use seccompiler::SeccompAction;
|
2019-05-09 05:01:48 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io;
|
2020-08-04 11:16:44 +00:00
|
|
|
use std::os::unix::io::AsRawFd;
|
2019-05-09 05:01:48 +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};
|
2021-05-06 13:34:31 +00:00
|
|
|
use versionize::{VersionMap, Versionize, VersionizeResult};
|
|
|
|
use versionize_derive::Versionize;
|
2021-10-21 10:41:16 +00:00
|
|
|
use virtio_queue::Queue;
|
|
|
|
use vm_memory::{Bytes, GuestMemoryAtomic};
|
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-05-09 05:01:48 +00:00
|
|
|
|
|
|
|
const QUEUE_SIZE: u16 = 256;
|
|
|
|
const QUEUE_SIZES: &[u16] = &[QUEUE_SIZE];
|
|
|
|
|
|
|
|
// 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-05-09 05:01:48 +00:00
|
|
|
|
|
|
|
struct RngEpollHandler {
|
2021-10-21 10:41:16 +00:00
|
|
|
queues: Vec<Queue<GuestMemoryAtomic<GuestMemoryMmap>>>,
|
2019-05-09 05:01:48 +00:00
|
|
|
random_file: File,
|
2020-01-13 17:52:19 +00:00
|
|
|
interrupt_cb: Arc<dyn VirtioInterrupt>,
|
2019-05-09 05:01:48 +00:00
|
|
|
queue_evt: EventFd,
|
|
|
|
kill_evt: EventFd,
|
2019-11-19 00:42:31 +00:00
|
|
|
pause_evt: EventFd,
|
2019-05-09 05:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RngEpollHandler {
|
|
|
|
fn process_queue(&mut self) -> bool {
|
|
|
|
let queue = &mut self.queues[0];
|
|
|
|
|
|
|
|
let mut used_desc_heads = [(0, 0); QUEUE_SIZE as usize];
|
|
|
|
let mut used_count = 0;
|
2021-10-21 10:41:16 +00:00
|
|
|
for mut desc_chain in queue.iter().unwrap() {
|
|
|
|
let desc = desc_chain.next().unwrap();
|
2019-05-09 05:01:48 +00:00
|
|
|
let mut len = 0;
|
|
|
|
|
|
|
|
// Drivers can only read from the random device.
|
2021-10-21 10:41:16 +00:00
|
|
|
if desc.is_write_only() {
|
2019-05-09 05:01:48 +00:00
|
|
|
// Fill the read with data from the random device on the host.
|
2021-10-21 10:41:16 +00:00
|
|
|
if desc_chain
|
|
|
|
.memory()
|
|
|
|
.read_from(desc.addr(), &mut self.random_file, desc.len() as usize)
|
2019-05-09 05:01:48 +00:00
|
|
|
.is_ok()
|
|
|
|
{
|
2021-10-21 10:41:16 +00:00
|
|
|
len = desc.len();
|
2019-05-09 05:01:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 10:41:16 +00:00
|
|
|
used_desc_heads[used_count] = (desc_chain.head_index(), len);
|
2019-05-09 05:01:48 +00:00
|
|
|
used_count += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for &(desc_index, len) in &used_desc_heads[..used_count] {
|
2021-10-21 10:41:16 +00:00
|
|
|
queue.add_used(desc_index, len).unwrap();
|
2019-05-09 05:01:48 +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.queues[0]))
|
|
|
|
.map_err(|e| {
|
|
|
|
error!("Failed to signal used queue: {:?}", e);
|
|
|
|
DeviceError::FailedSignalingUsedQueue(e)
|
|
|
|
})
|
2019-05-09 05:01:48 +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)?;
|
2019-05-09 05:01:48 +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 RngEpollHandler {
|
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-05-09 05:01:48 +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-05-09 05:01:48 +00:00
|
|
|
}
|
2020-08-04 11:16:44 +00:00
|
|
|
false
|
2019-05-09 05:01:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Virtio device for exposing entropy to the guest OS through virtio.
|
|
|
|
pub struct Rng {
|
2020-09-03 09:37:36 +00:00
|
|
|
common: VirtioCommon,
|
2020-04-27 09:39:41 +00:00
|
|
|
id: String,
|
2019-05-09 05:01:48 +00:00
|
|
|
random_file: Option<File>,
|
2020-08-04 17:46:49 +00:00
|
|
|
seccomp_action: SeccompAction,
|
2021-09-07 15:10:48 +00:00
|
|
|
exit_evt: EventFd,
|
2019-05-09 05:01:48 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 14:02:43 +00:00
|
|
|
#[derive(Versionize)]
|
2020-04-08 07:19:04 +00:00
|
|
|
pub struct RngState {
|
|
|
|
pub avail_features: u64,
|
|
|
|
pub acked_features: u64,
|
|
|
|
}
|
|
|
|
|
2021-05-06 13:34:31 +00:00
|
|
|
impl VersionMapped for RngState {}
|
|
|
|
|
2019-05-09 05:01:48 +00:00
|
|
|
impl Rng {
|
|
|
|
/// Create a new virtio rng device that gets random data from /dev/urandom.
|
2020-08-04 17:46:49 +00:00
|
|
|
pub fn new(
|
|
|
|
id: String,
|
|
|
|
path: &str,
|
|
|
|
iommu: bool,
|
|
|
|
seccomp_action: SeccompAction,
|
2021-09-07 15:10:48 +00:00
|
|
|
exit_evt: EventFd,
|
2020-08-04 17:46:49 +00:00
|
|
|
) -> io::Result<Rng> {
|
2019-05-09 05:01:48 +00:00
|
|
|
let random_file = File::open(path)?;
|
2019-10-04 17:39:42 +00:00
|
|
|
let mut avail_features = 1u64 << VIRTIO_F_VERSION_1;
|
|
|
|
|
|
|
|
if iommu {
|
|
|
|
avail_features |= 1u64 << VIRTIO_F_IOMMU_PLATFORM;
|
|
|
|
}
|
2019-05-09 05:01:48 +00:00
|
|
|
|
|
|
|
Ok(Rng {
|
2020-09-03 09:37:36 +00:00
|
|
|
common: VirtioCommon {
|
2021-03-25 16:54:09 +00:00
|
|
|
device_type: VirtioDeviceType::Rng 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 09:39:41 +00:00
|
|
|
id,
|
2019-05-09 05:01:48 +00:00
|
|
|
random_file: Some(random_file),
|
2020-08-04 17:46:49 +00:00
|
|
|
seccomp_action,
|
2021-09-07 15:10:48 +00:00
|
|
|
exit_evt,
|
2019-05-09 05:01:48 +00:00
|
|
|
})
|
|
|
|
}
|
2020-04-08 07:19:04 +00:00
|
|
|
|
|
|
|
fn state(&self) -> RngState {
|
|
|
|
RngState {
|
2020-09-03 09:37:36 +00:00
|
|
|
avail_features: self.common.avail_features,
|
|
|
|
acked_features: self.common.acked_features,
|
2020-04-08 07:19:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 16:00:53 +00:00
|
|
|
fn set_state(&mut self, state: &RngState) {
|
2020-09-03 09:37:36 +00:00
|
|
|
self.common.avail_features = state.avail_features;
|
|
|
|
self.common.acked_features = state.acked_features;
|
2020-04-08 07:19:04 +00:00
|
|
|
}
|
2019-05-09 05:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Rng {
|
|
|
|
fn drop(&mut self) {
|
2020-09-04 08:37:37 +00:00
|
|
|
if let Some(kill_evt) = self.common.kill_evt.take() {
|
2019-05-09 05:01:48 +00:00
|
|
|
// Ignore the result because there is nothing we can do about it.
|
|
|
|
let _ = kill_evt.write(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VirtioDevice for Rng {
|
|
|
|
fn device_type(&self) -> u32 {
|
2020-09-04 08:37:37 +00:00
|
|
|
self.common.device_type
|
2019-05-09 05:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn queue_max_sizes(&self) -> &[u16] {
|
2020-09-04 08:37:37 +00:00
|
|
|
&self.common.queue_sizes
|
2019-05-09 05:01:48 +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-05-09 05:01:48 +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-05-09 05:01:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn activate(
|
|
|
|
&mut self,
|
2021-10-21 10:41:16 +00:00
|
|
|
_mem: GuestMemoryAtomic<GuestMemoryMmap>,
|
2020-01-13 17:52:19 +00:00
|
|
|
interrupt_cb: Arc<dyn VirtioInterrupt>,
|
2021-10-21 10:41:16 +00:00
|
|
|
queues: Vec<Queue<GuestMemoryAtomic<GuestMemoryMmap>>>,
|
2019-05-09 05:01:48 +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();
|
2019-10-03 23:51:21 +00:00
|
|
|
|
|
|
|
if let Some(file) = self.random_file.as_ref() {
|
|
|
|
let random_file = file.try_clone().map_err(|e| {
|
|
|
|
error!("failed cloning rng source: {}", e);
|
|
|
|
ActivateError::BadActivate
|
|
|
|
})?;
|
2019-05-09 05:01:48 +00:00
|
|
|
let mut handler = RngEpollHandler {
|
|
|
|
queues,
|
|
|
|
random_file,
|
2019-06-03 20:57:26 +00:00
|
|
|
interrupt_cb,
|
2019-05-09 05:01:48 +00:00
|
|
|
queue_evt: queue_evts.remove(0),
|
|
|
|
kill_evt,
|
2019-11-19 00:42:31 +00:00
|
|
|
pause_evt,
|
2019-05-09 05:01:48 +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();
|
2021-09-03 10:43:30 +00:00
|
|
|
spawn_virtio_thread(
|
|
|
|
&self.id,
|
|
|
|
&self.seccomp_action,
|
|
|
|
Thread::VirtioRng,
|
|
|
|
&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-05 08:06:05 +00:00
|
|
|
error!("Error running worker: {:?}", e);
|
|
|
|
}
|
2021-09-03 10:43:30 +00:00
|
|
|
},
|
|
|
|
)?;
|
2019-05-09 05:01:48 +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-05-09 05:01:48 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
Err(ActivateError::BadActivate)
|
|
|
|
}
|
2019-10-03 23:51:21 +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
|
2020-09-04 08:37:37 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-19 00:42:31 +00:00
|
|
|
|
2020-09-04 08:37:37 +00:00
|
|
|
impl Pausable for Rng {
|
|
|
|
fn pause(&mut self) -> result::Result<(), MigratableError> {
|
|
|
|
self.common.pause()
|
|
|
|
}
|
2019-10-03 23:51:21 +00:00
|
|
|
|
2020-09-04 08:37:37 +00:00
|
|
|
fn resume(&mut self) -> result::Result<(), MigratableError> {
|
|
|
|
self.common.resume()
|
2019-10-03 23:51:21 +00:00
|
|
|
}
|
2019-05-09 05:01:48 +00:00
|
|
|
}
|
2019-11-19 00:42:31 +00:00
|
|
|
|
2020-04-08 07:19:04 +00:00
|
|
|
impl Snapshottable for Rng {
|
|
|
|
fn id(&self) -> String {
|
2020-04-27 09:39:41 +00:00
|
|
|
self.id.clone()
|
2020-04-08 07:19:04 +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-08 07:19:04 +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-08 07:19:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 16:59:51 +00:00
|
|
|
impl Transportable for Rng {}
|
2019-11-19 00:42:31 +00:00
|
|
|
impl Migratable for Rng {}
|