Skip to content

Commit 2531880

Browse files
bchaliosShadowCurse
andcommitted
refactor(serial): remove generics from SerialDevice
We use `SerialDevice` with Stdin as the input source. Encode this in the type so that we don't spill the generic all over the place. Co-authored-by: Egor Lazarchuk <[email protected]> Signed-off-by: Babis Chalios <[email protected]>
1 parent f99c0d3 commit 2531880

File tree

3 files changed

+39
-36
lines changed

3 files changed

+39
-36
lines changed

src/vmm/src/builder.rs

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ use std::sync::mpsc;
1010
use std::sync::{Arc, Mutex};
1111

1212
use event_manager::{MutEventSubscriber, SubscriberOps};
13-
use libc::EFD_NONBLOCK;
1413
use linux_loader::cmdline::Cmdline as LoaderKernelCmdline;
1514
use userfaultfd::Uffd;
1615
use utils::time::TimestampUs;
1716
#[cfg(target_arch = "aarch64")]
1817
use vm_superio::Rtc;
19-
use vm_superio::Serial;
20-
use vmm_sys_util::eventfd::EventFd;
2118

2219
use crate::arch::{ConfigurationError, configure_system_for_boot, load_kernel};
2320
#[cfg(target_arch = "aarch64")]
@@ -37,8 +34,8 @@ use crate::devices::BusDevice;
3734
use crate::devices::acpi::vmgenid::{VmGenId, VmGenIdError};
3835
#[cfg(target_arch = "aarch64")]
3936
use crate::devices::legacy::RTCDevice;
37+
use crate::devices::legacy::SerialDevice;
4038
use crate::devices::legacy::serial::SerialOut;
41-
use crate::devices::legacy::{EventFdTrigger, SerialEventsWrapper, SerialWrapper};
4239
use crate::devices::virtio::balloon::Balloon;
4340
use crate::devices::virtio::block::device::Block;
4441
use crate::devices::virtio::device::VirtioDevice;
@@ -158,7 +155,7 @@ fn create_vmm_and_vcpus(
158155
set_stdout_nonblocking();
159156

160157
// Serial device setup.
161-
let serial_device = setup_serial_device(event_manager, std::io::stdin(), io::stdout())?;
158+
let serial_device = setup_serial_device(event_manager)?;
162159

163160
// x86_64 uses the i8042 reset event as the Vmm exit event.
164161
let reset_evt = vcpus_exit_evt.try_clone().map_err(VmmError::EventFd)?;
@@ -532,22 +529,11 @@ pub fn build_microvm_from_snapshot(
532529
/// Sets up the serial device.
533530
pub fn setup_serial_device(
534531
event_manager: &mut EventManager,
535-
input: std::io::Stdin,
536-
out: std::io::Stdout,
537532
) -> Result<Arc<Mutex<BusDevice>>, VmmError> {
538-
let interrupt_evt = EventFdTrigger::new(EventFd::new(EFD_NONBLOCK).map_err(VmmError::EventFd)?);
539-
let kick_stdin_read_evt =
540-
EventFdTrigger::new(EventFd::new(EFD_NONBLOCK).map_err(VmmError::EventFd)?);
541-
let serial = Arc::new(Mutex::new(BusDevice::Serial(SerialWrapper {
542-
serial: Serial::with_events(
543-
interrupt_evt,
544-
SerialEventsWrapper {
545-
buffer_ready_event_fd: Some(kick_stdin_read_evt),
546-
},
547-
SerialOut::Stdout(out),
548-
),
549-
input: Some(input),
550-
})));
533+
let serial = Arc::new(Mutex::new(BusDevice::Serial(
534+
SerialDevice::new(Some(std::io::stdin()), SerialOut::Stdout(std::io::stdout()))
535+
.map_err(VmmError::EventFd)?,
536+
)));
551537
event_manager.add_subscriber(serial.clone());
552538
Ok(serial)
553539
}
@@ -569,7 +555,7 @@ fn attach_legacy_devices_aarch64(
569555
if cmdline_contains_console {
570556
// Make stdout non-blocking.
571557
set_stdout_nonblocking();
572-
let serial = setup_serial_device(event_manager, std::io::stdin(), std::io::stdout())?;
558+
let serial = setup_serial_device(event_manager)?;
573559
vmm.mmio_device_manager
574560
.register_mmio_serial(vmm.vm.fd(), &mut vmm.resource_allocator, serial, None)
575561
.map_err(VmmError::RegisterMMIODevice)?;
@@ -749,10 +735,12 @@ pub(crate) fn set_stdout_nonblocking() {
749735
pub(crate) mod tests {
750736

751737
use linux_loader::cmdline::Cmdline;
738+
use vmm_sys_util::eventfd::EventFd;
752739
use vmm_sys_util::tempfile::TempFile;
753740

754741
use super::*;
755742
use crate::device_manager::resources::ResourceAllocator;
743+
use crate::devices::legacy::serial::SerialOut;
756744
use crate::devices::virtio::block::CacheType;
757745
use crate::devices::virtio::rng::device::ENTROPY_DEV_ID;
758746
use crate::devices::virtio::vsock::{TYPE_VSOCK, VSOCK_DEV_ID};
@@ -829,16 +817,9 @@ pub(crate) mod tests {
829817
let acpi_device_manager = ACPIDeviceManager::new();
830818
#[cfg(target_arch = "x86_64")]
831819
let pio_device_manager = PortIODeviceManager::new(
832-
Arc::new(Mutex::new(BusDevice::Serial(SerialWrapper {
833-
serial: Serial::with_events(
834-
EventFdTrigger::new(EventFd::new(EFD_NONBLOCK).unwrap()),
835-
SerialEventsWrapper {
836-
buffer_ready_event_fd: None,
837-
},
838-
SerialOut::Sink(std::io::sink()),
839-
),
840-
input: None,
841-
}))),
820+
Arc::new(Mutex::new(BusDevice::Serial(
821+
SerialDevice::new(None, SerialOut::Sink(std::io::sink())).unwrap(),
822+
))),
842823
EventFd::new(libc::EFD_NONBLOCK).unwrap(),
843824
)
844825
.unwrap();

src/vmm/src/devices/bus.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub enum BusDevice {
6363
I8042Device(I8042Device),
6464
#[cfg(target_arch = "aarch64")]
6565
RTCDevice(RTCDevice),
66-
Serial(SerialDevice<std::io::Stdin>),
66+
Serial(SerialDevice),
6767
#[cfg(test)]
6868
Constant(ConstantDevice),
6969
}
@@ -113,7 +113,7 @@ impl BusDevice {
113113
_ => None,
114114
}
115115
}
116-
pub fn serial_ref(&self) -> Option<&SerialDevice<std::io::Stdin>> {
116+
pub fn serial_ref(&self) -> Option<&SerialDevice> {
117117
match self {
118118
Self::Serial(x) => Some(x),
119119
_ => None,
@@ -133,7 +133,7 @@ impl BusDevice {
133133
_ => None,
134134
}
135135
}
136-
pub fn serial_mut(&mut self) -> Option<&mut SerialDevice<std::io::Stdin>> {
136+
pub fn serial_mut(&mut self) -> Option<&mut SerialDevice> {
137137
match self {
138138
Self::Serial(x) => Some(x),
139139
_ => None,

src/vmm/src/devices/legacy/serial.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77

88
//! Implements a wrapper over an UART serial device.
99
use std::fmt::Debug;
10-
use std::io;
10+
use std::io::{self, Stdin};
1111
use std::io::{Read, Write};
1212
use std::os::unix::io::{AsRawFd, RawFd};
1313

1414
use event_manager::{EventOps, Events, MutEventSubscriber};
15+
use libc::EFD_NONBLOCK;
1516
use log::{error, warn};
1617
use serde::Serialize;
1718
use vm_superio::serial::{Error as SerialError, SerialEvents};
1819
use vm_superio::{Serial, Trigger};
1920
use vmm_sys_util::epoll::EventSet;
21+
use vmm_sys_util::eventfd::EventFd;
2022

2123
use crate::devices::legacy::EventFdTrigger;
2224
use crate::logger::{IncMetric, SharedIncMetric};
@@ -220,7 +222,27 @@ impl<I: Read + AsRawFd + Send + Debug> SerialWrapper<EventFdTrigger, SerialEvent
220222
}
221223

222224
/// Type for representing a serial device.
223-
pub type SerialDevice<I> = SerialWrapper<EventFdTrigger, SerialEventsWrapper, I>;
225+
pub type SerialDevice = SerialWrapper<EventFdTrigger, SerialEventsWrapper, Stdin>;
226+
227+
impl SerialDevice {
228+
pub fn new(serial_in: Option<Stdin>, serial_out: SerialOut) -> Result<Self, std::io::Error> {
229+
let interrupt_evt = EventFdTrigger::new(EventFd::new(EFD_NONBLOCK)?);
230+
let buffer_read_event_fd = EventFdTrigger::new(EventFd::new(EFD_NONBLOCK)?);
231+
232+
let serial = Serial::with_events(
233+
interrupt_evt,
234+
SerialEventsWrapper {
235+
buffer_ready_event_fd: Some(buffer_read_event_fd),
236+
},
237+
serial_out,
238+
);
239+
240+
Ok(SerialDevice {
241+
serial,
242+
input: serial_in,
243+
})
244+
}
245+
}
224246

225247
impl<I: Read + AsRawFd + Send + Debug> MutEventSubscriber
226248
for SerialWrapper<EventFdTrigger, SerialEventsWrapper, I>

0 commit comments

Comments
 (0)