Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cpu-template-helper/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ fn run(cli: Cli) -> Result<(), HelperError> {
let (vmm, vm_resources) = utils::build_microvm_from_config(config, template)?;

let cpu_template = vm_resources
.machine_config
.machine_spec
.cpu_template
.get_cpu_template()?
.into_owned();
Expand Down
6 changes: 3 additions & 3 deletions src/cpu-template-helper/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub fn add_suffix(path: &Path, suffix: &str) -> PathBuf {
pub mod tests {
use std::fmt::Display;

use vmm::resources::VmmConfig;
use vmm::resources::VmmSpec;

use super::*;

Expand Down Expand Up @@ -217,8 +217,8 @@ pub mod tests {
assert!(kernel.as_file().metadata().unwrap().len() > 0);
// Ensure the rootfs exists and it is empty.
assert_eq!(rootfs.as_file().metadata().unwrap().len(), 0);
// Ensure the generated config is valid as `VmmConfig`.
serde_json::from_str::<VmmConfig>(&config).unwrap();
// Ensure the generated config is valid as `VmmSpec`.
serde_json::from_str::<VmmSpec>(&config).unwrap();
}
// Ensure the temporary mock resources are deleted.
assert!(!kernel_path.exists());
Expand Down
51 changes: 31 additions & 20 deletions src/firecracker/src/api_server/parsed_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl TryFrom<&Request> for ParsedRequest {
(Method::Get, "balloon", None) => parse_get_balloon(path_tokens),
(Method::Get, "version", None) => parse_get_version(),
(Method::Get, "vm", None) if path_tokens.next() == Some("config") => {
Ok(ParsedRequest::new_sync(VmmAction::GetFullVmConfig))
Ok(ParsedRequest::new_sync(VmmAction::GetFullVmSpec))
}
(Method::Get, "machine-config", None) => parse_get_machine_config(),
(Method::Get, "mmds", None) => parse_get_mmds(),
Expand Down Expand Up @@ -179,12 +179,12 @@ impl ParsedRequest {
info!("The request was executed successfully. Status code: 204 No Content.");
Response::new(Version::Http11, StatusCode::NoContent)
}
VmmData::MachineConfiguration(machine_config) => {
Self::success_response_with_data(machine_config)
VmmData::MachineSpec(machine_spec) => {
Self::success_response_with_data(machine_spec)
}
VmmData::MmdsValue(value) => Self::success_response_with_mmds_value(value),
VmmData::BalloonConfig(balloon_config) => {
Self::success_response_with_data(balloon_config)
VmmData::BalloonSpec(balloon_spec) => {
Self::success_response_with_data(balloon_spec)
}
VmmData::BalloonStats(stats) => Self::success_response_with_data(stats),
VmmData::VirtioMemStatus(data) => Self::success_response_with_data(data),
Expand All @@ -195,7 +195,7 @@ impl ParsedRequest {
VmmData::VmmVersion(version) => Self::success_response_with_data(
&serde_json::json!({ "firecracker_version": version.as_str() }),
),
VmmData::FullVmConfig(config) => Self::success_response_with_data(config),
VmmData::FullVmSpec(vmm_spec) => Self::success_response_with_data(vmm_spec),
},
Err(vmm_action_error) => {
let mut response = match vmm_action_error {
Expand Down Expand Up @@ -223,8 +223,19 @@ impl ParsedRequest {
}

/// Helper function to avoid boiler-plate code.
pub(crate) fn new_sync(vmm_action: VmmAction) -> ParsedRequest {
ParsedRequest::new(RequestAction::Sync(Box::new(vmm_action)))
pub(crate) fn new_sync<T>(vmm_action: T) -> ParsedRequest
where
T: Into<VmmAction>,
{
ParsedRequest::new(RequestAction::Sync(Box::new(vmm_action.into())))
}

pub(crate) fn new_stateless<F, A>(entrypoint: F, args: A) -> ParsedRequest
where
F: FnOnce(A) -> VmmAction,
A: vmm::vmm_config::StatelessArgs,
{
ParsedRequest::new_sync(vmm::vmm_config::VmmActionPayload::new(entrypoint, args))
}
}

Expand Down Expand Up @@ -342,11 +353,11 @@ pub mod tests {
use vmm::builder::StartMicrovmError;
use vmm::cpu_config::templates::test_utils::build_test_template;
use vmm::devices::virtio::balloon::device::HintingStatus;
use vmm::resources::VmmConfig;
use vmm::resources::VmmSpec;
use vmm::rpc_interface::VmmActionError;
use vmm::vmm_config::balloon::{BalloonDeviceConfig, BalloonStats};
use vmm::vmm_config::balloon::{BalloonDeviceSpec, BalloonStats};
use vmm::vmm_config::instance_info::InstanceInfo;
use vmm::vmm_config::machine_config::MachineConfig;
use vmm::vmm_config::machine_config::MachineSpec;

use super::*;

Expand Down Expand Up @@ -581,8 +592,8 @@ pub mod tests {
let data = Ok(vmm_data);
let mut buf = Cursor::new(vec![0]);
let expected_response = match data.as_ref().unwrap() {
VmmData::BalloonConfig(cfg) => {
http_response(&serde_json::to_string(cfg).unwrap(), 200)
VmmData::BalloonSpec(spec) => {
http_response(&serde_json::to_string(spec).unwrap(), 200)
}
VmmData::BalloonStats(stats) => {
http_response(&serde_json::to_string(stats).unwrap(), 200)
Expand All @@ -594,11 +605,11 @@ pub mod tests {
http_response(&serde_json::to_string(status).unwrap(), 200)
}
VmmData::Empty => http_response("", 204),
VmmData::FullVmConfig(cfg) => {
http_response(&serde_json::to_string(cfg).unwrap(), 200)
VmmData::FullVmSpec(spec) => {
http_response(&serde_json::to_string(spec).unwrap(), 200)
}
VmmData::MachineConfiguration(cfg) => {
http_response(&serde_json::to_string(cfg).unwrap(), 200)
VmmData::MachineSpec(spec) => {
http_response(&serde_json::to_string(spec).unwrap(), 200)
}
VmmData::MmdsValue(value) => {
http_response(&serde_json::to_string(value).unwrap(), 200)
Expand All @@ -616,7 +627,7 @@ pub mod tests {
assert_eq!(buf.into_inner(), expected_response.as_bytes());
};

verify_ok_response_with(VmmData::BalloonConfig(BalloonDeviceConfig::default()));
verify_ok_response_with(VmmData::BalloonSpec(BalloonDeviceSpec::default()));
verify_ok_response_with(VmmData::BalloonStats(BalloonStats {
swap_in: Some(1),
swap_out: Some(1),
Expand All @@ -626,8 +637,8 @@ pub mod tests {
..Default::default()
}));
verify_ok_response_with(VmmData::Empty);
verify_ok_response_with(VmmData::FullVmConfig(VmmConfig::default()));
verify_ok_response_with(VmmData::MachineConfiguration(MachineConfig::default()));
verify_ok_response_with(VmmData::FullVmSpec(VmmSpec::default()));
verify_ok_response_with(VmmData::MachineSpec(MachineSpec::default()));
verify_ok_response_with(VmmData::MmdsValue(serde_json::from_str("{}").unwrap()));
verify_ok_response_with(VmmData::InstanceInformation(InstanceInfo::default()));
verify_ok_response_with(VmmData::VmmVersion(String::default()));
Expand Down
42 changes: 21 additions & 21 deletions src/firecracker/src/api_server/request/balloon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

use micro_http::{Method, StatusCode};
use vmm::rpc_interface::VmmAction;
use vmm::vmm_config::balloon::{
BalloonDeviceConfig, BalloonUpdateConfig, BalloonUpdateStatsConfig,
};
use vmm::vmm_config::balloon::{BalloonDeviceSpec, BalloonUpdateSpec, BalloonUpdateStatsSpec};

use super::super::parsed_request::{ParsedRequest, RequestError};
use super::Body;
Expand Down Expand Up @@ -39,14 +37,15 @@ where
StatusCode::BadRequest,
format!("Unrecognized GET request path `{}`.", stats_path),
)),
None => Ok(ParsedRequest::new_sync(VmmAction::GetBalloonConfig)),
None => Ok(ParsedRequest::new_sync(VmmAction::GetBalloonSpec)),
}
}

pub(crate) fn parse_put_balloon(body: &Body) -> Result<ParsedRequest, RequestError> {
Ok(ParsedRequest::new_sync(VmmAction::SetBalloonDevice(
serde_json::from_slice::<BalloonDeviceConfig>(body.raw())?,
)))
Ok(ParsedRequest::new_stateless(
VmmAction::SetBalloonDevice,
serde_json::from_slice::<BalloonDeviceSpec>(body.raw())?,
))
}

fn parse_patch_hinting<'a, T>(
Expand All @@ -64,9 +63,10 @@ where
Some(b) => serde_json::from_slice(b.raw())?,
};

Ok(ParsedRequest::new_sync(VmmAction::StartFreePageHinting(
Ok(ParsedRequest::new_stateless(
VmmAction::StartFreePageHinting,
cmd,
)))
))
}
Some("stop") => Ok(ParsedRequest::new_sync(VmmAction::StopFreePageHinting)),
Some(stats_path) => Err(RequestError::Generic(
Expand All @@ -88,15 +88,15 @@ where
T: Iterator<Item = &'a str>,
{
match (path_tokens.next(), body) {
(Some("statistics"), Some(body)) => {
Ok(ParsedRequest::new_sync(VmmAction::UpdateBalloonStatistics(
serde_json::from_slice::<BalloonUpdateStatsConfig>(body.raw())?,
)))
}
(Some("statistics"), Some(body)) => Ok(ParsedRequest::new_stateless(
VmmAction::UpdateBalloonStatistics,
serde_json::from_slice::<BalloonUpdateStatsSpec>(body.raw())?,
)),
(Some("hinting"), body) => parse_patch_hinting(body, path_tokens),
(_, Some(body)) => Ok(ParsedRequest::new_sync(VmmAction::UpdateBalloon(
serde_json::from_slice::<BalloonUpdateConfig>(body.raw())?,
))),
(_, Some(body)) => Ok(ParsedRequest::new_stateless(
VmmAction::UpdateBalloon,
serde_json::from_slice::<BalloonUpdateSpec>(body.raw())?,
)),
(_, None) => method_to_error(Method::Patch),
}
}
Expand Down Expand Up @@ -183,25 +183,25 @@ mod tests {
let body = r#"{
"amount_mib": 1
}"#;
let expected_config = BalloonUpdateConfig { amount_mib: 1 };
let expected_spec = BalloonUpdateSpec { amount_mib: 1 };
assert_eq!(
vmm_action_from_request(
parse_patch_balloon(Some(&Body::new(body)), [].into_iter()).unwrap()
),
VmmAction::UpdateBalloon(expected_config)
VmmAction::UpdateBalloon(expected_spec)
);

let body = r#"{
"stats_polling_interval_s": 1
}"#;
let expected_config = BalloonUpdateStatsConfig {
let expected_spec = BalloonUpdateStatsSpec {
stats_polling_interval_s: 1,
};
assert_eq!(
vmm_action_from_request(
parse_patch_balloon(Some(&Body::new(body)), ["statistics"].into_iter()).unwrap()
),
VmmAction::UpdateBalloonStatistics(expected_config)
VmmAction::UpdateBalloonStatistics(expected_spec)
);

// PATCH start hinting run valid data
Expand Down
11 changes: 6 additions & 5 deletions src/firecracker/src/api_server/request/boot_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@

use vmm::logger::{IncMetric, METRICS};
use vmm::rpc_interface::VmmAction;
use vmm::vmm_config::boot_source::BootSourceConfig;
use vmm::vmm_config::boot_source::BootSourceSpec;

use super::super::parsed_request::{ParsedRequest, RequestError};
use super::Body;

pub(crate) fn parse_put_boot_source(body: &Body) -> Result<ParsedRequest, RequestError> {
METRICS.put_api_requests.boot_source_count.inc();
Ok(ParsedRequest::new_sync(VmmAction::ConfigureBootSource(
serde_json::from_slice::<BootSourceConfig>(body.raw()).inspect_err(|_| {
Ok(ParsedRequest::new_stateless(
VmmAction::ConfigureBootSource,
serde_json::from_slice::<BootSourceSpec>(body.raw()).inspect_err(|_| {
METRICS.put_api_requests.boot_source_fails.inc();
})?,
)))
))
}

#[cfg(test)]
Expand All @@ -30,7 +31,7 @@ mod tests {
"initrd_path": "/bar/foo",
"boot_args": "foobar"
}"#;
let same_body = BootSourceConfig {
let same_body = BootSourceSpec {
kernel_image_path: String::from("/foo/bar"),
initrd_path: Some(String::from("/bar/foo")),
boot_args: Some(String::from("foobar")),
Expand Down
5 changes: 3 additions & 2 deletions src/firecracker/src/api_server/request/cpu_configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ pub(crate) fn parse_put_cpu_config(body: &Body) -> Result<ParsedRequest, Request
METRICS.put_api_requests.cpu_cfg_count.inc();

// Convert the API request into a a deserialized/binary format
Ok(ParsedRequest::new_sync(VmmAction::PutCpuConfiguration(
Ok(ParsedRequest::new_stateless(
VmmAction::PutCpuConfiguration,
CustomCpuTemplate::try_from(body.raw()).map_err(|err| {
METRICS.put_api_requests.cpu_cfg_fails.inc();
RequestError::SerdeJson(err)
})?,
)))
))
}

#[cfg(test)]
Expand Down
30 changes: 16 additions & 14 deletions src/firecracker/src/api_server/request/drive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use vmm::logger::{IncMetric, METRICS};
use vmm::rpc_interface::VmmAction;
use vmm::vmm_config::drive::{BlockDeviceConfig, BlockDeviceUpdateConfig};
use vmm::vmm_config::drive::{BlockDeviceSpec, BlockDeviceUpdateSpec};

use super::super::parsed_request::{ParsedRequest, RequestError, checked_id};
use super::{Body, StatusCode};
Expand All @@ -20,20 +20,21 @@ pub(crate) fn parse_put_drive(
return Err(RequestError::EmptyID);
};

let device_cfg = serde_json::from_slice::<BlockDeviceConfig>(body.raw()).inspect_err(|_| {
let device_spec = serde_json::from_slice::<BlockDeviceSpec>(body.raw()).inspect_err(|_| {
METRICS.put_api_requests.drive_fails.inc();
})?;

if id != device_cfg.drive_id {
if id != device_spec.drive_id {
METRICS.put_api_requests.drive_fails.inc();
Err(RequestError::Generic(
StatusCode::BadRequest,
"The id from the path does not match the id from the body!".to_string(),
))
} else {
Ok(ParsedRequest::new_sync(VmmAction::InsertBlockDevice(
device_cfg,
)))
Ok(ParsedRequest::new_stateless(
VmmAction::InsertBlockDevice,
device_spec,
))
}
}

Expand All @@ -49,22 +50,23 @@ pub(crate) fn parse_patch_drive(
return Err(RequestError::EmptyID);
};

let block_device_update_cfg: BlockDeviceUpdateConfig =
serde_json::from_slice::<BlockDeviceUpdateConfig>(body.raw()).inspect_err(|_| {
let block_device_update_spec: BlockDeviceUpdateSpec =
serde_json::from_slice::<BlockDeviceUpdateSpec>(body.raw()).inspect_err(|_| {
METRICS.patch_api_requests.drive_fails.inc();
})?;

if id != block_device_update_cfg.drive_id {
if id != block_device_update_spec.drive_id {
METRICS.patch_api_requests.drive_fails.inc();
return Err(RequestError::Generic(
StatusCode::BadRequest,
String::from("The id from the path does not match the id from the body!"),
));
}

Ok(ParsedRequest::new_sync(VmmAction::UpdateBlockDevice(
block_device_update_cfg,
)))
Ok(ParsedRequest::new_stateless(
VmmAction::UpdateBlockDevice,
block_device_update_spec,
))
}

#[cfg(test)]
Expand Down Expand Up @@ -133,14 +135,14 @@ mod tests {
"drive_id": "foo",
"path_on_host": "dummy"
}"#;
let expected_config = BlockDeviceUpdateConfig {
let expected_spec = BlockDeviceUpdateSpec {
drive_id: "foo".to_string(),
path_on_host: Some("dummy".to_string()),
rate_limiter: None,
};
assert_eq!(
vmm_action_from_request(parse_patch_drive(&Body::new(body), Some("foo")).unwrap()),
VmmAction::UpdateBlockDevice(expected_config)
VmmAction::UpdateBlockDevice(expected_spec)
);

let body = r#"{
Expand Down
9 changes: 6 additions & 3 deletions src/firecracker/src/api_server/request/entropy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
// SPDX-License-Identifier: Apache-2.0

use vmm::rpc_interface::VmmAction;
use vmm::vmm_config::entropy::EntropyDeviceConfig;
use vmm::vmm_config::entropy::EntropyDeviceSpec;

use super::super::parsed_request::{ParsedRequest, RequestError};
use super::Body;

pub(crate) fn parse_put_entropy(body: &Body) -> Result<ParsedRequest, RequestError> {
let cfg = serde_json::from_slice::<EntropyDeviceConfig>(body.raw())?;
Ok(ParsedRequest::new_sync(VmmAction::SetEntropyDevice(cfg)))
let spec = serde_json::from_slice::<EntropyDeviceSpec>(body.raw())?;
Ok(ParsedRequest::new_stateless(
VmmAction::SetEntropyDevice,
spec,
))
}

#[cfg(test)]
Expand Down
Loading