2019-09-18 09:14:49 +00:00
|
|
|
// Copyright © 2019 Intel Corporation
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
2019-11-26 16:46:10 +00:00
|
|
|
use crate::api::http_endpoint::{
|
2020-04-14 08:13:48 +00:00
|
|
|
VmActionHandler, VmAddDevice, VmAddDisk, VmAddFs, VmAddNet, VmAddPmem, VmCreate, VmInfo,
|
|
|
|
VmRemoveDevice, VmResize, VmRestore, VmSnapshot, VmmPing, VmmShutdown,
|
2019-11-26 16:46:10 +00:00
|
|
|
};
|
2019-09-26 23:37:06 +00:00
|
|
|
use crate::api::{ApiRequest, VmAction};
|
2020-03-20 16:57:03 +00:00
|
|
|
use crate::seccomp_filters::{get_seccomp_filter, Thread};
|
2019-09-18 09:14:49 +00:00
|
|
|
use crate::{Error, Result};
|
2019-11-08 21:59:08 +00:00
|
|
|
use micro_http::{HttpServer, MediaType, Request, Response, StatusCode, Version};
|
2020-03-20 16:31:15 +00:00
|
|
|
use seccomp::{SeccompFilter, SeccompLevel};
|
2019-09-18 09:14:49 +00:00
|
|
|
use std::collections::HashMap;
|
2019-11-08 21:59:08 +00:00
|
|
|
use std::path::PathBuf;
|
2019-09-18 09:14:49 +00:00
|
|
|
use std::sync::mpsc::Sender;
|
|
|
|
use std::thread;
|
|
|
|
use vmm_sys_util::eventfd::EventFd;
|
|
|
|
|
|
|
|
const HTTP_ROOT: &str = "/api/v1";
|
|
|
|
|
|
|
|
/// An HTTP endpoint handler interface
|
|
|
|
pub trait EndpointHandler: Sync + Send {
|
|
|
|
/// Handles an HTTP request.
|
|
|
|
/// After parsing the request, the handler could decide to send an
|
|
|
|
/// associated API request down to the VMM API server to e.g. create
|
|
|
|
/// or start a VM. The request will block waiting for an answer from the
|
|
|
|
/// API server and translate that into an HTTP response.
|
|
|
|
fn handle_request(
|
|
|
|
&self,
|
|
|
|
req: &Request,
|
|
|
|
api_notifier: EventFd,
|
|
|
|
api_sender: Sender<ApiRequest>,
|
|
|
|
) -> Response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An HTTP routes structure.
|
|
|
|
pub struct HttpRoutes {
|
|
|
|
/// routes is a hash table mapping endpoint URIs to their endpoint handlers.
|
|
|
|
pub routes: HashMap<String, Box<dyn EndpointHandler + Sync + Send>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! endpoint {
|
|
|
|
($path:expr) => {
|
|
|
|
format!("{}{}", HTTP_ROOT, $path)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
/// HTTP_ROUTES contain all the cloud-hypervisor HTTP routes.
|
|
|
|
pub static ref HTTP_ROUTES: HttpRoutes = {
|
2019-09-26 23:37:06 +00:00
|
|
|
let mut r = HttpRoutes {
|
2019-09-18 09:14:49 +00:00
|
|
|
routes: HashMap::new(),
|
|
|
|
};
|
|
|
|
|
2019-09-26 23:37:06 +00:00
|
|
|
r.routes.insert(endpoint!("/vm.create"), Box::new(VmCreate {}));
|
|
|
|
r.routes.insert(endpoint!("/vm.boot"), Box::new(VmActionHandler::new(VmAction::Boot)));
|
2019-10-01 15:24:39 +00:00
|
|
|
r.routes.insert(endpoint!("/vm.delete"), Box::new(VmActionHandler::new(VmAction::Delete)));
|
2019-10-01 12:07:15 +00:00
|
|
|
r.routes.insert(endpoint!("/vm.info"), Box::new(VmInfo {}));
|
2019-10-10 15:16:58 +00:00
|
|
|
r.routes.insert(endpoint!("/vm.pause"), Box::new(VmActionHandler::new(VmAction::Pause)));
|
2019-10-10 15:34:55 +00:00
|
|
|
r.routes.insert(endpoint!("/vm.resume"), Box::new(VmActionHandler::new(VmAction::Resume)));
|
2019-09-26 23:37:06 +00:00
|
|
|
r.routes.insert(endpoint!("/vm.shutdown"), Box::new(VmActionHandler::new(VmAction::Shutdown)));
|
|
|
|
r.routes.insert(endpoint!("/vm.reboot"), Box::new(VmActionHandler::new(VmAction::Reboot)));
|
2019-11-24 18:37:08 +00:00
|
|
|
r.routes.insert(endpoint!("/vm.snapshot"), Box::new(VmSnapshot {}));
|
2020-02-26 00:00:26 +00:00
|
|
|
r.routes.insert(endpoint!("/vm.restore"), Box::new(VmRestore {}));
|
2019-10-08 13:23:29 +00:00
|
|
|
r.routes.insert(endpoint!("/vmm.shutdown"), Box::new(VmmShutdown {}));
|
2019-11-04 18:06:45 +00:00
|
|
|
r.routes.insert(endpoint!("/vmm.ping"), Box::new(VmmPing {}));
|
2019-11-26 16:46:10 +00:00
|
|
|
r.routes.insert(endpoint!("/vm.resize"), Box::new(VmResize {}));
|
2020-02-27 13:00:46 +00:00
|
|
|
r.routes.insert(endpoint!("/vm.add-device"), Box::new(VmAddDevice {}));
|
2020-03-09 10:49:15 +00:00
|
|
|
r.routes.insert(endpoint!("/vm.remove-device"), Box::new(VmRemoveDevice {}));
|
2020-03-23 16:21:58 +00:00
|
|
|
r.routes.insert(endpoint!("/vm.add-disk"), Box::new(VmAddDisk {}));
|
2020-04-14 08:13:48 +00:00
|
|
|
r.routes.insert(endpoint!("/vm.add-fs"), Box::new(VmAddFs {}));
|
2020-03-23 16:21:58 +00:00
|
|
|
r.routes.insert(endpoint!("/vm.add-pmem"), Box::new(VmAddPmem {}));
|
2020-03-23 16:21:58 +00:00
|
|
|
r.routes.insert(endpoint!("/vm.add-net"), Box::new(VmAddNet {}));
|
2019-09-26 23:37:06 +00:00
|
|
|
|
2019-09-18 09:14:49 +00:00
|
|
|
r
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-11-08 21:59:08 +00:00
|
|
|
fn handle_http_request(
|
|
|
|
request: &Request,
|
|
|
|
api_notifier: &EventFd,
|
|
|
|
api_sender: &Sender<ApiRequest>,
|
|
|
|
) -> Response {
|
|
|
|
let path = request.uri().get_abs_path().to_string();
|
|
|
|
let mut response = match HTTP_ROUTES.routes.get(&path) {
|
|
|
|
Some(route) => match api_notifier.try_clone() {
|
|
|
|
Ok(notifier) => route.handle_request(&request, notifier, api_sender.clone()),
|
|
|
|
Err(_) => Response::new(Version::Http11, StatusCode::InternalServerError),
|
|
|
|
},
|
|
|
|
None => Response::new(Version::Http11, StatusCode::NotFound),
|
|
|
|
};
|
2019-09-18 09:14:49 +00:00
|
|
|
|
2019-11-08 21:59:08 +00:00
|
|
|
response.set_server("Cloud Hypervisor API");
|
|
|
|
response.set_content_type(MediaType::ApplicationJson);
|
|
|
|
response
|
2019-09-18 09:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn start_http_thread(
|
|
|
|
path: &str,
|
|
|
|
api_notifier: EventFd,
|
|
|
|
api_sender: Sender<ApiRequest>,
|
2020-03-20 16:31:15 +00:00
|
|
|
seccomp_level: &SeccompLevel,
|
2019-09-18 09:14:49 +00:00
|
|
|
) -> Result<thread::JoinHandle<Result<()>>> {
|
2019-11-08 21:59:08 +00:00
|
|
|
std::fs::remove_file(path).unwrap_or_default();
|
|
|
|
let socket_path = PathBuf::from(path);
|
2019-09-18 09:14:49 +00:00
|
|
|
|
2020-03-20 16:31:15 +00:00
|
|
|
// Retrieve seccomp filter for API thread
|
|
|
|
let api_seccomp_filter =
|
2020-03-20 16:57:03 +00:00
|
|
|
get_seccomp_filter(seccomp_level, Thread::Api).map_err(Error::CreateSeccompFilter)?;
|
2020-03-20 16:31:15 +00:00
|
|
|
|
2019-09-18 09:14:49 +00:00
|
|
|
thread::Builder::new()
|
|
|
|
.name("http-server".to_string())
|
|
|
|
.spawn(move || {
|
2020-03-20 16:31:15 +00:00
|
|
|
// Apply seccomp filter for API thread.
|
|
|
|
SeccompFilter::apply(api_seccomp_filter).map_err(Error::ApplySeccompFilter)?;
|
|
|
|
|
2019-11-08 21:59:08 +00:00
|
|
|
let mut server = HttpServer::new(socket_path).unwrap();
|
|
|
|
server.start_server().unwrap();
|
|
|
|
loop {
|
|
|
|
match server.requests() {
|
|
|
|
Ok(request_vec) => {
|
|
|
|
for server_request in request_vec {
|
|
|
|
server
|
|
|
|
.respond(server_request.process(|request| {
|
|
|
|
handle_http_request(request, &api_notifier, &api_sender)
|
|
|
|
}))
|
|
|
|
.or_else(|e| {
|
|
|
|
error!("HTTP server error on response: {}", e);
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!(
|
|
|
|
"HTTP server error on retrieving incoming request. Error: {}",
|
|
|
|
e
|
|
|
|
);
|
2019-09-18 09:14:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map_err(Error::HttpThreadSpawn)
|
|
|
|
}
|