vmm: api: Introduce new "remove-device" HTTP endpoint

This commit introduces the new command "remove-device" that will let a
user hot-unplug a VFIO PCI device from an already running VM.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf
2020-03-09 11:49:15 +01:00
committed by Rob Bradford
parent 991f3bb5da
commit 6cbdb9aa47
8 changed files with 175 additions and 17 deletions

View File

@@ -4,7 +4,7 @@
//
use crate::api::http_endpoint::{
VmActionHandler, VmAddDevice, VmCreate, VmInfo, VmResize, VmmPing, VmmShutdown,
VmActionHandler, VmAddDevice, VmCreate, VmInfo, VmRemoveDevice, VmResize, VmmPing, VmmShutdown,
};
use crate::api::{ApiRequest, VmAction};
use crate::{Error, Result};
@@ -63,6 +63,7 @@ lazy_static! {
r.routes.insert(endpoint!("/vmm.ping"), Box::new(VmmPing {}));
r.routes.insert(endpoint!("/vm.resize"), Box::new(VmResize {}));
r.routes.insert(endpoint!("/vm.add-device"), Box::new(VmAddDevice {}));
r.routes.insert(endpoint!("/vm.remove-device"), Box::new(VmRemoveDevice {}));
r
};

View File

@@ -5,9 +5,9 @@
use crate::api::http::EndpointHandler;
use crate::api::{
vm_add_device, vm_boot, vm_create, vm_delete, vm_info, vm_pause, vm_reboot, vm_resize,
vm_resume, vm_shutdown, vmm_ping, vmm_shutdown, ApiError, ApiRequest, ApiResult, VmAction,
VmAddDeviceData, VmConfig, VmResizeData,
vm_add_device, vm_boot, vm_create, vm_delete, vm_info, vm_pause, vm_reboot, vm_remove_device,
vm_resize, vm_resume, vm_shutdown, vmm_ping, vmm_shutdown, ApiError, ApiRequest, ApiResult,
VmAction, VmAddDeviceData, VmConfig, VmRemoveDeviceData, VmResizeData,
};
use micro_http::{Body, Method, Request, Response, StatusCode, Version};
use serde_json::Error as SerdeError;
@@ -51,6 +51,9 @@ pub enum HttpError {
/// Could not add a device to a VM
VmAddDevice(ApiError),
/// Could not remove a device from a VM
VmRemoveDevice(ApiError),
/// Could not shut the VMM down
VmmShutdown(ApiError),
@@ -305,3 +308,47 @@ impl EndpointHandler for VmAddDevice {
}
}
}
// /api/v1/vm.remove-device handler
pub struct VmRemoveDevice {}
impl EndpointHandler for VmRemoveDevice {
fn handle_request(
&self,
req: &Request,
api_notifier: EventFd,
api_sender: Sender<ApiRequest>,
) -> Response {
match req.method() {
Method::Put => {
match &req.body {
Some(body) => {
// Deserialize into a VmRemoveDeviceData
let vm_remove_device_data: VmRemoveDeviceData =
match serde_json::from_slice(body.raw())
.map_err(HttpError::SerdeJsonDeserialize)
{
Ok(config) => config,
Err(e) => return error_response(e, StatusCode::BadRequest),
};
// Call vm_remove_device()
match vm_remove_device(
api_notifier,
api_sender,
Arc::new(vm_remove_device_data),
)
.map_err(HttpError::VmRemoveDevice)
{
Ok(_) => Response::new(Version::Http11, StatusCode::NoContent),
Err(e) => error_response(e, StatusCode::InternalServerError),
}
}
None => Response::new(Version::Http11, StatusCode::BadRequest),
}
}
_ => Response::new(Version::Http11, StatusCode::BadRequest),
}
}
}

View File

@@ -102,6 +102,9 @@ pub enum ApiError {
/// The device could not be added to the VM.
VmAddDevice(VmError),
/// The device could not be removed from the VM.
VmRemoveDevice(VmError),
}
pub type ApiResult<T> = std::result::Result<T, ApiError>;
@@ -127,6 +130,11 @@ pub struct VmAddDeviceData {
pub path: String,
}
#[derive(Clone, Deserialize, Serialize)]
pub struct VmRemoveDeviceData {
pub id: String,
}
pub enum ApiResponsePayload {
/// No data is sent on the channel.
Empty,
@@ -192,6 +200,9 @@ pub enum ApiRequest {
/// Add a device to the VM.
VmAddDevice(Arc<VmAddDeviceData>, Sender<ApiResponse>),
/// Remove a device from the VM.
VmRemoveDevice(Arc<VmRemoveDeviceData>, Sender<ApiResponse>),
}
pub fn vm_create(
@@ -362,3 +373,21 @@ pub fn vm_add_device(
Ok(())
}
pub fn vm_remove_device(
api_evt: EventFd,
api_sender: Sender<ApiRequest>,
data: Arc<VmRemoveDeviceData>,
) -> ApiResult<()> {
let (response_sender, response_receiver) = channel();
// Send the VM remove-device request.
api_sender
.send(ApiRequest::VmRemoveDevice(data, response_sender))
.map_err(ApiError::RequestSend)?;
api_evt.write(1).map_err(ApiError::EventFdWrite)?;
response_receiver.recv().map_err(ApiError::ResponseRecv)??;
Ok(())
}

View File

@@ -155,6 +155,22 @@ paths:
404:
description: The new device could not be added to the VM instance.
/vm.remove-device:
put:
summary: Remove a device from the VM
requestBody:
description: The identifier of the device
content:
application/json:
schema:
$ref: '#/components/schemas/VmRemoveDevice'
required: true
responses:
204:
description: The device was successfully removed from the VM instance.
404:
description: The device could not be removed from the VM instance.
components:
schemas:
@@ -449,3 +465,9 @@ components:
properties:
path:
type: string
VmRemoveDevice:
type: object
properties:
id:
type: string