From 5aa8b028503237757b95f5629ebdbb4d56a67d51 Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Tue, 3 Nov 2026 15:15:58 -0800 Subject: [PATCH] microVM: drain the boot console's terminal output at guest exit A guest-requested exit drained portb's output and its host relay, and then OpenVMM terminated the process. When --virtio-console console attaches the boot console to the terminal, its relay copies the anonymous pair to stdout on a thread of its own, which nothing awaited, so the process could end before the relay wrote the output that the guest had printed last. The guest's virtio-console driver waits until the device completes each of its writes, and the device writes each descriptor to the pair before completing it, so the output was already in the pair; only the relay, waiting for the reactor to report it, was behind. microsoft/nvx#549 moves NVX's interactive and test consoles to the boot console, where a benchmark lost its completion marker at nvx-exit. The boot console's relay now takes drain requests. It holds a non-blocking duplicate of its end of the pair, from which it reads what the pair holds without waiting for the reactor, and writes and flushes it before it answers. MicrovmOutputDrain asks it at a guest-requested exit, after portb, within the same five-second deadline, and before a guest-requested capture, whose source process ends once the snapshot commits. Without a boot console on the terminal, nothing changes. Tests: the exit drain writes the output that the reactor has yet to report, which it lost before this change; the capture flush writes the pending output and the relay keeps copying; a failed write fails the drain; and on both Linux sockets and Windows pipes, the duplicate reads a pair until it is empty without waiting for more. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Guide/src/reference/openvmm/management/cli.md | 13 +- openvmm/openvmm_entry/Cargo.toml | 1 + openvmm/openvmm_entry/src/microvm/config.rs | 35 +- openvmm/openvmm_entry/src/microvm/output.rs | 337 ++++++++++++++++-- openvmm/openvmm_entry/src/serial_io.rs | 53 +++ openvmm/openvmm_entry/src/ttrpc/microvm.rs | 2 +- .../src/vm_controller/microvm.rs | 4 +- 7 files changed, 408 insertions(+), 37 deletions(-) diff --git a/Guide/src/reference/openvmm/management/cli.md b/Guide/src/reference/openvmm/management/cli.md index 4f9b8a748e6..685121148af 100644 --- a/Guide/src/reference/openvmm/management/cli.md +++ b/Guide/src/reference/openvmm/management/cli.md @@ -1023,7 +1023,8 @@ Serial devices can be configured to appear as different devices inside the guest restore command must explicitly resupply the matching client attachment. * `console`: require the restore caller to supply the same inherited terminal attachment. Portb recovery output moves to stderr while the terminal is - attached to `hvc1`. + attached to `hvc1`. A guest-requested exit writes the boot console output + that the device accepted before OpenVMM exits. * `none`: keep the device present and explicitly discard guest TX while disconnected. @@ -1144,13 +1145,17 @@ a supervisor can tell the exit reasons apart. For microVMs, a process-status shutdown through port `0x604`, or a power event configured to exit, drains previously accepted portb console output before the process exits. This includes the host stdout/stderr relay, not just the device's -transmit buffer. Draining is bounded to five seconds; an output error or timeout +transmit buffer. When `--virtio-console console` attaches the boot console to +the terminal, the drain also writes the boot console output that the device +accepted before the shutdown, which the host relay has yet to write to stdout. +Draining is bounded to five seconds; an output error or timeout is reported as a failure rather than a successful exit with truncated output. In RPC-server mode, a drain failure also fails pending `WaitVm` requests and terminates the server with an error instead of leaving it uninitialized. A guest-requested snapshot capture instead flushes accepted portb output to its -endpoint before quiescing, without closing it because the capture can still roll -back; see `--snapshot-destination`. +endpoint, and the boot console's terminal output, before quiescing, without +closing them because the capture can still roll back; see +`--snapshot-destination`. * `--crash-dump-path `: when the guest triple-faults, write a WinDbg-compatible `.vmrs` dump of the VM's processor state and guest memory to diff --git a/openvmm/openvmm_entry/Cargo.toml b/openvmm/openvmm_entry/Cargo.toml index 9e499eaa807..e5b711133a4 100644 --- a/openvmm/openvmm_entry/Cargo.toml +++ b/openvmm/openvmm_entry/Cargo.toml @@ -126,6 +126,7 @@ workspace = true features = [ "Win32_Foundation", "Win32_Storage_FileSystem", + "Win32_System_Pipes", ] [build-dependencies] diff --git a/openvmm/openvmm_entry/src/microvm/config.rs b/openvmm/openvmm_entry/src/microvm/config.rs index 69dc408e05c..2400ce7c8dc 100644 --- a/openvmm/openvmm_entry/src/microvm/config.rs +++ b/openvmm/openvmm_entry/src/microvm/config.rs @@ -22,6 +22,8 @@ use super::network::microvm_network_endpoint; use super::network::microvm_restore_network; use super::output::MicrovmOutputDrain; use super::output::OutputCompletion; +use super::output::RelayDrain; +use super::output::spawn_drainable_output; use super::output::spawn_output; use super::restore::fresh_microvm_generation_id; use crate::ConsoleState; @@ -78,6 +80,8 @@ pub(crate) struct MicrovmConfigBuilder<'a> { control_broker_config: Option, control_console_backend: Option>, portb: Option<(Resource, OutputCompletion)>, + /// The drain of the boot console's relay to the terminal. + console_relay: Option, resources: MicrovmResources, } @@ -224,6 +228,7 @@ impl<'a> MicrovmConfigBuilder<'a> { control_broker_config, control_console_backend: None, portb: None, + console_relay: None, resources, }) } @@ -264,13 +269,14 @@ impl<'a> MicrovmConfigBuilder<'a> { } else { SerialConfigCli::Console }; - self.portb = Some(setup_host_console( + let (backend, output, _) = setup_host_console( "microvm-portb", backend, "hvc0", console_state, serial_driver, - )?); + )?; + self.portb = Some((backend, output)); Ok(()) } @@ -320,13 +326,14 @@ impl<'a> MicrovmConfigBuilder<'a> { )?) } SerialConfigCli::Console => { - let (backend, _) = setup_host_console( + let (backend, _, relay) = setup_host_console( "virtio-console", SerialConfigCli::Console, "hvc1", console_state, serial_driver, )?; + self.console_relay = relay; Some(backend) } SerialConfigCli::None => Some(DisconnectedSerialBackendHandle.into_resource()), @@ -407,7 +414,8 @@ impl<'a> MicrovmConfigBuilder<'a> { return Ok(()); }; let opt = self.opt; - let (drain, output_drain) = MicrovmOutputDrain::new(Some(output_completion)); + let (drain, output_drain) = + MicrovmOutputDrain::new(Some(output_completion), self.console_relay.take()); self.resources.output_drain = Some(drain); let (generation_id, time_abi_restore) = if self.restore.machine_contract.is_some() { let network = if self.restore.network_settings { @@ -856,32 +864,39 @@ fn aggregate_child_kind( } /// Connects a portb or boot virtio-console endpoint to the host console or -/// stderr, returning its backend and output-relay completion. +/// stderr, returning its backend, its output-relay completion, and, for the +/// console, the drain of its relay. fn setup_host_console( name: &str, backend: SerialConfigCli, device: &'static str, console_state: &RefCell>>, serial_driver: &DefaultDriver, -) -> anyhow::Result<(Resource, OutputCompletion)> { +) -> anyhow::Result<( + Resource, + OutputCompletion, + Option, +)> { Ok(match backend { SerialConfigCli::Console => { if let Some(console_state) = console_state.borrow().as_ref() { bail!("console already set by {}", console_state.device); } - let (config, serial) = serial_io::anonymous_serial_pair(serial_driver)?; + let (config, serial, pending) = + serial_io::anonymous_serial_pair_with_pending(serial_driver)?; let (serial_read, serial_write) = AsyncReadExt::split(serial); *console_state.borrow_mut() = Some(ConsoleState { device, input: Box::new(serial_write), }); - let completed = spawn_output(name, serial_read, term::raw_stdout())?; - (config, completed) + let (completed, relay) = + spawn_drainable_output(name, serial_read, pending, term::raw_stdout())?; + (config, completed, Some(relay)) } SerialConfigCli::Stderr => { let (config, serial) = serial_io::anonymous_serial_pair(serial_driver)?; let completed = spawn_output(name, serial, term::raw_stderr())?; - (config, completed) + (config, completed, None) } _ => unreachable!("microVM host consoles use the console or stderr"), }) diff --git a/openvmm/openvmm_entry/src/microvm/output.rs b/openvmm/openvmm_entry/src/microvm/output.rs index 0f4b79da283..8f59e1a807a 100644 --- a/openvmm/openvmm_entry/src/microvm/output.rs +++ b/openvmm/openvmm_entry/src/microvm/output.rs @@ -1,14 +1,17 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -//! Delivers microVM portb output to the host before a guest-requested snapshot -//! capture and drains it, with its host output relay, before process exit. +//! Delivers microVM console output to the host before a guest-requested +//! snapshot capture and drains it, with its host output relays, before process +//! exit: portb's, and the boot console's when it writes to the terminal. use anyhow::Context; use chipset_resources::microvm::MicrovmPortbDrain; use futures::AsyncRead; +use futures::AsyncReadExt; use futures::AsyncWriteExt; use futures::executor::block_on; +use futures::future::Either; use futures::io::AllowStdIo; use mesh::CancelContext; use mesh::OneshotReceiver; @@ -16,6 +19,7 @@ use mesh::oneshot; use mesh::rpc::FailableRpc; use mesh::rpc::RpcSend; use std::io; +use std::pin::pin; use std::thread; use std::time::Duration; @@ -40,34 +44,167 @@ pub(crate) fn spawn_output( Ok(completed) } +/// Asks a relay that [`spawn_drainable_output`] started to write the output +/// that its pair holds. +pub(crate) struct RelayDrain(mesh::Sender>); + +impl RelayDrain { + /// Waits until the relay has written and flushed every byte that its pair + /// held when the relay took the request. + async fn write_pending(&self) -> anyhow::Result<()> { + self.0 + .call_failable(std::convert::identity, ()) + .await + .context("failed to write the microVM boot console's output") + } +} + +/// Starts a relay that copies `input` to `output`, as [`spawn_output`] does, +/// and that also writes, when asked, the output that its pair holds. +/// +/// The reactor reports `input`'s data some time after the device writes it, so +/// a process that exits once the guest does could lose the output that the +/// relay has yet to see. `pending` is a non-blocking duplicate of `input`'s +/// object, from which the relay reads the pair's contents without waiting for +/// the reactor. +pub(crate) fn spawn_drainable_output( + name: &str, + input: impl AsyncRead + Send + Unpin + 'static, + pending: impl io::Read + Send + 'static, + output: impl io::Write + Send + 'static, +) -> io::Result<(OutputCompletion, RelayDrain)> { + let (complete, completed) = oneshot(); + let (requests_send, requests) = mesh::channel(); + thread::Builder::new() + .name(name.to_owned()) + .spawn(move || { + complete.send(block_on(relay(input, pending, output, requests))); + })?; + Ok((completed, RelayDrain(requests_send))) +} + +async fn relay( + mut input: impl AsyncRead + Unpin, + mut pending: impl io::Read, + mut output: impl io::Write, + mut requests: mesh::Receiver>, +) -> io::Result<()> { + enum Event { + Read(io::Result), + Request(Option>), + } + + let mut buffer = vec![0; 8192]; + let mut requests_open = true; + loop { + // An unfinished read holds no data, so dropping it for a request loses + // nothing. + let event = { + let read = pin!(input.read(&mut buffer)); + let request = pin!(async { + if requests_open { + requests.recv().await.ok() + } else { + std::future::pending().await + } + }); + match futures::future::select(read, request).await { + Either::Left((read, _)) => Event::Read(read), + Either::Right((request, _)) => Event::Request(request), + } + }; + match event { + Event::Read(read) => { + let count = read?; + if count == 0 { + break; + } + output.write_all(&buffer[..count])?; + } + Event::Request(Some(request)) => request + .handle_failable_sync(|()| write_pending(&mut pending, &mut buffer, &mut output)), + Event::Request(None) => requests_open = false, + } + } + output.flush() +} + +/// Writes and flushes what `pending`, a non-blocking end of a pair, holds, +/// without waiting for more. +fn write_pending( + pending: &mut impl io::Read, + buffer: &mut [u8], + output: &mut impl io::Write, +) -> io::Result<()> { + loop { + match pending.read(buffer) { + Ok(0) => break, + Ok(count) => output.write_all(&buffer[..count])?, + Err(error) if error.kind() == io::ErrorKind::Interrupted => {} + Err(error) if pair_is_empty(&error) => break, + Err(error) => return Err(error), + } + } + output.flush() +} + +/// Returns whether a read of a non-blocking pair failed only because the pair +/// holds nothing: a socket reports `WouldBlock`, and a Windows pipe in +/// `PIPE_NOWAIT` mode reports `ERROR_NO_DATA`. +fn pair_is_empty(error: &io::Error) -> bool { + #[cfg(windows)] + if error.raw_os_error() == Some(windows_sys::Win32::Foundation::ERROR_NO_DATA as i32) { + return true; + } + error.kind() == io::ErrorKind::WouldBlock +} + pub(crate) struct MicrovmOutputDrain { portb: mesh::Sender>, output: Option, + console: Option, } impl MicrovmOutputDrain { + /// Returns a drain of portb, whose relay is `output`, and of the boot + /// console's relay to the terminal, `console`, with the receiver of + /// portb's requests. pub(crate) fn new( output: Option, + console: Option, ) -> (Self, mesh::Receiver>) { let (portb, requests) = mesh::channel(); - (Self { portb, output }, requests) + ( + Self { + portb, + output, + console, + }, + requests, + ) } /// Writes and flushes the output accepted by portb to its endpoint, which - /// stays open, and fails after `timeout`. + /// stays open, and the output that the boot console's terminal relay + /// holds, and fails after `timeout`. /// - /// The host relay is not awaited because it completes only once the - /// endpoint closes. + /// The host relay of portb is not awaited because it completes only once + /// the endpoint closes. pub(crate) async fn flush(&self, timeout: Duration) -> anyhow::Result<()> { CancelContext::new() .with_timeout(timeout) - .until_cancelled( + .until_cancelled(async { self.portb - .call_failable(std::convert::identity, MicrovmPortbDrain::Flush), - ) + .call_failable(std::convert::identity, MicrovmPortbDrain::Flush) + .await + .context("failed to flush the microVM portb endpoint")?; + if let Some(console) = &self.console { + console.write_pending().await?; + } + anyhow::Ok(()) + }) .await .context("microVM console output flush timed out")? - .context("failed to flush the microVM portb endpoint") } pub(crate) async fn drain(self) -> anyhow::Result<()> { @@ -75,19 +212,30 @@ impl MicrovmOutputDrain { } async fn drain_with_timeout(self, timeout: Duration) -> anyhow::Result<()> { + let Self { + portb, + output, + console, + } = self; CancelContext::new() .with_timeout(timeout) .until_cancelled(async { - self.portb + portb .call_failable(std::convert::identity, MicrovmPortbDrain::Close) .await .context("failed to drain the microVM portb endpoint")?; - if let Some(output) = self.output { + if let Some(output) = output { output .await .context("microVM console output relay stopped without completing")? .context("microVM console output relay failed")?; } + // The guest has stopped, and the boot console wrote each of + // its descriptors to the pair before completing it, so the + // pair holds the rest of the guest's output. + if let Some(console) = &console { + console.write_pending().await?; + } tracing::debug!("microVM console output drained"); anyhow::Ok(()) }) @@ -111,7 +259,7 @@ mod tests { fn drain_waits_for_both_device_and_relay() { block_on(async { let (complete, completed) = oneshot(); - let (drain, mut requests) = MicrovmOutputDrain::new(Some(completed)); + let (drain, mut requests) = MicrovmOutputDrain::new(Some(completed), None); let mut drain = pin!(drain.drain()); assert!(poll!(&mut drain).is_pending()); let request = requests.recv().await.unwrap(); @@ -127,7 +275,7 @@ mod tests { fn flush_keeps_the_endpoint_open_without_waiting_for_the_relay() { block_on(async { let (_complete, completed) = oneshot(); - let (drain, mut requests) = MicrovmOutputDrain::new(Some(completed)); + let (drain, mut requests) = MicrovmOutputDrain::new(Some(completed), None); { let mut flush = pin!(drain.flush(Duration::from_secs(5))); assert!(poll!(&mut flush).is_pending()); @@ -149,7 +297,7 @@ mod tests { #[test] fn flush_reports_endpoint_failure() { block_on(async { - let (drain, mut requests) = MicrovmOutputDrain::new(None); + let (drain, mut requests) = MicrovmOutputDrain::new(None, None); let mut flush = pin!(drain.flush(Duration::from_secs(5))); assert!(poll!(&mut flush).is_pending()); requests @@ -164,7 +312,7 @@ mod tests { #[test] fn flush_has_a_bounded_deadline() { block_on(async { - let (drain, _requests) = MicrovmOutputDrain::new(None); + let (drain, _requests) = MicrovmOutputDrain::new(None, None); let error = drain.flush(Duration::from_millis(10)).await.unwrap_err(); assert!(format!("{error:#}").contains("timed out")); }); @@ -173,7 +321,7 @@ mod tests { #[test] fn drain_reports_endpoint_failure() { block_on(async { - let (drain, mut requests) = MicrovmOutputDrain::new(None); + let (drain, mut requests) = MicrovmOutputDrain::new(None, None); let mut drain = pin!(drain.drain()); assert!(poll!(&mut drain).is_pending()); requests @@ -189,7 +337,7 @@ mod tests { fn drain_reports_relay_failure() { block_on(async { let (complete, completed) = oneshot(); - let (drain, mut requests) = MicrovmOutputDrain::new(Some(completed)); + let (drain, mut requests) = MicrovmOutputDrain::new(Some(completed), None); let mut drain = pin!(drain.drain()); assert!(poll!(&mut drain).is_pending()); requests.recv().await.unwrap().complete(Ok(())); @@ -201,7 +349,7 @@ mod tests { #[test] fn drain_has_a_bounded_deadline() { block_on(async { - let (drain, _requests) = MicrovmOutputDrain::new(None); + let (drain, _requests) = MicrovmOutputDrain::new(None, None); let error = drain .drain_with_timeout(Duration::from_millis(10)) .await @@ -276,10 +424,159 @@ mod tests { #[test] fn drain_without_relay_only_waits_for_the_endpoint() { - let (drain, mut requests) = MicrovmOutputDrain::new(None); + let (drain, mut requests) = MicrovmOutputDrain::new(None, None); let mut drain = Box::pin(drain.drain()); assert!(drain.as_mut().now_or_never().is_none()); requests.try_recv().unwrap().complete(Ok(())); block_on(drain).unwrap(); } + + /// The output of a relay, which the test reads after the relay writes it. + #[derive(Clone, Default)] + struct SharedOutput(Arc>>); + + impl io::Write for SharedOutput { + fn write(&mut self, buffer: &[u8]) -> io::Result { + self.0.lock().extend_from_slice(buffer); + Ok(buffer.len()) + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } + } + + /// Returns a relay input whose data the test supplies, and which ends once + /// the test drops the sender. + fn relay_input() -> ( + mesh::Sender>, + impl AsyncRead + Send + Unpin + 'static, + ) { + struct ChannelInput { + receiver: mesh::Receiver>, + data: Vec, + } + + impl AsyncRead for ChannelInput { + fn poll_read( + mut self: std::pin::Pin<&mut Self>, + cx: &mut std::task::Context<'_>, + buffer: &mut [u8], + ) -> std::task::Poll> { + let this = &mut *self; + if this.data.is_empty() { + match std::task::ready!(this.receiver.poll_recv(cx)) { + Ok(data) => this.data = data, + Err(_) => return std::task::Poll::Ready(Ok(0)), + } + } + let count = buffer.len().min(this.data.len()); + buffer[..count].copy_from_slice(&this.data[..count]); + this.data.drain(..count); + std::task::Poll::Ready(Ok(count)) + } + } + + let (send, receiver) = mesh::channel(); + ( + send, + ChannelInput { + receiver, + data: Vec::new(), + }, + ) + } + + #[test] + fn exit_drain_writes_the_boot_console_output_that_the_reactor_has_yet_to_report() { + // The reactor has not reported the pair's last bytes, so only the + // pending handle reads them. + let (input_send, input) = relay_input(); + let output = SharedOutput::default(); + let (completed, relay) = spawn_drainable_output( + "microvm-boot-console-test", + input, + io::Cursor::new(b"NVX-FINAL-OUTPUT\n".to_vec()), + output.clone(), + ) + .unwrap(); + block_on(async { + let (drain, mut requests) = MicrovmOutputDrain::new(None, Some(relay)); + let (drained, ()) = futures::join!(drain.drain(), async { + requests.recv().await.unwrap().complete(Ok(())); + }); + drained.unwrap(); + }); + assert_eq!(*output.0.lock(), b"NVX-FINAL-OUTPUT\n"); + drop(input_send); + block_on(completed).unwrap().unwrap(); + } + + #[test] + fn capture_flush_writes_the_boot_console_output_and_keeps_relaying() { + let (input_send, input) = relay_input(); + let output = SharedOutput::default(); + let (completed, relay) = spawn_drainable_output( + "microvm-boot-console-flush-test", + input, + io::Cursor::new(b"before\n".to_vec()), + output.clone(), + ) + .unwrap(); + block_on(async { + let (drain, mut requests) = MicrovmOutputDrain::new(None, Some(relay)); + let (flushed, ()) = futures::join!(drain.flush(Duration::from_secs(5)), async { + let request = requests.recv().await.unwrap(); + assert_eq!(*request.input(), MicrovmPortbDrain::Flush); + request.complete(Ok(())); + }); + flushed.unwrap(); + }); + assert_eq!(*output.0.lock(), b"before\n"); + // The relay keeps copying the console's output after the flush. + input_send.send(b"after\n".to_vec()); + drop(input_send); + block_on(completed).unwrap().unwrap(); + assert_eq!(*output.0.lock(), b"before\nafter\n"); + } + + #[test] + fn relay_reports_a_failed_write_of_the_pending_output() { + struct FailingOutput; + impl io::Write for FailingOutput { + fn write(&mut self, _buffer: &[u8]) -> io::Result { + Err(io::ErrorKind::BrokenPipe.into()) + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } + } + let (_input_send, input) = relay_input(); + let (_completed, relay) = spawn_drainable_output( + "microvm-boot-console-error-test", + input, + io::Cursor::new(b"lost".to_vec()), + FailingOutput, + ) + .unwrap(); + let error = block_on(relay.write_pending()).unwrap_err(); + assert!(format!("{error:#}").contains("boot console's output")); + } + + #[test] + fn write_pending_empties_a_pair_without_waiting_for_more() { + use std::io::Write as _; + pal_async::DefaultPool::run_with(async |driver| { + let (mut vm, _host, mut pending) = crate::serial_io::pending_pair(&driver).unwrap(); + vm.write_all(b"NVX-PAIR-OUTPUT").unwrap(); + let mut output = Vec::new(); + // A buffer smaller than the output takes several reads. + write_pending(&mut pending, &mut [0; 4], &mut output).unwrap(); + assert_eq!(output, b"NVX-PAIR-OUTPUT"); + // An empty pair ends the write at once rather than waiting. + write_pending(&mut pending, &mut [0; 4], &mut output).unwrap(); + assert_eq!(output, b"NVX-PAIR-OUTPUT"); + }); + } } diff --git a/openvmm/openvmm_entry/src/serial_io.rs b/openvmm/openvmm_entry/src/serial_io.rs index f2abf43f136..ed095fa645a 100644 --- a/openvmm/openvmm_entry/src/serial_io.rs +++ b/openvmm/openvmm_entry/src/serial_io.rs @@ -49,6 +49,59 @@ pub fn anonymous_serial_pair( )) } +/// Returns an anonymous serial pair, as [`anonymous_serial_pair`] does, and a +/// non-blocking duplicate of its host end. The duplicate reads what the pair +/// holds without waiting for the reactor to report it, and then reports that +/// the pair is empty. +pub(crate) fn anonymous_serial_pair_with_pending( + driver: &(impl Driver + ?Sized), +) -> io::Result<(Resource, PolledHostEnd, PendingHostEnd)> { + let (vm, host, pending) = pending_pair(driver)?; + #[cfg(unix)] + let vm = OpenSocketSerialConfig::from(vm).into_resource(); + #[cfg(windows)] + let vm = serial_socket::windows::OpenWindowsPipeSerialConfig::from(vm).into_resource(); + Ok((vm, host, pending)) +} + +#[cfg(unix)] +pub(crate) type PolledHostEnd = pal_async::socket::PolledSocket; +#[cfg(unix)] +pub(crate) type PendingHostEnd = unix_socket::UnixStream; +#[cfg(windows)] +pub(crate) type PolledHostEnd = PolledPipe; +#[cfg(windows)] +pub(crate) type PendingHostEnd = std::fs::File; + +/// Returns the VM end of an anonymous pair, its polled host end, and a +/// non-blocking duplicate of the host end. +#[cfg(unix)] +pub(crate) fn pending_pair( + driver: &(impl Driver + ?Sized), +) -> io::Result<(unix_socket::UnixStream, PolledHostEnd, PendingHostEnd)> { + let (vm, host) = unix_socket::UnixStream::pair()?; + let pending = host.try_clone()?; + let host = pal_async::socket::PolledSocket::new(driver, host)?; + pending.set_nonblocking(true)?; + Ok((vm, host, pending)) +} + +/// Returns the VM end of an anonymous pair, its polled host end, and a +/// non-blocking duplicate of the host end. +#[cfg(windows)] +pub(crate) fn pending_pair( + driver: &(impl Driver + ?Sized), +) -> io::Result<(std::fs::File, PolledHostEnd, PendingHostEnd)> { + use pal::windows::pipe::PipeExt; + + // As in anonymous_serial_pair, the VM takes the client. + let (server, client) = pal::windows::pipe::bidirectional_pair(false)?; + let pending = server.try_clone()?; + let server = PolledPipe::new(driver, server)?; + pending.set_pipe_mode(windows_sys::Win32::System::Pipes::PIPE_NOWAIT)?; + Ok((client, server, pending)) +} + pub fn file_serial(path: &Path, append: bool) -> anyhow::Result> { let file = fs_err::OpenOptions::new() .create(true) diff --git a/openvmm/openvmm_entry/src/ttrpc/microvm.rs b/openvmm/openvmm_entry/src/ttrpc/microvm.rs index b455743c758..7f9e75d1040 100644 --- a/openvmm/openvmm_entry/src/ttrpc/microvm.rs +++ b/openvmm/openvmm_entry/src/ttrpc/microvm.rs @@ -727,7 +727,7 @@ impl CreateVm { return Ok(()); }; let output_drain = self.portb_has_output.then(|| { - let (drain, requests) = crate::microvm::output::MicrovmOutputDrain::new(None); + let (drain, requests) = crate::microvm::output::MicrovmOutputDrain::new(None, None); self.resources.output_drain = Some(drain); requests }); diff --git a/openvmm/openvmm_entry/src/vm_controller/microvm.rs b/openvmm/openvmm_entry/src/vm_controller/microvm.rs index 72c73c7a532..5e06c5c6121 100644 --- a/openvmm/openvmm_entry/src/vm_controller/microvm.rs +++ b/openvmm/openvmm_entry/src/vm_controller/microvm.rs @@ -562,7 +562,7 @@ mod tests { fn successful_drain_preserves_guest_exit_status() { block_on(async { for code in [0, 37] { - let (drain, mut requests) = MicrovmOutputDrain::new(None); + let (drain, mut requests) = MicrovmOutputDrain::new(None, None); let (event, ()) = futures::join!(guest_exit_event(code, Some(drain)), async { requests.recv().await.unwrap().complete(Ok(())); }); @@ -576,7 +576,7 @@ mod tests { #[test] fn failed_drain_requests_process_exit_not_worker_stopped() { - let (drain, requests) = MicrovmOutputDrain::new(None); + let (drain, requests) = MicrovmOutputDrain::new(None, None); drop(requests); let event = block_on(guest_exit_event(0, Some(drain))); assert!(matches!(