Skip to content

Commit fc294d6

Browse files
committed
Expose memory mapping; Add optional memfile dump
1 parent e7ae239 commit fc294d6

File tree

10 files changed

+68
-11
lines changed

10 files changed

+68
-11
lines changed

.tool-versions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rust 1.85.0

src/firecracker/src/api_server/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ mod tests {
274274
Box::new(VmmAction::CreateSnapshot(CreateSnapshotParams {
275275
snapshot_type: SnapshotType::Diff,
276276
snapshot_path: PathBuf::new(),
277-
mem_file_path: PathBuf::new(),
277+
mem_file_path: Some(PathBuf::new()),
278278
})),
279279
start_time_us,
280280
);
@@ -287,7 +287,7 @@ mod tests {
287287
Box::new(VmmAction::CreateSnapshot(CreateSnapshotParams {
288288
snapshot_type: SnapshotType::Diff,
289289
snapshot_path: PathBuf::new(),
290-
mem_file_path: PathBuf::new(),
290+
mem_file_path: Some(PathBuf::new()),
291291
})),
292292
start_time_us,
293293
);

src/firecracker/src/api_server/request/snapshot.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ mod tests {
139139
let expected_config = CreateSnapshotParams {
140140
snapshot_type: SnapshotType::Diff,
141141
snapshot_path: PathBuf::from("foo"),
142-
mem_file_path: PathBuf::from("bar"),
142+
mem_file_path: Some(PathBuf::from("bar")),
143143
};
144144
assert_eq!(
145145
vmm_action_from_request(parse_put_snapshot(&Body::new(body), Some("create")).unwrap()),
@@ -153,7 +153,7 @@ mod tests {
153153
let expected_config = CreateSnapshotParams {
154154
snapshot_type: SnapshotType::Full,
155155
snapshot_path: PathBuf::from("foo"),
156-
mem_file_path: PathBuf::from("bar"),
156+
mem_file_path: Some(PathBuf::from("bar")),
157157
};
158158
assert_eq!(
159159
vmm_action_from_request(parse_put_snapshot(&Body::new(body), Some("create")).unwrap()),

src/firecracker/swagger/firecracker.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,32 @@ definitions:
990990
vmm_version:
991991
description: MicroVM hypervisor build version.
992992
type: string
993+
memory_regions:
994+
type: array
995+
description: The regions of the guest memory.
996+
items:
997+
$ref: "#/definitions/GuestMemoryRegion"
998+
999+
GuestMemoryRegionMapping:
1000+
type: object
1001+
description: Describes the region of guest memory that can be used for creating the memfile.
1002+
required:
1003+
- base_host_virt_addr
1004+
- size
1005+
- offset
1006+
- page_size
1007+
properties:
1008+
base_host_virt_addr:
1009+
type: integer
1010+
size:
1011+
description: The size of the region in bytes.
1012+
type: integer
1013+
offset:
1014+
description: The offset of the region in bytes.
1015+
type: integer
1016+
page_size:
1017+
description: The page size of the region in pages.
1018+
type: integer
9931019

9941020
Logger:
9951021
type: object

src/vmm/src/persist.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,10 @@ pub fn create_snapshot(
171171

172172
snapshot_state_to_file(&microvm_state, &params.snapshot_path)?;
173173

174-
snapshot_memory_to_file(vmm, &params.mem_file_path, params.snapshot_type)?;
174+
// Dump memory to file only if mem_file_path is specified
175+
if let Some(ref mem_file_path) = params.mem_file_path {
176+
snapshot_memory_to_file(vmm, mem_file_path, params.snapshot_type)?;
177+
}
175178

176179
Ok(())
177180
}

src/vmm/src/rpc_interface.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -646,9 +646,17 @@ impl RuntimeApiController {
646646
GetVmMachineConfig => Ok(VmmData::MachineConfiguration(MachineConfig::from(
647647
&self.vm_resources.vm_config,
648648
))),
649-
GetVmInstanceInfo => Ok(VmmData::InstanceInformation(
650-
self.vmm.lock().expect("Poisoned lock").instance_info(),
651-
)),
649+
GetVmInstanceInfo => {
650+
let locked_vmm = self.vmm.lock().expect("Poisoned lock");
651+
652+
let mut instance_info = locked_vmm.instance_info();
653+
654+
instance_info.memory_regions = locked_vmm
655+
.vm
656+
.guest_memory_mappings(&VmInfo::from(&self.vm_resources));
657+
658+
Ok(VmmData::InstanceInformation(instance_info))
659+
}
652660
GetVmmVersion => Ok(VmmData::VmmVersion(
653661
self.vmm.lock().expect("Poisoned lock").version(),
654662
)),
@@ -1150,7 +1158,7 @@ mod tests {
11501158
CreateSnapshotParams {
11511159
snapshot_type: SnapshotType::Full,
11521160
snapshot_path: PathBuf::new(),
1153-
mem_file_path: PathBuf::new(),
1161+
mem_file_path: Some(PathBuf::new()),
11541162
},
11551163
)));
11561164
#[cfg(target_arch = "x86_64")]

src/vmm/src/vmm_config/instance_info.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
use std::fmt::{self, Display, Formatter};
44

5+
use crate::vstate::vm::GuestMemoryRegionMapping;
56
use serde::{ser, Serialize};
67

78
/// Enumerates microVM runtime states.
@@ -46,4 +47,6 @@ pub struct InstanceInfo {
4647
pub vmm_version: String,
4748
/// The name of the application that runs the microVM.
4849
pub app_name: String,
50+
/// The regions of the guest memory.
51+
pub memory_regions: Vec<GuestMemoryRegionMapping>,
4952
}

src/vmm/src/vmm_config/snapshot.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ pub struct CreateSnapshotParams {
4444
/// Path to the file that will contain the microVM state.
4545
pub snapshot_path: PathBuf,
4646
/// Path to the file that will contain the guest memory.
47-
pub mem_file_path: PathBuf,
47+
/// If not specified, the memory is not dumped to a file.
48+
#[serde(skip_serializing_if = "Option::is_none")]
49+
pub mem_file_path: Option<PathBuf>,
4850
}
4951

5052
/// Stores the configuration that will be used for loading a snapshot.

src/vmm/src/vstate/vm.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ use crate::cpu_config::templates::KvmCapability;
2727
use crate::utils::u64_to_usize;
2828
use crate::vstate::memory::{Address, GuestMemory, GuestMemoryMmap, GuestMemoryRegion};
2929

30+
/// Describes the region of guest memory that can be used for creating the memfile.
31+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
32+
pub struct GuestMemoryRegionMapping {
33+
/// Base host virtual address where the guest memory contents for this region
34+
/// should be copied/populated.
35+
pub base_host_virt_addr: u64,
36+
/// Region size.
37+
pub size: usize,
38+
/// Offset in the backend file/buffer where the region contents are.
39+
pub offset: u64,
40+
/// The configured page size for this memory region.
41+
pub page_size: usize,
42+
}
43+
3044
/// Errors associated with the wrappers over KVM ioctls.
3145
/// Needs `rustfmt::skip` to make multiline comments work
3246
#[rustfmt::skip]

src/vmm/tests/integration_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ fn verify_create_snapshot(is_diff: bool) -> (TempFile, TempFile) {
212212
let snapshot_params = CreateSnapshotParams {
213213
snapshot_type,
214214
snapshot_path: snapshot_file.as_path().to_path_buf(),
215-
mem_file_path: memory_file.as_path().to_path_buf(),
215+
mem_file_path: Some(memory_file.as_path().to_path_buf()),
216216
};
217217

218218
controller

0 commit comments

Comments
 (0)