-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathlib.rs
1095 lines (984 loc) · 41.4 KB
/
lib.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
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 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the THIRD-PARTY file.
//! Virtual Machine Monitor that leverages the Linux Kernel-based Virtual Machine (KVM),
//! and other virtualization features to run a single lightweight micro-virtual
//! machine (microVM).
#![warn(missing_docs)]
#![warn(clippy::undocumented_unsafe_blocks)]
#![allow(clippy::blanket_clippy_restriction_lints)]
/// Architecture specific bindings.
#[allow(missing_docs)]
pub mod arch_gen;
/// Implements platform specific functionality.
/// Supported platforms: x86_64 and aarch64.
pub mod arch;
/// High-level interface over Linux io_uring.
///
/// Aims to provide an easy-to-use interface, while making some Firecracker-specific simplifying
/// assumptions. The crate does not currently aim at supporting all io_uring features and use
/// cases. For example, it only works with pre-registered fds and read/write/fsync requests.
///
/// Requires at least kernel version 5.10.51.
/// For more information on io_uring, refer to the man pages.
/// [This pdf](https://kernel.dk/io_uring.pdf) is also very useful, though outdated at times.
pub mod io_uring;
/// # Rate Limiter
///
/// Provides a rate limiter written in Rust useful for IO operations that need to
/// be throttled.
///
/// ## Behavior
///
/// The rate limiter starts off as 'unblocked' with two token buckets configured
/// with the values passed in the `RateLimiter::new()` constructor.
/// All subsequent accounting is done independently for each token bucket based
/// on the `TokenType` used. If any of the buckets runs out of budget, the limiter
/// goes in the 'blocked' state. At this point an internal timer is set up which
/// will later 'wake up' the user in order to retry sending data. The 'wake up'
/// notification will be dispatched as an event on the FD provided by the `AsRawFD`
/// trait implementation.
///
/// The contract is that the user shall also call the `event_handler()` method on
/// receipt of such an event.
///
/// The token buckets are replenished when a called `consume()` doesn't find enough
/// tokens in the bucket. The amount of tokens replenished is automatically calculated
/// to respect the `complete_refill_time` configuration parameter provided by the user.
/// The token buckets will never replenish above their respective `size`.
///
/// Each token bucket can start off with a `one_time_burst` initial extra capacity
/// on top of their `size`. This initial extra credit does not replenish and
/// can be used for an initial burst of data.
///
/// The granularity for 'wake up' events when the rate limiter is blocked is
/// currently hardcoded to `100 milliseconds`.
///
/// ## Limitations
///
/// This rate limiter implementation relies on the *Linux kernel's timerfd* so its
/// usage is limited to Linux systems.
///
/// Another particularity of this implementation is that it is not self-driving.
/// It is meant to be used in an external event loop and thus implements the `AsRawFd`
/// trait and provides an *event-handler* as part of its API. This *event-handler*
/// needs to be called by the user on every event on the rate limiter's `AsRawFd` FD.
pub mod rate_limiter;
/// Module for handling ACPI tables.
/// Currently, we only use ACPI on x86 microVMs.
#[cfg(target_arch = "x86_64")]
pub mod acpi;
/// Handles setup and initialization a `Vmm` object.
pub mod builder;
/// Types for guest configuration.
pub mod cpu_config;
pub(crate) mod device_manager;
/// Emulates virtual and hardware devices.
#[allow(missing_docs)]
pub mod devices;
/// minimalist HTTP/TCP/IPv4 stack named DUMBO
pub mod dumbo;
/// Logger
pub mod logger;
/// microVM Metadata Service MMDS
pub mod mmds;
/// Save/restore utilities.
pub mod persist;
/// Resource store for configured microVM resources.
pub mod resources;
/// microVM RPC API adapters.
pub mod rpc_interface;
/// Seccomp filter utilities.
pub mod seccomp_filters;
/// Signal handling utilities.
pub mod signal_handler;
/// Serialization and deserialization facilities
pub mod snapshot;
/// Utility functions for integration and benchmark testing
pub mod utilities;
/// Wrappers over structures used to configure the VMM.
pub mod vmm_config;
/// Module with virtual state structs.
pub mod vstate;
use std::collections::HashMap;
use std::io;
use std::os::unix::io::AsRawFd;
use std::sync::mpsc::RecvTimeoutError;
use std::sync::{Arc, Barrier, Mutex};
use std::time::Duration;
use device_manager::acpi::ACPIDeviceManager;
use device_manager::resources::ResourceAllocator;
use devices::acpi::vmgenid::VmGenIdError;
use event_manager::{EventManager as BaseEventManager, EventOps, Events, MutEventSubscriber};
use seccompiler::BpfProgram;
#[cfg(target_arch = "x86_64")]
use seccompiler::BpfThreadMap;
use userfaultfd::Uffd;
use utils::epoll::EventSet;
use utils::eventfd::EventFd;
use utils::terminal::Terminal;
use utils::u64_to_usize;
#[cfg(target_arch = "x86_64")]
use vmm_config::hotplug::{HotplugVcpuConfig, HotplugVcpuError};
#[cfg(target_arch = "x86_64")]
use vmm_config::machine_config::{MachineConfigUpdate, MAX_SUPPORTED_VCPUS};
use vstate::vcpu::{self, KvmVcpuConfigureError, StartThreadedError, VcpuSendEventError};
use crate::arch::DeviceType;
use crate::cpu_config::templates::CpuConfiguration;
#[cfg(target_arch = "x86_64")]
use crate::device_manager::legacy::PortIODeviceManager;
use crate::device_manager::mmio::MMIODeviceManager;
use crate::devices::legacy::{IER_RDA_BIT, IER_RDA_OFFSET};
use crate::devices::virtio::balloon::{
Balloon, BalloonConfig, BalloonError, BalloonStats, BALLOON_DEV_ID,
};
use crate::devices::virtio::block::device::Block;
use crate::devices::virtio::net::Net;
use crate::devices::virtio::{TYPE_BALLOON, TYPE_BLOCK, TYPE_NET};
use crate::logger::{error, info, warn, MetricsError, METRICS};
use crate::persist::{MicrovmState, MicrovmStateError, VmInfo};
use crate::rate_limiter::BucketUpdate;
use crate::snapshot::Persist;
use crate::vmm_config::instance_info::{InstanceInfo, VmState};
use crate::vstate::memory::{
GuestMemory, GuestMemoryExtension, GuestMemoryMmap, GuestMemoryRegion,
};
use crate::vstate::vcpu::VcpuState;
pub use crate::vstate::vcpu::{Vcpu, VcpuConfig, VcpuEvent, VcpuHandle, VcpuResponse};
pub use crate::vstate::vm::Vm;
/// Shorthand type for the EventManager flavour used by Firecracker.
pub type EventManager = BaseEventManager<Arc<Mutex<dyn MutEventSubscriber>>>;
// Since the exit code names e.g. `SIGBUS` are most appropriate yet trigger a test error with the
// clippy lint `upper_case_acronyms` we have disabled this lint for this enum.
/// Vmm exit-code type.
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FcExitCode {
/// Success exit code.
Ok = 0,
/// Generic error exit code.
GenericError = 1,
/// Generic exit code error; not possible to occur if the program logic is sound.
UnexpectedError = 2,
/// Firecracker was shut down after intercepting a restricted system call.
BadSyscall = 148,
/// Firecracker was shut down after intercepting `SIGBUS`.
SIGBUS = 149,
/// Firecracker was shut down after intercepting `SIGSEGV`.
SIGSEGV = 150,
/// Firecracker was shut down after intercepting `SIGXFSZ`.
SIGXFSZ = 151,
/// Firecracker was shut down after intercepting `SIGXCPU`.
SIGXCPU = 154,
/// Firecracker was shut down after intercepting `SIGPIPE`.
SIGPIPE = 155,
/// Firecracker was shut down after intercepting `SIGHUP`.
SIGHUP = 156,
/// Firecracker was shut down after intercepting `SIGILL`.
SIGILL = 157,
/// Bad configuration for microvm's resources, when using a single json.
BadConfiguration = 152,
/// Command line arguments parsing error.
ArgParsing = 153,
}
/// Timeout used in recv_timeout, when waiting for a vcpu response on
/// Pause/Resume/Save/Restore. A high enough limit that should not be reached during normal usage,
/// used to detect a potential vcpu deadlock.
pub const RECV_TIMEOUT_SEC: Duration = Duration::from_secs(30);
/// Default byte limit of accepted http requests on API and MMDS servers.
pub const HTTP_MAX_PAYLOAD_SIZE: usize = 51200;
/// Errors associated with the VMM internal logic. These errors cannot be generated by direct user
/// input, but can result from bad configuration of the host (for example if Firecracker doesn't
/// have permissions to open the KVM fd).
#[derive(Debug, thiserror::Error, displaydoc::Display)]
pub enum VmmError {
#[cfg(target_arch = "aarch64")]
/// Invalid command line error.
Cmdline,
/// Device manager error: {0}
DeviceManager(device_manager::mmio::MmioError),
/// Error getting the KVM dirty bitmap. {0}
DirtyBitmap(kvm_ioctls::Error),
/// Event fd error: {0}
EventFd(io::Error),
/// I8042 error: {0}
I8042Error(devices::legacy::I8042DeviceError),
/// Cannot access kernel file: {0}
KernelFile(io::Error),
#[cfg(target_arch = "x86_64")]
/// Cannot add devices to the legacy I/O Bus. {0}
LegacyIOBus(device_manager::legacy::LegacyDeviceError),
/// Metrics error: {0}
Metrics(MetricsError),
/// Cannot add a device to the MMIO Bus. {0}
RegisterMMIODevice(device_manager::mmio::MmioError),
/// Cannot install seccomp filters: {0}
SeccompFilters(seccompiler::InstallationError),
/// Error writing to the serial console: {0}
Serial(io::Error),
/// Error creating timer fd: {0}
TimerFd(io::Error),
/// Error configuring the vcpu for boot: {0}
VcpuConfigure(KvmVcpuConfigureError),
/// Error creating the vcpu: {0}
VcpuCreate(vstate::vcpu::VcpuError),
/// Cannot send event to vCPU. {0}
VcpuEvent(vstate::vcpu::VcpuError),
/// Cannot create a vCPU handle. {0}
VcpuHandle(vstate::vcpu::VcpuError),
#[cfg(target_arch = "aarch64")]
/// Error initializing the vcpu: {0}
VcpuInit(vstate::vcpu::KvmVcpuError),
/// Failed to start vCPUs
VcpuStart(StartVcpusError),
/// Failed to pause the vCPUs.
VcpuPause,
/// Failed to exit the vCPUs.
VcpuExit,
/// Failed to resume the vCPUs.
VcpuResume,
/// Failed to message the vCPUs.
VcpuMessage,
/// Cannot spawn Vcpu thread: {0}
VcpuSpawn(io::Error),
/// Vm error: {0}
Vm(vstate::vm::VmError),
/// Error thrown by observer object on Vmm initialization: {0}
VmmObserverInit(utils::errno::Error),
/// Error thrown by observer object on Vmm teardown: {0}
VmmObserverTeardown(utils::errno::Error),
/// VMGenID error: {0}
VMGenID(#[from] VmGenIdError),
}
/// Shorthand type for KVM dirty page bitmap.
pub type DirtyBitmap = HashMap<usize, Vec<u64>>;
/// Returns the size of guest memory, in MiB.
pub(crate) fn mem_size_mib(guest_memory: &GuestMemoryMmap) -> u64 {
guest_memory.iter().map(|region| region.len()).sum::<u64>() >> 20
}
// Error type for [`Vmm::emulate_serial_init`].
/// Emulate serial init error: {0}
#[derive(Debug, thiserror::Error, displaydoc::Display)]
pub struct EmulateSerialInitError(#[from] std::io::Error);
/// Error type for [`Vmm::start_vcpus`].
#[derive(Debug, thiserror::Error, displaydoc::Display)]
pub enum StartVcpusError {
/// VMM observer init error: {0}
VmmObserverInit(#[from] utils::errno::Error),
/// Vcpu handle error: {0}
VcpuHandle(#[from] StartThreadedError),
}
/// Error type for [`Vmm::dump_cpu_config()`]
#[derive(Debug, thiserror::Error, displaydoc::Display)]
pub enum DumpCpuConfigError {
/// Failed to send event to vcpu thread: {0}
SendEvent(#[from] VcpuSendEventError),
/// Got unexpected response from vcpu thread.
UnexpectedResponse,
/// Failed to dump CPU config: {0}
DumpCpuConfig(#[from] vcpu::VcpuError),
/// Operation not allowed: {0}
NotAllowed(String),
}
/// Contains the state and associated methods required for the Firecracker VMM.
#[derive(Debug)]
pub struct Vmm {
events_observer: Option<std::io::Stdin>,
instance_info: InstanceInfo,
shutdown_exit_code: Option<FcExitCode>,
// Guest VM core resources.
vm: Vm,
guest_memory: GuestMemoryMmap,
// Save UFFD in order to keep it open in the Firecracker process, as well.
// Since this field is never read again, we need to allow `dead_code`.
#[allow(dead_code)]
uffd: Option<Uffd>,
vcpus_handles: Vec<VcpuHandle>,
// Used by Vcpus and devices to initiate teardown; Vmm should never write here.
vcpus_exit_evt: EventFd,
// Used to configure kvm vcpus during hotplugging.
#[cfg(target_arch = "x86_64")]
vcpu_config: Option<VcpuConfig>,
// seccomp_filters are only needed in VMM for hotplugging vCPUS.
#[cfg(target_arch = "x86_64")]
seccomp_filters: BpfThreadMap,
// Allocator for guest resources
resource_allocator: ResourceAllocator,
// Guest VM devices.
mmio_device_manager: MMIODeviceManager,
#[cfg(target_arch = "x86_64")]
pio_device_manager: PortIODeviceManager,
acpi_device_manager: ACPIDeviceManager,
}
impl Vmm {
/// Gets Vmm version.
pub fn version(&self) -> String {
self.instance_info.vmm_version.clone()
}
/// Gets Vmm instance info.
pub fn instance_info(&self) -> InstanceInfo {
self.instance_info.clone()
}
/// Provides the Vmm shutdown exit code if there is one.
pub fn shutdown_exit_code(&self) -> Option<FcExitCode> {
self.shutdown_exit_code
}
/// Gets the specified bus device.
pub fn get_bus_device(
&self,
device_type: DeviceType,
device_id: &str,
) -> Option<&devices::bus::BusDevice> {
self.mmio_device_manager.get_device(device_type, device_id)
}
/// Starts the microVM vcpus.
///
/// # Errors
///
/// When:
/// - [`vmm::VmmEventsObserver::on_vmm_boot`] errors.
/// - [`vmm::vstate::vcpu::Vcpu::start_threaded`] errors.
pub fn start_vcpus(
&mut self,
mut vcpus: Vec<Vcpu>,
vcpu_seccomp_filter: Arc<BpfProgram>,
) -> Result<(), StartVcpusError> {
let vcpu_count = vcpus.len();
let barrier = Arc::new(Barrier::new(vcpu_count + 1));
if let Some(stdin) = self.events_observer.as_mut() {
// Set raw mode for stdin.
stdin.lock().set_raw_mode().map_err(|err| {
warn!("Cannot set raw mode for the terminal. {:?}", err);
err
})?;
// Set non blocking stdin.
stdin.lock().set_non_block(true).map_err(|err| {
warn!("Cannot set non block for the terminal. {:?}", err);
err
})?;
}
Vcpu::register_kick_signal_handler();
self.vcpus_handles.reserve(vcpu_count);
for mut vcpu in vcpus.drain(..) {
vcpu.set_mmio_bus(self.mmio_device_manager.bus.clone());
#[cfg(target_arch = "x86_64")]
vcpu.kvm_vcpu
.set_pio_bus(self.pio_device_manager.io_bus.clone());
self.vcpus_handles
.push(vcpu.start_threaded(vcpu_seccomp_filter.clone(), barrier.clone())?);
}
self.instance_info.state = VmState::Paused;
// Wait for vCPUs to initialize their TLS before moving forward.
barrier.wait();
Ok(())
}
/// Sends a resume command to the vCPUs.
pub fn resume_vm(&mut self) -> Result<(), VmmError> {
self.mmio_device_manager.kick_devices();
self.resume_vcpu_threads(0)?;
self.instance_info.state = VmState::Running;
Ok(())
}
/// Resume vCPU threads
fn resume_vcpu_threads(&mut self, start_idx: usize) -> Result<(), VmmError> {
if start_idx >= self.vcpus_handles.len() {
return Err(VmmError::VcpuMessage);
}
self.vcpus_handles[start_idx..]
.iter()
.try_for_each(|handle| handle.send_event(VcpuEvent::Resume))
.map_err(|_| VmmError::VcpuMessage)?;
// Check the responses.
if self.vcpus_handles[start_idx..]
.iter()
.map(|handle| handle.response_receiver().recv_timeout(RECV_TIMEOUT_SEC))
.any(|response| !matches!(response, Ok(VcpuResponse::Resumed)))
{
return Err(VmmError::VcpuMessage);
}
Ok(())
}
/// Sends a pause command to the vCPUs.
pub fn pause_vm(&mut self) -> Result<(), VmmError> {
// Send the events.
self.vcpus_handles
.iter()
.try_for_each(|handle| handle.send_event(VcpuEvent::Pause))
.map_err(|_| VmmError::VcpuMessage)?;
// Check the responses.
if self
.vcpus_handles
.iter()
.map(|handle| handle.response_receiver().recv_timeout(RECV_TIMEOUT_SEC))
.any(|response| !matches!(response, Ok(VcpuResponse::Paused)))
{
return Err(VmmError::VcpuMessage);
}
self.instance_info.state = VmState::Paused;
Ok(())
}
/// Returns a reference to the inner `GuestMemoryMmap` object.
pub fn guest_memory(&self) -> &GuestMemoryMmap {
&self.guest_memory
}
/// Sets RDA bit in serial console
pub fn emulate_serial_init(&self) -> Result<(), EmulateSerialInitError> {
// When restoring from a previously saved state, there is no serial
// driver initialization, therefore the RDA (Received Data Available)
// interrupt is not enabled. Because of that, the driver won't get
// notified of any bytes that we send to the guest. The clean solution
// would be to save the whole serial device state when we do the vm
// serialization. For now we set that bit manually
#[cfg(target_arch = "aarch64")]
{
let serial_bus_device = self.get_bus_device(DeviceType::Serial, "Serial");
let Some(serial) = serial_bus_device else {
return Ok(());
};
let mut serial = serial.serial_ref().expect("Unexpected device type");
serial
.serial
.write(IER_RDA_OFFSET, IER_RDA_BIT)
.map_err(|_| EmulateSerialInitError(std::io::Error::last_os_error()))?;
Ok(())
}
#[cfg(target_arch = "x86_64")]
{
let serial = &mut self
.pio_device_manager
.stdio_serial
.lock()
.expect("Poisoned lock");
serial
.serial
.write(IER_RDA_OFFSET, IER_RDA_BIT)
.map_err(|_| EmulateSerialInitError(std::io::Error::last_os_error()))?;
Ok(())
}
}
/// Injects CTRL+ALT+DEL keystroke combo in the i8042 device.
#[cfg(target_arch = "x86_64")]
pub fn send_ctrl_alt_del(&mut self) -> Result<(), VmmError> {
self.pio_device_manager
.i8042
.lock()
.expect("Poisoned lock")
.trigger_ctrl_alt_del()
.map_err(VmmError::I8042Error)
}
/// Saves the state of a paused Microvm.
pub fn save_state(&mut self, vm_info: &VmInfo) -> Result<MicrovmState, MicrovmStateError> {
use self::MicrovmStateError::SaveVmState;
let vcpu_states = self.save_vcpu_states()?;
let vm_state = {
#[cfg(target_arch = "x86_64")]
{
self.vm.save_state().map_err(SaveVmState)?
}
#[cfg(target_arch = "aarch64")]
{
let mpidrs = construct_kvm_mpidrs(&vcpu_states);
self.vm.save_state(&mpidrs).map_err(SaveVmState)?
}
};
let device_states = self.mmio_device_manager.save();
let memory_state = self.guest_memory().describe();
let acpi_dev_state = self.acpi_device_manager.save();
Ok(MicrovmState {
vm_info: vm_info.clone(),
memory_state,
vm_state,
vcpu_states,
device_states,
acpi_dev_state,
})
}
fn save_vcpu_states(&mut self) -> Result<Vec<VcpuState>, MicrovmStateError> {
for handle in self.vcpus_handles.iter() {
handle
.send_event(VcpuEvent::SaveState)
.map_err(MicrovmStateError::SignalVcpu)?;
}
let vcpu_responses = self
.vcpus_handles
.iter()
// `Iterator::collect` can transform a `Vec<Result>` into a `Result<Vec>`.
.map(|handle| handle.response_receiver().recv_timeout(RECV_TIMEOUT_SEC))
.collect::<Result<Vec<VcpuResponse>, RecvTimeoutError>>()
.map_err(|_| MicrovmStateError::UnexpectedVcpuResponse)?;
let vcpu_states = vcpu_responses
.into_iter()
.map(|response| match response {
VcpuResponse::SavedState(state) => Ok(*state),
VcpuResponse::Error(err) => Err(MicrovmStateError::SaveVcpuState(err)),
VcpuResponse::NotAllowed(reason) => Err(MicrovmStateError::NotAllowed(reason)),
_ => Err(MicrovmStateError::UnexpectedVcpuResponse),
})
.collect::<Result<Vec<VcpuState>, MicrovmStateError>>()?;
Ok(vcpu_states)
}
/// Dumps CPU configuration.
pub fn dump_cpu_config(&mut self) -> Result<Vec<CpuConfiguration>, DumpCpuConfigError> {
for handle in self.vcpus_handles.iter() {
handle
.send_event(VcpuEvent::DumpCpuConfig)
.map_err(DumpCpuConfigError::SendEvent)?;
}
let vcpu_responses = self
.vcpus_handles
.iter()
.map(|handle| handle.response_receiver().recv_timeout(RECV_TIMEOUT_SEC))
.collect::<Result<Vec<VcpuResponse>, RecvTimeoutError>>()
.map_err(|_| DumpCpuConfigError::UnexpectedResponse)?;
let cpu_configs = vcpu_responses
.into_iter()
.map(|response| match response {
VcpuResponse::DumpedCpuConfig(cpu_config) => Ok(*cpu_config),
VcpuResponse::Error(err) => Err(DumpCpuConfigError::DumpCpuConfig(err)),
VcpuResponse::NotAllowed(reason) => Err(DumpCpuConfigError::NotAllowed(reason)),
_ => Err(DumpCpuConfigError::UnexpectedResponse),
})
.collect::<Result<Vec<CpuConfiguration>, DumpCpuConfigError>>()?;
Ok(cpu_configs)
}
/// Adds new vCPUs to VMM.
#[cfg(target_arch = "x86_64")]
pub fn hotplug_vcpus(
&mut self,
config: HotplugVcpuConfig,
) -> Result<MachineConfigUpdate, HotplugVcpuError> {
use crate::logger::IncMetric;
if config.target > MAX_SUPPORTED_VCPUS {
return Err(HotplugVcpuError::VcpuCountTooHigh);
}
if let Some(kvm_config) = self.vcpu_config.as_mut() {
kvm_config.vcpu_count = config.target;
}
// Create and start new vcpus
let mut vcpus = Vec::with_capacity(config.target.into());
#[allow(clippy::cast_possible_truncation)]
let start_idx = self.vcpus_handles.len().try_into().unwrap();
if let Some(devices::BusDevice::CpuContainer(cont)) =
self.get_bus_device(DeviceType::CpuContainer, "CpuContainer")
{
let mut locked_container = cont.lock().expect("Poisoned lock");
for cpu_idx in start_idx..config.target {
let exit_evt = self
.vcpus_exit_evt
.try_clone()
.map_err(HotplugVcpuError::EventFd)?;
let mut vcpu =
Vcpu::new(cpu_idx, &self.vm, exit_evt).map_err(HotplugVcpuError::VcpuCreate)?;
if let Some(kvm_config) = self.vcpu_config.as_ref() {
vcpu.kvm_vcpu.hotplug_configure(kvm_config)?;
} else {
return Err(HotplugVcpuError::RestoredFromSnapshot);
}
locked_container.cpu_devices[cpu_idx as usize].inserting = true;
vcpus.push(vcpu);
}
}
self.start_vcpus(
vcpus,
self.seccomp_filters
.get("vcpu")
.ok_or_else(|| HotplugVcpuError::MissingSeccompFilters("vcpu".to_string()))?
.clone(),
)
.map_err(HotplugVcpuError::VcpuStart)?;
#[allow(clippy::cast_lossless)]
METRICS
.hotplug
.vcpus_added
.add(self.vcpus_handles.len() as u64 - config.target as u64);
// Update VM config to reflect new CPUs added
#[allow(clippy::cast_possible_truncation)]
let new_machine_config = MachineConfigUpdate {
vcpu_count: Some(self.vcpus_handles.len() as u8),
mem_size_mib: None,
smt: None,
cpu_template: None,
track_dirty_pages: None,
huge_pages: None,
};
self.resume_vcpu_threads(start_idx.into())?;
self.acpi_device_manager.notify_cpu_container()?;
Ok(new_machine_config)
}
/// Removes vCPUs from VMM.
#[cfg(target_arch = "x86_64")]
pub fn hotunplug_vcpus(
&mut self,
config: HotplugVcpuConfig,
) -> Result<MachineConfigUpdate, HotplugVcpuError> {
use crate::logger::IncMetric;
if config.target < 1 {
return Err(HotplugVcpuError::VcpuCountTooLow);
}
if let Some(kvm_config) = self.vcpu_config.as_mut() {
kvm_config.vcpu_count = config.target;
}
#[allow(clippy::cast_possible_truncation)]
let start_idx: u8 = config.target;
if let Some(devices::BusDevice::CpuContainer(cont)) =
self.get_bus_device(DeviceType::CpuContainer, "CpuContainer")
{
let mut locked_container = cont.lock().expect("Poisoned lock");
for cpu_idx in start_idx..u8::try_from(self.vcpus_handles.len()).unwrap() {
locked_container.cpu_devices[cpu_idx as usize].removing = true;
}
}
#[allow(clippy::cast_lossless)]
METRICS
.hotplug
.vcpus_added
.add(self.vcpus_handles.len() as u64 - config.target as u64);
// Update VM config to reflect new CPUs added
#[allow(clippy::cast_possible_truncation)]
let new_machine_config = MachineConfigUpdate {
vcpu_count: Some(self.vcpus_handles.len() as u8),
mem_size_mib: None,
smt: None,
cpu_template: None,
track_dirty_pages: None,
huge_pages: None,
};
self.acpi_device_manager.notify_cpu_container()?;
Ok(new_machine_config)
}
/// Retrieves the KVM dirty bitmap for each of the guest's memory regions.
pub fn reset_dirty_bitmap(&self) {
self.guest_memory
.iter()
.enumerate()
.for_each(|(slot, region)| {
let _ = self
.vm
.fd()
.get_dirty_log(u32::try_from(slot).unwrap(), u64_to_usize(region.len()));
});
}
/// Retrieves the KVM dirty bitmap for each of the guest's memory regions.
pub fn get_dirty_bitmap(&self) -> Result<DirtyBitmap, VmmError> {
let mut bitmap: DirtyBitmap = HashMap::new();
self.guest_memory
.iter()
.enumerate()
.try_for_each(|(slot, region)| {
let bitmap_region = self
.vm
.fd()
.get_dirty_log(u32::try_from(slot).unwrap(), u64_to_usize(region.len()))?;
bitmap.insert(slot, bitmap_region);
Ok(())
})
.map_err(VmmError::DirtyBitmap)?;
Ok(bitmap)
}
/// Enables or disables KVM dirty page tracking.
pub fn set_dirty_page_tracking(&mut self, enable: bool) -> Result<(), VmmError> {
// This function _always_ results in an ioctl update. The VMM is stateless in the sense
// that it's unaware of the current dirty page tracking setting.
// The VMM's consumer will need to cache the dirty tracking setting internally. For
// example, if this function were to be exposed through the VMM controller, the VMM
// resources should cache the flag.
self.vm
.set_kvm_memory_regions(&self.guest_memory, enable)
.map_err(VmmError::Vm)
}
/// Updates the path of the host file backing the emulated block device with id `drive_id`.
/// We update the disk image on the device and its virtio configuration.
pub fn update_block_device_path(
&mut self,
drive_id: &str,
path_on_host: String,
) -> Result<(), VmmError> {
self.mmio_device_manager
.with_virtio_device_with_id(TYPE_BLOCK, drive_id, |block: &mut Block| {
block
.update_disk_image(path_on_host)
.map_err(|err| err.to_string())
})
.map_err(VmmError::DeviceManager)
}
/// Updates the rate limiter parameters for block device with `drive_id` id.
pub fn update_block_rate_limiter(
&mut self,
drive_id: &str,
rl_bytes: BucketUpdate,
rl_ops: BucketUpdate,
) -> Result<(), VmmError> {
self.mmio_device_manager
.with_virtio_device_with_id(TYPE_BLOCK, drive_id, |block: &mut Block| {
block
.update_rate_limiter(rl_bytes, rl_ops)
.map_err(|err| err.to_string())
})
.map_err(VmmError::DeviceManager)
}
/// Updates the rate limiter parameters for block device with `drive_id` id.
pub fn update_vhost_user_block_config(&mut self, drive_id: &str) -> Result<(), VmmError> {
self.mmio_device_manager
.with_virtio_device_with_id(TYPE_BLOCK, drive_id, |block: &mut Block| {
block.update_config().map_err(|err| err.to_string())
})
.map_err(VmmError::DeviceManager)
}
/// Updates the rate limiter parameters for net device with `net_id` id.
pub fn update_net_rate_limiters(
&mut self,
net_id: &str,
rx_bytes: BucketUpdate,
rx_ops: BucketUpdate,
tx_bytes: BucketUpdate,
tx_ops: BucketUpdate,
) -> Result<(), VmmError> {
self.mmio_device_manager
.with_virtio_device_with_id(TYPE_NET, net_id, |net: &mut Net| {
net.patch_rate_limiters(rx_bytes, rx_ops, tx_bytes, tx_ops);
Ok(())
})
.map_err(VmmError::DeviceManager)
}
/// Returns a reference to the balloon device if present.
pub fn balloon_config(&self) -> Result<BalloonConfig, BalloonError> {
if let Some(busdev) = self.get_bus_device(DeviceType::Virtio(TYPE_BALLOON), BALLOON_DEV_ID)
{
let virtio_device = busdev
.mmio_transport_ref()
.expect("Unexpected device type")
.device();
let config = virtio_device
.lock()
.expect("Poisoned lock")
.as_mut_any()
.downcast_mut::<Balloon>()
.unwrap()
.config();
Ok(config)
} else {
Err(BalloonError::DeviceNotFound)
}
}
/// Returns the latest balloon statistics if they are enabled.
pub fn latest_balloon_stats(&self) -> Result<BalloonStats, BalloonError> {
if let Some(busdev) = self.get_bus_device(DeviceType::Virtio(TYPE_BALLOON), BALLOON_DEV_ID)
{
let virtio_device = busdev
.mmio_transport_ref()
.expect("Unexpected device type")
.device();
let latest_stats = virtio_device
.lock()
.expect("Poisoned lock")
.as_mut_any()
.downcast_mut::<Balloon>()
.unwrap()
.latest_stats()
.ok_or(BalloonError::StatisticsDisabled)
.cloned()?;
Ok(latest_stats)
} else {
Err(BalloonError::DeviceNotFound)
}
}
/// Updates configuration for the balloon device target size.
pub fn update_balloon_config(&mut self, amount_mib: u32) -> Result<(), BalloonError> {
// The balloon cannot have a target size greater than the size of
// the guest memory.
if u64::from(amount_mib) > mem_size_mib(self.guest_memory()) {
return Err(BalloonError::TooManyPagesRequested);
}
if let Some(busdev) = self.get_bus_device(DeviceType::Virtio(TYPE_BALLOON), BALLOON_DEV_ID)
{
{
let virtio_device = busdev
.mmio_transport_ref()
.expect("Unexpected device type")
.device();
virtio_device
.lock()
.expect("Poisoned lock")
.as_mut_any()
.downcast_mut::<Balloon>()
.unwrap()
.update_size(amount_mib)?;
Ok(())
}
} else {
Err(BalloonError::DeviceNotFound)
}
}
/// Updates configuration for the balloon device as described in `balloon_stats_update`.
pub fn update_balloon_stats_config(
&mut self,
stats_polling_interval_s: u16,
) -> Result<(), BalloonError> {
if let Some(busdev) = self.get_bus_device(DeviceType::Virtio(TYPE_BALLOON), BALLOON_DEV_ID)
{
{
let virtio_device = busdev
.mmio_transport_ref()
.expect("Unexpected device type")
.device();
virtio_device
.lock()
.expect("Poisoned lock")
.as_mut_any()
.downcast_mut::<Balloon>()
.unwrap()
.update_stats_polling_interval(stats_polling_interval_s)?;
}
Ok(())
} else {
Err(BalloonError::DeviceNotFound)
}
}
/// Add the vcpu configuration used during boot to the VMM. This is required as part of the
/// hotplugging process, to correctly configure KVM vCPUs.
#[cfg(target_arch = "x86_64")]
pub fn attach_vcpu_config(&mut self, vcpu_config: VcpuConfig) {
self.vcpu_config = Some(vcpu_config)
}
/// Signals Vmm to stop and exit.
pub fn stop(&mut self, exit_code: FcExitCode) {
// To avoid cycles, all teardown paths take the following route:
// +------------------------+----------------------------+------------------------+
// | Vmm | Action | Vcpu |
// +------------------------+----------------------------+------------------------+
// 1 | | | vcpu.exit(exit_code) |
// 2 | | | vcpu.exit_evt.write(1) |
// 3 | | <--- EventFd::exit_evt --- | |
// 4 | vmm.stop() | | |
// 5 | | --- VcpuEvent::Finish ---> | |
// 6 | | | StateMachine::finish() |
// 7 | VcpuHandle::join() | | |
// 8 | vmm.shutdown_exit_code becomes Some(exit_code) breaking the main event loop |
// +------------------------+----------------------------+------------------------+
// Vcpu initiated teardown starts from `fn Vcpu::exit()` (step 1).
// Vmm initiated teardown starts from `pub fn Vmm::stop()` (step 4).
// Once `vmm.shutdown_exit_code` becomes `Some(exit_code)`, it is the upper layer's
// responsibility to break main event loop and propagate the exit code value.
info!("Vmm is stopping.");
// We send a "Finish" event. If a VCPU has already exited, this is the only
// message it will accept... but running and paused will take it as well.
// It breaks out of the state machine loop so that the thread can be joined.
for (idx, handle) in self.vcpus_handles.iter().enumerate() {
if let Err(err) = handle.send_event(VcpuEvent::Finish) {
error!("Failed to send VcpuEvent::Finish to vCPU {}: {}", idx, err);
}
}
// The actual thread::join() that runs to release the thread's resource is done in
// the VcpuHandle's Drop trait. We can trigger that to happen now by clearing the
// list of handles. Do it here instead of Vmm::Drop to avoid dependency cycles.
// (Vmm's Drop will also check if this list is empty).
self.vcpus_handles.clear();
// Break the main event loop, propagating the Vmm exit-code.
self.shutdown_exit_code = Some(exit_code);
}
}
/// Process the content of the MPIDR_EL1 register in order to be able to pass it to KVM
///
/// The kernel expects to find the four affinity levels of the MPIDR in the first 32 bits of the
/// VGIC register attribute:
/// https://elixir.free-electrons.com/linux/v4.14.203/source/virt/kvm/arm/vgic/vgic-kvm-device.c#L445.
///
/// The format of the MPIDR_EL1 register is:
/// | 39 .... 32 | 31 .... 24 | 23 .... 16 | 15 .... 8 | 7 .... 0 |
/// | Aff3 | Other | Aff2 | Aff1 | Aff0 |
///
/// The KVM mpidr format is:
/// | 63 .... 56 | 55 .... 48 | 47 .... 40 | 39 .... 32 |
/// | Aff3 | Aff2 | Aff1 | Aff0 |
/// As specified in the linux kernel: Documentation/virt/kvm/devices/arm-vgic-v3.rst
#[cfg(target_arch = "aarch64")]
fn construct_kvm_mpidrs(vcpu_states: &[VcpuState]) -> Vec<u64> {