Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime-rs: Add Hybrid VSOCK device handling for CH #7815

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/runtime-rs/crates/hypervisor/ch-config/src/ch_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: Apache-2.0

use crate::{DeviceConfig, DiskConfig, FsConfig, VmConfig};
use crate::{DeviceConfig, DiskConfig, FsConfig, VmConfig, VsockConfig};
use anyhow::{anyhow, Result};
use api_client::simple_api_full_command_and_response;

Expand Down Expand Up @@ -154,3 +154,21 @@ pub async fn cloud_hypervisor_vm_fs_add(
})
.await?
}

pub async fn cloud_hypervisor_vm_vsock_add(
mut socket: UnixStream,
vsock_config: VsockConfig,
) -> Result<Option<String>> {
task::spawn_blocking(move || -> Result<Option<String>> {
let response = simple_api_full_command_and_response(
&mut socket,
"PUT",
"vm.add-vsock",
Some(&serde_json::to_string(&vsock_config)?),
)
.map_err(|e| anyhow!(e))?;

Ok(response)
})
.await?
}
35 changes: 29 additions & 6 deletions src/runtime-rs/crates/hypervisor/src/ch/inner_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ use anyhow::{anyhow, Context, Result};
use ch_config::ch_api::cloud_hypervisor_vm_device_add;
use ch_config::ch_api::{
cloud_hypervisor_vm_blockdev_add, cloud_hypervisor_vm_device_remove,
cloud_hypervisor_vm_fs_add, PciDeviceInfo, VmRemoveDeviceData,
cloud_hypervisor_vm_fs_add, cloud_hypervisor_vm_vsock_add, PciDeviceInfo, VmRemoveDeviceData,
};
use ch_config::convert::{DEFAULT_DISK_QUEUES, DEFAULT_DISK_QUEUE_SIZE, DEFAULT_NUM_PCI_SEGMENTS};
use ch_config::DiskConfig;
use ch_config::{net_util::MacAddr, DeviceConfig, FsConfig, NetConfig};
use ch_config::{net_util::MacAddr, DeviceConfig, FsConfig, NetConfig, VsockConfig};
use safe_path::scoped_join;
use std::convert::TryFrom;
use std::path::PathBuf;
Expand Down Expand Up @@ -168,10 +168,6 @@ impl CloudHypervisorInner {
Ok(DeviceType::ShareFs(sharefs))
}

async fn handle_hvsock_device(&mut self, device: HybridVsockDevice) -> Result<DeviceType> {
Ok(DeviceType::HybridVsock(device))
}

async fn handle_vfio_device(&mut self, device: VfioDevice) -> Result<DeviceType> {
let mut vfio_device: VfioDevice = device.clone();

Expand Down Expand Up @@ -281,6 +277,33 @@ impl CloudHypervisorInner {
PciPath::convert_from_string(toks[0])
}

async fn handle_hvsock_device(&mut self, device: HybridVsockDevice) -> Result<DeviceType> {
let hvsock_config = device.config.clone();
let socket = self
.api_socket
.as_ref()
.ok_or("missing socket")
.map_err(|e| anyhow!(e))?;

let vsock_config = VsockConfig {
cid: hvsock_config.guest_cid.into(),
socket: hvsock_config.uds_path.into(),
..Default::default()
};

let response = cloud_hypervisor_vm_vsock_add(
socket.try_clone().context("failed to clone socket")?,
vsock_config,
)
.await?;

if let Some(detail) = response {
debug!(sl!(), "hvsock add response: {:?}", detail);
}

Ok(DeviceType::HybridVsock(device))
}

async fn handle_block_device(&mut self, device: BlockDevice) -> Result<DeviceType> {
let mut block_dev = device.clone();
let socket = self
Expand Down