vmm: Implement VM resume

To resume a VM, we unpark all its vCPU threads.

Fixes: #333

Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
This commit is contained in:
Samuel Ortiz 2019-10-10 17:34:55 +02:00 committed by Sebastien Boeuf
parent 4ac0cb9cff
commit dbbd04a4cf
6 changed files with 72 additions and 2 deletions

View File

@ -59,6 +59,7 @@ lazy_static! {
r.routes.insert(endpoint!("/vm.delete"), Box::new(VmActionHandler::new(VmAction::Delete)));
r.routes.insert(endpoint!("/vm.info"), Box::new(VmInfo {}));
r.routes.insert(endpoint!("/vm.pause"), Box::new(VmActionHandler::new(VmAction::Pause)));
r.routes.insert(endpoint!("/vm.resume"), Box::new(VmActionHandler::new(VmAction::Resume)));
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!("/vmm.shutdown"), Box::new(VmmShutdown {}));

View File

@ -5,8 +5,8 @@
use crate::api::http::EndpointHandler;
use crate::api::{
vm_boot, vm_create, vm_delete, vm_info, vm_pause, vm_reboot, vm_shutdown, vmm_shutdown,
ApiError, ApiRequest, ApiResult, VmAction, VmConfig,
vm_boot, vm_create, vm_delete, vm_info, vm_pause, vm_reboot, vm_resume, vm_shutdown,
vmm_shutdown, ApiError, ApiRequest, ApiResult, VmAction, VmConfig,
};
use micro_http::{Body, Method, Request, Response, StatusCode, Version};
use serde_json::Error as SerdeError;
@ -32,6 +32,9 @@ pub enum HttpError {
/// Could not pause the VM
VmPause(ApiError),
/// Could not pause the VM
VmResume(ApiError),
/// Could not shut a VM down
VmShutdown(ApiError),
@ -107,6 +110,7 @@ impl VmActionHandler {
VmAction::Shutdown => vm_shutdown,
VmAction::Reboot => vm_reboot,
VmAction::Pause => vm_pause,
VmAction::Resume => vm_resume,
});
VmActionHandler { action_fn }
@ -127,6 +131,7 @@ impl EndpointHandler for VmActionHandler {
ApiError::VmShutdown(_) => HttpError::VmShutdown(e),
ApiError::VmReboot(_) => HttpError::VmReboot(e),
ApiError::VmPause(_) => HttpError::VmPause(e),
ApiError::VmResume(_) => HttpError::VmResume(e),
_ => HttpError::VmAction(e),
}) {
Ok(_) => Response::new(Version::Http11, StatusCode::OK),

View File

@ -79,6 +79,9 @@ pub enum ApiError {
/// The VM could not be paused.
VmPause(VmError),
/// The VM could not resume.
VmResume(VmError),
/// The VM is not booted.
VmNotBooted,
@ -138,6 +141,9 @@ pub enum ApiRequest {
/// Pause a VM.
VmPause(Sender<ApiResponse>),
/// Resume a VM.
VmResume(Sender<ApiResponse>),
/// Shut the previously booted virtual machine down.
/// If the VM was not previously booted or created, the VMM API server
/// will send a VmShutdown error back.
@ -190,6 +196,9 @@ pub enum VmAction {
/// Pause a VM
Pause,
/// Resume a VM
Resume,
}
fn vm_action(api_evt: EventFd, api_sender: Sender<ApiRequest>, action: VmAction) -> ApiResult<()> {
@ -201,6 +210,7 @@ fn vm_action(api_evt: EventFd, api_sender: Sender<ApiRequest>, action: VmAction)
VmAction::Shutdown => ApiRequest::VmShutdown(response_sender),
VmAction::Reboot => ApiRequest::VmReboot(response_sender),
VmAction::Pause => ApiRequest::VmPause(response_sender),
VmAction::Resume => ApiRequest::VmResume(response_sender),
};
// Send the VM request.
@ -232,6 +242,10 @@ pub fn vm_pause(api_evt: EventFd, api_sender: Sender<ApiRequest>) -> ApiResult<(
vm_action(api_evt, api_sender, VmAction::Pause)
}
pub fn vm_resume(api_evt: EventFd, api_sender: Sender<ApiRequest>) -> ApiResult<()> {
vm_action(api_evt, api_sender, VmAction::Resume)
}
pub fn vm_info(api_evt: EventFd, api_sender: Sender<ApiRequest>) -> ApiResult<VmInfo> {
let (response_sender, response_receiver) = channel();

View File

@ -95,6 +95,21 @@ paths:
description: The VM instance could not pause because it is not booted.
content: {}
/vm.resume:
put:
summary: Resume a previously paused VM instance.
operationId: resumeVM
responses:
201:
description: The VM instance successfully paused.
content: {}
404:
description: The VM instance could not resume because it is not booted yet
content: {}
405:
description: The VM instance could not resume because it is not paused.
content: {}
/vm.shutdown:
put:
summary: Shut the VM instance down.

View File

@ -235,6 +235,14 @@ impl Vmm {
}
}
fn vm_resume(&mut self) -> result::Result<(), VmError> {
if let Some(ref mut vm) = self.vm {
vm.resume()
} else {
Err(VmError::VmNotBooted)
}
}
fn vm_shutdown(&mut self) -> result::Result<(), VmError> {
if let Some(ref mut vm) = self.vm.take() {
vm.shutdown()
@ -430,6 +438,14 @@ impl Vmm {
sender.send(response).map_err(Error::ApiResponseSend)?;
}
ApiRequest::VmResume(sender) => {
let response = self
.vm_resume()
.map_err(ApiError::VmResume)
.map(|_| ApiResponsePayload::Empty);
sender.send(response).map_err(Error::ApiResponseSend)?;
}
ApiRequest::VmmShutdown(sender) => {
let response = self
.vmm_shutdown()

View File

@ -831,6 +831,25 @@ impl Vm {
Ok(())
}
pub fn resume(&mut self) -> Result<()> {
// Toggle the vCPUs pause boolean
self.vcpus_pause_signalled.store(false, Ordering::SeqCst);
// Unpark all the VCPU threads.
// Once unparked, the next thing they will do is checking for the pause
// boolean. Since it'll be set to false, they will exit their pause loop
// and go back to vmx root.
for vcpu_thread in self.threads.iter() {
vcpu_thread.thread().unpark();
}
// And we're back to Booted state.
let mut state = self.state.try_write().map_err(|_| Error::PoisonedState)?;
*state = VmState::Booted;
Ok(())
}
fn os_signal_handler(signals: Signals, console_input_clone: Arc<Console>) {
for signal in signals.forever() {
if signal == SIGWINCH {