Skip to content

Commit 4d17006

Browse files
authored
Merge pull request #2335 from hermit-os/vsock-client
feat(xtask): support vsock client
2 parents 16c32e5 + 298b5d5 commit 4d17006

3 files changed

Lines changed: 30 additions & 9 deletions

File tree

xtask/src/ci/c.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl C {
4545
match self.action {
4646
Action::Build => Ok(()),
4747
Action::Firecracker(firecracker) => firecracker.run(&image, self.smp),
48-
Action::Qemu(qemu) => qemu.run(&image, self.smp, self.arch, false),
48+
Action::Qemu(qemu) => qemu.run(&image, &[], self.smp, self.arch, false),
4949
Action::Uhyve(uhyve) => uhyve.run(&image, self.smp),
5050
}
5151
}

xtask/src/ci/qemu.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{env, fs, io, thread};
99
use anyhow::{Context, Result, bail, ensure};
1010
use clap::{Args, ValueEnum};
1111
use sysinfo::{CpuRefreshKind, System};
12-
use vsock::VsockStream;
12+
use vsock::{VsockListener, VsockStream};
1313
use wait_timeout::ChildExt as _;
1414
use xshell::cmd;
1515

@@ -89,7 +89,14 @@ pub enum Device {
8989
}
9090

9191
impl Qemu {
92-
pub fn run(self, image: &Path, smp: usize, arch: Arch, small: bool) -> Result<()> {
92+
pub fn run(
93+
self,
94+
image: &Path,
95+
features: &[String],
96+
smp: usize,
97+
arch: Arch,
98+
small: bool,
99+
) -> Result<()> {
93100
let sh = crate::sh()?;
94101

95102
let virtiofsd = self
@@ -160,7 +167,13 @@ impl Qemu {
160167
"mioudp" => test_mioudp(guest_ip)?,
161168
"poll" => test_poll(guest_ip)?,
162169
"stdin" => test_stdin(&mut qemu.0)?,
163-
"vsock" => test_vsock()?,
170+
"vsock" => {
171+
let has_client = features
172+
.iter()
173+
.flat_map(|s| s.split(&[' ', ','][..]))
174+
.any(|feature| feature == "client");
175+
test_vsock(has_client)?
176+
}
164177
_ => {}
165178
}
166179

@@ -580,11 +593,17 @@ fn test_stdin(child: &mut Child) -> Result<()> {
580593
Ok(())
581594
}
582595

583-
fn test_vsock() -> Result<()> {
584-
thread::sleep(Duration::from_secs(10));
585-
let messages = ["Hello, there!", "Hello, again!", "Bye-bye!"];
596+
fn test_vsock(has_client: bool) -> Result<()> {
597+
let mut stream = if has_client {
598+
let listener = VsockListener::bind_with_cid_port(vsock::VMADDR_CID_ANY, 9975)?;
599+
let (stream, _addr) = listener.accept()?;
600+
stream
601+
} else {
602+
thread::sleep(Duration::from_secs(10));
603+
VsockStream::connect_with_cid_port(3, 9975)?
604+
};
586605

587-
let mut stream = VsockStream::connect_with_cid_port(3, 9975)?;
606+
let messages = ["Hello, there!", "Hello, again!", "Bye-bye!"];
588607
for message in messages {
589608
writeln!(&mut stream, "{message}")?;
590609
thread::sleep(Duration::from_secs(1));

xtask/src/ci/rs.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ impl Rs {
4141
match self.action {
4242
Action::Build => Ok(()),
4343
Action::Firecracker(firecracker) => firecracker.run(&image, self.smp),
44-
Action::Qemu(qemu) => qemu.run(&image, self.smp, arch, small),
44+
Action::Qemu(qemu) => {
45+
qemu.run(&image, &self.cargo_build.features, self.smp, arch, small)
46+
}
4547
Action::Uhyve(uhyve) => uhyve.run(&image, self.smp),
4648
}
4749
}

0 commit comments

Comments
 (0)