vm-virtio: Implement reset() for virtio-rng

The virtio specification defines a device can be reset, which was not
supported by this virtio-rng implementation. The reason it is needed
is to support unbinding this device from the guest driver, and rebind
it to vfio-pci driver.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2019-10-03 16:51:21 -07:00 committed by Samuel Ortiz
parent 59b4aaba87
commit eb91bc812b

View File

@ -158,6 +158,8 @@ pub struct Rng {
random_file: Option<File>, random_file: Option<File>,
avail_features: u64, avail_features: u64,
acked_features: u64, acked_features: u64,
queue_evts: Option<Vec<EventFd>>,
interrupt_cb: Option<Arc<VirtioInterrupt>>,
} }
impl Rng { impl Rng {
@ -171,6 +173,8 @@ impl Rng {
random_file: Some(random_file), random_file: Some(random_file),
avail_features, avail_features,
acked_features: 0u64, acked_features: 0u64,
queue_evts: None,
interrupt_cb: None,
}) })
} }
} }
@ -261,7 +265,26 @@ impl VirtioDevice for Rng {
}; };
self.kill_evt = Some(self_kill_evt); self.kill_evt = Some(self_kill_evt);
if let Some(random_file) = self.random_file.take() { // Save the interrupt EventFD as we need to return it on reset
// but clone it to pass into the thread.
self.interrupt_cb = Some(interrupt_cb.clone());
let mut tmp_queue_evts: Vec<EventFd> = Vec::new();
for queue_evt in queue_evts.iter() {
// Save the queue EventFD as we need to return it on reset
// but clone it to pass into the thread.
tmp_queue_evts.push(queue_evt.try_clone().map_err(|e| {
error!("failed to clone queue EventFd: {}", e);
ActivateError::BadActivate
})?);
}
self.queue_evts = Some(tmp_queue_evts);
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
})?;
let mut handler = RngEpollHandler { let mut handler = RngEpollHandler {
queues, queues,
mem, mem,
@ -284,4 +307,17 @@ impl VirtioDevice for Rng {
} }
Err(ActivateError::BadActivate) Err(ActivateError::BadActivate)
} }
fn reset(&mut self) -> Option<(Arc<VirtioInterrupt>, Vec<EventFd>)> {
if let Some(kill_evt) = self.kill_evt.take() {
// Ignore the result because there is nothing we can do about it.
let _ = kill_evt.write(1);
}
// Return the interrupt and queue EventFDs
Some((
self.interrupt_cb.take().unwrap(),
self.queue_evts.take().unwrap(),
))
}
} }