Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update vhost-device-console backend & move it in main workspace #727

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
resolver = "2"

members = [
"vhost-device-console",
"vhost-device-gpio",
"vhost-device-i2c",
"vhost-device-input",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ To be included here device backends must:

Here is the list of device backends that we support:

- [Console](https://github.com/rust-vmm/vhost-device/blob/main/vhost-device-console/README.md)
- [GPIO](https://github.com/rust-vmm/vhost-device/blob/main/vhost-device-gpio/README.md)
- [I2C](https://github.com/rust-vmm/vhost-device/blob/main/vhost-device-i2c/README.md)
- [Input](https://github.com/rust-vmm/vhost-device/blob/main/vhost-device-input/README.md)
Expand Down Expand Up @@ -48,7 +49,6 @@ Here is the list of device backends in **staging**:

- [Video](https://github.com/rust-vmm/vhost-device/blob/main/staging/vhost-device-video/README.md)
- [Can](https://github.com/rust-vmm/vhost-device/blob/main/staging/vhost-device-can/README.md)
- [Console](https://github.com/rust-vmm/vhost-device/blob/main/staging/vhost-device-console/README.md)

<!--
Template:
Expand Down
1 change: 0 additions & 1 deletion staging/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ resolver = "2"
members = [
"vhost-device-video",
"vhost-device-can",
"vhost-device-console",
]
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ xen = ["vm-memory/xen", "vhost/xen", "vhost-user-backend/xen"]
[dependencies]
console = "0.15.7"
crossterm = "0.27.0"
nix = "0.26.4"
queues = "1.0.2"
clap = { version = "4.5", features = ["derive"] }
env_logger = "0.10"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ arguments) and allows input/output to the guest console via that socket.
This program is tested with QEMU's `vhost-user-device-pci` device.
Examples' section below.

## Staging Device
This device will be in `staging` until we complete the following steps:
- [ ] Increase test coverage
- [ ] Support VIRTIO_CONSOLE_F_SIZE feature (optional)
- [ ] Support VIRTIO_CONSOLE_F_EMERG_WRITE feature (optional)
Comment on lines -23 to -24
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we supporting these?


## Synopsis
```text
vhost-device-console --socket-path=<SOCKET_PATH>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//
// SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause

use log::{error, info, warn};
use log::{error, info};
use std::any::Any;
use std::collections::HashMap;
use std::path::PathBuf;
Expand All @@ -28,6 +28,8 @@ pub(crate) enum Error {
SocketCountInvalid(usize),
#[error("Could not create console backend: {0}")]
CouldNotCreateBackend(crate::vhu_console::Error),
#[error("Could not create console backend: {0}")]
CouldNotInitBackend(crate::vhu_console::Error),
#[error("Could not create daemon: {0}")]
CouldNotCreateDaemon(vhost_user_backend::Error),
#[error("Fatal error: {0}")]
Expand Down Expand Up @@ -92,6 +94,12 @@ pub(crate) fn start_backend_server(
VhostUserConsoleBackend::new(arc_controller).map_err(Error::CouldNotCreateBackend)?,
));

vu_console_backend
.write()
.unwrap()
.assign_input_method(tcp_addr.clone())
.map_err(Error::CouldNotInitBackend)?;

let mut daemon = VhostUserDaemon::new(
String::from("vhost-device-console-backend"),
vu_console_backend.clone(),
Expand All @@ -102,26 +110,15 @@ pub(crate) fn start_backend_server(
let vring_workers = daemon.get_epoll_handlers();
vu_console_backend
.read()
.unwrap()
.set_vring_worker(&vring_workers[0]);

// Start the corresponding console thread
let read_handle = if backend == BackendType::Nested {
VhostUserConsoleBackend::start_console_thread(&vu_console_backend)
} else {
VhostUserConsoleBackend::start_tcp_console_thread(&vu_console_backend, tcp_addr.clone())
};

daemon.serve(&socket).map_err(Error::ServeFailed)?;

// Kill console input thread
vu_console_backend.read().unwrap().kill_console_thread();

// Wait for read thread to exit
match read_handle.join() {
Ok(_) => info!("The read thread returned successfully"),
Err(e) => warn!("The read thread returned the error: {:?}", e),
}
.expect("Cannot open as write\n")
.set_vring_worker(vring_workers[0].clone());

daemon.serve(&socket).map_err(|e| {
// Even if daemon stops unexpectedly, the backend should
// be terminated properly (disable raw mode).
vu_console_backend.read().unwrap().prepare_exit();
Error::ServeFailed(e)
})?;
}
}

Expand Down Expand Up @@ -243,71 +240,44 @@ mod tests {
assert!(VuConsoleConfig::try_from(args).is_ok());
}

fn test_backend_start_and_stop(args: ConsoleArgs) {
fn test_backend_start_and_stop(args: ConsoleArgs) -> Result<()> {
let config = VuConsoleConfig::try_from(args).expect("Wrong config");

let tcp_addrs = config.generate_tcp_addrs();
let backend = config.backend;

for (_socket, tcp_addr) in config
for (socket, tcp_addr) in config
.generate_socket_paths()
.into_iter()
.zip(tcp_addrs.iter())
{
let controller = ConsoleController::new(backend);
let arc_controller = Arc::new(RwLock::new(controller));
let vu_console_backend = Arc::new(RwLock::new(
VhostUserConsoleBackend::new(arc_controller)
.map_err(Error::CouldNotCreateBackend)
.expect("Fail create vhuconsole backend"),
));

let mut _daemon = VhostUserDaemon::new(
String::from("vhost-device-console-backend"),
vu_console_backend.clone(),
GuestMemoryAtomic::new(GuestMemoryMmap::new()),
)
.map_err(Error::CouldNotCreateDaemon)
.expect("Failed create daemon");

// Start the corresponinding console thread
let read_handle = if backend == BackendType::Nested {
VhostUserConsoleBackend::start_console_thread(&vu_console_backend)
} else {
VhostUserConsoleBackend::start_tcp_console_thread(
&vu_console_backend,
tcp_addr.clone(),
)
};

// Kill console input thread
vu_console_backend.read().unwrap().kill_console_thread();

// Wait for read thread to exit
assert_matches!(read_handle.join(), Ok(_));
start_backend_server(socket, tcp_addr.to_string(), backend)?;
}
Ok(())
}

#[test]
fn test_start_net_backend_success() {
fn test_start_backend_server_success() {
let args = ConsoleArgs {
socket_path: String::from("/tmp/vhost.sock").into(),
socket_path: String::from("/not_a_dir/vhost.sock").into(),
//socket_path: String::from("/tmp/vhost.sock").into(),
backend: BackendType::Network,
tcp_port: String::from("12345"),
socket_count: 1,
};

test_backend_start_and_stop(args);
assert!(test_backend_start_and_stop(args).is_err());
}

#[test]
fn test_start_nested_backend_success() {
let args = ConsoleArgs {
socket_path: String::from("/tmp/vhost.sock").into(),
backend: BackendType::Nested,
tcp_port: String::from("12345"),
fn test_start_backend_success() {
let config = VuConsoleConfig {
socket_path: String::from("/not_a_dir/vhost.sock").into(),
backend: BackendType::Network,
tcp_port: String::from("12346"),
socket_count: 1,
};

test_backend_start_and_stop(args);
assert!(start_backend(config).is_err());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub enum BackendType {
pub(crate) struct ConsoleController {
config: VirtioConsoleConfig,
pub backend: BackendType,
pub exit: bool,
}

impl ConsoleController {
Expand All @@ -33,7 +32,6 @@ impl ConsoleController {
emerg_wr: 64.into(),
},
backend,
exit: false,
}
}

Expand Down
Loading