diff --git a/petri/src/vm/openvmm/microvm.rs b/petri/src/vm/openvmm/microvm.rs index bd36250c43e..d3f0808edd0 100644 --- a/petri/src/vm/openvmm/microvm.rs +++ b/petri/src/vm/openvmm/microvm.rs @@ -14,7 +14,6 @@ use chipset_resources::microvm::MicrovmShutdownHandle; use chipset_resources::microvm::MicrovmSnapshotRequestHandle; use chipset_resources::pit::PitDeviceHandle; use cpu_profile::HostCpuSignature; -use futures::AsyncWriteExt; use openvmm_defs::config::Config; use openvmm_defs::config::LinuxDirectBootMode; use openvmm_defs::config::LinuxIsolationConfig; @@ -24,7 +23,6 @@ use openvmm_defs::microvm::MicrovmConfig; use openvmm_defs::time_abi::TimeAbiParameters; use pal_async::DefaultDriver; use pal_async::socket::PolledSocket; -use pal_async::socket::WriteHalf; use pal_async::task::Spawn; use petri_artifacts_common::tags::MachineArch; use serial_core::resources::DisconnectedSerialBackendHandle; @@ -38,8 +36,9 @@ use vm_resource::kind::SerialBackendHandle; use vmotherboard::ChipsetDeviceHandle; pub(super) struct RuntimeResources { - input: WriteHalf, output: mesh::Receiver>, + /// The output that followed the marker of the latest wait. + unread: Vec, } pub(super) struct PortbSerial { @@ -356,11 +355,10 @@ pub(super) fn configure_serial( host: PolledSocket, io: Option>, ) -> anyhow::Result { - let (read, input) = host.split(); let (output_send, output) = mesh::channel(); let task = driver.spawn( "microvm-portb-console", - crate::log_task_with_output(log_file, read, "microvm-portb-console", output_send), + crate::log_task_with_output(log_file, host, "microvm-portb-console", output_send), ); Ok(SerialData { emulated_serial_config: [None, None, None, None], @@ -368,7 +366,10 @@ pub(super) fn configure_serial( linux_direct_serial_agent: None, microvm: Some(PortbSerial { io: io.unwrap_or_else(|| DisconnectedSerialBackendHandle.into_resource()), - runtime: RuntimeResources { input, output }, + runtime: RuntimeResources { + output, + unread: Vec::new(), + }, }), }) } @@ -469,46 +470,37 @@ impl PetriVmInner { self.wait_for_microvm_portb_bytes(marker.as_bytes()).await } + /// Waits for `marker` in the microVM portb output, consuming the output + /// through it. The output that follows it is kept for the next wait, so + /// that consecutive waits find markers that arrived together. pub(super) async fn wait_for_microvm_portb_bytes( &mut self, marker: &[u8], ) -> anyhow::Result<()> { anyhow::ensure!(!marker.is_empty(), "microVM portb marker cannot be empty"); - let output = &mut self + let RuntimeResources { output, unread } = self .resources .microvm .as_mut() - .context("microVM portb output is not configured")? - .output; - let mut buffered = Vec::new(); + .context("microVM portb output is not configured")?; loop { + if let Some(start) = unread + .windows(marker.len()) + .position(|window| window == marker) + { + unread.drain(..start + marker.len()); + return Ok(()); + } let chunk = output.recv().await.map_err(|error| { anyhow::anyhow!( "microVM portb output disconnected ({error}) after bytes {:?}", - String::from_utf8_lossy(&buffered[..buffered.len().min(256)]) + String::from_utf8_lossy(&unread[..unread.len().min(256)]) ) })?; - buffered.extend_from_slice(&chunk); - if buffered - .windows(marker.len()) - .any(|window| window == marker) - { - return Ok(()); - } + unread.extend_from_slice(&chunk); } } - pub(super) async fn write_microvm_portb_input(&mut self, input: &[u8]) -> anyhow::Result<()> { - self.resources - .microvm - .as_mut() - .context("microVM portb input is not configured")? - .input - .write_all(input) - .await - .context("writing microVM portb input") - } - pub(super) async fn pulse_save_restore(&self) -> anyhow::Result<()> { self.worker .pulse_save_restore() diff --git a/petri/src/vm/openvmm/runtime.rs b/petri/src/vm/openvmm/runtime.rs index 73e8f5cfb4a..8bce3f55fde 100644 --- a/petri/src/vm/openvmm/runtime.rs +++ b/petri/src/vm/openvmm/runtime.rs @@ -375,13 +375,10 @@ impl PetriVmOpenVmm { pub async fn wait_for_microvm_portb_output(&mut self, marker: &str) -> anyhow::Result<()> ); petri_vm_fn!( - /// Wait for an exact byte sequence from the microVM portb console. + /// Wait for an exact byte sequence from the microVM portb console, + /// consuming its output through the sequence. pub async fn wait_for_microvm_portb_bytes(&mut self, marker: &[u8]) -> anyhow::Result<()> ); - petri_vm_fn!( - /// Write raw bytes to the microVM portb console input stream. - pub async fn write_microvm_portb_input(&mut self, input: &[u8]) -> anyhow::Result<()> - ); petri_vm_fn!( /// Perform one pulse save/restore operation. pub async fn pulse_save_restore(&mut self) -> anyhow::Result<()> diff --git a/vmm_tests/vmm_tests/tests/tests/ttrpc/microvm.rs b/vmm_tests/vmm_tests/tests/tests/ttrpc/microvm.rs index ffab69c1190..7cde6f4788a 100644 --- a/vmm_tests/vmm_tests/tests/tests/ttrpc/microvm.rs +++ b/vmm_tests/vmm_tests/tests/tests/ttrpc/microvm.rs @@ -7,7 +7,6 @@ use super::launch_openvmm; use anyhow::Context; use futures::AsyncBufReadExt; use futures::AsyncReadExt; -use futures::AsyncWriteExt; #[cfg(windows)] use guid::Guid; use mesh::CancelContext; @@ -180,8 +179,6 @@ pub(super) async fn test_ttrpc_microvm_linux_direct_lifecycle_and_snapshot( const READY_MARKER: &[u8] = b"OPENVMM-LINUX-MPTABLE-SNAPSHOT-READY"; const SNAPSHOT_CONTINUED_MARKER: &[u8] = b"SNAPSHOT-CONTINUED=1"; const STATE_MARKER: &[u8] = b"STATE=1"; - const COMMAND_STATE: u8 = 5; - const COMMAND_SHUTDOWN: u8 = 6; const KERNEL_OVERRIDE: &str = "OPENVMM_MICROVM_TEST_KERNEL"; const INITRD_OVERRIDE: &str = "OPENVMM_MICROVM_TEST_INITRD"; @@ -361,25 +358,6 @@ write_port() {{ octal=$(printf '%03o' "$2") printf "\\$octal" | dd of=/dev/port bs=1 seek="$1" count=1 conv=notrunc 2>/dev/null }} -# Only kernels with the portb hvc driver expose /dev/hvc0, which then owns the -# portb input. Other kernels, such as the packaged test kernel, poll portb. -if [ -c /dev/hvc0 ]; then - stty -F /dev/hvc0 raw -echo - next_byte() {{ - dd if=/dev/hvc0 bs=1 count=1 2>/dev/null | od -An -tu1 | tr -d ' ' - }} -else - next_byte() {{ - while :; do - input_status=$(read_port 234) - [ -n "$input_status" ] || input_status=0 - if [ $((input_status & 1)) -ne 0 ]; then - read_port 233 - return - fi - done - }} -fi generation=0 {} write_port 1541 0 @@ -426,26 +404,15 @@ case "$range_count" in while :; do sleep 1; done ;; esac -while :; do - command=$(next_byte) - case "$command" in - {COMMAND_STATE}) - if [ "$generation" = 1 ]; then +# portb takes no host input, so the restored guest reports its state and +# shuts down without commands. +if [ "$generation" = 1 ]; then {} - else -{} - fi - ;; - {COMMAND_SHUTDOWN}) - exit_status=$(next_byte) - write_port 1540 "$exit_status" - while :; do sleep 1; done - ;; - *) +else {} - ;; - esac -done +fi +write_port 1540 0 +while :; do sleep 1; done "#, portb_output(READY_MARKER)?, portb_output(SNAPSHOT_CONTINUED_MARKER)?, @@ -457,7 +424,6 @@ done portb_output(b"RESTORE-RANGE-COUNT-INVALID")?, portb_output(STATE_MARKER)?, portb_output(b"STATE-INVALID")?, - portb_output(b"UNKNOWN-COMMAND")?, ); let tempdir = if cfg!(target_os = "linux") { @@ -609,7 +575,7 @@ done ) })?; let portb = PolledSocket::new(&driver, UnixStream::connect(&portb_path)?)?; - let (mut portb_read, mut portb_write) = portb.split(); + let (mut portb_read, _portb_write) = portb.split(); let mut resume_cancel = CancelContext::new().with_timeout(Duration::from_secs(15)); let (resume, readiness) = futures::join!( resume_cancel.until_cancelled(client.call().start(vmservice::Vm::ResumeVm, ())), @@ -643,15 +609,11 @@ done .with_context(|| { format!("Linux direct restore {restore_index} did not report restore targets") })?; - portb_write.write_all(&[COMMAND_STATE]).await?; - portb_write.flush().await?; wait_for_bytes(&mut portb_read, &mut restore_output, STATE_MARKER) .await .with_context(|| { - format!("Linux direct restore {restore_index} did not answer state query") + format!("Linux direct restore {restore_index} did not report its state") })?; - portb_write.write_all(&[COMMAND_SHUTDOWN, 0]).await?; - portb_write.flush().await?; CancelContext::new() .with_timeout(Duration::from_secs(15)) .until_cancelled(drain_until_closed(&mut portb_read, &mut restore_output)) diff --git a/vmm_tests/vmm_tests/tests/tests/x86_64/microvm.rs b/vmm_tests/vmm_tests/tests/tests/x86_64/microvm.rs index 6600c3bd756..d0f88429e8e 100644 --- a/vmm_tests/vmm_tests/tests/tests/x86_64/microvm.rs +++ b/vmm_tests/vmm_tests/tests/tests/x86_64/microvm.rs @@ -29,71 +29,25 @@ async fn phase_1_lifecycle(config: PetriVmBuilder) -> anyho const TIMEOUT: Duration = Duration::from_secs(30); const BOOT_MARKER: &[u8] = b"OPENVMM-LINUX-DIRECT-TEST-READY\n"; const RAW_MARKER: &[u8] = b"\0\r\n\x7f\xffLINUX-DIRECT-ECHO\n"; - const COMMAND_PING: u8 = 1; - const COMMAND_ECHO: u8 = 2; - const COMMAND_SNAPSHOT: u8 = 3; - const COMMAND_STATE: u8 = 5; - const COMMAND_SHUTDOWN: u8 = 6; + const SNAPSHOT_MARKER: &[u8] = b"SNAPSHOT-CONTINUED=1\n"; + // portb takes no host input, so the guest takes its steps without + // commands: it writes raw bytes, requests a snapshot that has no + // destination, and once the request continues, shuts down with status 37. let script = format!( "#!/bin/busybox sh\n\ mount -t devtmpfs devtmpfs /dev 2>/dev/null || true\n\ - read_port() {{\n\ - dd if=/dev/port bs=1 skip=\"$1\" count=1 2>/dev/null | od -An -tu1 | tr -d ' '\n\ - }}\n\ - next_byte() {{\n\ - while :; do\n\ - status=$(read_port 234)\n\ - [ -n \"$status\" ] || status=0\n\ - if [ $((status & 1)) -ne 0 ]; then\n\ - read_port 233\n\ - return\n\ - fi\n\ - done\n\ - }}\n\ - generation=0\n\ {}\ - while :; do\n\ - command=$(next_byte)\n\ - case \"$command\" in\n\ - {COMMAND_PING})\n\ - {}\ - ;;\n\ - {COMMAND_ECHO})\n\ - {}\ - ;;\n\ - {COMMAND_SNAPSHOT})\n\ - {}\ - printf '\\x00' | dd of=/dev/port bs=1 seek=1541 count=1 conv=notrunc 2>/dev/null\n\ - generation=$((generation + 1))\n\ - {}\ - ;;\n\ - {COMMAND_STATE})\n\ - if [ \"$generation\" = 1 ]; then\n\ - {}\ - else\n\ - {}\ - fi\n\ - ;;\n\ - {COMMAND_SHUTDOWN})\n\ - status=$(next_byte)\n\ - octal=$(printf '%03o' \"$status\")\n\ - printf \"\\\\$octal\" | dd of=/dev/port bs=1 seek=1540 count=1 conv=notrunc 2>/dev/null\n\ - while :; do sleep 1; done\n\ - ;;\n\ - *)\n\ - {}\ - ;;\n\ - esac\n\ - done\n", + {}\ + {}\ + printf '\\x00' | dd of=/dev/port bs=1 seek=1541 count=1 conv=notrunc 2>/dev/null\n\ + {}\ + printf '\\045' | dd of=/dev/port bs=1 seek=1540 count=1 conv=notrunc 2>/dev/null\n\ + while :; do sleep 1; done\n", portb_output(BOOT_MARKER)?, - portb_output(b"PONG\n")?, portb_output(RAW_MARKER)?, portb_output(b"SNAPSHOT-REQUESTED\n")?, - portb_output(b"SNAPSHOT-CONTINUED=1\n")?, - portb_output(b"STATE=1\n")?, - portb_output(b"STATE-INVALID\n")?, - portb_output(b"UNKNOWN-COMMAND\n")?, + portb_output(SNAPSHOT_MARKER)?, ); let config = config.with_microvm_machine(1); let initrd = config.prepare_initrd_with_file("microvm-test", script.as_bytes(), 0o100755)?; @@ -107,12 +61,38 @@ async fn phase_1_lifecycle(config: PetriVmBuilder) -> anyho .run_without_agent() .await?; - CancelContext::new() + for (marker, failure) in [ + ( + BOOT_MARKER, + "timed out waiting for microVM Linux direct boot marker", + ), + (RAW_MARKER, "microVM raw binary output was not observed"), + ( + SNAPSHOT_MARKER, + "microVM snapshot request without a destination did not continue", + ), + ] { + CancelContext::new() + .with_timeout(TIMEOUT) + .until_cancelled(vm.backend().wait_for_microvm_portb_bytes(marker)) + .await + .context(failure)??; + } + + let halt = CancelContext::new() .with_timeout(TIMEOUT) - .until_cancelled(vm.backend().wait_for_microvm_portb_bytes(BOOT_MARKER)) + .until_cancelled(vm.wait_for_halt()) .await - .context("timed out waiting for microVM Linux direct boot marker")??; + .context("timed out waiting for microVM status shutdown")??; + assert_eq!(halt.reason, PetriHaltReason::PowerOff); + assert!( + halt.detail.contains("code: 37"), + "expected microVM shutdown status 37, got {}", + halt.detail + ); + // The worker still serves the halted VM's management requests, and the + // microVM refuses host saves whatever the guest's state. let save_error = vm .backend() .save_state() @@ -133,60 +113,5 @@ async fn phase_1_lifecycle(config: PetriVmBuilder) -> anyho "unexpected microVM pulse-save error: {pulse_error:#}" ); - vm.backend() - .write_microvm_portb_input(&[COMMAND_PING]) - .await?; - CancelContext::new() - .with_timeout(TIMEOUT) - .until_cancelled(vm.backend().wait_for_microvm_portb_output("PONG")) - .await - .context("microVM Linux direct guest did not answer ping")??; - - vm.backend() - .write_microvm_portb_input(&[COMMAND_ECHO]) - .await?; - CancelContext::new() - .with_timeout(TIMEOUT) - .until_cancelled(vm.backend().wait_for_microvm_portb_bytes(RAW_MARKER)) - .await - .context("microVM raw binary echo was not observed")??; - - vm.backend() - .write_microvm_portb_input(&[COMMAND_SNAPSHOT]) - .await?; - CancelContext::new() - .with_timeout(TIMEOUT) - .until_cancelled( - vm.backend() - .wait_for_microvm_portb_output("SNAPSHOT-CONTINUED=1"), - ) - .await - .context("microVM snapshot request without a destination did not continue")??; - - vm.backend() - .write_microvm_portb_input(&[COMMAND_STATE]) - .await?; - CancelContext::new() - .with_timeout(TIMEOUT) - .until_cancelled(vm.backend().wait_for_microvm_portb_output("STATE=1")) - .await - .context("microVM guest state did not persist after the snapshot request")??; - - vm.backend() - .write_microvm_portb_input(&[COMMAND_SHUTDOWN, 37]) - .await?; - - let halt = CancelContext::new() - .with_timeout(TIMEOUT) - .until_cancelled(vm.wait_for_halt()) - .await - .context("timed out waiting for microVM status shutdown")??; - assert_eq!(halt.reason, PetriHaltReason::PowerOff); - assert!( - halt.detail.contains("code: 37"), - "expected microVM shutdown status 37, got {}", - halt.detail - ); - vm.teardown().await }