msb_krun_vmm 0.1.27

Virtual machine monitor for msb_krun microVMs
Documentation
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
// Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//#![deny(warnings)]

#[cfg(feature = "tee")]
use std::fs::File;
#[cfg(feature = "tee")]
use std::io::BufReader;
#[cfg(not(target_os = "windows"))]
use std::os::fd::RawFd;
use std::path::PathBuf;
use std::time::Duration;

#[cfg(feature = "tee")]
use serde::{Deserialize, Serialize};

#[cfg(feature = "blk")]
use crate::vmm_config::block::{BlockBuilder, BlockConfigError, BlockDeviceConfig};
use crate::vmm_config::external_kernel::ExternalKernel;
use crate::vmm_config::firmware::FirmwareConfig;
#[cfg(not(feature = "tee"))]
use crate::vmm_config::fs::*;
use crate::vmm_config::kernel_bundle::InitrdBundle;
use crate::vmm_config::kernel_bundle::{KernelBundle, KernelBundleError};
#[cfg(feature = "tee")]
use crate::vmm_config::kernel_bundle::{QbootBundle, QbootBundleError};
use crate::vmm_config::kernel_cmdline::{KernelCmdlineConfig, KernelCmdlineConfigError};
#[cfg(any(target_os = "linux", target_os = "windows"))]
use crate::vmm_config::machine_config::HostCpuId;
use crate::vmm_config::machine_config::{VmConfig, VmConfigError};
#[cfg(feature = "net")]
use crate::vmm_config::net::{NetBuilder, NetworkInterfaceConfig, NetworkInterfaceError};
use crate::vmm_config::vsock::*;
use crate::vstate::VcpuConfig;
#[cfg(feature = "gpu")]
use devices::virtio::display::DisplayInfo;
#[cfg(feature = "tee")]
use kbs_types::Tee;
#[cfg(feature = "gpu")]
use krun_display::DisplayBackend;
use utils::metrics::MetricsWriter;

type Result<E> = std::result::Result<(), E>;

#[cfg(target_os = "windows")]
pub use crate::vmm_config::vsock::TsiFlags;
#[cfg(not(target_os = "windows"))]
pub use devices::virtio::TsiFlags;

/// Errors encountered when configuring microVM resources.
#[derive(Debug)]
pub enum Error {
    /// JSON is invalid.
    InvalidJson,
    /// Boot source configuration error.
    KernelCmdline(KernelCmdlineConfigError),
    /// Error opening TEE config file.
    #[cfg(feature = "tee")]
    OpenTeeConfig(std::io::Error),
    /// Error parsing TEE config file.
    #[cfg(feature = "tee")]
    ParseTeeConfig(serde_json::Error),
    /// microVM vCpus or memory configuration error.
    VmConfig(VmConfigError),
    /// Vsock device configuration error.
    VsockDevice(VsockConfigError),
}

#[cfg(feature = "tee")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TeeConfig {
    pub workload_id: String,
    pub cpus: u8,
    pub ram_mib: usize,
    pub tee: Tee,
    pub tee_data: String,
    pub attestation_url: String,
}

#[cfg(feature = "tee")]
impl Default for TeeConfig {
    fn default() -> Self {
        Self {
            workload_id: "".to_string(),
            cpus: 0,
            ram_mib: 0,
            tee: Tee::Sev,
            tee_data: "".to_string(),
            attestation_url: "".to_string(),
        }
    }
}

#[cfg(not(target_os = "windows"))]
pub struct SerialConsoleConfig {
    pub input_fd: RawFd,
    pub output_fd: RawFd,
}

#[cfg(not(target_os = "windows"))]
pub struct DefaultVirtioConsoleConfig {
    pub input_fd: RawFd,
    pub output_fd: RawFd,
    pub err_fd: RawFd,
}

#[cfg(not(target_os = "windows"))]
pub enum VirtioConsoleConfigMode {
    Autoconfigure(DefaultVirtioConsoleConfig),
    Explicit(Vec<PortConfig>),
}

#[cfg(target_os = "windows")]
pub enum VirtioConsoleConfigMode {
    Explicit(Vec<PortConfig>),
}

#[cfg(not(target_os = "windows"))]
pub enum PortConfig {
    Tty {
        name: String,
        tty_fd: RawFd,
    },
    InOut {
        name: String,
        input_fd: RawFd,
        output_fd: RawFd,
    },
    Custom {
        name: String,
        input: Box<dyn devices::virtio::port_io::PortInput + Send>,
        output: Box<dyn devices::virtio::port_io::PortOutput + Send>,
    },
}

#[cfg(target_os = "windows")]
pub enum PortConfig {
    ConsoleOutputFile { path: PathBuf },
    NamedPipe { name: String, pipe_name: String },
}

/// Configuration for the vsock device
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub enum VsockConfig {
    /// Default behavior - vsock created implicitly with heuristics-based TSI
    #[default]
    Implicit,
    /// Explicit configuration with specified TSI features
    Explicit { tsi_flags: TsiFlags },
    /// Vsock device disabled
    Disabled,
}

/// A data structure that encapsulates the device configurations
/// held in the Vmm.
pub struct VmResources {
    /// The vCpu and memory configuration for this microVM.
    vm_config: VmConfig,
    /// Resolved host logical processor for every possible vCPU thread.
    #[cfg(any(target_os = "linux", target_os = "windows"))]
    pub vcpu_affinity: Option<Vec<HostCpuId>>,
    /// The firmware to be loaded into the microVM.
    pub firmware_config: Option<FirmwareConfig>,
    /// The kernel command line for this microVM.
    pub kernel_cmdline: KernelCmdlineConfig,
    /// The parameters for the kernel bundle to be loaded in this microVM.
    pub kernel_bundle: Option<KernelBundle>,
    /// The path to an external kernel, as an alternative to KernelBundle.
    pub external_kernel: Option<ExternalKernel>,
    /// The parameters for the qboot bundle to be loaded in this microVM.
    #[cfg(feature = "tee")]
    pub qboot_bundle: Option<QbootBundle>,
    /// The parameters for the initrd bundle to be loaded in this microVM.
    pub initrd_bundle: Option<InitrdBundle>,
    /// The fs device.
    #[cfg(not(feature = "tee"))]
    pub fs: Vec<FsDeviceConfig>,
    /// Custom filesystem devices.
    #[cfg(not(any(feature = "tee", feature = "aws-nitro")))]
    pub custom_fs: Vec<CustomFsDeviceConfig>,
    /// The vsock device.
    pub vsock: VsockBuilder,
    /// The virtio-blk device.
    #[cfg(feature = "blk")]
    pub block: BlockBuilder,
    /// The network devices builder.
    #[cfg(feature = "net")]
    pub net: NetBuilder,
    /// TEE configuration
    #[cfg(feature = "tee")]
    pub tee_config: TeeConfig,
    /// Flags for the virtio-gpu device.
    pub gpu_virgl_flags: Option<u32>,
    pub gpu_shm_size: Option<usize>,
    #[cfg(feature = "gpu")]
    pub display_backend: Option<DisplayBackend<'static>>,
    #[cfg(feature = "gpu")]
    pub displays: Vec<DisplayInfo>,
    #[cfg(feature = "input")]
    pub input_backends: Vec<(
        krun_input::InputConfigBackend<'static>,
        krun_input::InputEventProviderBackend<'static>,
    )>,
    #[cfg(feature = "snd")]
    /// Enable the virtio-snd device.
    pub snd_device: bool,
    /// File to send console output.
    pub console_output: Option<PathBuf>,
    /// SMBIOS OEM Strings
    pub smbios_oem_strings: Option<Vec<String>>,
    /// Whether to enable nested virtualization.
    pub nested_enabled: bool,
    /// Whether to enable split irqchip
    pub split_irqchip: bool,
    /// Shared metrics state for VMM and device counters.
    pub metrics: MetricsWriter,
    /// Whether to attach the virtio-balloon device.
    pub enable_balloon: bool,
    /// The virtio-mem device backing live memory resize, created by the API
    /// layer when max memory exceeds boot memory. The builder places the
    /// hotplug region and attaches the device; the API layer keeps a clone as
    /// the runtime control handle.
    #[cfg(not(feature = "tee"))]
    pub mem_device: Option<std::sync::Arc<std::sync::Mutex<devices::virtio::Mem>>>,
    /// The CPU capacity device backing live CPU resize, created by the API
    /// layer when max vCPUs exceed the boot count. Also the source of the
    /// enforcement state every vCPU run loop consults.
    #[cfg(not(feature = "tee"))]
    pub cpu_device: Option<std::sync::Arc<std::sync::Mutex<devices::virtio::Cpu>>>,
    /// Guest memory stats polling interval for the virtio-balloon device.
    pub balloon_stats_interval: Option<Duration>,
    /// Whether to attach the virtio-rng device.
    pub enable_rng: bool,
    /// Whether to attach the private microsandbox metrics device.
    pub enable_msb_metrics: bool,
    /// Do not create an implicit console device in the guest
    pub disable_implicit_console: bool,
    /// The console id to use for console= in the kernel cmdline
    pub kernel_console: Option<String>,
    /// Serial consoles to attach to the guest
    #[cfg(not(target_os = "windows"))]
    pub serial_consoles: Vec<SerialConsoleConfig>,
    /// Virtio consoles to attach to the guest
    pub virtio_consoles: Vec<VirtioConsoleConfigMode>,
}

impl Default for VmResources {
    fn default() -> Self {
        Self {
            vm_config: VmConfig::default(),
            #[cfg(any(target_os = "linux", target_os = "windows"))]
            vcpu_affinity: None,
            firmware_config: None,
            kernel_cmdline: KernelCmdlineConfig::default(),
            kernel_bundle: None,
            external_kernel: None,
            #[cfg(feature = "tee")]
            qboot_bundle: None,
            initrd_bundle: None,
            #[cfg(not(feature = "tee"))]
            fs: Vec::new(),
            #[cfg(not(any(feature = "tee", feature = "aws-nitro")))]
            custom_fs: Vec::new(),
            vsock: VsockBuilder::default(),
            #[cfg(feature = "blk")]
            block: BlockBuilder::default(),
            #[cfg(feature = "net")]
            net: NetBuilder::default(),
            #[cfg(feature = "tee")]
            tee_config: TeeConfig::default(),
            gpu_virgl_flags: None,
            gpu_shm_size: None,
            #[cfg(feature = "gpu")]
            display_backend: None,
            #[cfg(feature = "gpu")]
            displays: Vec::new(),
            #[cfg(feature = "input")]
            input_backends: Vec::new(),
            #[cfg(feature = "snd")]
            snd_device: false,
            console_output: None,
            smbios_oem_strings: None,
            nested_enabled: false,
            split_irqchip: false,
            metrics: MetricsWriter::default(),
            enable_balloon: true,
            #[cfg(not(feature = "tee"))]
            mem_device: None,
            #[cfg(not(feature = "tee"))]
            cpu_device: None,
            balloon_stats_interval: Some(Duration::from_secs(1)),
            enable_rng: true,
            enable_msb_metrics: true,
            disable_implicit_console: false,
            kernel_console: None,
            #[cfg(not(target_os = "windows"))]
            serial_consoles: Vec::new(),
            virtio_consoles: Vec::new(),
        }
    }
}

impl VmResources {
    /// Returns a VcpuConfig based on the vm config.
    pub fn vcpu_config(&self) -> VcpuConfig {
        // The unwraps are ok to use because the values are initialized using defaults if not
        // supplied by the user.
        let vcpu_count = self.vm_config().vcpu_count.unwrap();
        VcpuConfig {
            vcpu_count,
            max_vcpu_count: self.vm_config().max_vcpu_count.unwrap_or(vcpu_count),
            ht_enabled: self.vm_config().ht_enabled.unwrap(),
            cpu_template: self.vm_config().cpu_template,
        }
    }

    /// Returns the VmConfig.
    pub fn vm_config(&self) -> &VmConfig {
        &self.vm_config
    }

    /// Set the machine configuration of the microVM.
    pub fn set_vm_config(&mut self, machine_config: &VmConfig) -> Result<VmConfigError> {
        if machine_config.vcpu_count == Some(0) {
            return Err(VmConfigError::InvalidVcpuCount);
        }

        if machine_config.mem_size_mib == Some(0) {
            return Err(VmConfigError::InvalidMemorySize);
        }

        let ht_enabled = machine_config
            .ht_enabled
            .unwrap_or_else(|| self.vm_config.ht_enabled.unwrap());

        let vcpu_count_value = machine_config
            .vcpu_count
            .unwrap_or_else(|| self.vm_config.vcpu_count.unwrap());

        // If hyperthreading is enabled or is to be enabled in this call
        // only allow vcpu count to be 1 or even.
        if ht_enabled && vcpu_count_value > 1 && vcpu_count_value % 2 == 1 {
            return Err(VmConfigError::InvalidVcpuCount);
        }

        if let Some(max_vcpu_count) = machine_config.max_vcpu_count {
            if max_vcpu_count < vcpu_count_value
                || max_vcpu_count > crate::vmm_config::machine_config::MAX_SUPPORTED_VCPUS
            {
                return Err(VmConfigError::InvalidMaxVcpuCount);
            }
            if ht_enabled && max_vcpu_count > 1 && max_vcpu_count % 2 == 1 {
                return Err(VmConfigError::InvalidMaxVcpuCount);
            }
            // Booting a wider possible topology than the online count relies on parked
            // vCPUs waiting for wake-ups (INIT/SIPI on x86, PSCI on aarch64) and on
            // non-TEE boot topology tables. On x86 Windows the AP startup router
            // provides exactly that and on aarch64 Windows WHP's in-hypervisor PSCI
            // does, so only the still-unwired platforms reject.
            #[cfg(any(target_arch = "riscv64", feature = "tee"))]
            if max_vcpu_count > vcpu_count_value {
                return Err(VmConfigError::MaxCapacityUnsupported);
            }
        }

        if let Some(max_mem_size_mib) = machine_config.max_mem_size_mib {
            let mem_size_mib = machine_config
                .mem_size_mib
                .unwrap_or_else(|| self.vm_config.mem_size_mib.unwrap());
            if max_mem_size_mib < mem_size_mib {
                return Err(VmConfigError::InvalidMaxMemorySize);
            }
        }

        // Update all the fields that have a new value.
        self.vm_config.vcpu_count = Some(vcpu_count_value);
        self.vm_config.ht_enabled = Some(ht_enabled);
        self.vm_config.max_vcpu_count = machine_config.max_vcpu_count;
        self.vm_config.max_mem_size_mib = machine_config.max_mem_size_mib;

        if machine_config.mem_size_mib.is_some() {
            self.vm_config.mem_size_mib = machine_config.mem_size_mib;
        }
        let memory_total_bytes = self
            .vm_config
            .mem_size_mib
            .unwrap_or(128)
            .saturating_mul(1024)
            .saturating_mul(1024) as u64;
        self.metrics.set_memory_total_bytes(memory_total_bytes);

        if machine_config.cpu_template.is_some() {
            self.vm_config.cpu_template = machine_config.cpu_template;
        }

        Ok(())
    }

    /// Set the guest kernel cmdline configuration.
    pub fn set_kernel_cmdline(
        &mut self,
        kernel_cmdline_cfg: KernelCmdlineConfig,
    ) -> Result<KernelCmdlineConfigError> {
        self.kernel_cmdline = kernel_cmdline_cfg;
        Ok(())
    }

    pub fn kernel_bundle(&self) -> Option<&KernelBundle> {
        self.kernel_bundle.as_ref()
    }

    pub fn set_kernel_bundle(&mut self, kernel_bundle: KernelBundle) -> Result<KernelBundleError> {
        // Safe because this call just returns the page size and doesn't have any side effects.
        let page_size = utils::page_size();

        if kernel_bundle.host_addr == 0 || (kernel_bundle.host_addr as usize) & (page_size - 1) != 0
        {
            return Err(KernelBundleError::InvalidHostAddress);
        }

        if (kernel_bundle.guest_addr as usize) & (page_size - 1) != 0 {
            return Err(KernelBundleError::InvalidGuestAddress);
        }

        self.kernel_bundle = Some(kernel_bundle);
        Ok(())
    }

    pub fn external_kernel(&self) -> Option<&ExternalKernel> {
        self.external_kernel.as_ref()
    }

    pub fn set_external_kernel(&mut self, external_kernel: ExternalKernel) {
        self.external_kernel = Some(external_kernel);
    }

    pub fn set_firmware_config(&mut self, firmware_config: FirmwareConfig) {
        self.firmware_config = Some(firmware_config);
    }

    #[cfg(feature = "tee")]
    pub fn qboot_bundle(&self) -> Option<&QbootBundle> {
        self.qboot_bundle.as_ref()
    }

    #[cfg(feature = "tee")]
    pub fn set_qboot_bundle(&mut self, qboot_bundle: QbootBundle) -> Result<QbootBundleError> {
        if qboot_bundle.size != 0x10000 {
            return Err(QbootBundleError::InvalidSize);
        }

        self.qboot_bundle = Some(qboot_bundle);
        Ok(())
    }

    pub fn initrd_bundle(&self) -> Option<&InitrdBundle> {
        self.initrd_bundle.as_ref()
    }

    pub fn set_initrd_bundle(&mut self, initrd_bundle: InitrdBundle) -> Result<KernelBundleError> {
        self.initrd_bundle = Some(initrd_bundle);
        Ok(())
    }

    #[cfg(not(feature = "tee"))]
    pub fn add_fs_device(&mut self, config: FsDeviceConfig) {
        self.fs.push(config)
    }

    #[cfg(feature = "blk")]
    pub fn add_block_device(&mut self, config: BlockDeviceConfig) -> Result<BlockConfigError> {
        self.block.insert(config, self.metrics.clone())
    }

    /// Adds a block device with an optional per-device hard dirty-data budget.
    #[cfg(feature = "blk")]
    pub fn add_block_device_with_writeback_limit(
        &mut self,
        config: BlockDeviceConfig,
        writeback_limit_bytes: Option<u64>,
    ) -> Result<BlockConfigError> {
        self.block
            .insert_with_writeback_limit(config, writeback_limit_bytes, self.metrics.clone())
    }

    /// Sets a vsock device to be attached when the VM starts.
    pub fn set_vsock_device(&mut self, config: VsockDeviceConfig) -> Result<VsockConfigError> {
        self.vsock.insert(config)
    }

    pub fn set_gpu_virgl_flags(&mut self, virgl_flags: u32) {
        self.gpu_virgl_flags = Some(virgl_flags);
    }

    pub fn set_gpu_shm_size(&mut self, shm_size: usize) {
        self.gpu_shm_size = Some(shm_size);
    }

    #[cfg(feature = "snd")]
    pub fn set_snd_device(&mut self, enabled: bool) {
        self.snd_device = enabled;
    }

    pub fn set_console_output(&mut self, console_output: PathBuf) {
        self.console_output = Some(console_output);
    }

    /// Sets a network device to be attached when the VM starts.
    #[cfg(feature = "net")]
    pub fn add_network_interface(
        &mut self,
        config: NetworkInterfaceConfig,
    ) -> Result<NetworkInterfaceError> {
        self.net.insert(config)
    }

    #[cfg(feature = "tee")]
    pub fn tee_config(&self) -> &TeeConfig {
        &self.tee_config
    }

    #[cfg(feature = "tee")]
    pub fn set_tee_config(&mut self, filepath: PathBuf) -> Result<Error> {
        let file = File::open(filepath.as_path()).map_err(Error::OpenTeeConfig)?;
        let reader = BufReader::new(file);
        let tee_config: TeeConfig =
            serde_json::from_reader(reader).map_err(Error::ParseTeeConfig)?;

        // Override VmConfig with TeeConfig values
        self.set_vm_config(&VmConfig {
            vcpu_count: Some(tee_config.cpus),
            mem_size_mib: Some(tee_config.ram_mib),
            max_vcpu_count: None,
            max_mem_size_mib: None,
            ht_enabled: Some(false),
            cpu_template: None,
        })
        .map_err(Error::VmConfig)?;

        self.tee_config = tee_config;

        Ok(())
    }
}

#[cfg(all(test, not(target_os = "windows")))]
mod tests {
    #[cfg(feature = "gpu")]
    use crate::resources::DisplayBackendConfig;
    use crate::resources::VmResources;
    use crate::vmm_config::machine_config::{CpuFeaturesTemplate, VmConfig, VmConfigError};
    use crate::vmm_config::vsock::tests::{default_config, TempSockFile};
    use crate::vstate::VcpuConfig;
    use utils::tempfile::TempFile;

    fn default_vm_resources() -> VmResources {
        VmResources::default()
    }

    #[test]
    fn test_vcpu_config() {
        let vm_resources = default_vm_resources();
        let expected_vcpu_config = VcpuConfig {
            vcpu_count: vm_resources.vm_config().vcpu_count.unwrap(),
            max_vcpu_count: vm_resources.vm_config().vcpu_count.unwrap(),
            ht_enabled: vm_resources.vm_config().ht_enabled.unwrap(),
            cpu_template: vm_resources.vm_config().cpu_template,
        };

        let vcpu_config = vm_resources.vcpu_config();
        assert_eq!(vcpu_config, expected_vcpu_config);
    }

    #[test]
    fn test_vm_config() {
        let vm_resources = default_vm_resources();
        let expected_vm_cfg = VmConfig::default();

        assert_eq!(vm_resources.vm_config(), &expected_vm_cfg);
    }

    #[test]
    fn test_set_vm_config() {
        let mut vm_resources = default_vm_resources();
        let mut aux_vm_config = VmConfig {
            vcpu_count: Some(32),
            mem_size_mib: Some(512),
            max_vcpu_count: None,
            max_mem_size_mib: None,
            ht_enabled: Some(true),
            cpu_template: Some(CpuFeaturesTemplate::T2),
        };

        assert_ne!(vm_resources.vm_config, aux_vm_config);
        vm_resources.set_vm_config(&aux_vm_config).unwrap();
        assert_eq!(vm_resources.vm_config, aux_vm_config);

        // Invalid vcpu count.
        aux_vm_config.vcpu_count = Some(0);
        assert_eq!(
            vm_resources.set_vm_config(&aux_vm_config),
            Err(VmConfigError::InvalidVcpuCount)
        );
        aux_vm_config.vcpu_count = Some(33);
        assert_eq!(
            vm_resources.set_vm_config(&aux_vm_config),
            Err(VmConfigError::InvalidVcpuCount)
        );
        aux_vm_config.vcpu_count = Some(32);

        // Invalid mem_size_mib.
        aux_vm_config.mem_size_mib = Some(0);
        assert_eq!(
            vm_resources.set_vm_config(&aux_vm_config),
            Err(VmConfigError::InvalidMemorySize)
        );
    }

    #[test]
    fn test_set_vm_config_max_capacity() {
        let mut vm_resources = default_vm_resources();
        let mut vm_config = VmConfig {
            vcpu_count: Some(2),
            mem_size_mib: Some(1024),
            max_vcpu_count: Some(8),
            max_mem_size_mib: Some(8192),
            ht_enabled: Some(false),
            cpu_template: None,
        };

        vm_resources.set_vm_config(&vm_config).unwrap();
        let vcpu_config = vm_resources.vcpu_config();
        assert_eq!(vcpu_config.vcpu_count, 2);
        assert_eq!(vcpu_config.max_vcpu_count, 8);

        // Without explicit capacity, max tracks the effective count.
        vm_config.max_vcpu_count = None;
        vm_config.max_mem_size_mib = None;
        vm_resources.set_vm_config(&vm_config).unwrap();
        assert_eq!(vm_resources.vcpu_config().max_vcpu_count, 2);

        // Max vcpus below the effective count.
        vm_config.max_vcpu_count = Some(1);
        assert_eq!(
            vm_resources.set_vm_config(&vm_config),
            Err(VmConfigError::InvalidMaxVcpuCount)
        );

        // Max vcpus above the supported limit.
        vm_config.max_vcpu_count = Some(65);
        assert_eq!(
            vm_resources.set_vm_config(&vm_config),
            Err(VmConfigError::InvalidMaxVcpuCount)
        );

        // Odd max vcpus with hyperthreading enabled.
        vm_config.max_vcpu_count = Some(3);
        vm_config.ht_enabled = Some(true);
        assert_eq!(
            vm_resources.set_vm_config(&vm_config),
            Err(VmConfigError::InvalidMaxVcpuCount)
        );
        vm_config.ht_enabled = Some(false);

        // Max memory below the boot memory size.
        vm_config.max_vcpu_count = Some(8);
        vm_config.max_mem_size_mib = Some(512);
        assert_eq!(
            vm_resources.set_vm_config(&vm_config),
            Err(VmConfigError::InvalidMaxMemorySize)
        );
    }

    #[test]
    fn test_set_vsock_device() {
        let mut vm_resources = default_vm_resources();
        let tmp_sock_file = TempSockFile::new(TempFile::new().unwrap());
        let new_vsock_cfg = default_config(&tmp_sock_file);
        assert!(vm_resources.vsock.get().is_none());
        vm_resources
            .set_vsock_device(new_vsock_cfg.clone())
            .unwrap();
        let actual_vsock_cfg = vm_resources.vsock.get().unwrap();
        assert_eq!(
            actual_vsock_cfg.lock().unwrap().id(),
            &new_vsock_cfg.vsock_id
        );
    }
}