vm-virtio: pmem: 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:
Sebastien Boeuf 2020-04-27 13:36:41 +02:00 committed by Rob Bradford
parent 2e91b73881
commit 1592a9292f
2 changed files with 24 additions and 16 deletions

View File

@ -337,6 +337,7 @@ impl PmemEpollHandler {
}
pub struct Pmem {
id: String,
kill_evt: Option<EventFd>,
pause_evt: Option<EventFd>,
disk: Option<File>,
@ -363,6 +364,7 @@ pub struct PmemState {
impl Pmem {
pub fn new(
id: String,
disk: File,
addr: GuestAddress,
mapping: UserspaceMapping,
@ -381,6 +383,7 @@ impl Pmem {
}
Ok(Pmem {
id,
kill_evt: None,
pause_evt: None,
disk: Some(disk),
@ -571,19 +574,18 @@ impl VirtioDevice for Pmem {
}
virtio_pausable!(Pmem);
const PMEM_SNAPSHOT_ID: &str = "virtio-pmem";
impl Snapshottable for Pmem {
fn id(&self) -> String {
PMEM_SNAPSHOT_ID.to_string()
self.id.clone()
}
fn snapshot(&self) -> std::result::Result<Snapshot, MigratableError> {
let snapshot =
serde_json::to_vec(&self.state()).map_err(|e| MigratableError::Snapshot(e.into()))?;
let mut pmem_snapshot = Snapshot::new(PMEM_SNAPSHOT_ID);
let mut pmem_snapshot = Snapshot::new(self.id.as_str());
pmem_snapshot.add_data_section(SnapshotDataSection {
id: format!("{}-section", PMEM_SNAPSHOT_ID),
id: format!("{}-section", self.id),
snapshot,
});
@ -591,10 +593,7 @@ impl Snapshottable for Pmem {
}
fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> {
if let Some(pmem_section) = snapshot
.snapshot_data
.get(&format!("{}-section", PMEM_SNAPSHOT_ID))
{
if let Some(pmem_section) = snapshot.snapshot_data.get(&format!("{}-section", self.id)) {
let pmem_state = match serde_json::from_slice(&pmem_section.snapshot) {
Ok(state) => state,
Err(error) => {

View File

@ -1560,9 +1560,13 @@ impl DeviceManager {
&mut self,
pmem_cfg: &mut PmemConfig,
) -> DeviceManagerResult<(VirtioDeviceArc, bool, Option<String>)> {
if pmem_cfg.id.is_none() {
pmem_cfg.id = Some(self.next_device_name(PMEM_DEVICE_NAME_PREFIX)?);
}
let id = if let Some(id) = &pmem_cfg.id {
id.clone()
} else {
let id = self.next_device_name(PMEM_DEVICE_NAME_PREFIX)?;
pmem_cfg.id = Some(id.clone());
id
};
let (custom_flags, set_len) = if pmem_cfg.file.is_dir() {
if pmem_cfg.size.is_none() {
@ -1646,13 +1650,18 @@ impl DeviceManager {
};
let virtio_pmem_device = Arc::new(Mutex::new(
vm_virtio::Pmem::new(file, pmem_guest_addr, mapping, mmap_region, pmem_cfg.iommu)
vm_virtio::Pmem::new(
id,
file,
pmem_guest_addr,
mapping,
mmap_region,
pmem_cfg.iommu,
)
.map_err(DeviceManagerError::CreateVirtioPmem)?,
));
let migratable = Arc::clone(&virtio_pmem_device) as Arc<Mutex<dyn Migratable>>;
let id = migratable.lock().unwrap().id();
self.migratable_devices.push((id, migratable));
self.add_migratable_device(Arc::clone(&virtio_pmem_device) as Arc<Mutex<dyn Migratable>>);
Ok((
Arc::clone(&virtio_pmem_device) as VirtioDeviceArc,