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::{
|
2020-08-04 11:16:44 +00:00
|
|
|
ActivateError, ActivateResult, EpollHelper, EpollHelperError, EpollHelperHandler, Queue,
|
2020-09-03 09:37:36 +00:00
|
|
|
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
|
|
|
};
|
2020-08-04 17:46:49 +00:00
|
|
|
use crate::seccomp_filters::{get_seccomp_filter, Thread};
|
2019-12-31 10:49:11 +00:00
|
|
|
use crate::{VirtioInterrupt, VirtioInterruptType};
|
2020-04-08 07:19:04 +00:00
|
|
|
use anyhow::anyhow;
|
2020-08-04 17:46:49 +00:00
|
|
|
use seccomp::{SeccompAction, SeccompFilter};
|
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};
|
2019-05-09 05:01:48 +00:00
|
|
|
use std::thread;
|
2020-02-11 16:22:40 +00:00
|
|
|
use vm_memory::{Bytes, GuestAddressSpace, GuestMemoryAtomic, GuestMemoryMmap};
|
2020-04-08 07:19:04 +00:00
|
|
|
use vm_migration::{
|
|
|
|
Migratable, MigratableError, Pausable, Snapshot, SnapshotDataSection, 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 {
|
|
|
|
queues: Vec<Queue>,
|
2020-02-11 16:22:40 +00:00
|
|
|
mem: 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;
|
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 queue.iter(&mem) {
|
2019-05-09 05:01:48 +00:00
|
|
|
let mut len = 0;
|
|
|
|
|
|
|
|
// Drivers can only read from the random device.
|
|
|
|
if avail_desc.is_write_only() {
|
|
|
|
// Fill the read with data from the random device on the host.
|
2019-08-20 22:43:23 +00:00
|
|
|
if mem
|
2019-05-09 05:01:48 +00:00
|
|
|
.read_from(
|
|
|
|
avail_desc.addr,
|
|
|
|
&mut self.random_file,
|
|
|
|
avail_desc.len as usize,
|
|
|
|
)
|
|
|
|
.is_ok()
|
|
|
|
{
|
|
|
|
len = avail_desc.len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
queue.add_used(&mem, desc_index, len);
|
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,
|
2019-05-09 05:01:48 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 07:19:04 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct RngState {
|
|
|
|
pub avail_features: u64,
|
|
|
|
pub acked_features: u64,
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
) -> 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 {
|
2020-09-04 08:37:37 +00:00
|
|
|
device_type: VirtioDeviceType::TYPE_RNG as u32,
|
|
|
|
queue_sizes: QUEUE_SIZES.to_vec(),
|
|
|
|
paused_sync: Some(Arc::new(Barrier::new(2))),
|
2020-09-03 09:37:36 +00:00
|
|
|
avail_features,
|
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,
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_state(&mut self, state: &RngState) -> io::Result<()> {
|
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
|
|
|
Ok(())
|
|
|
|
}
|
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,
|
2020-02-11 16:22:40 +00:00
|
|
|
mem: GuestMemoryAtomic<GuestMemoryMmap>,
|
2020-01-13 17:52:19 +00:00
|
|
|
interrupt_cb: Arc<dyn VirtioInterrupt>,
|
2019-05-09 05:01:48 +00:00
|
|
|
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)?;
|
|
|
|
let kill_evt = self
|
|
|
|
.common
|
|
|
|
.kill_evt
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.try_clone()
|
2019-11-19 00:42:31 +00:00
|
|
|
.map_err(|e| {
|
2020-09-04 08:37:37 +00:00
|
|
|
error!("failed to clone kill_evt eventfd: {}", e);
|
2019-11-19 00:42:31 +00:00
|
|
|
ActivateError::BadActivate
|
|
|
|
})?;
|
2020-09-04 08:37:37 +00:00
|
|
|
let pause_evt = self
|
|
|
|
.common
|
|
|
|
.pause_evt
|
|
|
|
.as_ref()
|
|
|
|
.unwrap()
|
|
|
|
.try_clone()
|
2019-11-19 00:42:31 +00:00
|
|
|
.map_err(|e| {
|
2020-09-04 08:37:37 +00:00
|
|
|
error!("failed to clone pause_evt eventfd: {}", e);
|
2019-11-19 00:42:31 +00:00
|
|
|
ActivateError::BadActivate
|
|
|
|
})?;
|
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,
|
|
|
|
mem,
|
|
|
|
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();
|
2020-08-04 17:46:49 +00:00
|
|
|
// Retrieve seccomp filter for virtio_rng thread
|
|
|
|
let virtio_rng_seccomp_filter =
|
|
|
|
get_seccomp_filter(&self.seccomp_action, Thread::VirtioRng)
|
|
|
|
.map_err(ActivateError::CreateSeccompFilter)?;
|
2019-11-19 00:42:31 +00:00
|
|
|
thread::Builder::new()
|
2019-05-09 05:01:48 +00:00
|
|
|
.name("virtio_rng".to_string())
|
2020-08-04 17:46:49 +00:00
|
|
|
.spawn(move || {
|
2020-08-05 08:06:05 +00:00
|
|
|
if let Err(e) = SeccompFilter::apply(virtio_rng_seccomp_filter) {
|
|
|
|
error!("Error applying seccomp filter: {:?}", e);
|
2020-09-04 08:37:37 +00:00
|
|
|
} else 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 17:46:49 +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 the virtio-rng epoll thread: {}", e);
|
|
|
|
ActivateError::BadActivate
|
|
|
|
})?;
|
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
|
|
|
|
2019-05-09 05:01:48 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
Err(ActivateError::BadActivate)
|
|
|
|
}
|
2019-10-03 23:51:21 +00:00
|
|
|
|
2020-01-13 17:52:19 +00:00
|
|
|
fn reset(&mut self) -> Option<(Arc<dyn VirtioInterrupt>, Vec<EventFd>)> {
|
2020-09-04 08:37:37 +00:00
|
|
|
self.common.reset()
|
|
|
|
}
|
|
|
|
}
|
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> {
|
2020-04-08 07:19:04 +00:00
|
|
|
let snapshot =
|
|
|
|
serde_json::to_vec(&self.state()).map_err(|e| MigratableError::Snapshot(e.into()))?;
|
|
|
|
|
2020-04-27 09:39:41 +00:00
|
|
|
let mut rng_snapshot = Snapshot::new(self.id.as_str());
|
2020-04-08 07:19:04 +00:00
|
|
|
rng_snapshot.add_data_section(SnapshotDataSection {
|
2020-04-27 09:39:41 +00:00
|
|
|
id: format!("{}-section", self.id),
|
2020-04-08 07:19:04 +00:00
|
|
|
snapshot,
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(rng_snapshot)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> {
|
2020-04-27 09:39:41 +00:00
|
|
|
if let Some(rng_section) = snapshot.snapshot_data.get(&format!("{}-section", self.id)) {
|
2020-04-08 07:19:04 +00:00
|
|
|
let rng_state = match serde_json::from_slice(&rng_section.snapshot) {
|
|
|
|
Ok(state) => state,
|
|
|
|
Err(error) => {
|
|
|
|
return Err(MigratableError::Restore(anyhow!(
|
|
|
|
"Could not deserialize RNG {}",
|
|
|
|
error
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return self.set_state(&rng_state).map_err(|e| {
|
|
|
|
MigratableError::Restore(anyhow!("Could not restore RNG state {:?}", e))
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(MigratableError::Restore(anyhow!(
|
|
|
|
"Could not find RNG snapshot section"
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 16:59:51 +00:00
|
|
|
impl Transportable for Rng {}
|
2019-11-19 00:42:31 +00:00
|
|
|
impl Migratable for Rng {}
|