Skip to content

Commit 4b7a886

Browse files
committed
console: Flatten PortDescription enum into struct
Make PortDescription a struct instead of an enum with variants. This will allow for more flexibility when describing behavior of console ports. Signed-off-by: Matej Hrica <[email protected]>
1 parent 357ec63 commit 4b7a886

File tree

3 files changed

+59
-63
lines changed

3 files changed

+59
-63
lines changed

src/devices/src/virtio/console/device.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,7 @@ pub struct Console {
102102
impl Console {
103103
pub fn new(ports: Vec<PortDescription>) -> super::Result<Console> {
104104
assert!(!ports.is_empty(), "Expected at least 1 port");
105-
assert!(
106-
matches!(ports[0], PortDescription::Console { .. }),
107-
"First port must be a console"
108-
);
105+
assert!(ports[0].represents_console, "First port must be a console");
109106

110107
let num_queues = num_queues(ports.len());
111108
let queues = vec![VirtQueue::new(QUEUE_SIZE); num_queues];

src/devices/src/virtio/console/port.rs

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,49 @@ use crate::virtio::console::process_rx::process_rx;
1212
use crate::virtio::console::process_tx::process_tx;
1313
use crate::virtio::{InterruptTransport, Queue};
1414

15-
pub enum PortDescription {
16-
Console {
15+
pub struct PortDescription {
16+
pub name: Cow<'static, str>,
17+
pub represents_console: bool,
18+
pub input: Option<Box<dyn PortInput + Send>>,
19+
pub output: Option<Box<dyn PortOutput + Send>>,
20+
}
21+
22+
impl PortDescription {
23+
pub fn console(
1724
input: Option<Box<dyn PortInput + Send>>,
1825
output: Option<Box<dyn PortOutput + Send>>,
19-
},
20-
InputPipe {
21-
name: Cow<'static, str>,
22-
input: Box<dyn PortInput + Send>,
23-
},
24-
OutputPipe {
25-
name: Cow<'static, str>,
26+
) -> Self {
27+
Self {
28+
name: "".into(),
29+
represents_console: true,
30+
input,
31+
output,
32+
}
33+
}
34+
35+
pub fn output_pipe(
36+
name: impl Into<Cow<'static, str>>,
2637
output: Box<dyn PortOutput + Send>,
27-
},
38+
) -> Self {
39+
Self {
40+
name: name.into(),
41+
represents_console: false,
42+
input: None,
43+
output: Some(output),
44+
}
45+
}
46+
47+
pub fn input_pipe(
48+
name: impl Into<Cow<'static, str>>,
49+
input: Box<dyn PortInput + Send>,
50+
) -> Self {
51+
Self {
52+
name: name.into(),
53+
represents_console: false,
54+
input: Some(input),
55+
output: None,
56+
}
57+
}
2858
}
2959

3060
enum PortState {
@@ -49,31 +79,15 @@ pub(crate) struct Port {
4979

5080
impl Port {
5181
pub(crate) fn new(port_id: u32, description: PortDescription) -> Self {
52-
match description {
53-
PortDescription::Console { input, output } => Self {
54-
port_id,
55-
name: "".into(),
56-
represents_console: true,
57-
state: PortState::Inactive,
58-
input: input.map(|input| Arc::new(Mutex::new(input))),
59-
output: output.map(|output| Arc::new(Mutex::new(output))),
60-
},
61-
PortDescription::InputPipe { name, input } => Self {
62-
port_id,
63-
name,
64-
represents_console: false,
65-
state: PortState::Inactive,
66-
input: Some(Arc::new(Mutex::new(input))),
67-
output: None,
68-
},
69-
PortDescription::OutputPipe { name, output } => Self {
70-
port_id,
71-
name,
72-
represents_console: false,
73-
state: PortState::Inactive,
74-
input: None,
75-
output: Some(Arc::new(Mutex::new(output))),
76-
},
82+
Self {
83+
port_id,
84+
name: description.name,
85+
represents_console: description.represents_console,
86+
state: PortState::Inactive,
87+
input: description.input.map(|input| Arc::new(Mutex::new(input))),
88+
output: description
89+
.output
90+
.map(|output| Arc::new(Mutex::new(output))),
7791
}
7892
}
7993

src/vmm/src/builder.rs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,10 +1883,10 @@ fn attach_console_devices(
18831883

18841884
let ports = if console_output_path.is_some() {
18851885
let file = File::create(console_output_path.unwrap()).map_err(OpenConsoleFile)?;
1886-
vec![PortDescription::Console {
1887-
input: Some(port_io::input_empty().unwrap()),
1888-
output: Some(port_io::output_file(file).unwrap()),
1889-
}]
1886+
vec![PortDescription::console(
1887+
Some(port_io::input_empty().unwrap()),
1888+
Some(port_io::output_file(file).unwrap()),
1889+
)]
18901890
} else {
18911891
let (input_fd, output_fd, err_fd) = match cfg {
18921892
Some(c) => (c.input_fd, c.output_fd, c.err_fd),
@@ -1928,10 +1928,7 @@ fn attach_console_devices(
19281928
Some(port_io::output_to_log_as_err())
19291929
};
19301930

1931-
let mut ports = vec![PortDescription::Console {
1932-
input: console_input,
1933-
output: console_output,
1934-
}];
1931+
let mut ports = vec![PortDescription::console(console_input, console_output)];
19351932

19361933
let console_err = if stderr_is_terminal {
19371934
Some(port_io::stderr().unwrap())
@@ -1941,30 +1938,18 @@ fn attach_console_devices(
19411938
None
19421939
};
19431940

1944-
ports.push(PortDescription::Console {
1945-
input: None,
1946-
output: console_err,
1947-
});
1941+
ports.push(PortDescription::console(None, console_err));
19481942

19491943
if !stdin_is_terminal && input_fd == STDIN_FILENO {
1950-
ports.push(PortDescription::InputPipe {
1951-
name: "krun-stdin".into(),
1952-
input: port_io::stdin().unwrap(),
1953-
})
1944+
ports.push(PortDescription::input_pipe("krun-stdin", port_io::stdin().unwrap()));
19541945
}
19551946

19561947
if !stdout_is_terminal && output_fd == STDOUT_FILENO {
1957-
ports.push(PortDescription::OutputPipe {
1958-
name: "krun-stdout".into(),
1959-
output: port_io::stdout().unwrap(),
1960-
})
1948+
ports.push(PortDescription::output_pipe("krun-stdout", port_io::stdout().unwrap()));
19611949
};
19621950

19631951
if !stderr_is_terminal && err_fd == STDERR_FILENO {
1964-
ports.push(PortDescription::OutputPipe {
1965-
name: "krun-stderr".into(),
1966-
output: port_io::stderr().unwrap(),
1967-
});
1952+
ports.push(PortDescription::output_pipe("krun-stderr", port_io::stderr().unwrap()));
19681953
}
19691954

19701955
ports

0 commit comments

Comments
 (0)