-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathapi_spec_v0.rs
401 lines (358 loc) · 14.3 KB
/
api_spec_v0.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// 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/.
//! Conversions from version-0 instance specs in the [`propolis_api_types`]
//! crate to the internal [`super::Spec`] representation.
use std::collections::BTreeMap;
use propolis_api_types::instance_spec::{
components::{
backends::{DlpiNetworkBackend, VirtioNetworkBackend},
board::Board as InstanceSpecBoard,
devices::{BootSettings, SerialPort as SerialPortDesc},
},
v0::{ComponentV0, InstanceSpecV0},
SpecKey,
};
use thiserror::Error;
#[cfg(feature = "falcon")]
use propolis_api_types::instance_spec::components::devices::SoftNpuPort as SoftNpuPortSpec;
use super::{
builder::{SpecBuilder, SpecBuilderError},
Disk, Nic, QemuPvpanic, SerialPortDevice, Spec, StorageBackend,
StorageDevice,
};
#[cfg(feature = "failure-injection")]
use super::MigrationFailure;
#[cfg(feature = "falcon")]
use super::SoftNpuPort;
#[derive(Debug, Error)]
pub(crate) enum ApiSpecError {
#[error(transparent)]
Builder(#[from] SpecBuilderError),
#[error("storage backend {backend} not found for device {device}")]
StorageBackendNotFound { backend: SpecKey, device: SpecKey },
#[error("network backend {backend} not found for device {device}")]
NetworkBackendNotFound { backend: SpecKey, device: SpecKey },
#[allow(dead_code)]
#[error("support for component {component} compiled out via {feature}")]
FeatureCompiledOut { component: SpecKey, feature: &'static str },
#[error("backend {0} not used by any device")]
BackendNotUsed(SpecKey),
}
impl From<Spec> for InstanceSpecV0 {
fn from(val: Spec) -> Self {
// Exhaustively destructure the input spec so that adding a new field
// without considering it here will break the build.
let Spec {
board,
cpuid,
disks,
nics,
boot_settings,
serial,
pci_pci_bridges,
pvpanic,
#[cfg(feature = "failure-injection")]
migration_failure,
#[cfg(feature = "falcon")]
softnpu,
} = val;
// Inserts a component entry into the supplied map, asserting first that
// the supplied key is not present in that map.
//
// This assertion is valid because internal instance specs should assign
// a unique name to each component they describe. The spec builder
// upholds this invariant at spec creation time.
#[track_caller]
fn insert_component(
spec: &mut InstanceSpecV0,
key: SpecKey,
val: ComponentV0,
) {
assert!(
!spec.components.contains_key(&key),
"component name {} already exists in output spec",
&key
);
spec.components.insert(key, val);
}
let board = InstanceSpecBoard {
cpus: board.cpus,
memory_mb: board.memory_mb,
chipset: board.chipset,
guest_hv_interface: board.guest_hv_interface,
cpuid: Some(cpuid.into_instance_spec_cpuid()),
};
let mut spec = InstanceSpecV0 { board, components: Default::default() };
for (disk_id, disk) in disks {
let backend_id = disk.device_spec.backend_id().to_owned();
insert_component(&mut spec, disk_id, disk.device_spec.into());
insert_component(&mut spec, backend_id, disk.backend_spec.into());
}
for (nic_id, nic) in nics {
let backend_id = nic.device_spec.backend_id.clone();
insert_component(
&mut spec,
nic_id,
ComponentV0::VirtioNic(nic.device_spec),
);
insert_component(
&mut spec,
backend_id,
ComponentV0::VirtioNetworkBackend(nic.backend_spec),
);
}
for (name, desc) in serial {
if desc.device == SerialPortDevice::Uart {
insert_component(
&mut spec,
name,
ComponentV0::SerialPort(SerialPortDesc { num: desc.num }),
);
}
}
for (bridge_name, bridge) in pci_pci_bridges {
insert_component(
&mut spec,
bridge_name,
ComponentV0::PciPciBridge(bridge),
);
}
if let Some(pvpanic) = pvpanic {
insert_component(
&mut spec,
pvpanic.id,
ComponentV0::QemuPvpanic(pvpanic.spec),
);
}
if let Some(settings) = boot_settings {
insert_component(
&mut spec,
settings.name,
ComponentV0::BootSettings(BootSettings {
order: settings.order.into_iter().map(Into::into).collect(),
}),
);
}
#[cfg(feature = "failure-injection")]
if let Some(mig) = migration_failure {
insert_component(
&mut spec,
mig.id,
ComponentV0::MigrationFailureInjector(mig.spec),
);
}
#[cfg(feature = "falcon")]
{
if let Some(softnpu_pci) = softnpu.pci_port {
insert_component(
&mut spec,
SpecKey::Name(format!(
"softnpu-pci-{}",
softnpu_pci.pci_path
)),
ComponentV0::SoftNpuPciPort(softnpu_pci),
);
}
if let Some(p9) = softnpu.p9_device {
insert_component(
&mut spec,
SpecKey::Name(format!("softnpu-p9-{}", p9.pci_path)),
ComponentV0::SoftNpuP9(p9),
);
}
if let Some(p9fs) = softnpu.p9fs {
insert_component(
&mut spec,
SpecKey::Name(format!("p9fs-{}", p9fs.pci_path)),
ComponentV0::P9fs(p9fs),
);
}
for (port_name, port) in softnpu.ports {
insert_component(
&mut spec,
port_name.clone(),
ComponentV0::SoftNpuPort(SoftNpuPortSpec {
link_name: port.link_name,
backend_id: port.backend_name.clone(),
}),
);
insert_component(
&mut spec,
port.backend_name,
ComponentV0::DlpiNetworkBackend(port.backend_spec),
);
}
}
spec
}
}
impl TryFrom<InstanceSpecV0> for Spec {
type Error = ApiSpecError;
fn try_from(value: InstanceSpecV0) -> Result<Self, Self::Error> {
let mut builder = SpecBuilder::with_instance_spec_board(value.board)?;
let mut devices: Vec<(SpecKey, ComponentV0)> = vec![];
let mut boot_settings = None;
let mut storage_backends: BTreeMap<SpecKey, StorageBackend> =
BTreeMap::new();
let mut viona_backends: BTreeMap<SpecKey, VirtioNetworkBackend> =
BTreeMap::new();
let mut dlpi_backends: BTreeMap<SpecKey, DlpiNetworkBackend> =
BTreeMap::new();
for (id, component) in value.components.into_iter() {
match component {
ComponentV0::CrucibleStorageBackend(_)
| ComponentV0::FileStorageBackend(_)
| ComponentV0::BlobStorageBackend(_) => {
storage_backends.insert(
id,
component.try_into().expect(
"component is known to be a storage backend",
),
);
}
ComponentV0::VirtioNetworkBackend(viona) => {
viona_backends.insert(id, viona);
}
ComponentV0::DlpiNetworkBackend(dlpi) => {
dlpi_backends.insert(id, dlpi);
}
device => {
devices.push((id, device));
}
}
}
for (device_id, device_spec) in devices {
match device_spec {
ComponentV0::VirtioDisk(_) | ComponentV0::NvmeDisk(_) => {
let device_spec = StorageDevice::try_from(device_spec)
.expect("component is known to be a disk");
let (_, backend_spec) = storage_backends
.remove_entry(device_spec.backend_id())
.ok_or_else(|| {
ApiSpecError::StorageBackendNotFound {
backend: device_spec.backend_id().to_owned(),
device: device_id.clone(),
}
})?;
builder.add_storage_device(
device_id,
Disk { device_spec, backend_spec },
)?;
}
ComponentV0::VirtioNic(nic) => {
let (_, backend_spec) = viona_backends
.remove_entry(&nic.backend_id)
.ok_or_else(|| {
ApiSpecError::NetworkBackendNotFound {
backend: nic.backend_id.clone(),
device: device_id.clone(),
}
})?;
builder.add_network_device(
device_id,
Nic { device_spec: nic, backend_spec },
)?;
}
ComponentV0::SerialPort(port) => {
builder.add_serial_port(device_id, port.num)?;
}
ComponentV0::PciPciBridge(bridge) => {
builder.add_pci_bridge(device_id, bridge)?;
}
ComponentV0::QemuPvpanic(pvpanic) => {
builder.add_pvpanic_device(QemuPvpanic {
id: device_id,
spec: pvpanic,
})?;
}
ComponentV0::BootSettings(settings) => {
// The builder returns an error if its caller tries to add
// a boot option that isn't in the set of attached disks.
// Since there may be more disk devices left in the
// component map, just capture the boot order for now and
// apply it to the builder later.
boot_settings = Some((device_id, settings));
}
#[cfg(not(feature = "failure-injection"))]
ComponentV0::MigrationFailureInjector(_) => {
return Err(ApiSpecError::FeatureCompiledOut {
component: device_id,
feature: "failure-injection",
});
}
#[cfg(feature = "failure-injection")]
ComponentV0::MigrationFailureInjector(mig) => {
builder.add_migration_failure_device(MigrationFailure {
id: device_id,
spec: mig,
})?;
}
#[cfg(not(feature = "falcon"))]
ComponentV0::SoftNpuPciPort(_)
| ComponentV0::SoftNpuPort(_)
| ComponentV0::SoftNpuP9(_)
| ComponentV0::P9fs(_) => {
return Err(ApiSpecError::FeatureCompiledOut {
component: device_id,
feature: "falcon",
});
}
#[cfg(feature = "falcon")]
ComponentV0::SoftNpuPciPort(port) => {
builder.set_softnpu_pci_port(port)?;
}
#[cfg(feature = "falcon")]
ComponentV0::SoftNpuPort(port) => {
let (_, backend_spec) = dlpi_backends
.remove_entry(&port.backend_id)
.ok_or_else(|| {
ApiSpecError::NetworkBackendNotFound {
backend: port.backend_id.clone(),
device: device_id.clone(),
}
})?;
let port = SoftNpuPort {
link_name: port.link_name,
backend_name: port.backend_id,
backend_spec,
};
builder.add_softnpu_port(device_id, port)?;
}
#[cfg(feature = "falcon")]
ComponentV0::SoftNpuP9(p9) => {
builder.set_softnpu_p9(p9)?;
}
#[cfg(feature = "falcon")]
ComponentV0::P9fs(p9fs) => {
builder.set_p9fs(p9fs)?;
}
ComponentV0::CrucibleStorageBackend(_)
| ComponentV0::FileStorageBackend(_)
| ComponentV0::BlobStorageBackend(_)
| ComponentV0::VirtioNetworkBackend(_)
| ComponentV0::DlpiNetworkBackend(_) => {
unreachable!("already filtered out backends")
}
}
}
// Now that all disks have been attached, try to establish the boot
// order if one was supplied.
if let Some(settings) = boot_settings {
builder.add_boot_order(
settings.0,
settings.1.order.into_iter().map(Into::into),
)?;
}
if let Some(backend) = storage_backends.into_keys().next() {
return Err(ApiSpecError::BackendNotUsed(backend));
}
if let Some(backend) = viona_backends.into_keys().next() {
return Err(ApiSpecError::BackendNotUsed(backend));
}
if let Some(backend) = dlpi_backends.into_keys().next() {
return Err(ApiSpecError::BackendNotUsed(backend));
}
Ok(builder.finish())
}
}