vmm: Add support for hotplugging user devices

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford
2021-07-30 14:48:58 +00:00
parent f99462add4
commit 53b2e19934
6 changed files with 114 additions and 8 deletions

View File

@@ -77,6 +77,9 @@ pub enum HttpError {
/// Could not add a device to a VM
VmAddDevice(ApiError),
/// Could not add a user device to the VM
VmAddUserDevice(ApiError),
/// Could not remove a device from a VM
VmRemoveDevice(ApiError),
@@ -207,6 +210,7 @@ lazy_static! {
};
r.routes.insert(endpoint!("/vm.add-device"), Box::new(VmActionHandler::new(VmAction::AddDevice(Arc::default()))));
r.routes.insert(endpoint!("/vm.add-user-device"), Box::new(VmActionHandler::new(VmAction::AddUserDevice(Arc::default()))));
r.routes.insert(endpoint!("/vm.add-disk"), Box::new(VmActionHandler::new(VmAction::AddDisk(Arc::default()))));
r.routes.insert(endpoint!("/vm.add-fs"), Box::new(VmActionHandler::new(VmAction::AddFs(Arc::default()))));
r.routes.insert(endpoint!("/vm.add-net"), Box::new(VmActionHandler::new(VmAction::AddNet(Arc::default()))));

View File

@@ -5,11 +5,11 @@
use crate::api::http::{error_response, EndpointHandler, HttpError};
use crate::api::{
vm_add_device, vm_add_disk, vm_add_fs, vm_add_net, vm_add_pmem, vm_add_vsock, vm_boot,
vm_counters, vm_create, vm_delete, vm_info, vm_pause, vm_power_button, vm_reboot,
vm_receive_migration, vm_remove_device, vm_resize, vm_resize_zone, vm_restore, vm_resume,
vm_send_migration, vm_shutdown, vm_snapshot, vmm_ping, vmm_shutdown, ApiRequest, VmAction,
VmConfig,
vm_add_device, vm_add_disk, vm_add_fs, vm_add_net, vm_add_pmem, vm_add_user_device,
vm_add_vsock, vm_boot, vm_counters, vm_create, vm_delete, vm_info, vm_pause, vm_power_button,
vm_reboot, vm_receive_migration, vm_remove_device, vm_resize, vm_resize_zone, vm_restore,
vm_resume, vm_send_migration, vm_shutdown, vm_snapshot, vmm_ping, vmm_shutdown, ApiRequest,
VmAction, VmConfig,
};
use crate::config::NetConfig;
use micro_http::{Body, Method, Request, Response, StatusCode, Version};
@@ -127,6 +127,13 @@ impl EndpointHandler for VmActionHandler {
)
.map_err(HttpError::VmAddVsock),
AddUserDevice(_) => vm_add_user_device(
api_notifier,
api_sender,
Arc::new(serde_json::from_slice(body.raw())?),
)
.map_err(HttpError::VmAddUserDevice),
RemoveDevice(_) => vm_remove_device(
api_notifier,
api_sender,

View File

@@ -34,6 +34,7 @@ pub use self::http::start_http_path_thread;
pub mod http;
pub mod http_endpoint;
use crate::config::UserDeviceConfig;
use crate::config::{
DeviceConfig, DiskConfig, FsConfig, NetConfig, PmemConfig, RestoreConfig, VmConfig, VsockConfig,
};
@@ -109,6 +110,9 @@ pub enum ApiError {
/// The device could not be added to the VM.
VmAddDevice(VmError),
/// The user device could not be added to the VM.
VmAddUserDevice(VmError),
/// The device could not be removed from the VM.
VmRemoveDevice(VmError),
@@ -269,6 +273,9 @@ pub enum ApiRequest {
/// Add a device to the VM.
VmAddDevice(Arc<DeviceConfig>, Sender<ApiResponse>),
/// Add a user device to the VM.
VmAddUserDevice(Arc<UserDeviceConfig>, Sender<ApiResponse>),
/// Remove a device from the VM.
VmRemoveDevice(Arc<VmRemoveDeviceData>, Sender<ApiResponse>),
@@ -364,6 +371,9 @@ pub enum VmAction {
/// Add vsock
AddVsock(Arc<VsockConfig>),
/// Add user device
AddUserDevice(Arc<UserDeviceConfig>),
/// Remove VFIO device
RemoveDevice(Arc<VmRemoveDeviceData>),
@@ -411,6 +421,7 @@ fn vm_action(
AddPmem(v) => ApiRequest::VmAddPmem(v, response_sender),
AddNet(v) => ApiRequest::VmAddNet(v, response_sender),
AddVsock(v) => ApiRequest::VmAddVsock(v, response_sender),
AddUserDevice(v) => ApiRequest::VmAddUserDevice(v, response_sender),
RemoveDevice(v) => ApiRequest::VmRemoveDevice(v, response_sender),
Resize(v) => ApiRequest::VmResize(v, response_sender),
ResizeZone(v) => ApiRequest::VmResizeZone(v, response_sender),
@@ -572,6 +583,14 @@ pub fn vm_add_device(
vm_action(api_evt, api_sender, VmAction::AddDevice(data))
}
pub fn vm_add_user_device(
api_evt: EventFd,
api_sender: Sender<ApiRequest>,
data: Arc<UserDeviceConfig>,
) -> ApiResult<Option<Body>> {
vm_action(api_evt, api_sender, VmAction::AddUserDevice(data))
}
pub fn vm_remove_device(
api_evt: EventFd,
api_sender: Sender<ApiRequest>,