Skip to content

Commit 119a9a3

Browse files
committed
add mem_config parameter to machine-config endpoint (aarch64)
Add a mem_config parameter to the PUT/PATCH /machine-config endpoints, which allows configuration of the size of a swiotlb region to use. Signed-off-by: Patrick Roy <[email protected]>
1 parent 2bc5857 commit 119a9a3

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

src/firecracker/src/api_server/request/machine_configuration.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ mod tests {
119119
let expected_config = MachineConfigUpdate {
120120
vcpu_count: Some(8),
121121
mem_size_mib: Some(1024),
122+
mem_config: Some(Default::default()),
122123
smt: Some(false),
123124
cpu_template: None,
124125
track_dirty_pages: Some(false),
@@ -140,6 +141,7 @@ mod tests {
140141
let expected_config = MachineConfigUpdate {
141142
vcpu_count: Some(8),
142143
mem_size_mib: Some(1024),
144+
mem_config: Some(Default::default()),
143145
smt: Some(false),
144146
cpu_template: Some(StaticCpuTemplate::None),
145147
track_dirty_pages: Some(false),
@@ -161,6 +163,7 @@ mod tests {
161163
let expected_config = MachineConfigUpdate {
162164
vcpu_count: Some(8),
163165
mem_size_mib: Some(1024),
166+
mem_config: Some(Default::default()),
164167
smt: Some(false),
165168
cpu_template: None,
166169
track_dirty_pages: Some(true),
@@ -186,6 +189,7 @@ mod tests {
186189
let expected_config = MachineConfigUpdate {
187190
vcpu_count: Some(8),
188191
mem_size_mib: Some(1024),
192+
mem_config: Some(Default::default()),
189193
smt: Some(false),
190194
cpu_template: Some(StaticCpuTemplate::T2),
191195
track_dirty_pages: Some(true),
@@ -213,6 +217,7 @@ mod tests {
213217
let expected_config = MachineConfigUpdate {
214218
vcpu_count: Some(8),
215219
mem_size_mib: Some(1024),
220+
mem_config: Some(Default::default()),
216221
smt: Some(true),
217222
cpu_template: None,
218223
track_dirty_pages: Some(true),

src/vmm/src/persist.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ use crate::snapshot::Snapshot;
3333
use crate::utils::u64_to_usize;
3434
use crate::vmm_config::boot_source::BootSourceConfig;
3535
use crate::vmm_config::instance_info::InstanceInfo;
36-
use crate::vmm_config::machine_config::{HugePageConfig, MachineConfigError, MachineConfigUpdate};
36+
use crate::vmm_config::machine_config::{
37+
HugePageConfig, MachineConfigError, MachineConfigUpdate, MemoryConfig,
38+
};
3739
use crate::vmm_config::snapshot::{CreateSnapshotParams, LoadSnapshotParams, MemBackendType};
3840
use crate::vstate::kvm::KvmState;
3941
use crate::vstate::memory;
@@ -47,6 +49,8 @@ use crate::{EventManager, Vmm, vstate};
4749
pub struct VmInfo {
4850
/// Guest memory size.
4951
pub mem_size_mib: u64,
52+
/// Memory config
53+
pub mem_config: MemoryConfig,
5054
/// smt information
5155
pub smt: bool,
5256
/// CPU template type
@@ -61,6 +65,7 @@ impl From<&VmResources> for VmInfo {
6165
fn from(value: &VmResources) -> Self {
6266
Self {
6367
mem_size_mib: value.machine_config.mem_size_mib as u64,
68+
mem_config: value.machine_config.mem_config,
6469
smt: value.machine_config.smt,
6570
cpu_template: StaticCpuTemplate::from(&value.machine_config.cpu_template),
6671
boot_source: value.boot_source.config.clone(),
@@ -360,6 +365,7 @@ pub fn restore_from_snapshot(
360365
.update_machine_config(&MachineConfigUpdate {
361366
vcpu_count: Some(vcpu_count),
362367
mem_size_mib: Some(u64_to_usize(microvm_state.vm_info.mem_size_mib)),
368+
mem_config: Some(microvm_state.vm_info.mem_config),
363369
smt: Some(microvm_state.vm_info.smt),
364370
cpu_template: Some(microvm_state.vm_info.cpu_template),
365371
track_dirty_pages: Some(track_dirty_pages),

src/vmm/src/resources.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,7 @@ mod tests {
13021302
let mut aux_vm_config = MachineConfigUpdate {
13031303
vcpu_count: Some(32),
13041304
mem_size_mib: Some(512),
1305+
mem_config: Some(Default::default()),
13051306
smt: Some(false),
13061307
#[cfg(target_arch = "x86_64")]
13071308
cpu_template: Some(StaticCpuTemplate::T2),

src/vmm/src/vmm_config/machine_config.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ pub enum MachineConfigError {
2020
IncompatibleBalloonSize,
2121
/// The memory size (MiB) is either 0, or not a multiple of the configured page size.
2222
InvalidMemorySize,
23+
/// The specified swiotlb region matches or exceeds the total VM memory, or not a multiple of the configured page size.
24+
#[cfg(target_arch = "aarch64")]
25+
InvalidSwiotlbRegionSize,
2326
/// The number of vCPUs must be greater than 0, less than {MAX_SUPPORTED_VCPUS:} and must be 1 or an even number if SMT is enabled.
2427
InvalidVcpuCount,
2528
/// Could not get the configuration of the previously installed balloon device to validate the memory size.
@@ -89,6 +92,17 @@ impl From<HugePageConfig> for Option<memfd::HugetlbSize> {
8992
}
9093
}
9194

95+
/// Structure containing options for tweaking guest memory configuration.
96+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
97+
pub struct MemoryConfig {
98+
/// The initial size of the swiotlb region. If 0, no swiotlb region will be created.
99+
/// If non-zero, all device will be forced to bounce buffers through a swiotlb region
100+
/// of the specified size that will have been placed into a dedicated kvm memslot.
101+
#[cfg(target_arch = "aarch64")]
102+
#[serde(default)]
103+
pub initial_swiotlb_size: usize,
104+
}
105+
92106
/// Struct used in PUT `/machine-config` API call.
93107
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
94108
#[serde(deny_unknown_fields)]
@@ -97,6 +111,10 @@ pub struct MachineConfig {
97111
pub vcpu_count: u8,
98112
/// The memory size in MiB.
99113
pub mem_size_mib: usize,
114+
/// Additional configuration options for guest memory
115+
#[serde(default)]
116+
#[serde(skip_serializing_if = "is_default")]
117+
pub mem_config: MemoryConfig,
100118
/// Enables or disabled SMT.
101119
#[serde(default)]
102120
pub smt: bool,
@@ -121,6 +139,10 @@ pub struct MachineConfig {
121139
pub gdb_socket_path: Option<String>,
122140
}
123141

142+
fn is_default<T: Default + Eq>(t: &T) -> bool {
143+
t == &T::default()
144+
}
145+
124146
fn is_none_or_custom_template(template: &Option<CpuTemplateType>) -> bool {
125147
matches!(template, None | Some(CpuTemplateType::Custom(_)))
126148
}
@@ -153,6 +175,7 @@ impl Default for MachineConfig {
153175
Self {
154176
vcpu_count: 1,
155177
mem_size_mib: DEFAULT_MEM_SIZE_MIB,
178+
mem_config: Default::default(),
156179
smt: false,
157180
cpu_template: None,
158181
track_dirty_pages: false,
@@ -178,6 +201,9 @@ pub struct MachineConfigUpdate {
178201
/// The memory size in MiB.
179202
#[serde(default)]
180203
pub mem_size_mib: Option<usize>,
204+
/// The memory configuration
205+
#[serde(default)]
206+
pub mem_config: Option<MemoryConfig>,
181207
/// Enables or disabled SMT.
182208
#[serde(default)]
183209
pub smt: Option<bool>,
@@ -210,6 +236,7 @@ impl From<MachineConfig> for MachineConfigUpdate {
210236
MachineConfigUpdate {
211237
vcpu_count: Some(cfg.vcpu_count),
212238
mem_size_mib: Some(cfg.mem_size_mib),
239+
mem_config: Some(cfg.mem_config),
213240
smt: Some(cfg.smt),
214241
cpu_template: cfg.static_template(),
215242
track_dirty_pages: Some(cfg.track_dirty_pages),
@@ -263,11 +290,19 @@ impl MachineConfig {
263290

264291
let mem_size_mib = update.mem_size_mib.unwrap_or(self.mem_size_mib);
265292
let page_config = update.huge_pages.unwrap_or(self.huge_pages);
293+
let mem_config = update.mem_config.unwrap_or(self.mem_config);
266294

267295
if mem_size_mib == 0 || !page_config.is_valid_mem_size(mem_size_mib) {
268296
return Err(MachineConfigError::InvalidMemorySize);
269297
}
270298

299+
#[cfg(target_arch = "aarch64")]
300+
if mem_config.initial_swiotlb_size >= mem_size_mib
301+
|| !page_config.is_valid_mem_size(mem_config.initial_swiotlb_size)
302+
{
303+
return Err(MachineConfigError::InvalidSwiotlbRegionSize);
304+
}
305+
271306
let cpu_template = match update.cpu_template {
272307
None => self.cpu_template.clone(),
273308
Some(StaticCpuTemplate::None) => None,
@@ -277,6 +312,7 @@ impl MachineConfig {
277312
Ok(MachineConfig {
278313
vcpu_count,
279314
mem_size_mib,
315+
mem_config,
280316
smt,
281317
cpu_template,
282318
track_dirty_pages: update.track_dirty_pages.unwrap_or(self.track_dirty_pages),

0 commit comments

Comments
 (0)