summaryrefslogtreecommitdiffstats
path: root/meta-egvirt/recipes-extended/vhost-device-console/vhost-device-console-0.1.0/src/main.rs
blob: 216a01e64d95b7283f9642516e84dabed8d179e5 (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
// 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

mod backend;
mod console;
mod vhu_console;
mod virtio_console;
use crate::console::BackendType;
use clap::Parser;
use log::error;
use std::path::PathBuf;
use std::process::exit;

pub(crate) type Result<T> = std::result::Result<T, Error>;
use crate::backend::{start_backend, Error, VuConsoleConfig};

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct ConsoleArgs {
    /// Location of vhost-user Unix domain socket. This is suffixed by 0,1,2..socket_count-1.
    #[clap(short = 's', long, value_name = "SOCKET")]
    socket_path: PathBuf,

    /// Number of guests (sockets) to connect to.
    #[clap(short = 'c', long, default_value_t = 1)]
    socket_count: u32,

    /// Console backend (Network, Nested) to be used.
    #[clap(short = 'b', long, value_enum, default_value = "nested")]
    backend: BackendType,

    /// Initial tcp port to be used with "network" backend. If socket_count is N then
    /// the following tcp ports will be created: tcp_port, tcp_port + 1, ..., tcp_port + (N - 1).
    #[clap(short = 'p', long, value_name = "PORT", default_value = "12345")]
    tcp_port: String,
}

impl TryFrom<ConsoleArgs> for VuConsoleConfig {
    type Error = Error;

    fn try_from(args: ConsoleArgs) -> Result<Self> {
        if args.socket_count == 0 {
            return Err(Error::SocketCountInvalid(0));
        }

        if (args.backend == BackendType::Nested) && (args.socket_count != 1) {
            return Err(Error::WrongBackendSocket);
        }

        Ok(VuConsoleConfig {
            socket_path: args.socket_path,
            backend: args.backend,
            tcp_port: args.tcp_port,
            socket_count: args.socket_count,
        })
    }
}

fn main() {
    env_logger::init();
    if let Err(e) = VuConsoleConfig::try_from(ConsoleArgs::parse()).and_then(start_backend) {
        error!("{e}");
        exit(1);
    }
}