From bcad0b0e999b90d4207c262824836f513356c1e7 Mon Sep 17 00:00:00 2001 From: Anastassios Nanos Date: Thu, 10 Nov 2022 16:29:19 +0000 Subject: [PATCH] Add vsock cli param Using an extra CLI parameter, we are able to specify a unix socket to be used as a VSOCK endpoint for Guest-to-Host communication. For now, the argument is just the socket path, with a hardcoded Guest ID. The default value is empty so that the VSOCK endpoint is not added by default. Example invocation: ./dbs-cli --kernel-path ./vmlinux \ --rootfs ./rootfs.img \ --boot-args 'console=ttyS0 tty0 pci=off root=/dev/vda' \ --vsock vsock.sock Signed-off-by: Anastassios Nanos --- README.md | 18 +++++++++++++++++- src/cli_instance.rs | 24 +++++++++++++++++++++++- src/parser/args.rs | 13 +++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ae24c28..d796486 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,22 @@ Create a vsock console (communication with sock file) --serial-path "/tmp/dbs" ; ``` +Create a virtio-vsock tunnel for Guest-to-Host communication. + +> When the parameter `vsock` is not given, `dbs-cli` will not add a virtio-vsock device. +> +> Otherwise, `dbs-cli` will create a unix socket on the host using the argument +> specified with the `--vsock` parameter. + +``` +./dbs-cli \ + --log-file dbs-cli.log --log-level ERROR \ + --kernel-path ~/path/to/kernel/vmlinux.bin \ + --rootfs ~/path/to/rootfs/bionic.rootfs.ext4 \ + --boot-args "console=ttyS0 tty0 reboot=k debug panic=1 pci=off root=/dev/vda1" \ + --vsock /tmp/vsock.sock; +``` + # 2. Usage ## 1. Exit vm @@ -90,4 +106,4 @@ make build # License -`DBS-CLI` is licensed under [Apache License](http://www.apache.org/licenses/LICENSE-2.0), Version 2.0. \ No newline at end of file +`DBS-CLI` is licensed under [Apache License](http://www.apache.org/licenses/LICENSE-2.0), Version 2.0. diff --git a/src/cli_instance.rs b/src/cli_instance.rs index ec4712d..88ef68b 100644 --- a/src/cli_instance.rs +++ b/src/cli_instance.rs @@ -19,7 +19,7 @@ use vmm_sys_util::eventfd::EventFd; use dragonball::{ api::v1::{ BlockDeviceConfigInfo, BootSourceConfig, InstanceInfo, VmmAction, VmmActionError, VmmData, - VmmRequest, VmmResponse, + VmmRequest, VmmResponse, VsockDeviceConfigInfo, }, vm::{CpuTopology, VmConfigInfo}, }; @@ -117,6 +117,20 @@ impl CliInstance { self.insert_block_device(block_device_config_info) .expect("failed to set block device"); + if !args.create_args.vsock.is_empty() { + // VSOCK config + let mut vsock_config_info = VsockDeviceConfigInfo::default(); + vsock_config_info = VsockDeviceConfigInfo { + guest_cid: 42, // dummy value + uds_path: Some(args.create_args.vsock.to_string()), + ..vsock_config_info + }; + + // set vsock + self.insert_vsock(vsock_config_info) + .expect("failed to set vsock socket path"); + } + // start micro-vm self.instance_start().expect("failed to start micro-vm"); @@ -153,6 +167,14 @@ impl CliInstance { Ok(()) } + pub fn insert_vsock(&self, vsock_cfg: VsockDeviceConfigInfo) -> Result<()> { + self.handle_request(Request::Sync(VmmAction::InsertVsockDevice( + vsock_cfg.clone(), + ))) + .with_context(|| format!("Failed to insert vsock device {:?}", vsock_cfg))?; + Ok(()) + } + fn send_request(&self, vmm_action: VmmAction) -> Result { if let Some(ref to_vmm) = self.to_vmm { to_vmm diff --git a/src/parser/args.rs b/src/parser/args.rs index 7264372..3456a97 100644 --- a/src/parser/args.rs +++ b/src/parser/args.rs @@ -170,6 +170,19 @@ pub struct CreateArgs { display_order = 2 )] pub serial_path: String, + + // The path to a vsock socket file + // FIXME: add more params: + // cid="contextid",socket_path="somepath",gid="guest_id" + #[clap( + short, + long, + value_parser, + default_value = "", + help = "Virtio VSOCK socket path", + display_order = 2 + )] + pub vsock: String, } /// Config boot source including rootfs file path