Skip to content

Commit 4d5f0bd

Browse files
jiangliualxiord
authored andcommitted
vmm: Limit the maximum number of supported vCPUs to 32
The firecracker VMM targets to support small scale workloads, so limit the maximum number of supported vCPUs to 32. Signed-off-by: Liu Jiang <[email protected]>
1 parent 0dc7d89 commit 4d5f0bd

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Added
66

77
- Documentation for development environment setup on AWS.
8+
- Limit the maximum supported vCPUs to 32.
89

910
### Changed
1011
- Log the app version when the `Logger` is initialized.

api_server/src/http_service.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ mod tests {
11011101
fn test_parse_machine_config_req() {
11021102
let path = "/machine-config";
11031103
let json = "{
1104-
\"vcpu_count\": 42,
1104+
\"vcpu_count\": 32,
11051105
\"mem_size_mib\": 1025,
11061106
\"ht_enabled\": true,
11071107
\"cpu_template\": \"T2\"
@@ -1118,7 +1118,7 @@ mod tests {
11181118

11191119
// PUT
11201120
let vm_config = VmConfig {
1121-
vcpu_count: Some(42),
1121+
vcpu_count: Some(32),
11221122
mem_size_mib: Some(1025),
11231123
ht_enabled: Some(true),
11241124
cpu_template: Some(CpuFeaturesTemplate::T2),
@@ -1145,6 +1145,20 @@ mod tests {
11451145
String::from("Empty request."),
11461146
));
11471147
assert!(parse_machine_config_req(path, Method::Put, &Chunk::from("{}")) == expected_err);
1148+
1149+
// Error Case: cpu count exceeds limitation
1150+
let json = "{
1151+
\"vcpu_count\": 33,
1152+
\"mem_size_mib\": 1025,
1153+
\"ht_enabled\": true,
1154+
\"cpu_template\": \"T2\"
1155+
}";
1156+
let body: Chunk = Chunk::from(json);
1157+
if let Err(Error::SerdeJson(e)) = parse_machine_config_req(path, Method::Put, &body) {
1158+
assert!(e.is_data());
1159+
} else {
1160+
assert!(false);
1161+
}
11481162
}
11491163

11501164
#[test]

api_server/swagger/firecracker.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,8 @@ definitions:
430430
properties:
431431
vcpu_count:
432432
type: integer
433+
minimum: 1
434+
maximum: 32
433435
description: Number of vCPUs (either 1 or an even number)
434436
mem_size_mib:
435437
type: integer

vmm/src/vmm_config/machine_config.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33

4+
use serde::{de, Deserialize};
45
use std::fmt::{Display, Formatter, Result};
56

7+
/// Firecracker aims to support small scale workloads only, so limit the maximum
8+
/// vCPUs supported.
9+
pub const MAX_SUPPORTED_VCPUS: u8 = 32;
10+
611
/// Errors associated with configuring the microVM.
712
#[derive(Debug, PartialEq)]
813
pub enum VmConfigError {
@@ -38,7 +43,11 @@ impl Display for VmConfigError {
3843
#[serde(deny_unknown_fields)]
3944
pub struct VmConfig {
4045
/// Number of vcpu to start.
41-
#[serde(skip_serializing_if = "Option::is_none")]
46+
#[serde(
47+
default,
48+
skip_serializing_if = "Option::is_none",
49+
deserialize_with = "validate_vcpu_num"
50+
)]
4251
pub vcpu_count: Option<u8>,
4352
/// The memory size in MiB.
4453
#[serde(skip_serializing_if = "Option::is_none")]
@@ -62,6 +71,22 @@ impl Default for VmConfig {
6271
}
6372
}
6473

74+
fn validate_vcpu_num<'de, D>(d: D) -> std::result::Result<Option<u8>, D::Error>
75+
where
76+
D: de::Deserializer<'de>,
77+
{
78+
let val = Option::<u8>::deserialize(d)?;
79+
if let Some(ref value) = val {
80+
if *value > MAX_SUPPORTED_VCPUS {
81+
return Err(de::Error::invalid_value(
82+
de::Unexpected::Unsigned(*value as u64),
83+
&"number of vCPUs exceeds the maximum limitation",
84+
));
85+
}
86+
}
87+
Ok(val)
88+
}
89+
6590
/// Template types available for configuring the CPU features that map
6691
/// to EC2 instances.
6792
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]

0 commit comments

Comments
 (0)