xref: /cloud-hypervisor/vm-virtio/src/lib.rs (revision 9af2968a7dc47b89bf07ea9dc5e735084efcfa3a)
1 // Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 //
3 // Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE-BSD-3-Clause file.
6 //
7 // Copyright © 2019 Intel Corporation
8 //
9 // SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
10 
11 //! Implements virtio queues
12 
13 #[macro_use]
14 extern crate log;
15 
16 use std::fmt;
17 
18 pub mod queue;
19 pub use queue::*;
20 
21 pub type VirtioIommuRemapping =
22     Box<dyn Fn(u64) -> std::result::Result<u64, std::io::Error> + Send + Sync>;
23 
24 pub const VIRTIO_MSI_NO_VECTOR: u16 = 0xffff;
25 
26 // Types taken from linux/virtio_ids.h
27 #[derive(Copy, Clone, Debug)]
28 #[allow(dead_code)]
29 #[allow(non_camel_case_types)]
30 #[repr(C)]
31 pub enum VirtioDeviceType {
32     Net = 1,
33     Block = 2,
34     Console = 3,
35     Rng = 4,
36     Balloon = 5,
37     Fs9P = 9,
38     Gpu = 16,
39     Input = 18,
40     Vsock = 19,
41     Iommu = 23,
42     Mem = 24,
43     Fs = 26,
44     Pmem = 27,
45     Watchdog = 35, // Temporary until official number allocated
46     Unknown = 0xFF,
47 }
48 
49 impl From<u32> for VirtioDeviceType {
50     fn from(t: u32) -> Self {
51         match t {
52             1 => VirtioDeviceType::Net,
53             2 => VirtioDeviceType::Block,
54             3 => VirtioDeviceType::Console,
55             4 => VirtioDeviceType::Rng,
56             5 => VirtioDeviceType::Balloon,
57             9 => VirtioDeviceType::Fs9P,
58             16 => VirtioDeviceType::Gpu,
59             18 => VirtioDeviceType::Input,
60             19 => VirtioDeviceType::Vsock,
61             23 => VirtioDeviceType::Iommu,
62             24 => VirtioDeviceType::Mem,
63             26 => VirtioDeviceType::Fs,
64             27 => VirtioDeviceType::Pmem,
65             35 => VirtioDeviceType::Watchdog,
66             _ => VirtioDeviceType::Unknown,
67         }
68     }
69 }
70 
71 // In order to use the `{}` marker, the trait `fmt::Display` must be implemented
72 // manually for the type VirtioDeviceType.
73 impl fmt::Display for VirtioDeviceType {
74     // This trait requires `fmt` with this exact signature.
75     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
76         let output = match *self {
77             VirtioDeviceType::Net => "net",
78             VirtioDeviceType::Block => "block",
79             VirtioDeviceType::Console => "console",
80             VirtioDeviceType::Rng => "rng",
81             VirtioDeviceType::Balloon => "balloon",
82             VirtioDeviceType::Gpu => "gpu",
83             VirtioDeviceType::Fs9P => "9p",
84             VirtioDeviceType::Input => "input",
85             VirtioDeviceType::Vsock => "vsock",
86             VirtioDeviceType::Iommu => "iommu",
87             VirtioDeviceType::Mem => "mem",
88             VirtioDeviceType::Fs => "fs",
89             VirtioDeviceType::Pmem => "pmem",
90             VirtioDeviceType::Watchdog => "watchdog",
91             VirtioDeviceType::Unknown => "UNKNOWN",
92         };
93         write!(f, "{}", output)
94     }
95 }
96