-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathrpc_interface.rs
More file actions
1305 lines (1214 loc) · 49.6 KB
/
rpc_interface.rs
File metadata and controls
1305 lines (1214 loc) · 49.6 KB
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use std::fmt::{self, Debug};
use std::sync::{Arc, Mutex, MutexGuard};
use serde_json::Value;
use utils::time::{ClockType, get_time_us};
use super::builder::build_and_boot_microvm;
use super::persist::{create_snapshot, restore_from_snapshot};
use super::resources::VmResources;
use super::{Vmm, VmmError};
use crate::EventManager;
use crate::builder::StartMicrovmError;
use crate::cpu_config::templates::{CustomCpuTemplate, GuestConfigError};
use crate::devices::virtio::balloon::device::{HintingStatus, StartHintingCmd};
use crate::devices::virtio::mem::VirtioMemStatus;
use crate::logger::{LoggerConfig, info, warn, *};
use crate::mmds::data_store::{self, Mmds, MmdsDatastoreError};
use crate::persist::{CreateSnapshotError, RestoreFromSnapshotError, VmInfo};
use crate::resources::VmmConfig;
use crate::seccomp::BpfThreadMap;
use crate::vmm_config::balloon::{
BalloonConfigError, BalloonDeviceConfig, BalloonStats, BalloonUpdateConfig,
BalloonUpdateStatsConfig,
};
use crate::vmm_config::boot_source::{BootSourceConfig, BootSourceConfigError};
use crate::vmm_config::drive::{BlockDeviceConfig, BlockDeviceUpdateConfig, DriveError};
use crate::vmm_config::entropy::{EntropyDeviceConfig, EntropyDeviceError};
use crate::vmm_config::instance_info::InstanceInfo;
use crate::vmm_config::machine_config::{MachineConfig, MachineConfigError, MachineConfigUpdate};
use crate::vmm_config::memory_hotplug::{
MemoryHotplugConfig, MemoryHotplugConfigError, MemoryHotplugSizeUpdate,
};
use crate::vmm_config::metrics::{MetricsConfig, MetricsConfigError};
use crate::vmm_config::mmds::{MmdsConfig, MmdsConfigError};
use crate::vmm_config::net::{
NetworkInterfaceConfig, NetworkInterfaceError, NetworkInterfaceUpdateConfig,
};
use crate::vmm_config::pmem::{PmemConfig, PmemConfigError};
use crate::vmm_config::serial::SerialConfig;
use crate::vmm_config::snapshot::{CreateSnapshotParams, LoadSnapshotParams, SnapshotType};
use crate::vmm_config::vsock::{VsockConfigError, VsockDeviceConfig};
use crate::vmm_config::{self, RateLimiterUpdate};
/// This enum represents the public interface of the VMM. Each action contains various
/// bits of information (ids, paths, etc.).
#[derive(Debug, PartialEq, Eq)]
pub enum VmmAction {
/// Configure the boot source of the microVM using as input the `ConfigureBootSource`. This
/// action can only be called before the microVM has booted.
ConfigureBootSource(BootSourceConfig),
/// Configure the logger using as input the `LoggerConfig`. This action can only be called
/// before the microVM has booted.
ConfigureLogger(LoggerConfig),
/// Configure the metrics using as input the `MetricsConfig`. This action can only be called
/// before the microVM has booted.
ConfigureMetrics(MetricsConfig),
/// Configure the serial device. This action can only be called before the microVM has booted.
ConfigureSerial(SerialConfig),
/// Create a snapshot using as input the `CreateSnapshotParams`. This action can only be called
/// after the microVM has booted and only when the microVM is in `Paused` state.
CreateSnapshot(CreateSnapshotParams),
/// Get the balloon device configuration.
GetBalloonConfig,
/// Get the ballon device latest statistics.
GetBalloonStats,
/// Get complete microVM configuration in JSON format.
GetFullVmConfig,
/// Get MMDS contents.
GetMMDS,
/// Get the machine configuration of the microVM.
GetVmMachineConfig,
/// Get microVM instance information.
GetVmInstanceInfo,
/// Get microVM version.
GetVmmVersion,
/// Flush the metrics. This action can only be called after the logger has been configured.
FlushMetrics,
/// Add a new block device or update one that already exists using the `BlockDeviceConfig` as
/// input. This action can only be called before the microVM has booted.
InsertBlockDevice(BlockDeviceConfig),
/// Add a virtio-pmem device.
InsertPmemDevice(PmemConfig),
/// Add a new network interface config or update one that already exists using the
/// `NetworkInterfaceConfig` as input. This action can only be called before the microVM has
/// booted.
InsertNetworkDevice(NetworkInterfaceConfig),
/// Load the microVM state using as input the `LoadSnapshotParams`. This action can only be
/// called before the microVM has booted. If this action is successful, the loaded microVM will
/// be in `Paused` state. Should change this state to `Resumed` for the microVM to run.
LoadSnapshot(LoadSnapshotParams),
/// Partial update of the MMDS contents.
PatchMMDS(Value),
/// Pause the guest, by pausing the microVM VCPUs.
Pause,
/// Repopulate the MMDS contents.
PutMMDS(Value),
/// Configure the guest vCPU features.
PutCpuConfiguration(CustomCpuTemplate),
/// Resume the guest, by resuming the microVM VCPUs.
Resume,
/// Set the balloon device or update the one that already exists using the
/// `BalloonDeviceConfig` as input. This action can only be called before the microVM
/// has booted.
SetBalloonDevice(BalloonDeviceConfig),
/// Set the MMDS configuration.
SetMmdsConfiguration(MmdsConfig),
/// Set the vsock device or update the one that already exists using the
/// `VsockDeviceConfig` as input. This action can only be called before the microVM has
/// booted.
SetVsockDevice(VsockDeviceConfig),
/// Set the entropy device using `EntropyDeviceConfig` as input. This action can only be called
/// before the microVM has booted.
SetEntropyDevice(EntropyDeviceConfig),
/// Get the memory hotplug device configuration and status.
GetMemoryHotplugStatus,
/// Set the memory hotplug device using `MemoryHotplugConfig` as input. This action can only be
/// called before the microVM has booted.
SetMemoryHotplugDevice(MemoryHotplugConfig),
/// Updates the memory hotplug device using `MemoryHotplugConfigUpdate` as input. This action
/// can only be called after the microVM has booted.
UpdateMemoryHotplugSize(MemoryHotplugSizeUpdate),
/// Launch the microVM. This action can only be called before the microVM has booted.
StartMicroVm,
/// Send CTRL+ALT+DEL to the microVM, using the i8042 keyboard function. If an AT-keyboard
/// driver is listening on the guest end, this can be used to shut down the microVM gracefully.
#[cfg(target_arch = "x86_64")]
SendCtrlAltDel,
/// Update the balloon size, after microVM start.
UpdateBalloon(BalloonUpdateConfig),
/// Update the balloon statistics polling interval, after microVM start.
UpdateBalloonStatistics(BalloonUpdateStatsConfig),
/// Start a free page hinting run
StartFreePageHinting(StartHintingCmd),
/// Retrieve the status of the hinting run
GetFreePageHintingStatus,
/// Stops a free page hinting run
StopFreePageHinting,
/// Update existing block device properties such as `path_on_host` or `rate_limiter`.
UpdateBlockDevice(BlockDeviceUpdateConfig),
/// Update a network interface, after microVM start. Currently, the only updatable properties
/// are the RX and TX rate limiters.
UpdateNetworkInterface(NetworkInterfaceUpdateConfig),
/// Update the microVM configuration (memory & vcpu) using `VmUpdateConfig` as input. This
/// action can only be called before the microVM has booted.
UpdateMachineConfiguration(MachineConfigUpdate),
}
/// Wrapper for all errors associated with VMM actions.
#[derive(Debug, thiserror::Error, displaydoc::Display)]
pub enum VmmActionError {
/// Balloon config error: {0}
BalloonConfig(#[from] BalloonConfigError),
/// Balloon update error: {0}
BalloonUpdate(VmmError),
/// Boot source error: {0}
BootSource(#[from] BootSourceConfigError),
/// Create snapshot error: {0}
CreateSnapshot(#[from] CreateSnapshotError),
/// Configure CPU error: {0}
ConfigureCpu(#[from] GuestConfigError),
/// Drive config error: {0}
DriveConfig(#[from] DriveError),
/// Entropy device error: {0}
EntropyDevice(#[from] EntropyDeviceError),
/// Pmem device error: {0}
PmemDevice(#[from] PmemConfigError),
/// Memory hotplug config error: {0}
MemoryHotplugConfig(#[from] MemoryHotplugConfigError),
/// Memory hotplug update error: {0}
MemoryHotplugUpdate(VmmError),
/// Internal VMM error: {0}
InternalVmm(#[from] VmmError),
/// Load snapshot error: {0}
LoadSnapshot(#[from] LoadSnapshotError),
/// Logger error: {0}
Logger(#[from] crate::logger::LoggerUpdateError),
/// Machine config error: {0}
MachineConfig(#[from] MachineConfigError),
/// Metrics error: {0}
Metrics(#[from] MetricsConfigError),
#[from(ignore)]
/// MMDS error: {0}
Mmds(#[from] data_store::MmdsDatastoreError),
/// MMMDS config error: {0}
MmdsConfig(#[from] MmdsConfigError),
#[from(ignore)]
/// MMDS limit exceeded error: {0}
MmdsLimitExceeded(data_store::MmdsDatastoreError),
/// Network config error: {0}
NetworkConfig(#[from] NetworkInterfaceError),
/// The requested operation is not supported: {0}
NotSupported(String),
/// The requested operation is not supported after starting the microVM.
OperationNotSupportedPostBoot,
/// The requested operation is not supported before starting the microVM.
OperationNotSupportedPreBoot,
/// Start microvm error: {0}
StartMicrovm(#[from] StartMicrovmError),
/// Vsock config error: {0}
VsockConfig(#[from] VsockConfigError),
}
/// The enum represents the response sent by the VMM in case of success. The response is either
/// empty, when no data needs to be sent, or an internal VMM structure.
#[allow(clippy::large_enum_variant)]
#[derive(Debug, PartialEq, Eq)]
pub enum VmmData {
/// The balloon device configuration.
BalloonConfig(BalloonDeviceConfig),
/// The latest balloon device statistics.
BalloonStats(BalloonStats),
/// No data is sent on the channel.
Empty,
/// The complete microVM configuration in JSON format.
FullVmConfig(VmmConfig),
/// The microVM configuration represented by `VmConfig`.
MachineConfiguration(MachineConfig),
/// Mmds contents.
MmdsValue(serde_json::Value),
/// The microVM instance information.
InstanceInformation(InstanceInfo),
/// The microVM version.
VmmVersion(String),
/// The status of the memory hotplug device.
VirtioMemStatus(VirtioMemStatus),
/// The status of the virtio-balloon hinting run
HintingStatus(HintingStatus),
}
fn mmds_patch_data(
mut mmds: MutexGuard<'_, Mmds>,
value: serde_json::Value,
) -> Result<VmmData, VmmActionError> {
mmds.patch_data(value)
.map(|()| VmmData::Empty)
.map_err(|err| match err {
data_store::MmdsDatastoreError::DataStoreLimitExceeded => {
VmmActionError::MmdsLimitExceeded(
data_store::MmdsDatastoreError::DataStoreLimitExceeded,
)
}
_ => VmmActionError::Mmds(err),
})
}
fn mmds_put_data(
mut mmds: MutexGuard<'_, Mmds>,
value: serde_json::Value,
) -> Result<VmmData, VmmActionError> {
mmds.put_data(value)
.map(|()| VmmData::Empty)
.map_err(|err| match err {
data_store::MmdsDatastoreError::DataStoreLimitExceeded => {
VmmActionError::MmdsLimitExceeded(
data_store::MmdsDatastoreError::DataStoreLimitExceeded,
)
}
_ => VmmActionError::Mmds(err),
})
}
/// Enables pre-boot setup and instantiation of a Firecracker VMM.
pub struct PrebootApiController<'a> {
seccomp_filters: &'a BpfThreadMap,
instance_info: InstanceInfo,
vm_resources: &'a mut VmResources,
event_manager: &'a mut EventManager,
/// The [`Vmm`] object constructed through requests
pub built_vmm: Option<Arc<Mutex<Vmm>>>,
// Configuring boot specific resources will set this to true.
// Loading from snapshot will not be allowed once this is true.
boot_path: bool,
// Some PrebootApiRequest errors are irrecoverable and Firecracker
// should cleanly teardown if they occur.
fatal_error: Option<BuildMicrovmFromRequestsError>,
}
// TODO Remove when `EventManager` implements `std::fmt::Debug`.
impl fmt::Debug for PrebootApiController<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PrebootApiController")
.field("seccomp_filters", &self.seccomp_filters)
.field("instance_info", &self.instance_info)
.field("vm_resources", &self.vm_resources)
.field("event_manager", &"?")
.field("built_vmm", &self.built_vmm)
.field("boot_path", &self.boot_path)
.field("fatal_error", &self.fatal_error)
.finish()
}
}
/// Error type for [`PrebootApiController::load_snapshot`]
#[derive(Debug, thiserror::Error, displaydoc::Display)]
pub enum LoadSnapshotError {
/// Loading a microVM snapshot not allowed after configuring boot-specific resources.
LoadSnapshotNotAllowed,
/// Failed to restore from snapshot: {0}
RestoreFromSnapshot(#[from] RestoreFromSnapshotError),
/// Failed to resume microVM: {0}
ResumeMicrovm(#[from] VmmError),
}
/// Shorthand type for a request containing a boxed VmmAction.
pub type ApiRequest = Box<VmmAction>;
/// Shorthand type for a response containing a boxed Result.
pub type ApiResponse = Box<Result<VmmData, VmmActionError>>;
/// Error type for `PrebootApiController::build_microvm_from_requests`.
#[derive(Debug, thiserror::Error, displaydoc::Display)]
pub enum BuildMicrovmFromRequestsError {
/// Configuring MMDS failed: {0}.
ConfigureMmds(#[from] MmdsConfigError),
/// Populating MMDS from file failed: {0}.
PopulateMmds(#[from] data_store::MmdsDatastoreError),
/// Loading snapshot failed.
Restore,
/// Resuming MicroVM after loading snapshot failed.
Resume,
}
impl<'a> PrebootApiController<'a> {
/// Constructor for the PrebootApiController.
pub fn new(
seccomp_filters: &'a BpfThreadMap,
instance_info: InstanceInfo,
vm_resources: &'a mut VmResources,
event_manager: &'a mut EventManager,
) -> Self {
Self {
seccomp_filters,
instance_info,
vm_resources,
event_manager,
built_vmm: None,
boot_path: false,
fatal_error: None,
}
}
/// Default implementation for the function that builds and starts a microVM.
///
/// Returns a populated `VmResources` object and a running `Vmm` object.
#[allow(clippy::too_many_arguments)]
pub fn build_microvm_from_requests(
seccomp_filters: &BpfThreadMap,
event_manager: &mut EventManager,
instance_info: InstanceInfo,
from_api: &std::sync::mpsc::Receiver<ApiRequest>,
to_api: &std::sync::mpsc::Sender<ApiResponse>,
api_event_fd: &vmm_sys_util::eventfd::EventFd,
boot_timer_enabled: bool,
pci_enabled: bool,
mmds_size_limit: usize,
metadata_json: Option<&str>,
) -> Result<Arc<Mutex<Vmm>>, BuildMicrovmFromRequestsError> {
let mut vm_resources = VmResources {
boot_timer: boot_timer_enabled,
mmds_size_limit,
pci_enabled,
..Default::default()
};
// Init the data store from file, if present.
if let Some(data) = metadata_json {
vm_resources.locked_mmds_or_default()?.put_data(
serde_json::from_str(data).expect("MMDS error: metadata provided not valid json"),
)?;
info!("Successfully added metadata to mmds from file");
}
let mut preboot_controller = PrebootApiController::new(
seccomp_filters,
instance_info,
&mut vm_resources,
event_manager,
);
// Configure and start microVM through successive API calls.
// Iterate through API calls to configure microVm.
// The loop breaks when a microVM is successfully started, and a running Vmm is built.
while preboot_controller.built_vmm.is_none() {
// Get request
let req = from_api
.recv()
.expect("The channel's sending half was disconnected. Cannot receive data.");
// Also consume the API event along with the message. It is safe to unwrap()
// because this event_fd is blocking.
api_event_fd
.read()
.expect("VMM: Failed to read the API event_fd");
// Process the request.
let res = preboot_controller.handle_preboot_request(*req);
// Send back the response.
to_api.send(Box::new(res)).expect("one-shot channel closed");
// If any fatal errors were encountered, break the loop.
if let Some(preboot_error) = preboot_controller.fatal_error {
return Err(preboot_error);
}
}
// Safe to unwrap because previous loop cannot end on None.
let vmm = preboot_controller.built_vmm.unwrap();
Ok(vmm)
}
/// Handles the incoming preboot request and provides a response for it.
/// Returns a built/running `Vmm` after handling a successful `StartMicroVm` request.
pub fn handle_preboot_request(
&mut self,
request: VmmAction,
) -> Result<VmmData, VmmActionError> {
use self::VmmAction::*;
match request {
// Supported operations allowed pre-boot.
ConfigureBootSource(config) => self.set_boot_source(config),
ConfigureLogger(logger_cfg) => crate::logger::LOGGER
.update(logger_cfg)
.map(|()| VmmData::Empty)
.map_err(VmmActionError::Logger),
ConfigureMetrics(metrics_cfg) => vmm_config::metrics::init_metrics(metrics_cfg)
.map(|()| VmmData::Empty)
.map_err(VmmActionError::Metrics),
ConfigureSerial(serial_cfg) => {
self.vm_resources.serial_out_path = serial_cfg.serial_out_path;
Ok(VmmData::Empty)
}
GetBalloonConfig => self.balloon_config(),
GetFullVmConfig => {
warn!(
"If the VM was restored from snapshot, boot-source, machine-config.smt, and \
machine-config.cpu_template will all be empty."
);
Ok(VmmData::FullVmConfig((&*self.vm_resources).into()))
}
GetMMDS => Ok(VmmData::MmdsValue(
self.vm_resources
.locked_mmds_or_default()
.map_err(VmmActionError::MmdsConfig)?
.data_store_value(),
)),
GetVmMachineConfig => Ok(VmmData::MachineConfiguration(
self.vm_resources.machine_config.clone(),
)),
GetVmInstanceInfo => Ok(VmmData::InstanceInformation(self.instance_info.clone())),
GetVmmVersion => Ok(VmmData::VmmVersion(self.instance_info.vmm_version.clone())),
InsertBlockDevice(config) => self.insert_block_device(config),
InsertPmemDevice(config) => self.insert_pmem_device(config),
InsertNetworkDevice(config) => self.insert_net_device(config),
LoadSnapshot(config) => self
.load_snapshot(&config)
.map_err(VmmActionError::LoadSnapshot),
PatchMMDS(value) => mmds_patch_data(
self.vm_resources
.locked_mmds_or_default()
.map_err(VmmActionError::MmdsConfig)?,
value,
),
PutCpuConfiguration(custom_cpu_template) => {
self.set_custom_cpu_template(custom_cpu_template)
}
PutMMDS(value) => mmds_put_data(
self.vm_resources
.locked_mmds_or_default()
.map_err(VmmActionError::MmdsConfig)?,
value,
),
SetBalloonDevice(config) => self.set_balloon_device(config),
SetVsockDevice(config) => self.set_vsock_device(config),
SetMmdsConfiguration(config) => self.set_mmds_config(config),
StartMicroVm => self.start_microvm(),
UpdateMachineConfiguration(config) => self.update_machine_config(config),
SetEntropyDevice(config) => self.set_entropy_device(config),
SetMemoryHotplugDevice(config) => self.set_memory_hotplug_device(config),
// Operations not allowed pre-boot.
CreateSnapshot(_)
| FlushMetrics
| Pause
| Resume
| GetBalloonStats
| GetMemoryHotplugStatus
| UpdateBalloon(_)
| UpdateBalloonStatistics(_)
| UpdateBlockDevice(_)
| UpdateMemoryHotplugSize(_)
| UpdateNetworkInterface(_)
| StartFreePageHinting(_)
| GetFreePageHintingStatus
| StopFreePageHinting => Err(VmmActionError::OperationNotSupportedPreBoot),
#[cfg(target_arch = "x86_64")]
SendCtrlAltDel => Err(VmmActionError::OperationNotSupportedPreBoot),
}
}
fn balloon_config(&mut self) -> Result<VmmData, VmmActionError> {
self.vm_resources
.balloon
.get_config()
.map(VmmData::BalloonConfig)
.map_err(VmmActionError::BalloonConfig)
}
fn insert_block_device(&mut self, cfg: BlockDeviceConfig) -> Result<VmmData, VmmActionError> {
self.boot_path = true;
self.vm_resources
.set_block_device(cfg)
.map(|()| VmmData::Empty)
.map_err(VmmActionError::DriveConfig)
}
fn insert_net_device(
&mut self,
cfg: NetworkInterfaceConfig,
) -> Result<VmmData, VmmActionError> {
self.boot_path = true;
self.vm_resources
.build_net_device(cfg)
.map(|()| VmmData::Empty)
.map_err(VmmActionError::NetworkConfig)
}
fn insert_pmem_device(&mut self, cfg: PmemConfig) -> Result<VmmData, VmmActionError> {
self.boot_path = true;
self.vm_resources
.build_pmem_device(cfg)
.map(|()| VmmData::Empty)
.map_err(VmmActionError::PmemDevice)
}
fn set_balloon_device(&mut self, cfg: BalloonDeviceConfig) -> Result<VmmData, VmmActionError> {
self.boot_path = true;
self.vm_resources
.set_balloon_device(cfg)
.map(|()| VmmData::Empty)
.map_err(VmmActionError::BalloonConfig)
}
fn set_boot_source(&mut self, cfg: BootSourceConfig) -> Result<VmmData, VmmActionError> {
self.boot_path = true;
self.vm_resources
.build_boot_source(cfg)
.map(|()| VmmData::Empty)
.map_err(VmmActionError::BootSource)
}
fn set_mmds_config(&mut self, cfg: MmdsConfig) -> Result<VmmData, VmmActionError> {
self.boot_path = true;
self.vm_resources
.set_mmds_config(cfg, &self.instance_info.id)
.map(|()| VmmData::Empty)
.map_err(VmmActionError::MmdsConfig)
}
fn update_machine_config(
&mut self,
cfg: MachineConfigUpdate,
) -> Result<VmmData, VmmActionError> {
self.boot_path = true;
self.vm_resources
.update_machine_config(&cfg)
.map(|()| VmmData::Empty)
.map_err(VmmActionError::MachineConfig)
}
fn set_custom_cpu_template(
&mut self,
cpu_template: CustomCpuTemplate,
) -> Result<VmmData, VmmActionError> {
self.vm_resources.set_custom_cpu_template(cpu_template);
Ok(VmmData::Empty)
}
fn set_vsock_device(&mut self, cfg: VsockDeviceConfig) -> Result<VmmData, VmmActionError> {
self.boot_path = true;
self.vm_resources
.set_vsock_device(cfg)
.map(|()| VmmData::Empty)
.map_err(VmmActionError::VsockConfig)
}
fn set_entropy_device(&mut self, cfg: EntropyDeviceConfig) -> Result<VmmData, VmmActionError> {
self.boot_path = true;
self.vm_resources.build_entropy_device(cfg)?;
Ok(VmmData::Empty)
}
fn set_memory_hotplug_device(
&mut self,
cfg: MemoryHotplugConfig,
) -> Result<VmmData, VmmActionError> {
self.boot_path = true;
self.vm_resources.set_memory_hotplug_config(cfg)?;
Ok(VmmData::Empty)
}
// On success, this command will end the pre-boot stage and this controller
// will be replaced by a runtime controller.
fn start_microvm(&mut self) -> Result<VmmData, VmmActionError> {
build_and_boot_microvm(
&self.instance_info,
self.vm_resources,
self.event_manager,
self.seccomp_filters,
)
.map(|vmm| {
self.built_vmm = Some(vmm);
VmmData::Empty
})
.map_err(VmmActionError::StartMicrovm)
}
// On success, this command will end the pre-boot stage and this controller
// will be replaced by a runtime controller.
fn load_snapshot(
&mut self,
load_params: &LoadSnapshotParams,
) -> Result<VmmData, LoadSnapshotError> {
let load_start_us = get_time_us(ClockType::Monotonic);
if self.boot_path {
let err = LoadSnapshotError::LoadSnapshotNotAllowed;
info!("{}", err);
return Err(err);
}
// Restore VM from snapshot
let vmm = restore_from_snapshot(
&self.instance_info,
self.event_manager,
self.seccomp_filters,
load_params,
self.vm_resources,
)
.inspect_err(|_| {
// If restore fails, we consider the process is too dirty to recover.
self.fatal_error = Some(BuildMicrovmFromRequestsError::Restore);
})?;
// Resume VM
if load_params.resume_vm {
vmm.lock()
.expect("Poisoned lock")
.resume_vm()
.inspect_err(|_| {
// If resume fails, we consider the process is too dirty to recover.
self.fatal_error = Some(BuildMicrovmFromRequestsError::Resume);
})?;
}
// Set the VM
self.built_vmm = Some(vmm);
debug!(
"'load snapshot' VMM action took {} us.",
update_metric_with_elapsed_time(&METRICS.latencies_us.vmm_load_snapshot, load_start_us)
);
Ok(VmmData::Empty)
}
}
/// Enables RPC interaction with a running Firecracker VMM.
#[derive(Debug)]
pub struct RuntimeApiController {
vmm: Arc<Mutex<Vmm>>,
}
impl RuntimeApiController {
/// Handles the incoming runtime `VmmAction` request and provides a response for it.
pub fn handle_request(&mut self, request: VmmAction) -> Result<VmmData, VmmActionError> {
use self::VmmAction::*;
match request {
// Supported operations allowed post-boot.
CreateSnapshot(snapshot_create_cfg) => self.create_snapshot(&snapshot_create_cfg),
FlushMetrics => self.flush_metrics(),
GetBalloonConfig => self
.vmm
.lock()
.expect("Poisoned lock")
.balloon_config()
.map(|state| VmmData::BalloonConfig(BalloonDeviceConfig::from(state)))
.map_err(VmmActionError::InternalVmm),
GetBalloonStats => self
.vmm
.lock()
.expect("Poisoned lock")
.latest_balloon_stats()
.map(VmmData::BalloonStats)
.map_err(VmmActionError::InternalVmm),
GetFullVmConfig => Ok(VmmData::FullVmConfig(
self.vmm.lock().expect("Poisoned lock").full_config(),
)),
GetMemoryHotplugStatus => self
.vmm
.lock()
.expect("Poisoned lock")
.memory_hotplug_status()
.map(VmmData::VirtioMemStatus)
.map_err(VmmActionError::InternalVmm),
GetMMDS => Ok(VmmData::MmdsValue(
self.vmm
.lock()
.expect("Poisoned lock")
.get_mmds()
.ok_or(VmmActionError::Mmds(MmdsDatastoreError::NotInitialized))?
.lock()
.expect("Poisoned lock")
.data_store_value(),
)),
GetVmMachineConfig => Ok(VmmData::MachineConfiguration(
self.vmm
.lock()
.expect("Poisoned lock")
.machine_config
.clone(),
)),
GetVmInstanceInfo => Ok(VmmData::InstanceInformation(
self.vmm.lock().expect("Poisoned lock").instance_info(),
)),
GetVmmVersion => Ok(VmmData::VmmVersion(
self.vmm.lock().expect("Poisoned lock").version(),
)),
PatchMMDS(value) => mmds_patch_data(
self.vmm
.lock()
.expect("Poisoned lock")
.get_mmds()
.ok_or(VmmActionError::Mmds(MmdsDatastoreError::NotInitialized))?
.lock()
.expect("Poisoned lock"),
value,
),
Pause => self.pause(),
PutMMDS(value) => mmds_put_data(
self.vmm
.lock()
.expect("Poisoned lock")
.get_mmds()
.ok_or(VmmActionError::Mmds(MmdsDatastoreError::NotInitialized))?
.lock()
.expect("Poisoned lock"),
value,
),
Resume => self.resume(),
#[cfg(target_arch = "x86_64")]
SendCtrlAltDel => self.send_ctrl_alt_del(),
UpdateBalloon(balloon_update) => self
.vmm
.lock()
.expect("Poisoned lock")
.update_balloon_config(balloon_update.amount_mib)
.map(|_| VmmData::Empty)
.map_err(VmmActionError::BalloonUpdate),
UpdateBalloonStatistics(balloon_stats_update) => self
.vmm
.lock()
.expect("Poisoned lock")
.update_balloon_stats_config(balloon_stats_update.stats_polling_interval_s)
.map(|_| VmmData::Empty)
.map_err(VmmActionError::BalloonUpdate),
StartFreePageHinting(cmd) => self
.vmm
.lock()
.expect("Poisoned lock")
.start_balloon_hinting(cmd)
.map(|_| VmmData::Empty)
.map_err(VmmActionError::BalloonUpdate),
GetFreePageHintingStatus => self
.vmm
.lock()
.expect("Poisoned lock")
.get_balloon_hinting_status()
.map(VmmData::HintingStatus)
.map_err(VmmActionError::BalloonUpdate),
StopFreePageHinting => self
.vmm
.lock()
.expect("Poisoned lock")
.stop_balloon_hinting()
.map(|_| VmmData::Empty)
.map_err(VmmActionError::BalloonUpdate),
UpdateBlockDevice(new_cfg) => self.update_block_device(new_cfg),
UpdateNetworkInterface(netif_update) => self.update_net_rate_limiters(netif_update),
UpdateMemoryHotplugSize(cfg) => self
.vmm
.lock()
.expect("Poisoned lock")
.update_memory_hotplug_size(cfg.requested_size_mib)
.map(|_| VmmData::Empty)
.map_err(VmmActionError::MemoryHotplugUpdate),
// Operations not allowed post-boot.
ConfigureBootSource(_)
| ConfigureLogger(_)
| ConfigureMetrics(_)
| ConfigureSerial(_)
| InsertBlockDevice(_)
| InsertPmemDevice(_)
| InsertNetworkDevice(_)
| LoadSnapshot(_)
| PutCpuConfiguration(_)
| SetBalloonDevice(_)
| SetVsockDevice(_)
| SetMmdsConfiguration(_)
| SetEntropyDevice(_)
| SetMemoryHotplugDevice(_)
| StartMicroVm
| UpdateMachineConfiguration(_) => Err(VmmActionError::OperationNotSupportedPostBoot),
}
}
/// Creates a new `RuntimeApiController`.
pub fn new(vmm: Arc<Mutex<Vmm>>) -> Self {
Self { vmm }
}
/// Pauses the microVM by pausing the vCPUs.
pub fn pause(&mut self) -> Result<VmmData, VmmActionError> {
let pause_start_us = get_time_us(ClockType::Monotonic);
self.vmm.lock().expect("Poisoned lock").pause_vm()?;
let elapsed_time_us =
update_metric_with_elapsed_time(&METRICS.latencies_us.vmm_pause_vm, pause_start_us);
info!("'pause vm' VMM action took {} us.", elapsed_time_us);
Ok(VmmData::Empty)
}
/// Resumes the microVM by resuming the vCPUs.
pub fn resume(&mut self) -> Result<VmmData, VmmActionError> {
let resume_start_us = get_time_us(ClockType::Monotonic);
self.vmm.lock().expect("Poisoned lock").resume_vm()?;
let elapsed_time_us =
update_metric_with_elapsed_time(&METRICS.latencies_us.vmm_resume_vm, resume_start_us);
info!("'resume vm' VMM action took {} us.", elapsed_time_us);
Ok(VmmData::Empty)
}
/// Write the metrics on user demand (flush). We use the word `flush` here to highlight the fact
/// that the metrics will be written immediately.
/// Defer to inner Vmm. We'll move to a variant where the Vmm simply exposes functionality like
/// getting the dirty pages, and then we'll have the metrics flushing logic entirely on the
/// outside.
fn flush_metrics(&mut self) -> Result<VmmData, VmmActionError> {
// FIXME: we're losing the bool saying whether metrics were actually written.
METRICS
.write()
.map(|_| VmmData::Empty)
.map_err(super::VmmError::Metrics)
.map_err(VmmActionError::InternalVmm)
}
/// Injects CTRL+ALT+DEL keystroke combo to the inner Vmm (if present).
#[cfg(target_arch = "x86_64")]
fn send_ctrl_alt_del(&mut self) -> Result<VmmData, VmmActionError> {
self.vmm
.lock()
.expect("Poisoned lock")
.send_ctrl_alt_del()
.map(|()| VmmData::Empty)
.map_err(VmmActionError::InternalVmm)
}
fn create_snapshot(
&mut self,
create_params: &CreateSnapshotParams,
) -> Result<VmmData, VmmActionError> {
if create_params.snapshot_type == SnapshotType::Diff {
log_dev_preview_warning("Virtual machine diff snapshots", None);
}
let mut locked_vmm = self.vmm.lock().unwrap();
let vm_info = VmInfo::from(&*locked_vmm);
let create_start_us = get_time_us(ClockType::Monotonic);
create_snapshot(&mut locked_vmm, &vm_info, create_params)?;
match create_params.snapshot_type {
SnapshotType::Full => {
let elapsed_time_us = update_metric_with_elapsed_time(
&METRICS.latencies_us.vmm_full_create_snapshot,
create_start_us,
);
info!(
"'create full snapshot' VMM action took {} us.",
elapsed_time_us
);
}
SnapshotType::Diff => {
let elapsed_time_us = update_metric_with_elapsed_time(
&METRICS.latencies_us.vmm_diff_create_snapshot,
create_start_us,
);
info!(
"'create diff snapshot' VMM action took {} us.",
elapsed_time_us
);
}
}
Ok(VmmData::Empty)
}
/// Updates block device properties:
/// - path of the host file backing the emulated block device, update the disk image on the
/// device and its virtio configuration
/// - rate limiter configuration.
fn update_block_device(
&mut self,
new_cfg: BlockDeviceUpdateConfig,
) -> Result<VmmData, VmmActionError> {
let mut vmm = self.vmm.lock().expect("Poisoned lock");
// vhost-user-block updates
if new_cfg.path_on_host.is_none() && new_cfg.rate_limiter.is_none() {
vmm.update_vhost_user_block_config(&new_cfg.drive_id)
.map_err(DriveError::DeviceUpdate)?;
}
// virtio-block updates
if let Some(new_path) = new_cfg.path_on_host {
vmm.update_block_device_path(&new_cfg.drive_id, new_path)
.map_err(DriveError::DeviceUpdate)?;
}
if new_cfg.rate_limiter.is_some() {
vmm.update_block_rate_limiter(
&new_cfg.drive_id,
RateLimiterUpdate::from(new_cfg.rate_limiter).bandwidth,
RateLimiterUpdate::from(new_cfg.rate_limiter).ops,
)
.map_err(DriveError::DeviceUpdate)?;
}
Ok(VmmData::Empty)
}
/// Updates configuration for an emulated net device as described in `new_cfg`.
fn update_net_rate_limiters(
&mut self,
new_cfg: NetworkInterfaceUpdateConfig,
) -> Result<VmmData, VmmActionError> {
self.vmm
.lock()
.expect("Poisoned lock")
.update_net_rate_limiters(
&new_cfg.iface_id,
RateLimiterUpdate::from(new_cfg.rx_rate_limiter).bandwidth,
RateLimiterUpdate::from(new_cfg.rx_rate_limiter).ops,
RateLimiterUpdate::from(new_cfg.tx_rate_limiter).bandwidth,
RateLimiterUpdate::from(new_cfg.tx_rate_limiter).ops,
)
.map(|()| VmmData::Empty)
.map_err(NetworkInterfaceError::DeviceUpdate)
.map_err(VmmActionError::NetworkConfig)
}
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use super::*;
use crate::HTTP_MAX_PAYLOAD_SIZE;
use crate::builder::tests::default_vmm;
use crate::devices::virtio::block::CacheType;
use crate::mmds::data_store::MmdsVersion;
use crate::seccomp::BpfThreadMap;
use crate::vmm_config::snapshot::{MemBackendConfig, MemBackendType};
fn default_preboot<'a>(
vm_resources: &'a mut VmResources,
event_manager: &'a mut EventManager,
seccomp_filters: &'a BpfThreadMap,
) -> PrebootApiController<'a> {
let instance_info = InstanceInfo::default();
PrebootApiController::new(seccomp_filters, instance_info, vm_resources, event_manager)
}
fn preboot_request(request: VmmAction) -> Result<VmmData, VmmActionError> {
let mut vm_resources = VmResources::default();
let mut evmgr = EventManager::new().unwrap();
let seccomp_filters = BpfThreadMap::new();
let mut preboot = default_preboot(&mut vm_resources, &mut evmgr, &seccomp_filters);
preboot.handle_preboot_request(request)
}
fn preboot_request_with_mmds(
request: VmmAction,
mmds: Arc<Mutex<Mmds>>,
) -> Result<VmmData, VmmActionError> {
let mut vm_resources = VmResources {
mmds: Some(mmds),