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
// Copyright 2023 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::path::Path;
use std::path::PathBuf;

use serde::Deserialize;
use serde::Serialize;
use serde_keyvalue::FromKeyValues;

static VHOST_VSOCK_DEFAULT_PATH: &str = "/dev/vhost-vsock";

fn default_vsock_path() -> PathBuf {
    PathBuf::from(VHOST_VSOCK_DEFAULT_PATH)
}

#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, FromKeyValues)]
#[serde(deny_unknown_fields)]

/// Configuration for a Vsock device.
pub struct VsockConfig {
    /// CID to be used for this vsock device.
    pub cid: u64,
    /// Path to the vhost-vsock device.
    #[serde(default = "default_vsock_path", rename = "device")]
    pub vhost_device: PathBuf,
}

impl VsockConfig {
    /// Create a new vsock configuration. If `vhost_device` is `None`, the default vhost-vsock
    /// device path will be used.
    pub fn new<P: AsRef<Path>>(cid: u64, vhost_device: Option<P>) -> Self {
        Self {
            cid,
            #[cfg(any(target_os = "android", target_os = "linux"))]
            vhost_device: vhost_device
                .map(|p| PathBuf::from(p.as_ref()))
                .unwrap_or_else(|| PathBuf::from(VHOST_VSOCK_DEFAULT_PATH)),
        }
    }
}

#[cfg(test)]
mod tests {
    use serde_keyvalue::from_key_values;
    use serde_keyvalue::ErrorKind;
    use serde_keyvalue::ParseError;

    use super::*;

    fn from_vsock_arg(options: &str) -> Result<VsockConfig, ParseError> {
        from_key_values(options)
    }

    #[test]
    fn params_from_key_values() {
        // Default device
        assert_eq!(
            from_vsock_arg("cid=56").unwrap(),
            VsockConfig {
                vhost_device: VHOST_VSOCK_DEFAULT_PATH.into(),
                cid: 56,
            }
        );

        // No argument
        assert_eq!(
            from_vsock_arg("").unwrap_err(),
            ParseError {
                kind: ErrorKind::SerdeError("missing field `cid`".into()),
                pos: 0
            }
        );

        // CID passed without key
        assert_eq!(
            from_vsock_arg("78").unwrap(),
            VsockConfig {
                #[cfg(any(target_os = "android", target_os = "linux"))]
                vhost_device: VHOST_VSOCK_DEFAULT_PATH.into(),
                cid: 78,
            }
        );

        // CID passed twice
        assert_eq!(
            from_vsock_arg("cid=42,cid=56").unwrap_err(),
            ParseError {
                kind: ErrorKind::SerdeError("duplicate field `cid`".into()),
                pos: 0,
            }
        );

        // Invalid argument
        assert_eq!(
            from_vsock_arg("invalid=foo").unwrap_err(),
            ParseError {
                kind: ErrorKind::SerdeError(
                    "unknown field `invalid`, expected `cid` or `device`".into()
                ),
                pos: 0,
            }
        );

        // Path device
        assert_eq!(
            from_vsock_arg("device=/some/path,cid=56").unwrap(),
            VsockConfig {
                vhost_device: "/some/path".into(),
                cid: 56,
            }
        );

        // CID passed without key
        assert_eq!(
            from_vsock_arg("56,device=/some/path").unwrap(),
            VsockConfig {
                vhost_device: "/some/path".into(),
                cid: 56,
            }
        );

        // Missing cid
        assert_eq!(
            from_vsock_arg("device=42").unwrap_err(),
            ParseError {
                kind: ErrorKind::SerdeError("missing field `cid`".into()),
                pos: 0,
            }
        );

        // Device passed twice
        assert_eq!(
            from_vsock_arg("cid=56,device=42,device=/some/path").unwrap_err(),
            ParseError {
                kind: ErrorKind::SerdeError("duplicate field `device`".into()),
                pos: 0,
            }
        );
    }
}