vmm: Add an identifier to the --vsock device

It's possible to have multiple vsock devices so in preparation for
hotplug/unplug it is important to be able to have a unique identifier
for each device.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Sebastien Boeuf 2020-04-27 10:15:30 +02:00 committed by Rob Bradford
parent 10348f73e4
commit 6e049e0da1
3 changed files with 55 additions and 26 deletions

View File

@ -196,7 +196,8 @@ fn create_app<'a, 'b>(
Arg::with_name("vsock")
.long("vsock")
.help(
"Virtio VSOCK parameters \"cid=<context_id>,sock=<socket_path>,iommu=on|off\"",
"Virtio VSOCK parameters \
\"cid=<context_id>,sock=<socket_path>,iommu=on|off,id=<device_id>\"",
)
.takes_value(true)
.number_of_values(1)

View File

@ -1136,12 +1136,14 @@ pub struct VsockConfig {
pub sock: PathBuf,
#[serde(default)]
pub iommu: bool,
#[serde(default)]
pub id: Option<String>,
}
impl VsockConfig {
pub fn parse(vsock: &str) -> Result<Self> {
let mut parser = OptionParser::new();
parser.add("sock").add("cid").add("iommu");
parser.add("sock").add("cid").add("iommu").add("id");
parser.parse(vsock).map_err(Error::ParseVsock)?;
let sock = parser
@ -1157,8 +1159,14 @@ impl VsockConfig {
.convert("cid")
.map_err(Error::ParseVsock)?
.ok_or(Error::ParseVsockCidMissing)?;
let id = parser.get("id");
Ok(VsockConfig { cid, sock, iommu })
Ok(VsockConfig {
cid,
sock,
iommu,
id,
})
}
}
@ -1878,7 +1886,8 @@ mod tests {
VsockConfig {
cid: 1,
sock: PathBuf::from("/tmp/sock"),
iommu: false
iommu: false,
id: None,
}
);
assert_eq!(
@ -1886,7 +1895,8 @@ mod tests {
VsockConfig {
cid: 1,
sock: PathBuf::from("/tmp/sock"),
iommu: true
iommu: true,
id: None,
}
);
Ok(())

View File

@ -14,7 +14,7 @@ extern crate vm_device;
use crate::config::ConsoleOutputMode;
#[cfg(feature = "pci_support")]
use crate::config::DeviceConfig;
use crate::config::{DiskConfig, FsConfig, NetConfig, PmemConfig, VmConfig};
use crate::config::{DiskConfig, FsConfig, NetConfig, PmemConfig, VmConfig, VsockConfig};
use crate::interrupt::{
KvmLegacyUserspaceInterruptManager, KvmMsiInterruptManager, KvmRoutingEntry,
};
@ -76,6 +76,7 @@ const VFIO_DEVICE_NAME_PREFIX: &str = "vfio";
const DISK_DEVICE_NAME_PREFIX: &str = "disk";
const NET_DEVICE_NAME_PREFIX: &str = "net";
const PMEM_DEVICE_NAME_PREFIX: &str = "pmem";
const VSOCK_DEVICE_NAME_PREFIX: &str = "vsock";
/// Errors associated with device manager
#[derive(Debug)]
@ -1120,7 +1121,7 @@ impl DeviceManager {
devices.append(&mut self.make_virtio_pmem_devices()?);
// Add virtio-vsock if required
devices.append(&mut self.make_virtio_vsock_device()?);
devices.append(&mut self.make_virtio_vsock_devices()?);
devices.append(&mut self.make_virtio_mem_devices()?);
@ -1642,29 +1643,46 @@ impl DeviceManager {
fn make_virtio_vsock_device(
&mut self,
vsock_cfg: &mut VsockConfig,
) -> DeviceManagerResult<(VirtioDeviceArc, bool, Option<String>)> {
if vsock_cfg.id.is_none() {
vsock_cfg.id = self.next_device_name(VSOCK_DEVICE_NAME_PREFIX)?;
}
let socket_path = vsock_cfg
.sock
.to_str()
.ok_or(DeviceManagerError::CreateVsockConvertPath)?;
let backend =
vm_virtio::vsock::VsockUnixBackend::new(vsock_cfg.cid, socket_path.to_string())
.map_err(DeviceManagerError::CreateVsockBackend)?;
let vsock_device = Arc::new(Mutex::new(
vm_virtio::Vsock::new(vsock_cfg.cid, backend, vsock_cfg.iommu)
.map_err(DeviceManagerError::CreateVirtioVsock)?,
));
let migratable = Arc::clone(&vsock_device) as Arc<Mutex<dyn Migratable>>;
let id = migratable.lock().unwrap().id();
self.migratable_devices.push((id, migratable));
Ok((
Arc::clone(&vsock_device) as VirtioDeviceArc,
false,
vsock_cfg.id.clone(),
))
}
fn make_virtio_vsock_devices(
&mut self,
) -> DeviceManagerResult<Vec<(VirtioDeviceArc, bool, Option<String>)>> {
let mut devices = Vec::new();
// Add vsock if required
if let Some(vsock_cfg) = &self.config.lock().unwrap().vsock {
let socket_path = vsock_cfg
.sock
.to_str()
.ok_or(DeviceManagerError::CreateVsockConvertPath)?;
let backend =
vm_virtio::vsock::VsockUnixBackend::new(vsock_cfg.cid, socket_path.to_string())
.map_err(DeviceManagerError::CreateVsockBackend)?;
let vsock_device = Arc::new(Mutex::new(
vm_virtio::Vsock::new(vsock_cfg.cid, backend, vsock_cfg.iommu)
.map_err(DeviceManagerError::CreateVirtioVsock)?,
));
devices.push((Arc::clone(&vsock_device) as VirtioDeviceArc, false, None));
let migratable = Arc::clone(&vsock_device) as Arc<Mutex<dyn Migratable>>;
let id = migratable.lock().unwrap().id();
self.migratable_devices.push((id, migratable));
let mut vsock = self.config.lock().unwrap().vsock.clone();
if let Some(ref mut vsock_cfg) = &mut vsock {
devices.push(self.make_virtio_vsock_device(vsock_cfg)?);
}
self.config.lock().unwrap().vsock = vsock;
Ok(devices)
}