@@ -9,7 +9,7 @@ use std::{env, fs, io, thread};
99use anyhow:: { Context , Result , bail, ensure} ;
1010use clap:: { Args , ValueEnum } ;
1111use sysinfo:: { CpuRefreshKind , System } ;
12- use vsock:: VsockStream ;
12+ use vsock:: { VsockListener , VsockStream } ;
1313use wait_timeout:: ChildExt as _;
1414use xshell:: cmd;
1515
@@ -89,7 +89,14 @@ pub enum Device {
8989}
9090
9191impl 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 ) ) ;
0 commit comments