-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmod.rs
310 lines (268 loc) · 8.7 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! Instance specs describe how to configure a VM and what components it has.
//!
//! This module defines a crate-internal instance spec type, [`Spec`], and its
//! constituent types, like [`Disk`] and [`Nic`]. Unlike the types in
//! [`propolis_api_types::instance_spec`], these internal types are not
//! `Serialize` and are never meant to be used over the wire in API requests or
//! the migration protocol. This allows them to change freely between Propolis
//! versions, so long as they can consistently be converted to and from the
//! wire-format types in the [`propolis_api_types`] crate. This, in turn, allows
//! [`Spec`] and its component types to take forms that might otherwise be hard
//! to change in a backward-compatible way.
use std::collections::BTreeMap;
use cpuid_utils::CpuidSet;
use propolis_api_types::instance_spec::{
components::{
backends::{
BlobStorageBackend, CrucibleStorageBackend, FileStorageBackend,
VirtioNetworkBackend,
},
board::{Chipset, GuestHypervisorInterface, I440Fx},
devices::{
NvmeDisk, PciPciBridge, QemuPvpanic as QemuPvpanicDesc,
SerialPortNumber, VirtioDisk, VirtioNic,
},
},
v0::ComponentV0,
PciPath, SpecKey,
};
use thiserror::Error;
#[cfg(feature = "failure-injection")]
use propolis_api_types::instance_spec::components::devices::MigrationFailureInjector;
#[cfg(feature = "falcon")]
use propolis_api_types::instance_spec::components::{
backends::DlpiNetworkBackend,
devices::{P9fs, SoftNpuP9, SoftNpuPciPort},
};
// mod api_request;
pub(crate) mod api_spec_v0;
pub(crate) mod builder;
#[derive(Debug, Error)]
#[error("input component type can't convert to output type")]
pub struct ComponentTypeMismatch;
/// An instance specification that describes a VM's configuration and
/// components.
///
/// NOTE: This struct's fields are `pub` to make it convenient to access the
/// individual parts of a fully-constructed spec. Modules that consume specs may
/// assert that they are valid (no duplicate component names, no duplicate PCI
/// device paths, etc.). When constructing a new spec, use the
/// [`builder::SpecBuilder`] struct to catch requests that violate these
/// invariants.
#[derive(Clone, Debug, Default)]
pub(crate) struct Spec {
pub board: Board,
pub cpuid: CpuidSet,
pub disks: BTreeMap<SpecKey, Disk>,
pub nics: BTreeMap<SpecKey, Nic>,
pub boot_settings: Option<BootSettings>,
pub serial: BTreeMap<SpecKey, SerialPort>,
pub pci_pci_bridges: BTreeMap<SpecKey, PciPciBridge>,
pub pvpanic: Option<QemuPvpanic>,
#[cfg(feature = "failure-injection")]
pub migration_failure: Option<MigrationFailure>,
#[cfg(feature = "falcon")]
pub softnpu: SoftNpu,
}
/// The VM's mainboard.
///
/// This is distinct from the [instance spec `Board`] so that it can exclude
/// fields (such as CPUID information) that need to be checked for validity
/// before being included in an internal spec.
///
/// [instance spec `Board`]: propolis_api_types::instance_spec::components::board::Board
#[derive(Clone, Debug)]
pub(crate) struct Board {
pub cpus: u8,
pub memory_mb: u64,
pub chipset: Chipset,
pub guest_hv_interface: GuestHypervisorInterface,
}
impl Default for Board {
fn default() -> Self {
Self {
cpus: 0,
memory_mb: 0,
chipset: Chipset::I440Fx(I440Fx { enable_pcie: false }),
guest_hv_interface: GuestHypervisorInterface::Bhyve,
}
}
}
#[derive(Clone, Debug)]
pub(crate) struct BootSettings {
pub name: SpecKey,
pub order: Vec<BootOrderEntry>,
}
#[derive(Clone, Debug)]
pub(crate) struct BootOrderEntry {
pub device_id: SpecKey,
}
impl
From<propolis_api_types::instance_spec::components::devices::BootOrderEntry>
for BootOrderEntry
{
fn from(
value: propolis_api_types::instance_spec::components::devices::BootOrderEntry,
) -> Self {
Self { device_id: value.id.clone() }
}
}
impl From<BootOrderEntry>
for propolis_api_types::instance_spec::components::devices::BootOrderEntry
{
fn from(value: BootOrderEntry) -> Self {
Self { id: value.device_id }
}
}
/// Describes the device half of a [`Disk`].
#[derive(Clone, Debug)]
pub enum StorageDevice {
Virtio(VirtioDisk),
Nvme(NvmeDisk),
}
impl StorageDevice {
pub fn kind(&self) -> &'static str {
match self {
StorageDevice::Virtio(_) => "virtio",
StorageDevice::Nvme(_) => "nvme",
}
}
pub fn pci_path(&self) -> PciPath {
match self {
StorageDevice::Virtio(disk) => disk.pci_path,
StorageDevice::Nvme(disk) => disk.pci_path,
}
}
pub fn backend_id(&self) -> &SpecKey {
match self {
StorageDevice::Virtio(disk) => &disk.backend_id,
StorageDevice::Nvme(disk) => &disk.backend_id,
}
}
}
impl From<StorageDevice> for ComponentV0 {
fn from(value: StorageDevice) -> Self {
match value {
StorageDevice::Virtio(d) => Self::VirtioDisk(d),
StorageDevice::Nvme(d) => Self::NvmeDisk(d),
}
}
}
impl TryFrom<ComponentV0> for StorageDevice {
type Error = ComponentTypeMismatch;
fn try_from(value: ComponentV0) -> Result<Self, Self::Error> {
match value {
ComponentV0::VirtioDisk(d) => Ok(Self::Virtio(d)),
ComponentV0::NvmeDisk(d) => Ok(Self::Nvme(d)),
_ => Err(ComponentTypeMismatch),
}
}
}
/// Describes the backend half of a [`Disk`].
#[derive(Clone, Debug)]
pub enum StorageBackend {
Crucible(CrucibleStorageBackend),
File(FileStorageBackend),
Blob(BlobStorageBackend),
}
impl StorageBackend {
pub fn kind(&self) -> &'static str {
match self {
StorageBackend::Crucible(_) => "crucible",
StorageBackend::File(_) => "file",
StorageBackend::Blob(_) => "backend",
}
}
pub fn read_only(&self) -> bool {
match self {
StorageBackend::Crucible(be) => be.readonly,
StorageBackend::File(be) => be.readonly,
StorageBackend::Blob(be) => be.readonly,
}
}
}
impl From<StorageBackend> for ComponentV0 {
fn from(value: StorageBackend) -> Self {
match value {
StorageBackend::Crucible(be) => Self::CrucibleStorageBackend(be),
StorageBackend::File(be) => Self::FileStorageBackend(be),
StorageBackend::Blob(be) => Self::BlobStorageBackend(be),
}
}
}
impl TryFrom<ComponentV0> for StorageBackend {
type Error = ComponentTypeMismatch;
fn try_from(value: ComponentV0) -> Result<Self, Self::Error> {
match value {
ComponentV0::CrucibleStorageBackend(be) => Ok(Self::Crucible(be)),
ComponentV0::FileStorageBackend(be) => Ok(Self::File(be)),
ComponentV0::BlobStorageBackend(be) => Ok(Self::Blob(be)),
_ => Err(ComponentTypeMismatch),
}
}
}
#[derive(Clone, Debug)]
pub struct Disk {
pub device_spec: StorageDevice,
pub backend_spec: StorageBackend,
}
#[derive(Clone, Debug)]
pub struct Nic {
pub device_spec: VirtioNic,
pub backend_spec: VirtioNetworkBackend,
}
/// A kind of device to install as the listener on a COM port.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SerialPortDevice {
Uart,
#[cfg(feature = "falcon")]
SoftNpu,
}
impl std::fmt::Display for SerialPortDevice {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
SerialPortDevice::Uart => "uart",
#[cfg(feature = "falcon")]
SerialPortDevice::SoftNpu => "softnpu",
}
)
}
}
#[derive(Clone, Debug)]
pub struct SerialPort {
pub num: SerialPortNumber,
pub device: SerialPortDevice,
}
#[derive(Clone, Debug)]
pub struct QemuPvpanic {
#[allow(dead_code)]
pub id: SpecKey,
pub spec: QemuPvpanicDesc,
}
#[cfg(feature = "failure-injection")]
#[derive(Clone, Debug)]
pub struct MigrationFailure {
pub id: SpecKey,
pub spec: MigrationFailureInjector,
}
#[cfg(feature = "falcon")]
#[derive(Clone, Debug)]
pub struct SoftNpuPort {
pub link_name: String,
pub backend_name: SpecKey,
pub backend_spec: DlpiNetworkBackend,
}
#[cfg(feature = "falcon")]
#[derive(Clone, Debug, Default)]
pub struct SoftNpu {
pub pci_port: Option<SoftNpuPciPort>,
pub ports: BTreeMap<SpecKey, SoftNpuPort>,
pub p9_device: Option<SoftNpuP9>,
pub p9fs: Option<P9fs>,
}