mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2025-02-22 11:22:26 +00:00
vm-virtio: rng: Expect an identifier upon device creation
This identifier is chosen from the DeviceManager so that it will manage all identifiers across the VM, which will ensure uniqueness. Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
parent
9eb7413fab
commit
2e91b73881
@ -182,6 +182,7 @@ impl RngEpollHandler {
|
|||||||
|
|
||||||
/// Virtio device for exposing entropy to the guest OS through virtio.
|
/// Virtio device for exposing entropy to the guest OS through virtio.
|
||||||
pub struct Rng {
|
pub struct Rng {
|
||||||
|
id: String,
|
||||||
kill_evt: Option<EventFd>,
|
kill_evt: Option<EventFd>,
|
||||||
pause_evt: Option<EventFd>,
|
pause_evt: Option<EventFd>,
|
||||||
random_file: Option<File>,
|
random_file: Option<File>,
|
||||||
@ -202,7 +203,7 @@ pub struct RngState {
|
|||||||
|
|
||||||
impl Rng {
|
impl Rng {
|
||||||
/// Create a new virtio rng device that gets random data from /dev/urandom.
|
/// Create a new virtio rng device that gets random data from /dev/urandom.
|
||||||
pub fn new(path: &str, iommu: bool) -> io::Result<Rng> {
|
pub fn new(id: String, path: &str, iommu: bool) -> io::Result<Rng> {
|
||||||
let random_file = File::open(path)?;
|
let random_file = File::open(path)?;
|
||||||
let mut avail_features = 1u64 << VIRTIO_F_VERSION_1;
|
let mut avail_features = 1u64 << VIRTIO_F_VERSION_1;
|
||||||
|
|
||||||
@ -211,6 +212,7 @@ impl Rng {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Ok(Rng {
|
Ok(Rng {
|
||||||
|
id,
|
||||||
kill_evt: None,
|
kill_evt: None,
|
||||||
pause_evt: None,
|
pause_evt: None,
|
||||||
random_file: Some(random_file),
|
random_file: Some(random_file),
|
||||||
@ -384,19 +386,18 @@ impl VirtioDevice for Rng {
|
|||||||
}
|
}
|
||||||
|
|
||||||
virtio_pausable!(Rng);
|
virtio_pausable!(Rng);
|
||||||
const RNG_SNAPSHOT_ID: &str = "virtio-rng";
|
|
||||||
impl Snapshottable for Rng {
|
impl Snapshottable for Rng {
|
||||||
fn id(&self) -> String {
|
fn id(&self) -> String {
|
||||||
RNG_SNAPSHOT_ID.to_string()
|
self.id.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn snapshot(&self) -> std::result::Result<Snapshot, MigratableError> {
|
fn snapshot(&self) -> std::result::Result<Snapshot, MigratableError> {
|
||||||
let snapshot =
|
let snapshot =
|
||||||
serde_json::to_vec(&self.state()).map_err(|e| MigratableError::Snapshot(e.into()))?;
|
serde_json::to_vec(&self.state()).map_err(|e| MigratableError::Snapshot(e.into()))?;
|
||||||
|
|
||||||
let mut rng_snapshot = Snapshot::new(RNG_SNAPSHOT_ID);
|
let mut rng_snapshot = Snapshot::new(self.id.as_str());
|
||||||
rng_snapshot.add_data_section(SnapshotDataSection {
|
rng_snapshot.add_data_section(SnapshotDataSection {
|
||||||
id: format!("{}-section", RNG_SNAPSHOT_ID),
|
id: format!("{}-section", self.id),
|
||||||
snapshot,
|
snapshot,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -404,10 +405,7 @@ impl Snapshottable for Rng {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> {
|
fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> {
|
||||||
if let Some(rng_section) = snapshot
|
if let Some(rng_section) = snapshot.snapshot_data.get(&format!("{}-section", self.id)) {
|
||||||
.snapshot_data
|
|
||||||
.get(&format!("{}-section", RNG_SNAPSHOT_ID))
|
|
||||||
{
|
|
||||||
let rng_state = match serde_json::from_slice(&rng_section.snapshot) {
|
let rng_state = match serde_json::from_slice(&rng_section.snapshot) {
|
||||||
Ok(state) => state,
|
Ok(state) => state,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
|
@ -1437,7 +1437,7 @@ impl DeviceManager {
|
|||||||
let id = String::from(RNG_DEVICE_NAME);
|
let id = String::from(RNG_DEVICE_NAME);
|
||||||
|
|
||||||
let virtio_rng_device = Arc::new(Mutex::new(
|
let virtio_rng_device = Arc::new(Mutex::new(
|
||||||
vm_virtio::Rng::new(rng_path, rng_config.iommu)
|
vm_virtio::Rng::new(id.clone(), rng_path, rng_config.iommu)
|
||||||
.map_err(DeviceManagerError::CreateVirtioRng)?,
|
.map_err(DeviceManagerError::CreateVirtioRng)?,
|
||||||
));
|
));
|
||||||
devices.push((
|
devices.push((
|
||||||
|
Loading…
x
Reference in New Issue
Block a user