vmm: api: Add a VMM shutdown command

This shuts the current VM down, if any, and then exits the VMM process.

Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
This commit is contained in:
Samuel Ortiz 2019-10-08 15:23:29 +02:00 committed by Sebastien Boeuf
parent 228adebc32
commit a95fa1c4e8
5 changed files with 80 additions and 11 deletions

View File

@ -5,7 +5,7 @@
extern crate threadpool; extern crate threadpool;
use crate::api::http_endpoint::{VmActionHandler, VmCreate, VmInfo}; use crate::api::http_endpoint::{VmActionHandler, VmCreate, VmInfo, VmmShutdown};
use crate::api::{ApiRequest, VmAction}; use crate::api::{ApiRequest, VmAction};
use crate::{Error, Result}; use crate::{Error, Result};
use micro_http::{HttpConnection, Request, Response, StatusCode, Version}; use micro_http::{HttpConnection, Request, Response, StatusCode, Version};
@ -60,6 +60,7 @@ lazy_static! {
r.routes.insert(endpoint!("/vm.info"), Box::new(VmInfo {})); r.routes.insert(endpoint!("/vm.info"), Box::new(VmInfo {}));
r.routes.insert(endpoint!("/vm.shutdown"), Box::new(VmActionHandler::new(VmAction::Shutdown))); r.routes.insert(endpoint!("/vm.shutdown"), Box::new(VmActionHandler::new(VmAction::Shutdown)));
r.routes.insert(endpoint!("/vm.reboot"), Box::new(VmActionHandler::new(VmAction::Reboot))); r.routes.insert(endpoint!("/vm.reboot"), Box::new(VmActionHandler::new(VmAction::Reboot)));
r.routes.insert(endpoint!("/vmm.shutdown"), Box::new(VmmShutdown {}));
r r
}; };

View File

@ -5,8 +5,8 @@
use crate::api::http::EndpointHandler; use crate::api::http::EndpointHandler;
use crate::api::{ use crate::api::{
vm_boot, vm_create, vm_delete, vm_info, vm_reboot, vm_shutdown, ApiError, ApiRequest, vm_boot, vm_create, vm_delete, vm_info, vm_reboot, vm_shutdown, vmm_shutdown, ApiError,
ApiResult, VmAction, VmConfig, ApiRequest, ApiResult, VmAction, VmConfig,
}; };
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;
@ -37,6 +37,9 @@ pub enum HttpError {
/// Could not act on a VM /// Could not act on a VM
VmAction(ApiError), VmAction(ApiError),
/// Could not shut the VMM down
VmmShutdown(ApiError),
} }
fn error_response(error: HttpError, status: StatusCode) -> Response { fn error_response(error: HttpError, status: StatusCode) -> Response {
@ -155,3 +158,25 @@ impl EndpointHandler for VmInfo {
} }
} }
} }
// /api/v1/vmm.shutdown handler
pub struct VmmShutdown {}
impl EndpointHandler for VmmShutdown {
fn handle_request(
&self,
req: &Request,
api_notifier: EventFd,
api_sender: Sender<ApiRequest>,
) -> Response {
match req.method() {
Method::Put => {
match vmm_shutdown(api_notifier, api_sender).map_err(HttpError::VmmShutdown) {
Ok(_) => Response::new(Version::Http11, StatusCode::OK),
Err(e) => error_response(e, StatusCode::InternalServerError),
}
}
_ => Response::new(Version::Http11, StatusCode::BadRequest),
}
}
}

View File

@ -87,6 +87,9 @@ pub enum ApiError {
/// The VM could not reboot. /// The VM could not reboot.
VmReboot(VmError), VmReboot(VmError),
/// The VMM could not shutdown.
VmmShutdown(VmError),
} }
pub type ApiResult<T> = std::result::Result<T, ApiError>; pub type ApiResult<T> = std::result::Result<T, ApiError>;
@ -138,6 +141,11 @@ pub enum ApiRequest {
/// If the VM was not previously booted or created, the VMM API server /// If the VM was not previously booted or created, the VMM API server
/// will send a VmReboot error back. /// will send a VmReboot error back.
VmReboot(Sender<ApiResponse>), VmReboot(Sender<ApiResponse>),
/// Shut the VMM down.
/// This will shutdown and delete the current VM, if any, and then exit the
/// VMM process.
VmmShutdown(Sender<ApiResponse>),
} }
pub fn vm_create( pub fn vm_create(
@ -226,3 +234,17 @@ pub fn vm_info(api_evt: EventFd, api_sender: Sender<ApiRequest>) -> ApiResult<Vm
_ => Err(ApiError::ResponsePayloadType), _ => Err(ApiError::ResponsePayloadType),
} }
} }
pub fn vmm_shutdown(api_evt: EventFd, api_sender: Sender<ApiRequest>) -> ApiResult<()> {
let (response_sender, response_receiver) = channel();
// Send the VMM shutdown request.
api_sender
.send(ApiRequest::VmmShutdown(response_sender))
.map_err(ApiError::RequestSend)?;
api_evt.write(1).map_err(ApiError::EventFdWrite)?;
response_receiver.recv().map_err(ApiError::ResponseRecv)??;
Ok(())
}

View File

@ -23,6 +23,15 @@ paths:
schema: schema:
$ref: '#/components/schemas/VmmInfo' $ref: '#/components/schemas/VmmInfo'
/vmm.shutdown:
put:
summary: Shuts the cloud-hypervisor VMM.
operationId: shutdownVMM
responses:
201:
description: The VMM successfully shutdown.
content: {}
/vm.info: /vm.info:
get: get:
summary: Returns general information about the cloud-hypervisor Virtual Machine (VM) instance. summary: Returns general information about the cloud-hypervisor Virtual Machine (VM) instance.

View File

@ -69,6 +69,9 @@ pub enum Error {
/// Cannot create VMM thread /// Cannot create VMM thread
VmmThreadSpawn(io::Error), VmmThreadSpawn(io::Error),
/// Cannot shut the VMM down
VmmShutdown(VmError),
} }
pub type Result<T> = result::Result<T, Error>; pub type Result<T> = result::Result<T, Error>;
@ -168,14 +171,7 @@ pub fn start_vmm_thread(
// Continue and restart the VMM control loop // Continue and restart the VMM control loop
continue 'outer; continue 'outer;
} }
Ok(ExitBehaviour::Shutdown) => { Ok(ExitBehaviour::Shutdown) => break 'outer,
// The VMM control loop exites with a shutdown behaviour.
// We have to stop the VM and we exit thr thread.
if let Some(ref mut vm) = vmm.vm {
vm.shutdown().map_err(Error::VmShutdown)?;
}
break 'outer;
}
Err(e) => return Err(e), Err(e) => return Err(e),
} }
} }
@ -320,6 +316,10 @@ impl Vmm {
Ok(()) Ok(())
} }
fn vmm_shutdown(&mut self) -> result::Result<(), VmError> {
self.vm_delete()
}
fn control_loop(&mut self, api_receiver: Arc<Receiver<ApiRequest>>) -> Result<ExitBehaviour> { fn control_loop(&mut self, api_receiver: Arc<Receiver<ApiRequest>>) -> Result<ExitBehaviour> {
const EPOLL_EVENTS_LEN: usize = 100; const EPOLL_EVENTS_LEN: usize = 100;
@ -354,6 +354,7 @@ impl Vmm {
EpollDispatch::Exit => { EpollDispatch::Exit => {
// Consume the event. // Consume the event.
self.exit_evt.read().map_err(Error::EventFdRead)?; self.exit_evt.read().map_err(Error::EventFdRead)?;
self.vmm_shutdown().map_err(Error::VmmShutdown)?;
exit_behaviour = ExitBehaviour::Shutdown; exit_behaviour = ExitBehaviour::Shutdown;
break 'outer; break 'outer;
@ -438,6 +439,17 @@ impl Vmm {
sender.send(response).map_err(Error::ApiResponseSend)?; sender.send(response).map_err(Error::ApiResponseSend)?;
} }
ApiRequest::VmmShutdown(sender) => {
let response = self
.vmm_shutdown()
.map_err(ApiError::VmmShutdown)
.map(|_| ApiResponsePayload::Empty);
sender.send(response).map_err(Error::ApiResponseSend)?;
exit_behaviour = ExitBehaviour::Shutdown;
break 'outer;
}
} }
} }
} }