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 network hotplug for clh #8580

Merged
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, VsockConfig};
use crate::{DeviceConfig, DiskConfig, FsConfig, NetConfig, VmConfig, VsockConfig};
use anyhow::{anyhow, Result};
use api_client::simple_api_full_command_and_response;

Expand Down Expand Up @@ -100,6 +100,24 @@ pub async fn cloud_hypervisor_vm_blockdev_add(
.await?
}

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

Ok(response)
})
.await?
}

pub async fn cloud_hypervisor_vm_device_add(
mut socket: UnixStream,
device_config: DeviceConfig,
Expand Down
29 changes: 28 additions & 1 deletion src/runtime-rs/crates/hypervisor/src/ch/inner_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::device::DeviceType;
use crate::BlockDevice;
use crate::HybridVsockDevice;
use crate::NetworkConfig;
use crate::NetworkDevice;
use crate::PciPath;
use crate::ShareFsConfig;
use crate::ShareFsDevice;
Expand All @@ -18,7 +19,8 @@ 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, cloud_hypervisor_vm_vsock_add, PciDeviceInfo, VmRemoveDeviceData,
cloud_hypervisor_vm_fs_add, cloud_hypervisor_vm_netdev_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;
Expand Down Expand Up @@ -79,6 +81,7 @@ impl CloudHypervisorInner {
DeviceType::HybridVsock(hvsock) => self.handle_hvsock_device(hvsock).await,
DeviceType::Block(block) => self.handle_block_device(block).await,
DeviceType::Vfio(vfiodev) => self.handle_vfio_device(vfiodev).await,
DeviceType::Network(netdev) => self.handle_network_device(netdev).await,
_ => Err(anyhow!("unhandled device: {:?}", device)),
}
}
Expand Down Expand Up @@ -341,6 +344,30 @@ impl CloudHypervisorInner {
Ok(DeviceType::Block(block_dev))
}

async fn handle_network_device(&mut self, device: NetworkDevice) -> Result<DeviceType> {
let netdev = device.clone();

let socket = self
.api_socket
.as_ref()
.ok_or("missing socket")
.map_err(|e| anyhow!(e))?;

let clh_net_config = NetConfig::try_from(device.config)?;

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

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

Ok(DeviceType::Network(netdev))
}

pub(crate) async fn get_shared_devices(
&mut self,
) -> Result<(Option<Vec<FsConfig>>, Option<Vec<NetConfig>>)> {
Expand Down