Skip to content

Commit

Permalink
hypestv: support relaying serial ports to new windows (#374)
Browse files Browse the repository at this point in the history
Lift the console relay support from OpenVMM and use it to launch serial
ports in separate windows. This replaces the need to separately use
`hvc` for interactive serial ports.

To use:

```
serial 1 term
```

Also, add support for `quit`, and rename the `output` serial mode to
`log`.
  • Loading branch information
jstarks authored Nov 22, 2024
1 parent 58d7ac0 commit d5ffe88
Show file tree
Hide file tree
Showing 12 changed files with 536 additions and 147 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,18 @@ dependencies = [
"crossbeam-utils",
]

[[package]]
name = "console_relay"
version = "0.0.0"
dependencies = [
"anyhow",
"futures",
"getrandom",
"pal_async",
"term",
"unix_socket",
]

[[package]]
name = "consomme"
version = "0.0.0"
Expand Down Expand Up @@ -2955,9 +2967,11 @@ dependencies = [
"anyhow",
"clap",
"clap_dyn_complete",
"console_relay",
"diag_client",
"dirs",
"futures",
"futures-concurrency",
"guid",
"inspect",
"mesh",
Expand Down Expand Up @@ -4417,6 +4431,7 @@ dependencies = [
"chipset_resources",
"clap",
"clap_dyn_complete",
"console_relay",
"debug_worker_defs",
"diag_client",
"dirs",
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ cache_topology = { path = "support/cache_topology" }
closeable_mutex = { path = "support/closeable_mutex" }
ci_logger = { path = "support/ci_logger" }
clap_dyn_complete = { path = "support/clap_dyn_complete" }
console_relay = { path = "support/console_relay" }
safe_intrinsics = { path = "support/safe_intrinsics" }
debug_ptr = { path = "support/debug_ptr" }
fast_select = { path = "support/fast_select" }
Expand Down
9 changes: 6 additions & 3 deletions Guide/src/dev_guide/dev_tools/hypestv.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ After this, all commands will implicitly operate on `tdxvm`. Use `select` again
to work on another VM.

To enable serial port output, use the `serial` command. This can be used at any
time, even while the VM is not running. E.g., to enable serial port output for
COM2:
time, even while the VM is not running. E.g., to open a separate window for
interactive use of COM1 and enable logging serial port output for COM2:

```
tdxvm [off]> serial 2 output
tdxvm [off]> serial 1 term
tdxvm [off]> serial 2 log
```

Start a VM with `start`. This is an asynchronous command: you can continue to
Expand All @@ -61,6 +62,7 @@ on the prompt may not be accurate until you type another command or press Enter.

```
tdxvm [off]> start
serial port 1 connected
serial port 2 connected
VM started
tdxvm [off]>
Expand Down Expand Up @@ -92,6 +94,7 @@ VM.

```
tdxvm [running]> kill
serial port 1 disconnected
serial port 2 disconnected
VM killed
tdxvm [stopping]>
Expand Down
2 changes: 2 additions & 0 deletions hyperv/tools/hypestv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ rust-version.workspace = true

[dependencies]
clap_dyn_complete.workspace = true
console_relay.workspace = true
diag_client.workspace = true
guid.workspace = true
inspect.workspace = true
Expand All @@ -18,6 +19,7 @@ anyhow.workspace = true
clap.workspace = true
dirs.workspace = true
futures.workspace = true
futures-concurrency.workspace = true
parking_lot.workspace = true
rustyline = { workspace = true, features = ["derive"] }
shell-words.workspace = true
Expand Down
45 changes: 34 additions & 11 deletions hyperv/tools/hypestv/src/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use hyperv::run_hvc;
use mesh::rpc::RpcSend;
use pal_async::DefaultDriver;
use rustyline_printer::Printer;
use std::fmt::Display;
use std::path::PathBuf;
use std::sync::Arc;
use vm::Vm;

Expand All @@ -42,6 +44,10 @@ pub(crate) enum InteractiveCommand {
/// Detach from the active VM.
Detach,

/// Quit the interactive shell.
#[clap(visible_alias = "q")]
Quit,

#[clap(flatten)]
Vm(VmCommand),
}
Expand Down Expand Up @@ -74,12 +80,12 @@ pub(crate) enum VmCommand {
force: bool,
},

/// Sets the serial output mode.
/// Gets or sets the serial output mode.
Serial {
/// The serial port to configure (1 = COM1, etc.).
port: u32,
port: Option<u32>,
/// The serial output mode.
mode: SerialMode,
mode: Option<SerialMode>,
},

/// Inspect host or paravisor state.
Expand All @@ -102,20 +108,28 @@ pub(crate) enum VmCommand {
},
}

#[derive(ValueEnum, Clone)]
#[derive(ValueEnum, Copy, Clone)]
pub(crate) enum SerialMode {
/// The serial port is disconnected.
Off,
/// The serial port output is connected to the host's console.
Output,
// TODO: add Console mode for interactive console, and Terminal mode for
// launching a terminal emulator.
/// The serial port output is logged to standard output.
Log,
/// The serial port input and output are connected to a new terminal
/// emulator window.
Term,
// TODO: add Console mode for interactive console.
}

impl Display for SerialMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.pad(self.to_possible_value().unwrap().get_name())
}
}

pub(crate) enum Request {
Prompt(mesh::rpc::Rpc<(), String>),
Inspect(mesh::rpc::Rpc<(InspectTarget, String), anyhow::Result<inspect::Node>>),
Command(mesh::rpc::Rpc<InteractiveCommand, anyhow::Result<()>>),
Command(mesh::rpc::Rpc<InteractiveCommand, anyhow::Result<bool>>),
}

pub(crate) enum InspectTarget {
Expand All @@ -130,10 +144,15 @@ pub(crate) enum InspectTarget {
struct CommandLine {
/// The initial VM name. Use select to change the active VM.
vm: Option<String>,
#[clap(long, hide(true))]
relay_console_path: Option<PathBuf>,
}

pub async fn main(driver: DefaultDriver) -> anyhow::Result<()> {
let command_line = CommandLine::parse();
if let Some(relay_console_path) = command_line.relay_console_path {
return console_relay::relay_console(&relay_console_path);
}

let mut rl = rustyline::Editor::<_, rustyline::history::FileHistory>::with_config(
rustyline::Config::builder()
Expand Down Expand Up @@ -215,7 +234,8 @@ pub async fn main(driver: DefaultDriver) -> anyhow::Result<()> {

match parse(&mut template, trimmed) {
Ok(cmd) => match block_on(send.call_failable(Request::Command, cmd)) {
Ok(()) => {}
Ok(true) => {}
Ok(false) => break,
Err(err) => {
eprintln!("{:#}", err);
}
Expand Down Expand Up @@ -275,8 +295,11 @@ pub async fn main(driver: DefaultDriver) -> anyhow::Result<()> {
.handle_command(cmd)
.await?;
}
InteractiveCommand::Quit => {
return Ok(false);
}
}
Ok(())
Ok(true)
})
.await
}
Expand Down
Loading

0 comments on commit d5ffe88

Please sign in to comment.