mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
vmm: Add device_node!() macro to improve code readability
Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
+38
-82
@@ -581,6 +581,18 @@ impl DeviceNode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! device_node {
|
||||||
|
($id:ident) => {
|
||||||
|
DeviceNode::new($id.clone(), None)
|
||||||
|
};
|
||||||
|
($id:ident, $device:ident) => {
|
||||||
|
DeviceNode::new(
|
||||||
|
$id.clone(),
|
||||||
|
Some(Arc::clone(&$device) as Arc<Mutex<dyn Migratable>>),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize)]
|
#[derive(Clone, Serialize, Deserialize)]
|
||||||
struct DeviceTree(HashMap<String, DeviceNode>);
|
struct DeviceTree(HashMap<String, DeviceNode>);
|
||||||
|
|
||||||
@@ -953,13 +965,8 @@ impl DeviceManager {
|
|||||||
// Fill the device tree with a new node. In case of restore, we
|
// Fill the device tree with a new node. In case of restore, we
|
||||||
// know there is nothing to do, so we can simply override the
|
// know there is nothing to do, so we can simply override the
|
||||||
// existing entry.
|
// existing entry.
|
||||||
self.device_tree.insert(
|
self.device_tree
|
||||||
iommu_id.clone(),
|
.insert(iommu_id.clone(), device_node!(iommu_id, device));
|
||||||
DeviceNode::new(
|
|
||||||
iommu_id.clone(),
|
|
||||||
Some(Arc::clone(&device) as Arc<Mutex<dyn Migratable>>),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
(Some(device), Some(mapping))
|
(Some(device), Some(mapping))
|
||||||
} else {
|
} else {
|
||||||
@@ -1081,10 +1088,8 @@ impl DeviceManager {
|
|||||||
// Fill the device tree with a new node. In case of restore, we
|
// Fill the device tree with a new node. In case of restore, we
|
||||||
// know there is nothing to do, so we can simply override the
|
// know there is nothing to do, so we can simply override the
|
||||||
// existing entry.
|
// existing entry.
|
||||||
self.device_tree.insert(
|
self.device_tree
|
||||||
id.clone(),
|
.insert(id.clone(), device_node!(id, ioapic));
|
||||||
DeviceNode::new(id, Some(Arc::clone(&ioapic) as Arc<Mutex<dyn Migratable>>)),
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(ioapic)
|
Ok(ioapic)
|
||||||
}
|
}
|
||||||
@@ -1257,10 +1262,8 @@ impl DeviceManager {
|
|||||||
// Fill the device tree with a new node. In case of restore, we
|
// Fill the device tree with a new node. In case of restore, we
|
||||||
// know there is nothing to do, so we can simply override the
|
// know there is nothing to do, so we can simply override the
|
||||||
// existing entry.
|
// existing entry.
|
||||||
self.device_tree.insert(
|
self.device_tree
|
||||||
id.clone(),
|
.insert(id.clone(), device_node!(id, serial));
|
||||||
DeviceNode::new(id, Some(Arc::clone(&serial) as Arc<Mutex<dyn Migratable>>)),
|
|
||||||
);
|
|
||||||
|
|
||||||
Some(serial)
|
Some(serial)
|
||||||
} else {
|
} else {
|
||||||
@@ -1295,13 +1298,8 @@ impl DeviceManager {
|
|||||||
// Fill the device tree with a new node. In case of restore, we
|
// Fill the device tree with a new node. In case of restore, we
|
||||||
// know there is nothing to do, so we can simply override the
|
// know there is nothing to do, so we can simply override the
|
||||||
// existing entry.
|
// existing entry.
|
||||||
self.device_tree.insert(
|
self.device_tree
|
||||||
id.clone(),
|
.insert(id.clone(), device_node!(id, virtio_console_device));
|
||||||
DeviceNode::new(
|
|
||||||
id,
|
|
||||||
Some(Arc::clone(&virtio_console_device) as Arc<Mutex<dyn Migratable>>),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
Some(console_input)
|
Some(console_input)
|
||||||
} else {
|
} else {
|
||||||
@@ -1402,13 +1400,8 @@ impl DeviceManager {
|
|||||||
// Fill the device tree with a new node. In case of restore, we
|
// Fill the device tree with a new node. In case of restore, we
|
||||||
// know there is nothing to do, so we can simply override the
|
// know there is nothing to do, so we can simply override the
|
||||||
// existing entry.
|
// existing entry.
|
||||||
self.device_tree.insert(
|
self.device_tree
|
||||||
id.clone(),
|
.insert(id.clone(), device_node!(id, vhost_user_block_device));
|
||||||
DeviceNode::new(
|
|
||||||
id.clone(),
|
|
||||||
Some(Arc::clone(&vhost_user_block_device) as Arc<Mutex<dyn Migratable>>),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
Arc::clone(&vhost_user_block_device) as VirtioDeviceArc,
|
Arc::clone(&vhost_user_block_device) as VirtioDeviceArc,
|
||||||
@@ -1459,13 +1452,7 @@ impl DeviceManager {
|
|||||||
// Fill the device tree with a new node. In case of restore, we
|
// Fill the device tree with a new node. In case of restore, we
|
||||||
// know there is nothing to do, so we can simply override the
|
// know there is nothing to do, so we can simply override the
|
||||||
// existing entry.
|
// existing entry.
|
||||||
self.device_tree.insert(
|
self.device_tree.insert(id.clone(), device_node!(id, block));
|
||||||
id.clone(),
|
|
||||||
DeviceNode::new(
|
|
||||||
id.clone(),
|
|
||||||
Some(Arc::clone(&block) as Arc<Mutex<dyn Migratable>>),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok((Arc::clone(&block) as VirtioDeviceArc, disk_cfg.iommu, id))
|
Ok((Arc::clone(&block) as VirtioDeviceArc, disk_cfg.iommu, id))
|
||||||
}
|
}
|
||||||
@@ -1492,13 +1479,7 @@ impl DeviceManager {
|
|||||||
// Fill the device tree with a new node. In case of restore, we
|
// Fill the device tree with a new node. In case of restore, we
|
||||||
// know there is nothing to do, so we can simply override the
|
// know there is nothing to do, so we can simply override the
|
||||||
// existing entry.
|
// existing entry.
|
||||||
self.device_tree.insert(
|
self.device_tree.insert(id.clone(), device_node!(id, block));
|
||||||
id.clone(),
|
|
||||||
DeviceNode::new(
|
|
||||||
id.clone(),
|
|
||||||
Some(Arc::clone(&block) as Arc<Mutex<dyn Migratable>>),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok((Arc::clone(&block) as VirtioDeviceArc, disk_cfg.iommu, id))
|
Ok((Arc::clone(&block) as VirtioDeviceArc, disk_cfg.iommu, id))
|
||||||
}
|
}
|
||||||
@@ -1578,13 +1559,8 @@ impl DeviceManager {
|
|||||||
// Fill the device tree with a new node. In case of restore, we
|
// Fill the device tree with a new node. In case of restore, we
|
||||||
// know there is nothing to do, so we can simply override the
|
// know there is nothing to do, so we can simply override the
|
||||||
// existing entry.
|
// existing entry.
|
||||||
self.device_tree.insert(
|
self.device_tree
|
||||||
id.clone(),
|
.insert(id.clone(), device_node!(id, vhost_user_net_device));
|
||||||
DeviceNode::new(
|
|
||||||
id.clone(),
|
|
||||||
Some(Arc::clone(&vhost_user_net_device) as Arc<Mutex<dyn Migratable>>),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
Arc::clone(&vhost_user_net_device) as VirtioDeviceArc,
|
Arc::clone(&vhost_user_net_device) as VirtioDeviceArc,
|
||||||
@@ -1625,13 +1601,8 @@ impl DeviceManager {
|
|||||||
// Fill the device tree with a new node. In case of restore, we
|
// Fill the device tree with a new node. In case of restore, we
|
||||||
// know there is nothing to do, so we can simply override the
|
// know there is nothing to do, so we can simply override the
|
||||||
// existing entry.
|
// existing entry.
|
||||||
self.device_tree.insert(
|
self.device_tree
|
||||||
id.clone(),
|
.insert(id.clone(), device_node!(id, virtio_net_device));
|
||||||
DeviceNode::new(
|
|
||||||
id.clone(),
|
|
||||||
Some(Arc::clone(&virtio_net_device) as Arc<Mutex<dyn Migratable>>),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
Arc::clone(&virtio_net_device) as VirtioDeviceArc,
|
Arc::clone(&virtio_net_device) as VirtioDeviceArc,
|
||||||
@@ -1680,13 +1651,8 @@ impl DeviceManager {
|
|||||||
// Fill the device tree with a new node. In case of restore, we
|
// Fill the device tree with a new node. In case of restore, we
|
||||||
// know there is nothing to do, so we can simply override the
|
// know there is nothing to do, so we can simply override the
|
||||||
// existing entry.
|
// existing entry.
|
||||||
self.device_tree.insert(
|
self.device_tree
|
||||||
id.clone(),
|
.insert(id.clone(), device_node!(id, virtio_rng_device));
|
||||||
DeviceNode::new(
|
|
||||||
id,
|
|
||||||
Some(Arc::clone(&virtio_rng_device) as Arc<Mutex<dyn Migratable>>),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(devices)
|
Ok(devices)
|
||||||
@@ -1704,7 +1670,7 @@ impl DeviceManager {
|
|||||||
id
|
id
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut node = DeviceNode::new(id.clone(), None);
|
let mut node = device_node!(id);
|
||||||
|
|
||||||
// Look for the id in the device tree. If it can be found, that means
|
// Look for the id in the device tree. If it can be found, that means
|
||||||
// the device is being restored, otherwise it's created from scratch.
|
// the device is being restored, otherwise it's created from scratch.
|
||||||
@@ -1860,7 +1826,7 @@ impl DeviceManager {
|
|||||||
id
|
id
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut node = DeviceNode::new(id.clone(), None);
|
let mut node = device_node!(id);
|
||||||
|
|
||||||
// Look for the id in the device tree. If it can be found, that means
|
// Look for the id in the device tree. If it can be found, that means
|
||||||
// the device is being restored, otherwise it's created from scratch.
|
// the device is being restored, otherwise it's created from scratch.
|
||||||
@@ -2070,13 +2036,8 @@ impl DeviceManager {
|
|||||||
// Fill the device tree with a new node. In case of restore, we
|
// Fill the device tree with a new node. In case of restore, we
|
||||||
// know there is nothing to do, so we can simply override the
|
// know there is nothing to do, so we can simply override the
|
||||||
// existing entry.
|
// existing entry.
|
||||||
self.device_tree.insert(
|
self.device_tree
|
||||||
id.clone(),
|
.insert(id.clone(), device_node!(id, vsock_device));
|
||||||
DeviceNode::new(
|
|
||||||
id.clone(),
|
|
||||||
Some(Arc::clone(&vsock_device) as Arc<Mutex<dyn Migratable>>),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
Arc::clone(&vsock_device) as VirtioDeviceArc,
|
Arc::clone(&vsock_device) as VirtioDeviceArc,
|
||||||
@@ -2129,13 +2090,8 @@ impl DeviceManager {
|
|||||||
// Fill the device tree with a new node. In case of restore, we
|
// Fill the device tree with a new node. In case of restore, we
|
||||||
// know there is nothing to do, so we can simply override the
|
// know there is nothing to do, so we can simply override the
|
||||||
// existing entry.
|
// existing entry.
|
||||||
self.device_tree.insert(
|
self.device_tree
|
||||||
id.clone(),
|
.insert(id.clone(), device_node!(id, virtio_mem_device));
|
||||||
DeviceNode::new(
|
|
||||||
id,
|
|
||||||
Some(Arc::clone(&virtio_mem_device) as Arc<Mutex<dyn Migratable>>),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(devices)
|
Ok(devices)
|
||||||
@@ -2319,7 +2275,7 @@ impl DeviceManager {
|
|||||||
let id = format!("{}-{}", VIRTIO_PCI_DEVICE_NAME_PREFIX, virtio_device_id);
|
let id = format!("{}-{}", VIRTIO_PCI_DEVICE_NAME_PREFIX, virtio_device_id);
|
||||||
|
|
||||||
// Add the new virtio-pci node to the device tree.
|
// Add the new virtio-pci node to the device tree.
|
||||||
let mut node = DeviceNode::new(id.clone(), None);
|
let mut node = device_node!(id);
|
||||||
node.children = vec![virtio_device_id.clone()];
|
node.children = vec![virtio_device_id.clone()];
|
||||||
|
|
||||||
// Update the existing virtio node by setting the parent.
|
// Update the existing virtio node by setting the parent.
|
||||||
@@ -2431,7 +2387,7 @@ impl DeviceManager {
|
|||||||
|
|
||||||
// Create the new virtio-mmio node that will be added later to the
|
// Create the new virtio-mmio node that will be added later to the
|
||||||
// device tree.
|
// device tree.
|
||||||
let mut node = DeviceNode::new(id.clone(), None);
|
let mut node = device_node!(id);
|
||||||
node.children = vec![virtio_device_id.clone()];
|
node.children = vec![virtio_device_id.clone()];
|
||||||
|
|
||||||
// Look for the id in the device tree. If it can be found, that means
|
// Look for the id in the device tree. If it can be found, that means
|
||||||
|
|||||||
Reference in New Issue
Block a user