@@ -42,11 +42,14 @@ pub enum Error {
42
42
ThreadPanic ( String , Box < dyn Any + Send > ) ,
43
43
#[ error( "Error using multiple sockets with Nested backend" ) ]
44
44
WrongBackendSocket ,
45
+ #[ error( "Invalid uds file" ) ]
46
+ InvalidUdsFile ,
45
47
}
46
48
47
49
#[ derive( PartialEq , Eq , Debug ) ]
48
50
pub struct VuConsoleConfig {
49
51
pub socket_path : PathBuf ,
52
+ pub vm_sock : String ,
50
53
pub backend : BackendType ,
51
54
pub tcp_port : String ,
52
55
pub socket_count : u32 ,
@@ -73,23 +76,33 @@ impl VuConsoleConfig {
73
76
( 0 ..self . socket_count ) . map ( make_socket_path) . collect ( )
74
77
}
75
78
76
- pub fn generate_tcp_addrs ( & self ) -> Vec < String > {
77
- let tcp_port_base = self . tcp_port . clone ( ) ;
78
-
79
- let make_tcp_port = |i : u32 | -> String {
80
- let port_num: u32 = tcp_port_base. clone ( ) . parse ( ) . unwrap ( ) ;
81
- "127.0.0.1:" . to_owned ( ) + & ( port_num + i) . to_string ( )
82
- } ;
83
-
84
- ( 0 ..self . socket_count ) . map ( make_tcp_port) . collect ( )
79
+ pub fn generate_vm_sock_addrs ( & self ) -> Vec < String > {
80
+ match self . backend {
81
+ // if type is Nested, result will be dropped.
82
+ BackendType :: Nested => {
83
+ vec ! [ String :: new( ) ]
84
+ }
85
+
86
+ BackendType :: Network => {
87
+ let port_base: u32 = self . tcp_port . parse ( ) . unwrap ( ) ;
88
+ let make_tcp_port =
89
+ |i : u32 | -> String { "127.0.0.1:" . to_owned ( ) + & ( port_base + i) . to_string ( ) } ;
90
+ ( 0 ..self . socket_count ) . map ( make_tcp_port) . collect ( )
91
+ }
92
+
93
+ BackendType :: Uds => {
94
+ let make_uds_path = |i : u32 | -> String { self . vm_sock . to_owned ( ) + & i. to_string ( ) } ;
95
+ ( 0 ..self . socket_count ) . map ( make_uds_path) . collect ( )
96
+ }
97
+ }
85
98
}
86
99
}
87
100
88
101
/// This is the public API through which an external program starts the
89
102
/// vhost-device-console backend server.
90
103
pub fn start_backend_server (
91
104
socket : PathBuf ,
92
- tcp_addr : String ,
105
+ vm_sock : String ,
93
106
backend : BackendType ,
94
107
max_queue_size : usize ,
95
108
) -> Result < ( ) > {
@@ -104,7 +117,7 @@ pub fn start_backend_server(
104
117
vu_console_backend
105
118
. write ( )
106
119
. unwrap ( )
107
- . assign_input_method ( tcp_addr . clone ( ) )
120
+ . assign_input_method ( vm_sock . clone ( ) )
108
121
. map_err ( Error :: CouldNotInitBackend ) ?;
109
122
110
123
let mut daemon = VhostUserDaemon :: new (
@@ -132,26 +145,29 @@ pub fn start_backend_server(
132
145
pub fn start_backend ( config : VuConsoleConfig ) -> Result < ( ) > {
133
146
let mut handles = HashMap :: new ( ) ;
134
147
let ( senders, receiver) = std:: sync:: mpsc:: channel ( ) ;
135
- let tcp_addrs = config. generate_tcp_addrs ( ) ;
148
+ let vm_sock = config. generate_vm_sock_addrs ( ) ;
136
149
let backend = config. backend ;
137
150
let max_queue_size = config. max_queue_size ;
138
151
139
- for ( thread_id, ( socket, tcp_addr ) ) in config
152
+ for ( thread_id, ( socket, vm_sock ) ) in config
140
153
. generate_socket_paths ( )
141
154
. into_iter ( )
142
- . zip ( tcp_addrs . iter ( ) )
155
+ . zip ( vm_sock . iter ( ) )
143
156
. enumerate ( )
144
157
{
145
- let tcp_addr = tcp_addr. clone ( ) ;
146
- info ! ( "thread_id: {}, socket: {:?}" , thread_id, socket) ;
158
+ let vm_sock = vm_sock. clone ( ) ;
159
+ info ! (
160
+ "thread_id: {}, socket: {:?} mode: {:?} vm_sock: {}" ,
161
+ thread_id, socket, backend, vm_sock
162
+ ) ;
147
163
148
- let name = format ! ( "vhu-console-{}" , tcp_addr ) ;
164
+ let name = format ! ( "vhu-console-{}" , vm_sock ) ;
149
165
let sender = senders. clone ( ) ;
150
166
let handle = Builder :: new ( )
151
167
. name ( name. clone ( ) )
152
168
. spawn ( move || {
153
169
let result = std:: panic:: catch_unwind ( move || {
154
- start_backend_server ( socket, tcp_addr . to_string ( ) , backend, max_queue_size)
170
+ start_backend_server ( socket, vm_sock . to_string ( ) , backend, max_queue_size)
155
171
} ) ;
156
172
157
173
// Notify the main thread that we are done.
@@ -257,16 +273,16 @@ mod tests {
257
273
fn test_backend_start_and_stop ( args : ConsoleArgs ) -> Result < ( ) > {
258
274
let config = VuConsoleConfig :: try_from ( args) . expect ( "Wrong config" ) ;
259
275
260
- let tcp_addrs = config. generate_tcp_addrs ( ) ;
276
+ let vm_sock = config. generate_vm_sock_addrs ( ) ;
261
277
let backend = config. backend ;
262
278
let max_queue_size = config. max_queue_size ;
263
279
264
- for ( socket, tcp_addr ) in config
280
+ for ( socket, vm_sock ) in config
265
281
. generate_socket_paths ( )
266
282
. into_iter ( )
267
- . zip ( tcp_addrs . iter ( ) )
283
+ . zip ( vm_sock . iter ( ) )
268
284
{
269
- start_backend_server ( socket, tcp_addr . to_string ( ) , backend, max_queue_size) ?;
285
+ start_backend_server ( socket, vm_sock . to_string ( ) , backend, max_queue_size) ?;
270
286
}
271
287
Ok ( ( ) )
272
288
}
0 commit comments