summaryrefslogtreecommitdiffstats
path: root/meta-egvirt/recipes-extended/vhost-device-console/vhost-device-console-0.1.0/src/backend.rs
blob: 62d6745cdcd0b8c45a0a53e3858ac7c0e5b926a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// VIRTIO CONSOLE Emulation via vhost-user
//
// Copyright 2023-2024 VIRTUAL OPEN SYSTEMS SAS. All Rights Reserved.
//          Timos Ampelikiotis <t.ampelikiotis@virtualopensystems.com>
//
// SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause

use log::{error, info, warn};
use std::any::Any;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};
use std::thread::Builder;

use thiserror::Error as ThisError;
use vhost_user_backend::VhostUserDaemon;
use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap};

use crate::console::{BackendType, ConsoleController};
use crate::vhu_console::VhostUserConsoleBackend;

pub(crate) type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, ThisError)]
/// Errors related to low level Console helpers
pub(crate) enum Error {
    #[error("Invalid socket count: {0}")]
    SocketCountInvalid(usize),
    #[error("Could not create console backend: {0}")]
    CouldNotCreateBackend(crate::vhu_console::Error),
    #[error("Could not create daemon: {0}")]
    CouldNotCreateDaemon(vhost_user_backend::Error),
    #[error("Fatal error: {0}")]
    ServeFailed(vhost_user_backend::Error),
    #[error("Thread `{0}` panicked")]
    ThreadPanic(String, Box<dyn Any + Send>),
    #[error("Error using multiple sockets with Nested backend")]
    WrongBackendSocket,
}

#[derive(PartialEq, Debug)]
pub struct VuConsoleConfig {
    pub(crate) socket_path: PathBuf,
    pub(crate) backend: BackendType,
    pub(crate) tcp_port: String,
    pub(crate) socket_count: u32,
}

impl VuConsoleConfig {
    pub fn generate_socket_paths(&self) -> Vec<PathBuf> {
        let socket_file_name = self
            .socket_path
            .file_name()
            .expect("socket_path has no filename.");
        let socket_file_parent = self
            .socket_path
            .parent()
            .expect("socket_path has no parent directory.");

        let make_socket_path = |i: u32| -> PathBuf {
            let mut file_name = socket_file_name.to_os_string();
            file_name.push(std::ffi::OsStr::new(&i.to_string()));
            socket_file_parent.join(&file_name)
        };

        (0..self.socket_count).map(make_socket_path).collect()
    }

    pub fn generate_tcp_addrs(&self) -> Vec<String> {
        let tcp_port_base = self.tcp_port.clone();

        let make_tcp_port = |i: u32| -> String {
            let port_num: u32 = tcp_port_base.clone().parse().unwrap();
            "127.0.0.1:".to_owned() + &(port_num + i).to_string()
        };

        (0..self.socket_count).map(make_tcp_port).collect()
    }
}

// This is the public API through which an external program starts the
/// vhost-device-console backend server.
pub(crate) fn start_backend_server(
    socket: PathBuf,
    tcp_addr: String,
    backend: BackendType,
) -> Result<()> {
    loop {
        let controller = ConsoleController::new(backend);
        let arc_controller = Arc::new(RwLock::new(controller));
        let vu_console_backend = Arc::new(RwLock::new(
            VhostUserConsoleBackend::new(arc_controller).map_err(Error::CouldNotCreateBackend)?,
        ));

        let mut daemon = VhostUserDaemon::new(
            String::from("vhost-device-console-backend"),
            vu_console_backend.clone(),
            GuestMemoryAtomic::new(GuestMemoryMmap::new()),
        )
        .map_err(Error::CouldNotCreateDaemon)?;

        let vring_workers = daemon.get_epoll_handlers();
        vu_console_backend
            .read()
            .unwrap()
            .set_vring_worker(&vring_workers[0]);

        // Start the corresponding console thread
        let read_handle = if backend == BackendType::Nested {
            VhostUserConsoleBackend::start_console_thread(&vu_console_backend)
        } else {
            VhostUserConsoleBackend::start_tcp_console_thread(&vu_console_backend, tcp_addr.clone())
        };

        daemon.serve(&socket).map_err(Error::ServeFailed)?;

        // Kill console input thread
        vu_console_backend.read().unwrap().kill_console_thread();

        // Wait for read thread to exit
        match read_handle.join() {
            Ok(_) => info!("The read thread returned successfully"),
            Err(e) => warn!("The read thread returned the error: {:?}", e),
        }
    }
}

pub fn start_backend(config: VuConsoleConfig) -> Result<()> {
    let mut handles = HashMap::new();
    let (senders, receiver) = std::sync::mpsc::channel();
    let tcp_addrs = config.generate_tcp_addrs();
    let backend = config.backend;

    for (thread_id, (socket, tcp_addr)) in config
        .generate_socket_paths()
        .into_iter()
        .zip(tcp_addrs.iter())
        .enumerate()
    {
        let tcp_addr = tcp_addr.clone();
        info!("thread_id: {}, socket: {:?}", thread_id, socket);

        let name = format!("vhu-console-{}", tcp_addr);
        let sender = senders.clone();
        let handle = Builder::new()
            .name(name.clone())
            .spawn(move || {
                let result = std::panic::catch_unwind(move || {
                    start_backend_server(socket, tcp_addr.to_string(), backend)
                });

                // Notify the main thread that we are done.
                sender.send(thread_id).unwrap();

                result.map_err(|e| Error::ThreadPanic(name, e))?
            })
            .unwrap();
        handles.insert(thread_id, handle);
    }

    while !handles.is_empty() {
        let thread_id = receiver.recv().unwrap();
        handles
            .remove(&thread_id)
            .unwrap()
            .join()
            .map_err(std::panic::resume_unwind)
            .unwrap()?;
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ConsoleArgs;
    use assert_matches::assert_matches;

    #[test]
    fn test_console_valid_configuration_nested() {
        let args = ConsoleArgs {
            socket_path: String::from("/tmp/vhost.sock").into(),
            backend: BackendType::Nested,
            tcp_port: String::from("12345"),
            socket_count: 1,
        };

        assert!(VuConsoleConfig::try_from(args).is_ok());
    }

    #[test]
    fn test_console_invalid_configuration_nested_1() {
        let args = ConsoleArgs {
            socket_path: String::from("/tmp/vhost.sock").into(),
            backend: BackendType::Nested,
            tcp_port: String::from("12345"),
            socket_count: 0,
        };

        assert_matches!(
            VuConsoleConfig::try_from(args),
            Err(Error::SocketCountInvalid(0))
        );
    }

    #[test]
    fn test_console_invalid_configuration_nested_2() {
        let args = ConsoleArgs {
            socket_path: String::from("/tmp/vhost.sock").into(),
            backend: BackendType::Nested,
            tcp_port: String::from("12345"),
            socket_count: 2,
        };

        assert_matches!(
            VuConsoleConfig::try_from(args),
            Err(Error::WrongBackendSocket)
        );
    }

    #[test]
    fn test_console_valid_configuration_network_1() {
        let args = ConsoleArgs {
            socket_path: String::from("/tmp/vhost.sock").into(),
            backend: BackendType::Network,
            tcp_port: String::from("12345"),
            socket_count: 1,
        };

        assert!(VuConsoleConfig::try_from(args).is_ok());
    }

    #[test]
    fn test_console_valid_configuration_network_2() {
        let args = ConsoleArgs {
            socket_path: String::from("/tmp/vhost.sock").into(),
            backend: BackendType::Network,
            tcp_port: String::from("12345"),
            socket_count: 2,
        };

        assert!(VuConsoleConfig::try_from(args).is_ok());
    }

    fn test_backend_start_and_stop(args: ConsoleArgs) {
        let config = VuConsoleConfig::try_from(args).expect("Wrong config");

        let tcp_addrs = config.generate_tcp_addrs();
        let backend = config.backend;

        for (_socket, tcp_addr) in config
            .generate_socket_paths()
            .into_iter()
            .zip(tcp_addrs.iter())
        {
            let controller = ConsoleController::new(backend);
            let arc_controller = Arc::new(RwLock::new(controller));
            let vu_console_backend = Arc::new(RwLock::new(
                VhostUserConsoleBackend::new(arc_controller)
                    .map_err(Error::CouldNotCreateBackend)
                    .expect("Fail create vhuconsole backend"),
            ));

            let mut _daemon = VhostUserDaemon::new(
                String::from("vhost-device-console-backend"),
                vu_console_backend.clone(),
                GuestMemoryAtomic::new(GuestMemoryMmap::new()),
            )
            .map_err(Error::CouldNotCreateDaemon)
            .expect("Failed create daemon");

            // Start the corresponinding console thread
            let read_handle = if backend == BackendType::Nested {
                VhostUserConsoleBackend::start_console_thread(&vu_console_backend)
            } else {
                VhostUserConsoleBackend::start_tcp_console_thread(
                    &vu_console_backend,
                    tcp_addr.clone(),
                )
            };

            // Kill console input thread
            vu_console_backend.read().unwrap().kill_console_thread();

            // Wait for read thread to exit
            assert_matches!(read_handle.join(), Ok(_));
        }
    }
    #[test]
    fn test_start_net_backend_success() {
        let args = ConsoleArgs {
            socket_path: String::from("/tmp/vhost.sock").into(),
            backend: BackendType::Network,
            tcp_port: String::from("12345"),
            socket_count: 1,
        };

        test_backend_start_and_stop(args);
    }

    #[test]
    fn test_start_nested_backend_success() {
        let args = ConsoleArgs {
            socket_path: String::from("/tmp/vhost.sock").into(),
            backend: BackendType::Nested,
            tcp_port: String::from("12345"),
            socket_count: 1,
        };

        test_backend_start_and_stop(args);
    }
}