From 8fa1098629d5421dd17786809432738cfc3fe8fd Mon Sep 17 00:00:00 2001 From: Wei Liu Date: Mon, 20 Jun 2022 13:53:39 +0000 Subject: [PATCH] vmm: switch from lazy_static to once_cell Once_cell does not require using macro and is slated to become part of Rust std at some point. Signed-off-by: Wei Liu --- Cargo.lock | 2 +- vmm/Cargo.toml | 2 +- vmm/src/api/http.rs | 158 +++++++++++++++++++++++++++++++++----------- vmm/src/lib.rs | 2 - 4 files changed, 121 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1083fc764..5363b23aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1378,12 +1378,12 @@ dependencies = [ "gdbstub", "gdbstub_arch", "hypervisor", - "lazy_static", "libc", "linux-loader", "log", "micro_http", "net_util", + "once_cell", "option_parser", "pci", "qcow", diff --git a/vmm/Cargo.toml b/vmm/Cargo.toml index 3f8e296dd..9da99347e 100644 --- a/vmm/Cargo.toml +++ b/vmm/Cargo.toml @@ -29,12 +29,12 @@ event_monitor = { path = "../event_monitor" } gdbstub = { version = "0.6.2", optional = true } gdbstub_arch = { version = "0.2.3", optional = true } hypervisor = { path = "../hypervisor" } -lazy_static = "1.4.0" libc = "0.2.126" linux-loader = { version = "0.4.0", features = ["elf", "bzimage", "pe"] } log = "0.4.17" micro_http = { git = "https://github.com/firecracker-microvm/micro-http", branch = "main" } net_util = { path = "../net_util" } +once_cell = "1.12.0" option_parser = { path = "../option_parser" } pci = { path = "../pci" } qcow = { path = "../qcow" } diff --git a/vmm/src/api/http.rs b/vmm/src/api/http.rs index 17a520657..70749dc36 100644 --- a/vmm/src/api/http.rs +++ b/vmm/src/api/http.rs @@ -8,6 +8,7 @@ use crate::api::{ApiError, ApiRequest, VmAction}; use crate::seccomp_filters::{get_seccomp_filter, Thread}; use crate::{Error as VmmError, Result}; use micro_http::{Body, HttpServer, MediaType, Method, Request, Response, StatusCode, Version}; +use once_cell::sync::Lazy; use seccompiler::{apply_filter, SeccompAction}; use serde_json::Error as SerdeError; use std::collections::HashMap; @@ -129,46 +130,125 @@ macro_rules! endpoint { }; } -lazy_static! { - /// HTTP_ROUTES contain all the cloud-hypervisor HTTP routes. - pub static ref HTTP_ROUTES: HttpRoutes = { - let mut r = HttpRoutes { - routes: HashMap::new(), - }; - - r.routes.insert(endpoint!("/vm.add-device"), Box::new(VmActionHandler::new(VmAction::AddDevice(Arc::default())))); - r.routes.insert(endpoint!("/vm.add-user-device"), Box::new(VmActionHandler::new(VmAction::AddUserDevice(Arc::default())))); - r.routes.insert(endpoint!("/vm.add-disk"), Box::new(VmActionHandler::new(VmAction::AddDisk(Arc::default())))); - r.routes.insert(endpoint!("/vm.add-fs"), Box::new(VmActionHandler::new(VmAction::AddFs(Arc::default())))); - r.routes.insert(endpoint!("/vm.add-net"), Box::new(VmActionHandler::new(VmAction::AddNet(Arc::default())))); - r.routes.insert(endpoint!("/vm.add-pmem"), Box::new(VmActionHandler::new(VmAction::AddPmem(Arc::default())))); - r.routes.insert(endpoint!("/vm.add-vdpa"), Box::new(VmActionHandler::new(VmAction::AddVdpa(Arc::default())))); - r.routes.insert(endpoint!("/vm.add-vsock"), Box::new(VmActionHandler::new(VmAction::AddVsock(Arc::default())))); - r.routes.insert(endpoint!("/vm.boot"), Box::new(VmActionHandler::new(VmAction::Boot))); - r.routes.insert(endpoint!("/vm.counters"), Box::new(VmActionHandler::new(VmAction::Counters))); - r.routes.insert(endpoint!("/vm.create"), Box::new(VmCreate {})); - 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.power-button"), Box::new(VmActionHandler::new(VmAction::PowerButton))); - 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.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.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.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.snapshot"), Box::new(VmActionHandler::new(VmAction::Snapshot(Arc::default())))); - #[cfg(feature = "guest_debug")] - r.routes.insert(endpoint!("/vm.coredump"), Box::new(VmActionHandler::new(VmAction::Coredump(Arc::default())))); - r.routes.insert(endpoint!("/vmm.ping"), Box::new(VmmPing {})); - r.routes.insert(endpoint!("/vmm.shutdown"), Box::new(VmmShutdown {})); - - r +/// HTTP_ROUTES contain all the cloud-hypervisor HTTP routes. +pub static HTTP_ROUTES: Lazy = Lazy::new(|| { + let mut r = HttpRoutes { + routes: HashMap::new(), }; -} + + r.routes.insert( + endpoint!("/vm.add-device"), + Box::new(VmActionHandler::new(VmAction::AddDevice(Arc::default()))), + ); + r.routes.insert( + endpoint!("/vm.add-user-device"), + Box::new(VmActionHandler::new( + VmAction::AddUserDevice(Arc::default()), + )), + ); + r.routes.insert( + endpoint!("/vm.add-disk"), + Box::new(VmActionHandler::new(VmAction::AddDisk(Arc::default()))), + ); + r.routes.insert( + endpoint!("/vm.add-fs"), + Box::new(VmActionHandler::new(VmAction::AddFs(Arc::default()))), + ); + r.routes.insert( + endpoint!("/vm.add-net"), + Box::new(VmActionHandler::new(VmAction::AddNet(Arc::default()))), + ); + r.routes.insert( + endpoint!("/vm.add-pmem"), + Box::new(VmActionHandler::new(VmAction::AddPmem(Arc::default()))), + ); + r.routes.insert( + endpoint!("/vm.add-vdpa"), + Box::new(VmActionHandler::new(VmAction::AddVdpa(Arc::default()))), + ); + r.routes.insert( + endpoint!("/vm.add-vsock"), + Box::new(VmActionHandler::new(VmAction::AddVsock(Arc::default()))), + ); + r.routes.insert( + endpoint!("/vm.boot"), + Box::new(VmActionHandler::new(VmAction::Boot)), + ); + r.routes.insert( + endpoint!("/vm.counters"), + Box::new(VmActionHandler::new(VmAction::Counters)), + ); + r.routes + .insert(endpoint!("/vm.create"), Box::new(VmCreate {})); + 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.power-button"), + Box::new(VmActionHandler::new(VmAction::PowerButton)), + ); + 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.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.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.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.snapshot"), + Box::new(VmActionHandler::new(VmAction::Snapshot(Arc::default()))), + ); + #[cfg(feature = "guest_debug")] + r.routes.insert( + endpoint!("/vm.coredump"), + Box::new(VmActionHandler::new(VmAction::Coredump(Arc::default()))), + ); + r.routes + .insert(endpoint!("/vmm.ping"), Box::new(VmmPing {})); + r.routes + .insert(endpoint!("/vmm.shutdown"), Box::new(VmmShutdown {})); + + r +}); fn handle_http_request( request: &Request, diff --git a/vmm/src/lib.rs b/vmm/src/lib.rs index d61683bd3..ee337a19d 100644 --- a/vmm/src/lib.rs +++ b/vmm/src/lib.rs @@ -6,8 +6,6 @@ #[macro_use] extern crate event_monitor; #[macro_use] -extern crate lazy_static; -#[macro_use] extern crate log; use crate::api::{