vmm: Add "add-pmem" to the API

Add the HTTP and internal API entry points for adding persistent memory
at runtime.

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2020-03-23 16:21:58 +00:00 committed by Sebastien Boeuf
parent 15de30f141
commit f6f4c68fb4
4 changed files with 95 additions and 9 deletions

View File

@ -4,8 +4,8 @@
//
use crate::api::http_endpoint::{
VmActionHandler, VmAddDevice, VmAddDisk, VmCreate, VmInfo, VmRemoveDevice, VmResize, VmmPing,
VmmShutdown,
VmActionHandler, VmAddDevice, VmAddDisk, VmAddPmem, VmCreate, VmInfo, VmRemoveDevice, VmResize,
VmmPing, VmmShutdown,
};
use crate::api::{ApiRequest, VmAction};
use crate::seccomp_filters::{get_seccomp_filter, Thread};
@ -68,6 +68,7 @@ lazy_static! {
r.routes.insert(endpoint!("/vm.add-device"), Box::new(VmAddDevice {}));
r.routes.insert(endpoint!("/vm.remove-device"), Box::new(VmRemoveDevice {}));
r.routes.insert(endpoint!("/vm.add-disk"), Box::new(VmAddDisk {}));
r.routes.insert(endpoint!("/vm.add-pmem"), Box::new(VmAddPmem {}));
r
};

View File

@ -5,10 +5,10 @@
use crate::api::http::EndpointHandler;
use crate::api::{
vm_add_device, vm_add_disk, 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, DeviceConfig, DiskConfig, VmAction, VmConfig, VmRemoveDeviceData,
VmResizeData,
vm_add_device, vm_add_disk, vm_add_pmem, 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, DeviceConfig, DiskConfig, PmemConfig, VmAction, VmConfig,
VmRemoveDeviceData, VmResizeData,
};
use micro_http::{Body, Method, Request, Response, StatusCode, Version};
use serde_json::Error as SerdeError;
@ -63,6 +63,9 @@ pub enum HttpError {
/// Could not add a disk to a VM
VmAddDisk(ApiError),
/// Could not add a pmem device to a VM
VmAddPmem(ApiError),
}
fn error_response(error: HttpError, status: StatusCode) -> Response {
@ -395,3 +398,41 @@ impl EndpointHandler for VmAddDisk {
}
}
}
// /api/v1/vm.add-pmem handler
pub struct VmAddPmem {}
impl EndpointHandler for VmAddPmem {
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 PmemConfig
let vm_add_pmem_data: PmemConfig = match serde_json::from_slice(body.raw())
.map_err(HttpError::SerdeJsonDeserialize)
{
Ok(config) => config,
Err(e) => return error_response(e, StatusCode::BadRequest),
};
match vm_add_pmem(api_notifier, api_sender, Arc::new(vm_add_pmem_data))
.map_err(HttpError::VmAddPmem)
{
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

@ -36,7 +36,7 @@ pub use self::http::start_http_thread;
pub mod http;
pub mod http_endpoint;
use crate::config::{DeviceConfig, DiskConfig, VmConfig};
use crate::config::{DeviceConfig, DiskConfig, PmemConfig, VmConfig};
use crate::vm::{Error as VmError, VmState};
use std::io;
use std::sync::mpsc::{channel, RecvError, SendError, Sender};
@ -112,8 +112,11 @@ pub enum ApiError {
/// Cannot apply seccomp filter
ApplySeccompFilter(seccomp::Error),
/// The device could not be added to the VM.
/// The disk could not be added to the VM.
VmAddDisk(VmError),
/// The pmem device could not be added to the VM.
VmAddPmem(VmError),
}
pub type ApiResult<T> = std::result::Result<T, ApiError>;
@ -210,6 +213,9 @@ pub enum ApiRequest {
/// Add a disk to the VM.
VmAddDisk(Arc<DiskConfig>, Sender<ApiResponse>),
/// Add a pmem device to the VM.
VmAddPmem(Arc<PmemConfig>, Sender<ApiResponse>),
}
pub fn vm_create(
@ -416,3 +422,21 @@ pub fn vm_add_disk(
Ok(())
}
pub fn vm_add_pmem(
api_evt: EventFd,
api_sender: Sender<ApiRequest>,
data: Arc<PmemConfig>,
) -> ApiResult<()> {
let (response_sender, response_receiver) = channel();
// Send the VM add-pmem request.
api_sender
.send(ApiRequest::VmAddPmem(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

@ -16,7 +16,7 @@ extern crate tempfile;
extern crate vmm_sys_util;
use crate::api::{ApiError, ApiRequest, ApiResponse, ApiResponsePayload, VmInfo, VmmPingResponse};
use crate::config::{DeviceConfig, DiskConfig, VmConfig};
use crate::config::{DeviceConfig, DiskConfig, PmemConfig, VmConfig};
use crate::seccomp_filters::{get_seccomp_filter, Thread};
use crate::vm::{Error as VmError, Vm, VmState};
use libc::EFD_NONBLOCK;
@ -428,6 +428,19 @@ impl Vmm {
}
}
fn vm_add_pmem(&mut self, pmem_cfg: PmemConfig) -> result::Result<(), VmError> {
if let Some(ref mut vm) = self.vm {
if let Err(e) = vm.add_pmem(pmem_cfg) {
error!("Error when adding new pmem device to the VM: {:?}", e);
Err(e)
} else {
Ok(())
}
} else {
Err(VmError::VmNotRunning)
}
}
fn control_loop(&mut self, api_receiver: Arc<Receiver<ApiRequest>>) -> Result<()> {
const EPOLL_EVENTS_LEN: usize = 100;
@ -604,6 +617,13 @@ impl Vmm {
.map(|_| ApiResponsePayload::Empty);
sender.send(response).map_err(Error::ApiResponseSend)?;
}
ApiRequest::VmAddPmem(add_pmem_data, sender) => {
let response = self
.vm_add_pmem(add_pmem_data.as_ref().clone())
.map_err(ApiError::VmAddPmem)
.map(|_| ApiResponsePayload::Empty);
sender.send(response).map_err(Error::ApiResponseSend)?;
}
}
}
}