crosvm: add --vsock command-line option and config-file parameter.

Add a `--vsock` command-line option that is designed to replace the
`--cid`, `--vhost-vsock-device` and `--vhost-vsock-fd` parameters into a
single one. This parameter can be used on Windows as well as Linux, and
on the latter users can also specify the vhost-vsock device to use.

This parameter is the preferred way to add vsock devices, and thus is
also enabled for configuration files. Also update the book's
instructions in lockstep.

BUG=b:255223604
TEST=run crosvm with `--vsock cid=42` option and ensure the guest device
is working using socat to communicate with the host.

Change-Id: I9515922415e5a55289a9e21d5f2f247a30fadc74
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4253292
Commit-Queue: Alexandre Courbot <acourbot@chromium.org>
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
This commit is contained in:
Alexandre Courbot 2023-02-14 16:05:12 +09:00 committed by crosvm LUCI
parent 99af34acc4
commit 4b06502db1
4 changed files with 28 additions and 7 deletions

View file

@ -7,6 +7,7 @@ use std::path::PathBuf;
use serde::Deserialize;
use serde::Serialize;
use serde_keyvalue::FromKeyValues;
static VHOST_VSOCK_DEFAULT_PATH: &str = "/dev/vhost-vsock";
@ -14,7 +15,7 @@ fn default_vsock_path() -> PathBuf {
PathBuf::from(VHOST_VSOCK_DEFAULT_PATH)
}
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, FromKeyValues)]
#[serde(deny_unknown_fields)]
/// Configuration for a Vsock device.

View file

@ -11,8 +11,9 @@ pub use vsock::VsockError;
use serde::Deserialize;
use serde::Serialize;
use serde_keyvalue::FromKeyValues;
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, FromKeyValues)]
#[serde(deny_unknown_fields)]
// Configuration for a Vsock device.
pub struct VsockConfig {

View file

@ -2,13 +2,13 @@
crosvm supports [virtio-vsock] device for communication between the host and a guest VM.
Assign a context id to a guest VM by passing it with `--cid` flag.
Assign a context id to a guest VM by passing it with the `--vsock` flag.
```sh
GUEST_CID=3
crosvm run \
--cid "${GUEST_CID}" \
--vsock "${GUEST_CID}" \
<usual crosvm arguments>
/path/to/bzImage
```

View file

@ -885,7 +885,7 @@ pub struct RunCommand {
cfg: Option<Box<Self>>,
#[argh(option, arg_name = "CID")]
#[serde(skip)] // TODO(b/255223604)
#[serde(skip)] // Deprecated - use `vsock` instead.
#[merge(strategy = overwrite_option)]
/// context ID for virtual sockets.
pub cid: Option<u64>,
@ -2098,14 +2098,14 @@ pub struct RunCommand {
#[cfg(unix)]
#[argh(option, arg_name = "SOCKET_PATH")]
#[serde(skip)] // TODO(b/255223604)
#[serde(skip)] // Deprecated - use `vsock` instead.
#[merge(strategy = overwrite_option)]
/// path to the vhost-vsock device. (default /dev/vhost-vsock)
pub vhost_vsock_device: Option<PathBuf>,
#[cfg(unix)]
#[argh(option, arg_name = "FD")]
#[serde(skip)] // TODO(b/255223604)
#[serde(skip)] // Deprecated - use `vsock` instead.
#[merge(strategy = overwrite_option)]
/// open FD to the vhost-vsock device, mutually exclusive with vhost-vsock-device
pub vhost_vsock_fd: Option<RawDescriptor>,
@ -2158,6 +2158,16 @@ pub struct RunCommand {
/// per device.
pub virtio_snd: Vec<SndParameters>,
#[argh(option, arg_name = "cid=CID[,device=VHOST_DEVICE]")]
#[serde(default)]
#[merge(strategy = overwrite_option)]
/// add a vsock device. Since a guest can only have one CID,
/// this option can only be specified once.
/// cid=CID - CID to use for the device.
/// device=VHOST_DEVICE - path to the vhost-vsock device to
/// use (Linux only). Defaults to /dev/vhost-vsock.
pub vsock: Option<VsockConfig>,
#[cfg(all(feature = "vtpm", target_arch = "x86_64"))]
#[argh(switch)]
#[serde(skip)] // TODO(b/255223604)
@ -2535,7 +2545,16 @@ impl TryFrom<RunCommand> for super::config::Config {
cfg.balloon_control = cmd.balloon_control;
cfg.vsock = cmd.vsock;
// Legacy vsock options.
if let Some(cid) = cmd.cid {
if cfg.vsock.is_some() {
return Err(
"`cid` and `vsock` cannot be specified together. Use `vsock` only.".to_string(),
);
}
let legacy_vsock_config = VsockConfig::new(
cid,
#[cfg(unix)]