vmm: api: Implement API support for migration

Add API entry points with stub implementation for sending and receiving
a VM from one VMM to another.

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2020-11-02 15:18:31 +00:00 committed by Samuel Ortiz
parent aa98589bb4
commit 7ac764518c
4 changed files with 114 additions and 5 deletions

View File

@ -100,6 +100,12 @@ pub enum HttpError {
/// Could not get counters from VM /// Could not get counters from VM
VmCounters(ApiError), VmCounters(ApiError),
/// Error setting up migration received
VmReceiveMigration(ApiError),
/// Error setting up migration sender
VmSendMigration(ApiError),
} }
impl From<serde_json::Error> for HttpError { impl From<serde_json::Error> for HttpError {
@ -205,11 +211,13 @@ 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.pause"), Box::new(VmActionHandler::new(VmAction::Pause))); r.routes.insert(endpoint!("/vm.pause"), Box::new(VmActionHandler::new(VmAction::Pause)));
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!("/vm.receive-migration"), Box::new(VmActionHandler::new(VmAction::ReceiveMigration(Arc::default()))));
r.routes.insert(endpoint!("/vm.remove-device"), Box::new(VmActionHandler::new(VmAction::RemoveDevice(Arc::default())))); r.routes.insert(endpoint!("/vm.remove-device"), Box::new(VmActionHandler::new(VmAction::RemoveDevice(Arc::default()))));
r.routes.insert(endpoint!("/vm.resize"), Box::new(VmActionHandler::new(VmAction::Resize(Arc::default())))); r.routes.insert(endpoint!("/vm.resize"), Box::new(VmActionHandler::new(VmAction::Resize(Arc::default()))));
r.routes.insert(endpoint!("/vm.resize-zone"), Box::new(VmActionHandler::new(VmAction::ResizeZone(Arc::default())))); r.routes.insert(endpoint!("/vm.resize-zone"), Box::new(VmActionHandler::new(VmAction::ResizeZone(Arc::default()))));
r.routes.insert(endpoint!("/vm.restore"), Box::new(VmActionHandler::new(VmAction::Restore(Arc::default())))); r.routes.insert(endpoint!("/vm.restore"), Box::new(VmActionHandler::new(VmAction::Restore(Arc::default()))));
r.routes.insert(endpoint!("/vm.resume"), Box::new(VmActionHandler::new(VmAction::Resume))); r.routes.insert(endpoint!("/vm.resume"), Box::new(VmActionHandler::new(VmAction::Resume)));
r.routes.insert(endpoint!("/vm.send-migration"), Box::new(VmActionHandler::new(VmAction::SendMigration(Arc::default()))));
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.snapshot"), Box::new(VmActionHandler::new(VmAction::Snapshot(Arc::default())))); r.routes.insert(endpoint!("/vm.snapshot"), Box::new(VmActionHandler::new(VmAction::Snapshot(Arc::default()))));
r.routes.insert(endpoint!("/vmm.ping"), Box::new(VmmPing {})); r.routes.insert(endpoint!("/vmm.ping"), Box::new(VmmPing {}));

View File

@ -6,9 +6,9 @@
use crate::api::http::{error_response, EndpointHandler, HttpError}; use crate::api::http::{error_response, EndpointHandler, HttpError};
use crate::api::{ use crate::api::{
vm_add_device, vm_add_disk, vm_add_fs, vm_add_net, vm_add_pmem, vm_add_vsock, vm_boot, vm_add_device, vm_add_disk, vm_add_fs, vm_add_net, vm_add_pmem, vm_add_vsock, vm_boot,
vm_counters, vm_create, vm_delete, vm_info, vm_pause, vm_reboot, vm_remove_device, vm_resize, vm_counters, vm_create, vm_delete, vm_info, vm_pause, vm_reboot, vm_receive_migration,
vm_resize_zone, vm_restore, vm_resume, vm_shutdown, vm_snapshot, vmm_ping, vmm_shutdown, vm_remove_device, vm_resize, vm_resize_zone, vm_restore, vm_resume, vm_send_migration,
ApiRequest, VmAction, VmConfig, vm_shutdown, vm_snapshot, vmm_ping, vmm_shutdown, ApiRequest, VmAction, VmConfig,
}; };
use micro_http::{Body, Method, Request, Response, StatusCode, Version}; use micro_http::{Body, Method, Request, Response, StatusCode, Version};
use std::sync::mpsc::Sender; use std::sync::mpsc::Sender;
@ -153,6 +153,20 @@ impl EndpointHandler for VmActionHandler {
) )
.map_err(HttpError::VmSnapshot), .map_err(HttpError::VmSnapshot),
ReceiveMigration(_) => vm_receive_migration(
api_notifier,
api_sender,
Arc::new(serde_json::from_slice(body.raw())?),
)
.map_err(HttpError::VmReceiveMigration),
SendMigration(_) => vm_send_migration(
api_notifier,
api_sender,
Arc::new(serde_json::from_slice(body.raw())?),
)
.map_err(HttpError::VmSendMigration),
_ => Err(HttpError::BadRequest), _ => Err(HttpError::BadRequest),
} }
} else { } else {

View File

@ -44,6 +44,7 @@ use micro_http::Body;
use std::io; use std::io;
use std::sync::mpsc::{channel, RecvError, SendError, Sender}; use std::sync::mpsc::{channel, RecvError, SendError, Sender};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use vm_migration::MigratableError;
use vmm_sys_util::eventfd::EventFd; use vmm_sys_util::eventfd::EventFd;
/// API errors are sent back from the VMM API server through the ApiResponse. /// API errors are sent back from the VMM API server through the ApiResponse.
@ -138,6 +139,12 @@ pub enum ApiError {
/// The vsock device could not be added to the VM. /// The vsock device could not be added to the VM.
VmAddVsock(VmError), VmAddVsock(VmError),
/// Error starting migration receiever
VmReceiveMigration(MigratableError),
/// Error starting migration sender
VmSendMigration(MigratableError),
} }
pub type ApiResult<T> = std::result::Result<T, ApiError>; pub type ApiResult<T> = std::result::Result<T, ApiError>;
@ -177,6 +184,18 @@ pub struct VmSnapshotConfig {
pub destination_url: String, pub destination_url: String,
} }
#[derive(Clone, Deserialize, Serialize, Default)]
pub struct VmReceiveMigrationData {
/// URL for the reception of migration state
pub receiver_url: String,
}
#[derive(Clone, Deserialize, Serialize, Default)]
pub struct VmSendMigrationData {
/// URL to migrate the VM to
pub destination_url: String,
}
pub enum ApiResponsePayload { pub enum ApiResponsePayload {
/// No data is sent on the channel. /// No data is sent on the channel.
Empty, Empty,
@ -275,6 +294,12 @@ pub enum ApiRequest {
/// Restore from a VM snapshot /// Restore from a VM snapshot
VmRestore(Arc<RestoreConfig>, Sender<ApiResponse>), VmRestore(Arc<RestoreConfig>, Sender<ApiResponse>),
/// Incoming migration
VmReceiveMigration(Arc<VmReceiveMigrationData>, Sender<ApiResponse>),
/// Outgoing migration
VmSendMigration(Arc<VmSendMigrationData>, Sender<ApiResponse>),
} }
pub fn vm_create( pub fn vm_create(
@ -352,6 +377,12 @@ pub enum VmAction {
/// Snapshot VM /// Snapshot VM
Snapshot(Arc<VmSnapshotConfig>), Snapshot(Arc<VmSnapshotConfig>),
/// Incoming migration
ReceiveMigration(Arc<VmReceiveMigrationData>),
/// Outgoing migration
SendMigration(Arc<VmSendMigrationData>),
} }
fn vm_action( fn vm_action(
@ -381,6 +412,8 @@ fn vm_action(
ResizeZone(v) => ApiRequest::VmResizeZone(v, response_sender), ResizeZone(v) => ApiRequest::VmResizeZone(v, response_sender),
Restore(v) => ApiRequest::VmRestore(v, response_sender), Restore(v) => ApiRequest::VmRestore(v, response_sender),
Snapshot(v) => ApiRequest::VmSnapshot(v, response_sender), Snapshot(v) => ApiRequest::VmSnapshot(v, response_sender),
ReceiveMigration(v) => ApiRequest::VmReceiveMigration(v, response_sender),
SendMigration(v) => ApiRequest::VmSendMigration(v, response_sender),
}; };
// Send the VM request. // Send the VM request.
@ -424,6 +457,22 @@ pub fn vm_counters(api_evt: EventFd, api_sender: Sender<ApiRequest>) -> ApiResul
vm_action(api_evt, api_sender, VmAction::Counters) vm_action(api_evt, api_sender, VmAction::Counters)
} }
pub fn vm_receive_migration(
api_evt: EventFd,
api_sender: Sender<ApiRequest>,
data: Arc<VmReceiveMigrationData>,
) -> ApiResult<Option<Body>> {
vm_action(api_evt, api_sender, VmAction::ReceiveMigration(data))
}
pub fn vm_send_migration(
api_evt: EventFd,
api_sender: Sender<ApiRequest>,
data: Arc<VmSendMigrationData>,
) -> ApiResult<Option<Body>> {
vm_action(api_evt, api_sender, VmAction::SendMigration(data))
}
pub fn vm_snapshot( pub fn vm_snapshot(
api_evt: EventFd, api_evt: EventFd,
api_sender: Sender<ApiRequest>, api_sender: Sender<ApiRequest>,

View File

@ -22,7 +22,10 @@ extern crate vmm_sys_util;
#[macro_use] #[macro_use]
extern crate credibility; extern crate credibility;
use crate::api::{ApiError, ApiRequest, ApiResponse, ApiResponsePayload, VmInfo, VmmPingResponse}; use crate::api::{
ApiError, ApiRequest, ApiResponse, ApiResponsePayload, VmInfo, VmReceiveMigrationData,
VmSendMigrationData, VmmPingResponse,
};
use crate::config::{ use crate::config::{
DeviceConfig, DiskConfig, FsConfig, NetConfig, PmemConfig, RestoreConfig, VmConfig, VsockConfig, DeviceConfig, DiskConfig, FsConfig, NetConfig, PmemConfig, RestoreConfig, VmConfig, VsockConfig,
}; };
@ -39,7 +42,7 @@ use std::sync::mpsc::{Receiver, RecvError, SendError, Sender};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::{result, thread}; use std::{result, thread};
use thiserror::Error; use thiserror::Error;
use vm_migration::{Pausable, Snapshottable, Transportable}; use vm_migration::{MigratableError, Pausable, Snapshottable, Transportable};
use vmm_sys_util::eventfd::EventFd; use vmm_sys_util::eventfd::EventFd;
pub mod api; pub mod api;
@ -639,6 +642,25 @@ impl Vmm {
} }
} }
fn vm_receive_migration(
&mut self,
receive_data_migration: VmReceiveMigrationData,
) -> result::Result<(), MigratableError> {
info!(
"Migration Receiver: {}",
receive_data_migration.receiver_url
);
Ok(())
}
fn vm_send_migration(
&mut self,
send_data_migration: VmSendMigrationData,
) -> result::Result<(), MigratableError> {
info!("Migration Sender: {}", send_data_migration.destination_url);
Ok(())
}
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;
@ -878,6 +900,22 @@ impl Vmm {
sender.send(response).map_err(Error::ApiResponseSend)?; sender.send(response).map_err(Error::ApiResponseSend)?;
} }
ApiRequest::VmReceiveMigration(receive_migration_data, sender) => {
let response = self
.vm_receive_migration(
receive_migration_data.as_ref().clone(),
)
.map_err(ApiError::VmReceiveMigration)
.map(|_| ApiResponsePayload::Empty);
sender.send(response).map_err(Error::ApiResponseSend)?;
}
ApiRequest::VmSendMigration(send_migration_data, sender) => {
let response = self
.vm_send_migration(send_migration_data.as_ref().clone())
.map_err(ApiError::VmSendMigration)
.map(|_| ApiResponsePayload::Empty);
sender.send(response).map_err(Error::ApiResponseSend)?;
}
} }
} }
} }