Skip to content

Commit a5d1f47

Browse files
committed
vmm: Seperate config structs for virito and serial consoles
Signed-off-by: Matej Hrica <[email protected]>
1 parent cf56ee9 commit a5d1f47

File tree

3 files changed

+18
-39
lines changed

3 files changed

+18
-39
lines changed

src/libkrun/src/lib.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use std::sync::atomic::{AtomicI32, Ordering};
3838
use std::sync::LazyLock;
3939
use std::sync::Mutex;
4040
use utils::eventfd::EventFd;
41-
use vmm::resources::{ConsoleConfig, ConsoleType, VmResources};
41+
use vmm::resources::{DefaultVirtioConsoleConfig, SerialConsoleConfig, VmResources};
4242
#[cfg(feature = "blk")]
4343
use vmm::vmm_config::block::{BlockDeviceConfig, BlockRootConfig};
4444
#[cfg(not(feature = "tee"))]
@@ -2117,12 +2117,8 @@ pub unsafe extern "C" fn krun_add_virtio_console_default(
21172117
match CTX_MAP.lock().unwrap().entry(ctx_id) {
21182118
Entry::Occupied(mut ctx_cfg) => {
21192119
let cfg = ctx_cfg.get_mut();
2120-
cfg.vmr.consoles.entry(ConsoleType::Virtio).or_default();
21212120

2122-
// safe to unwrap since we inserted an empty Vec if the key didn't exist
2123-
let consoles = cfg.vmr.consoles.get_mut(&ConsoleType::Virtio).unwrap();
2124-
consoles.push(ConsoleConfig {
2125-
output_path: None,
2121+
cfg.vmr.virtio_consoles.push(DefaultVirtioConsoleConfig {
21262122
input_fd,
21272123
output_fd,
21282124
err_fd,
@@ -2144,15 +2140,9 @@ pub unsafe extern "C" fn krun_add_serial_console_default(
21442140
match CTX_MAP.lock().unwrap().entry(ctx_id) {
21452141
Entry::Occupied(mut ctx_cfg) => {
21462142
let cfg = ctx_cfg.get_mut();
2147-
cfg.vmr.consoles.entry(ConsoleType::Serial).or_default();
2148-
2149-
// safe to unwrap since we inserted an empty Vec if the key didn't exist
2150-
let consoles = cfg.vmr.consoles.get_mut(&ConsoleType::Serial).unwrap();
2151-
consoles.push(ConsoleConfig {
2152-
output_path: None,
2143+
cfg.vmr.serial_consoles.push(SerialConsoleConfig {
21532144
input_fd,
21542145
output_fd,
2155-
err_fd: -1,
21562146
});
21572147
}
21582148
Entry::Vacant(_) => return -libc::ENOENT,

src/vmm/src/builder.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use super::{Error, Vmm};
2424
#[cfg(target_arch = "x86_64")]
2525
use crate::device_manager::legacy::PortIODeviceManager;
2626
use crate::device_manager::mmio::MMIODeviceManager;
27-
use crate::resources::{ConsoleType, VmResources};
27+
use crate::resources::{DefaultVirtioConsoleConfig, VmResources};
2828
use crate::vmm_config::external_kernel::{ExternalKernel, KernelFormat};
2929
#[cfg(feature = "net")]
3030
use crate::vmm_config::net::NetBuilder;
@@ -722,11 +722,7 @@ pub fn build_microvm(
722722
)?);
723723
};
724724

725-
for s in vm_resources
726-
.consoles
727-
.get(&ConsoleType::Serial)
728-
.unwrap_or(&Vec::new())
729-
{
725+
for s in &vm_resources.serial_consoles {
730726
let input: Option<Box<dyn devices::legacy::ReadableFd + Send>> = if s.input_fd >= 0 {
731727
Some(Box::new(unsafe { File::from_raw_fd(s.input_fd) }))
732728
} else {
@@ -941,12 +937,7 @@ pub fn build_microvm(
941937
console_id += 1;
942938
}
943939

944-
for console in vm_resources
945-
.consoles
946-
.get(&ConsoleType::Virtio)
947-
.unwrap_or(&Vec::new())
948-
.iter()
949-
{
940+
for console in vm_resources.virtio_consoles.iter() {
950941
attach_console_devices(
951942
&mut vmm,
952943
event_manager,
@@ -1865,7 +1856,7 @@ fn attach_console_devices(
18651856
event_manager: &mut EventManager,
18661857
intc: IrqChip,
18671858
vm_resources: &VmResources,
1868-
cfg: Option<&super::resources::ConsoleConfig>,
1859+
cfg: Option<&DefaultVirtioConsoleConfig>,
18691860
id_number: u32,
18701861
) -> std::result::Result<(), StartMicrovmError> {
18711862
use self::StartMicrovmError::*;

src/vmm/src/resources.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
//#![deny(warnings)]
55

6-
use std::collections::HashMap;
76
#[cfg(feature = "tee")]
87
use std::fs::File;
98
#[cfg(feature = "tee")]
@@ -29,7 +28,6 @@ use crate::vmm_config::machine_config::{VmConfig, VmConfigError};
2928
use crate::vmm_config::net::{NetBuilder, NetworkInterfaceConfig, NetworkInterfaceError};
3029
use crate::vmm_config::vsock::*;
3130
use crate::vstate::VcpuConfig;
32-
3331
#[cfg(feature = "gpu")]
3432
use devices::virtio::display::DisplayInfo;
3533
#[cfg(feature = "tee")]
@@ -83,15 +81,12 @@ impl Default for TeeConfig {
8381
}
8482
}
8583

86-
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
87-
pub enum ConsoleType {
88-
Serial,
89-
Virtio,
84+
pub struct SerialConsoleConfig {
85+
pub input_fd: RawFd,
86+
pub output_fd: RawFd,
9087
}
9188

92-
#[derive(Debug, Default)]
93-
pub struct ConsoleConfig {
94-
pub output_path: Option<PathBuf>,
89+
pub struct DefaultVirtioConsoleConfig {
9590
pub input_fd: RawFd,
9691
pub output_fd: RawFd,
9792
pub err_fd: RawFd,
@@ -153,8 +148,10 @@ pub struct VmResources {
153148
pub disable_implicit_console: bool,
154149
/// The console id to use for console= in the kernel cmdline
155150
pub kernel_console: Option<String>,
156-
/// Consoles to attach to the guest
157-
pub consoles: HashMap<ConsoleType, Vec<ConsoleConfig>>,
151+
/// Serial consoles to attach to the guest
152+
pub serial_consoles: Vec<SerialConsoleConfig>,
153+
/// Virtio consoles to attach to the guest
154+
pub virtio_consoles: Vec<DefaultVirtioConsoleConfig>,
158155
}
159156

160157
impl VmResources {
@@ -353,7 +350,7 @@ impl VmResources {
353350
mod tests {
354351
#[cfg(feature = "gpu")]
355352
use crate::resources::DisplayBackendConfig;
356-
use crate::resources::VmResources;
353+
use crate::resources::{DefaultVirtioConsoleConfig, VmResources};
357354
use crate::vmm_config::kernel_cmdline::KernelCmdlineConfig;
358355
use crate::vmm_config::machine_config::{CpuFeaturesTemplate, VmConfig, VmConfigError};
359356
use crate::vmm_config::vsock::tests::{default_config, TempSockFile};
@@ -393,7 +390,8 @@ mod tests {
393390
nested_enabled: false,
394391
split_irqchip: false,
395392
disable_implicit_console: false,
396-
consoles: HashMap::new(),
393+
serial_consoles: Vec::new(),
394+
virtio_consoles: Vec::new(),
397395
kernel_console: None,
398396
}
399397
}

0 commit comments

Comments
 (0)