mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2024-10-03 20:15:45 +00:00
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:
parent
991f3bb5da
commit
6cbdb9aa47
@ -79,7 +79,7 @@ Shut the VMM down | `/vmm.shutdown` | N/A | N/A
|
|||||||
#### Virtual Machine (VM) Actions
|
#### Virtual Machine (VM) Actions
|
||||||
|
|
||||||
Action | Endpoint | Request Body | Response Body | Prerequisites
|
Action | Endpoint | Request Body | Response Body | Prerequisites
|
||||||
---------------------------------|------------------|------------------------|-------------------|---------------------------
|
-----------------------------------|---------------------|---------------------------|-------------------|---------------------------
|
||||||
Create the VM | `/vm.create` | `/schemas/VmConfig` | N/A | The VM is not created yet
|
Create the VM | `/vm.create` | `/schemas/VmConfig` | N/A | The VM is not created yet
|
||||||
Delete the VM | `/vm.delete` | N/A | N/A | The VM is created but not booted
|
Delete the VM | `/vm.delete` | N/A | N/A | The VM is created but not booted
|
||||||
Boot the VM | `/vm.boot` | N/A | N/A | The VM is created
|
Boot the VM | `/vm.boot` | N/A | N/A | The VM is created
|
||||||
@ -91,6 +91,7 @@ Add/remove CPUs to/from the VM | `/vm.resize` | `/schemas/VmResize` | N
|
|||||||
Remove memory from the VM | `/vm.resize` | `/schemas/VmResize` | N/A | The VM is booted
|
Remove memory from the VM | `/vm.resize` | `/schemas/VmResize` | N/A | The VM is booted
|
||||||
Dump the VM information | `/vm.info` | N/A | `/schemas/VmInfo` | The VM is created
|
Dump the VM information | `/vm.info` | N/A | `/schemas/VmInfo` | The VM is created
|
||||||
Add VFIO PCI device to the VM | `/vm.add-device` | `/schemas/VmAddDevice` | N/A | The VM is booted
|
Add VFIO PCI device to the VM | `/vm.add-device` | `/schemas/VmAddDevice` | N/A | The VM is booted
|
||||||
|
Remove VFIO PCI device from the VM | `/vm.remove-device` | `/schemas/VmRemoveDevice` | N/A | The VM is booted
|
||||||
|
|
||||||
### REST API Examples
|
### REST API Examples
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
use crate::api::http_endpoint::{
|
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::api::{ApiRequest, VmAction};
|
||||||
use crate::{Error, Result};
|
use crate::{Error, Result};
|
||||||
@ -63,6 +63,7 @@ lazy_static! {
|
|||||||
r.routes.insert(endpoint!("/vmm.ping"), Box::new(VmmPing {}));
|
r.routes.insert(endpoint!("/vmm.ping"), Box::new(VmmPing {}));
|
||||||
r.routes.insert(endpoint!("/vm.resize"), Box::new(VmResize {}));
|
r.routes.insert(endpoint!("/vm.resize"), Box::new(VmResize {}));
|
||||||
r.routes.insert(endpoint!("/vm.add-device"), Box::new(VmAddDevice {}));
|
r.routes.insert(endpoint!("/vm.add-device"), Box::new(VmAddDevice {}));
|
||||||
|
r.routes.insert(endpoint!("/vm.remove-device"), Box::new(VmRemoveDevice {}));
|
||||||
|
|
||||||
r
|
r
|
||||||
};
|
};
|
||||||
|
@ -5,9 +5,9 @@
|
|||||||
|
|
||||||
use crate::api::http::EndpointHandler;
|
use crate::api::http::EndpointHandler;
|
||||||
use crate::api::{
|
use crate::api::{
|
||||||
vm_add_device, vm_boot, vm_create, vm_delete, vm_info, vm_pause, vm_reboot, vm_resize,
|
vm_add_device, vm_boot, vm_create, vm_delete, vm_info, vm_pause, vm_reboot, vm_remove_device,
|
||||||
vm_resume, vm_shutdown, vmm_ping, vmm_shutdown, ApiError, ApiRequest, ApiResult, VmAction,
|
vm_resize, vm_resume, vm_shutdown, vmm_ping, vmm_shutdown, ApiError, ApiRequest, ApiResult,
|
||||||
VmAddDeviceData, VmConfig, VmResizeData,
|
VmAction, VmAddDeviceData, VmConfig, VmRemoveDeviceData, VmResizeData,
|
||||||
};
|
};
|
||||||
use micro_http::{Body, Method, Request, Response, StatusCode, Version};
|
use micro_http::{Body, Method, Request, Response, StatusCode, Version};
|
||||||
use serde_json::Error as SerdeError;
|
use serde_json::Error as SerdeError;
|
||||||
@ -51,6 +51,9 @@ pub enum HttpError {
|
|||||||
/// Could not add a device to a VM
|
/// Could not add a device to a VM
|
||||||
VmAddDevice(ApiError),
|
VmAddDevice(ApiError),
|
||||||
|
|
||||||
|
/// Could not remove a device from a VM
|
||||||
|
VmRemoveDevice(ApiError),
|
||||||
|
|
||||||
/// Could not shut the VMM down
|
/// Could not shut the VMM down
|
||||||
VmmShutdown(ApiError),
|
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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -102,6 +102,9 @@ pub enum ApiError {
|
|||||||
|
|
||||||
/// The device could not be added to the VM.
|
/// The device could not be added to the VM.
|
||||||
VmAddDevice(VmError),
|
VmAddDevice(VmError),
|
||||||
|
|
||||||
|
/// The device could not be removed from the VM.
|
||||||
|
VmRemoveDevice(VmError),
|
||||||
}
|
}
|
||||||
pub type ApiResult<T> = std::result::Result<T, ApiError>;
|
pub type ApiResult<T> = std::result::Result<T, ApiError>;
|
||||||
|
|
||||||
@ -127,6 +130,11 @@ pub struct VmAddDeviceData {
|
|||||||
pub path: String,
|
pub path: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Deserialize, Serialize)]
|
||||||
|
pub struct VmRemoveDeviceData {
|
||||||
|
pub id: String,
|
||||||
|
}
|
||||||
|
|
||||||
pub enum ApiResponsePayload {
|
pub enum ApiResponsePayload {
|
||||||
/// No data is sent on the channel.
|
/// No data is sent on the channel.
|
||||||
Empty,
|
Empty,
|
||||||
@ -192,6 +200,9 @@ pub enum ApiRequest {
|
|||||||
|
|
||||||
/// Add a device to the VM.
|
/// Add a device to the VM.
|
||||||
VmAddDevice(Arc<VmAddDeviceData>, Sender<ApiResponse>),
|
VmAddDevice(Arc<VmAddDeviceData>, Sender<ApiResponse>),
|
||||||
|
|
||||||
|
/// Remove a device from the VM.
|
||||||
|
VmRemoveDevice(Arc<VmRemoveDeviceData>, Sender<ApiResponse>),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn vm_create(
|
pub fn vm_create(
|
||||||
@ -362,3 +373,21 @@ pub fn vm_add_device(
|
|||||||
|
|
||||||
Ok(())
|
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(())
|
||||||
|
}
|
||||||
|
@ -155,6 +155,22 @@ paths:
|
|||||||
404:
|
404:
|
||||||
description: The new device could not be added to the VM instance.
|
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:
|
components:
|
||||||
schemas:
|
schemas:
|
||||||
|
|
||||||
@ -449,3 +465,9 @@ components:
|
|||||||
properties:
|
properties:
|
||||||
path:
|
path:
|
||||||
type: string
|
type: string
|
||||||
|
|
||||||
|
VmRemoveDevice:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
id:
|
||||||
|
type: string
|
||||||
|
@ -236,6 +236,10 @@ pub enum DeviceManagerError {
|
|||||||
|
|
||||||
/// Failed removing a bus device from the MMIO bus.
|
/// Failed removing a bus device from the MMIO bus.
|
||||||
RemoveDeviceFromMmioBus(devices::BusError),
|
RemoveDeviceFromMmioBus(devices::BusError),
|
||||||
|
|
||||||
|
/// Failed to find VFIO device corresponding to the given identifier.
|
||||||
|
#[cfg(feature = "pci_support")]
|
||||||
|
UnknownVfioDeviceId(String),
|
||||||
}
|
}
|
||||||
pub type DeviceManagerResult<T> = result::Result<T, DeviceManagerError>;
|
pub type DeviceManagerResult<T> = result::Result<T, DeviceManagerError>;
|
||||||
|
|
||||||
@ -1791,6 +1795,18 @@ impl DeviceManager {
|
|||||||
Ok(device_cfg)
|
Ok(device_cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "pci_support")]
|
||||||
|
pub fn remove_device(&mut self, id: String) -> DeviceManagerResult<()> {
|
||||||
|
if let Some(pci_device_bdf) = self.pci_id_list.get(&id) {
|
||||||
|
// Update the PCID bitmap
|
||||||
|
self.pci_devices_down |= 1 << (*pci_device_bdf >> 3);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(DeviceManagerError::UnknownVfioDeviceId(id))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "pci_support")]
|
#[cfg(feature = "pci_support")]
|
||||||
pub fn eject_device(&mut self, device_id: u8) -> DeviceManagerResult<()> {
|
pub fn eject_device(&mut self, device_id: u8) -> DeviceManagerResult<()> {
|
||||||
// Retrieve the PCI bus.
|
// Retrieve the PCI bus.
|
||||||
|
@ -385,6 +385,19 @@ impl Vmm {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn vm_remove_device(&mut self, id: String) -> result::Result<(), VmError> {
|
||||||
|
if let Some(ref mut vm) = self.vm {
|
||||||
|
if let Err(e) = vm.remove_device(id) {
|
||||||
|
error!("Error when adding new device to the VM: {:?}", e);
|
||||||
|
Err(e)
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Err(VmError::VmNotRunning)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn control_loop(&mut self, api_receiver: Arc<Receiver<ApiRequest>>) -> Result<()> {
|
fn control_loop(&mut self, api_receiver: Arc<Receiver<ApiRequest>>) -> Result<()> {
|
||||||
const EPOLL_EVENTS_LEN: usize = 100;
|
const EPOLL_EVENTS_LEN: usize = 100;
|
||||||
|
|
||||||
@ -547,6 +560,13 @@ impl Vmm {
|
|||||||
.map(|_| ApiResponsePayload::Empty);
|
.map(|_| ApiResponsePayload::Empty);
|
||||||
sender.send(response).map_err(Error::ApiResponseSend)?;
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
||||||
}
|
}
|
||||||
|
ApiRequest::VmRemoveDevice(remove_device_data, sender) => {
|
||||||
|
let response = self
|
||||||
|
.vm_remove_device(remove_device_data.id.clone())
|
||||||
|
.map_err(ApiError::VmRemoveDevice)
|
||||||
|
.map(|_| ApiResponsePayload::Empty);
|
||||||
|
sender.send(response).map_err(Error::ApiResponseSend)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -577,6 +577,28 @@ impl Vm {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn remove_device(&mut self, _id: String) -> Result<()> {
|
||||||
|
if cfg!(feature = "pci_support") {
|
||||||
|
#[cfg(feature = "pci_support")]
|
||||||
|
{
|
||||||
|
self.device_manager
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.remove_device(_id)
|
||||||
|
.map_err(Error::DeviceManager)?;
|
||||||
|
|
||||||
|
self.device_manager
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.notify_hotplug(HotPlugNotificationFlags::PCI_DEVICES_CHANGED)
|
||||||
|
.map_err(Error::DeviceManager)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(Error::NoPciSupport)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn os_signal_handler(signals: Signals, console_input_clone: Arc<Console>, on_tty: bool) {
|
fn os_signal_handler(signals: Signals, console_input_clone: Arc<Console>, on_tty: bool) {
|
||||||
for signal in signals.forever() {
|
for signal in signals.forever() {
|
||||||
match signal {
|
match signal {
|
||||||
|
Loading…
Reference in New Issue
Block a user