vmm/vmm_config/snapshot.rs
1// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Configurations used in the snapshotting context.
5
6use std::path::PathBuf;
7
8/// For crates that depend on `vmm` we export.
9pub use semver::Version;
10use serde::{Deserialize, Serialize};
11
12/// The snapshot type options that are available when
13/// creating a new snapshot.
14#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Deserialize, Serialize)]
15pub enum SnapshotType {
16 /// Diff snapshot.
17 Diff,
18 /// Full snapshot.
19 #[default]
20 Full,
21}
22
23/// Specifies the method through which guest memory will get populated when
24/// resuming from a snapshot:
25/// 1) A file that contains the guest memory to be loaded,
26/// 2) An UDS where a custom page-fault handler process is listening for the UFFD set up by
27/// Firecracker to handle its guest memory page faults.
28#[derive(Debug, PartialEq, Eq, Deserialize)]
29pub enum MemBackendType {
30 /// Guest memory contents will be loaded from a file.
31 File,
32 /// Guest memory will be served through UFFD by a separate process.
33 Uffd,
34}
35
36/// Stores the configuration that will be used for creating a snapshot.
37#[derive(Debug, PartialEq, Eq, Deserialize)]
38#[serde(deny_unknown_fields)]
39pub struct CreateSnapshotParams {
40 /// This marks the type of snapshot we want to create.
41 /// The default value is `Full`, which means a full snapshot.
42 #[serde(default = "SnapshotType::default")]
43 pub snapshot_type: SnapshotType,
44 /// Path to the file that will contain the microVM state.
45 pub snapshot_path: PathBuf,
46 /// Path to the file that will contain the guest memory.
47 pub mem_file_path: PathBuf,
48}
49
50/// Allows for changing the mapping between tap devices and host devices
51/// during snapshot restore
52#[derive(Debug, PartialEq, Eq, Deserialize)]
53pub struct NetworkOverride {
54 /// The index of the interface to modify
55 pub iface_id: String,
56 /// The new name of the interface to be assigned
57 pub host_dev_name: String,
58}
59
60/// Stores the configuration that will be used for loading a snapshot.
61#[derive(Debug, PartialEq, Eq)]
62pub struct LoadSnapshotParams {
63 /// Path to the file that contains the microVM state to be loaded.
64 pub snapshot_path: PathBuf,
65 /// Specifies guest memory backend configuration.
66 pub mem_backend: MemBackendConfig,
67 /// Whether KVM dirty page tracking should be enabled, to space optimization
68 /// of differential snapshots.
69 pub track_dirty_pages: bool,
70 /// When set to true, the vm is also resumed if the snapshot load
71 /// is successful.
72 pub resume_vm: bool,
73 /// The network devices to override on load.
74 pub network_overrides: Vec<NetworkOverride>,
75}
76
77/// Stores the configuration for loading a snapshot that is provided by the user.
78#[derive(Debug, Deserialize)]
79#[serde(deny_unknown_fields)]
80pub struct LoadSnapshotConfig {
81 /// Path to the file that contains the microVM state to be loaded.
82 pub snapshot_path: PathBuf,
83 /// Path to the file that contains the guest memory to be loaded. To be used only if
84 /// `mem_backend` is not specified.
85 #[serde(skip_serializing_if = "Option::is_none")]
86 pub mem_file_path: Option<PathBuf>,
87 /// Guest memory backend configuration. Is not to be used in conjunction with `mem_file_path`.
88 /// None value is allowed only if `mem_file_path` is present.
89 #[serde(skip_serializing_if = "Option::is_none")]
90 pub mem_backend: Option<MemBackendConfig>,
91 /// Whether or not to enable KVM dirty page tracking.
92 #[serde(default)]
93 #[deprecated]
94 pub enable_diff_snapshots: bool,
95 /// Whether KVM dirty page tracking should be enabled.
96 #[serde(default)]
97 pub track_dirty_pages: bool,
98 /// Whether or not to resume the vm post snapshot load.
99 #[serde(default)]
100 pub resume_vm: bool,
101 /// The network devices to override on load.
102 #[serde(default)]
103 pub network_overrides: Vec<NetworkOverride>,
104}
105
106/// Stores the configuration used for managing snapshot memory.
107#[derive(Debug, PartialEq, Eq, Deserialize)]
108#[serde(deny_unknown_fields)]
109pub struct MemBackendConfig {
110 /// Path to the backend used to handle the guest memory.
111 pub backend_path: PathBuf,
112 /// Specifies the guest memory backend type.
113 pub backend_type: MemBackendType,
114}
115
116/// The microVM state options.
117#[derive(Debug, Deserialize, Serialize)]
118pub enum VmState {
119 /// The microVM is paused, which means that we can create a snapshot of it.
120 Paused,
121 /// The microVM is resumed; this state should be set after we load a snapshot.
122 Resumed,
123}
124
125/// Keeps the microVM state necessary in the snapshotting context.
126#[derive(Debug, Deserialize, Serialize)]
127#[serde(deny_unknown_fields)]
128pub struct Vm {
129 /// The microVM state, which can be `paused` or `resumed`.
130 pub state: VmState,
131}