Skip to content

Commit eae96e0

Browse files
author
Hang SU
committed
console: check parameter mutual exclusivity
- Introduce `InvalidCmdlineOption` error type to handle conflicting parameters - Implement backend-specific parameter checks: * Nested backend: - Require single socket (socket_count=1) - Disallow TCP port/UDS path * Network backend: Disallow UDS path * UDS backend: Disallow TCP port - Remove default TCP port value to enforce explicit configuration Signed-off-by: Hang SU <[email protected]>
1 parent d910fe6 commit eae96e0

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

vhost-device-console/src/backend.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ pub enum Error {
4242
ThreadPanic(String, Box<dyn Any + Send>),
4343
#[error("Error using multiple sockets with Nested backend")]
4444
WrongBackendSocket,
45+
#[error("Invalid cmdline option")]
46+
InvalidCmdlineOption,
4547
#[error("Invalid uds file")]
4648
InvalidUdsFile,
4749
}
@@ -217,7 +219,7 @@ mod tests {
217219
socket_path: String::from("/tmp/vhost.sock").into(),
218220
uds_path: None,
219221
backend: BackendType::Nested,
220-
tcp_port: String::from("12345"),
222+
tcp_port: None,
221223
socket_count: 1,
222224
max_queue_size: DEFAULT_QUEUE_SIZE,
223225
};
@@ -231,7 +233,7 @@ mod tests {
231233
socket_path: String::from("/tmp/vhost.sock").into(),
232234
uds_path: None,
233235
backend: BackendType::Nested,
234-
tcp_port: String::from("12345"),
236+
tcp_port: None,
235237
socket_count: 0,
236238
max_queue_size: DEFAULT_QUEUE_SIZE,
237239
};
@@ -248,7 +250,7 @@ mod tests {
248250
socket_path: String::from("/tmp/vhost.sock").into(),
249251
uds_path: None,
250252
backend: BackendType::Nested,
251-
tcp_port: String::from("12345"),
253+
tcp_port: None,
252254
socket_count: 2,
253255
max_queue_size: DEFAULT_QUEUE_SIZE,
254256
};
@@ -265,7 +267,7 @@ mod tests {
265267
socket_path: String::from("/tmp/vhost.sock").into(),
266268
uds_path: None,
267269
backend: BackendType::Network,
268-
tcp_port: String::from("12345"),
270+
tcp_port: Some(String::from("12345")),
269271
socket_count: 1,
270272
max_queue_size: DEFAULT_QUEUE_SIZE,
271273
};
@@ -279,7 +281,7 @@ mod tests {
279281
socket_path: String::from("/tmp/vhost.sock").into(),
280282
uds_path: None,
281283
backend: BackendType::Network,
282-
tcp_port: String::from("12345"),
284+
tcp_port: Some(String::from("12345")),
283285
socket_count: 2,
284286
max_queue_size: DEFAULT_QUEUE_SIZE,
285287
};
@@ -310,7 +312,7 @@ mod tests {
310312
socket_path: String::from("/not_a_dir/vhost.sock").into(),
311313
uds_path: None,
312314
backend: BackendType::Network,
313-
tcp_port: String::from("12345"),
315+
tcp_port: Some(String::from("12345")),
314316
socket_count: 1,
315317
max_queue_size: DEFAULT_QUEUE_SIZE,
316318
};
@@ -338,7 +340,7 @@ mod tests {
338340
socket_path: PathBuf::from("/tmp/vhost.sock"),
339341
uds_path: Some("/non_existing_dir/test.sock".to_string().into()),
340342
backend: BackendType::Uds,
341-
tcp_port: String::new(),
343+
tcp_port: Some(String::new()),
342344
socket_count: 1,
343345
max_queue_size: 128,
344346
};

vhost-device-console/src/main.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ struct ConsoleArgs {
7070
/// Initial tcp port to be used with `network` backend. If socket_count is
7171
/// `N` then the following tcp ports will be created: `tcp_port`,
7272
/// `tcp_port + 1`, ... , `tcp_port + (N - 1)`.
73-
#[clap(short = 'p', long, value_name = "PORT", default_value = "12345")]
74-
tcp_port: String,
73+
#[clap(short = 'p', long, value_name = "PORT")]
74+
tcp_port: Option<String>,
7575

7676
/// Specify the maximum size of virtqueue, the default is 128.
7777
#[clap(short = 'q', long, default_value_t = DEFAULT_QUEUE_SIZE)]
@@ -86,8 +86,34 @@ impl TryFrom<ConsoleArgs> for VuConsoleConfig {
8686
return Err(Error::SocketCountInvalid(0));
8787
}
8888

89-
if (args.backend == BackendType::Nested) && (args.socket_count != 1) {
90-
return Err(Error::WrongBackendSocket);
89+
if args.backend == BackendType::Nested {
90+
if args.socket_count != 1 {
91+
return Err(Error::WrongBackendSocket);
92+
}
93+
94+
if (args.tcp_port.as_ref().map_or(false, |s| !s.is_empty()))
95+
|| (args
96+
.uds_path
97+
.as_ref()
98+
.map_or(false, |path| !path.as_os_str().is_empty()))
99+
{
100+
return Err(Error::InvalidCmdlineOption);
101+
}
102+
}
103+
104+
if args.backend == BackendType::Network
105+
&& args
106+
.uds_path
107+
.as_ref()
108+
.map_or(false, |path| !path.as_os_str().is_empty())
109+
{
110+
return Err(Error::InvalidCmdlineOption);
111+
}
112+
113+
if args.backend == BackendType::Uds
114+
&& args.tcp_port.as_ref().map_or(false, |s| !s.is_empty())
115+
{
116+
return Err(Error::InvalidCmdlineOption);
91117
}
92118

93119
let ConsoleArgs {
@@ -117,7 +143,7 @@ impl TryFrom<ConsoleArgs> for VuConsoleConfig {
117143
socket_path,
118144
uds_path: uds_path.unwrap_or_default(),
119145
backend,
120-
tcp_port,
146+
tcp_port: tcp_port.unwrap_or_default(),
121147
socket_count,
122148
max_queue_size,
123149
})

0 commit comments

Comments
 (0)