Skip to content

Commit 26927c6

Browse files
authored
Merge pull request #2294 from hermit-os/xtask-vsock
feat(xtask): add support for testing vsock
2 parents f844e66 + c522ea6 commit 26927c6

4 files changed

Lines changed: 84 additions & 4 deletions

File tree

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ jobs:
271271
if: matrix.arch == 'x86_64'
272272
- run: cargo clean
273273
working-directory: .
274+
- run: cargo xtask ci rs --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.rs_flags }} --package vsock qemu ${{ matrix.qemu_flags }} --sudo --devices virtio-vsock-pci
275+
if: matrix.arch != 'riscv64' && matrix.arch != 'aarch64_be'
274276
- run: cargo xtask ci rs --arch ${{ matrix.arch }} --profile ${{ matrix.profile }} ${{ matrix.rs_flags }} --package httpd --features ci,hermit/dhcpv4,hermit/virtio-net qemu ${{ matrix.qemu_flags }} --devices virtio-net-pci
275277
if: matrix.arch != 'riscv64'
276278
# FIXME: this is broken on QEMU 8.2.2

Cargo.lock

Lines changed: 41 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

xtask/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2024"
55

66
[features]
77
default = ["ci"]
8-
ci = ["dep:ovmf-prebuilt", "dep:sysinfo", "dep:ureq", "dep:wait-timeout"]
8+
ci = ["dep:ovmf-prebuilt", "dep:sysinfo", "dep:ureq", "dep:vsock", "dep:wait-timeout"]
99

1010
[dependencies]
1111
anyhow = "1.0"
@@ -15,5 +15,6 @@ home = "0.5"
1515
ovmf-prebuilt = { version = "0.2", optional = true }
1616
sysinfo = { version = "0.38", optional = true }
1717
ureq = { version = "3", default-features = false, features = ["rustls"], optional = true }
18+
vsock = { version = "0.5", optional = true }
1819
wait-timeout = { version = "0.2", optional = true }
1920
xshell = "0.2"

xtask/src/ci/qemu.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::{env, fs, thread};
99
use anyhow::{Context, Result, bail, ensure};
1010
use clap::{Args, ValueEnum};
1111
use sysinfo::{CpuRefreshKind, System};
12+
use vsock::VsockStream;
1213
use wait_timeout::ChildExt;
1314
use xshell::cmd;
1415

@@ -79,6 +80,12 @@ pub enum Device {
7980

8081
/// virtio-net via PCI.
8182
VirtioNetPci,
83+
84+
/// virtio-vsock via MMIO.
85+
VirtioVsockMmio,
86+
87+
/// virtio-vsock via PCI.
88+
VirtioVsockPci,
8289
}
8390

8491
impl Qemu {
@@ -152,12 +159,13 @@ impl Qemu {
152159
"mioudp" => test_mioudp(guest_ip)?,
153160
"poll" => test_poll(guest_ip)?,
154161
"stdin" => test_stdin(&mut qemu.0)?,
162+
"vsock" => test_vsock()?,
155163
_ => {}
156164
}
157165

158166
if matches!(
159167
image_name,
160-
"axum-example" | "http_server" | "http_server_poll" | "http_server_select"
168+
"axum-example" | "http_server" | "http_server_poll" | "http_server_select" | "vsock"
161169
) || self.devices.contains(&Device::CadenceGem)
162170
// sifive_u, on which we test CadenceGem, does not support software shutdowns, so we have to kill the machine ourselves.
163171
{
@@ -430,6 +438,16 @@ impl Qemu {
430438
"virtconsole,chardev=char0".to_owned(),
431439
]
432440
}
441+
device @ (Device::VirtioVsockMmio | Device::VirtioVsockPci) => {
442+
let device_arg = match device {
443+
Device::VirtioVsockMmio => "vhost-vsock-device",
444+
Device::VirtioVsockPci => "vhost-vsock-pci,disable-legacy=on",
445+
_ => unreachable!(),
446+
};
447+
let device_arg = format!("{device_arg},guest-cid=3");
448+
449+
vec!["-device".to_owned(), device_arg]
450+
}
433451
})
434452
.collect()
435453
}
@@ -545,6 +563,26 @@ fn test_stdin(child: &mut Child) -> Result<()> {
545563
Ok(())
546564
}
547565

566+
fn test_vsock() -> Result<()> {
567+
thread::sleep(Duration::from_secs(10));
568+
let messages = ["Hello, there!", "Hello, again!", "Bye-bye!"];
569+
570+
let mut stream = VsockStream::connect_with_cid_port(3, 9975)?;
571+
for message in messages {
572+
writeln!(&mut stream, "{message}")?;
573+
thread::sleep(Duration::from_secs(1));
574+
}
575+
576+
const BUF_SIZE: usize = 8 * 1024;
577+
let mut buf = vec![0; BUF_SIZE];
578+
let n = stream.read(&mut buf)?;
579+
let s = str::from_utf8(&buf[0..n])?;
580+
let received_messages = s.trim().split('\n').collect::<Vec<_>>();
581+
assert_eq!(received_messages, messages);
582+
583+
Ok(())
584+
}
585+
548586
fn test_http_server(guest_ip: IpAddr) -> Result<()> {
549587
thread::sleep(Duration::from_secs(10));
550588
let url = format!("http://{guest_ip}:9975");

0 commit comments

Comments
 (0)