Skip to content

Commit 72ae395

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 <darcy.sh@antgroup.com>
1 parent d910fe6 commit 72ae395

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

vhost-device-console/src/backend.rs

Lines changed: 5 additions & 3 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: String::new(),
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: String::new(),
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: String::new(),
252254
socket_count: 2,
253255
max_queue_size: DEFAULT_QUEUE_SIZE,
254256
};

vhost-device-console/src/main.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ 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")]
73+
#[clap(short = 'p', long, value_name = "PORT")]
7474
tcp_port: String,
7575

7676
/// Specify the maximum size of virtqueue, the default is 128.
@@ -86,8 +86,22 @@ 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.is_empty()) || (args.uds_path.is_some()) {
95+
return Err(Error::InvalidCmdlineOption);
96+
}
97+
}
98+
99+
if args.backend == BackendType::Network && args.uds_path.is_some() {
100+
return Err(Error::InvalidCmdlineOption);
101+
}
102+
103+
if args.backend == BackendType::Uds && !args.tcp_port.is_empty() {
104+
return Err(Error::InvalidCmdlineOption);
91105
}
92106

93107
let ConsoleArgs {

0 commit comments

Comments
 (0)