xref: /cloud-hypervisor/virtio-devices/src/lib.rs (revision f67b3f79ea19c9a66e04074cbbf5d292f6529e43)
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 devices, queues, and transport mechanisms.
12 
13 #[macro_use]
14 extern crate event_monitor;
15 #[macro_use]
16 extern crate log;
17 #[macro_use]
18 extern crate serde_derive;
19 
20 use std::convert::TryInto;
21 use std::io;
22 
23 #[macro_use]
24 mod device;
25 pub mod balloon;
26 pub mod block;
27 mod console;
28 pub mod epoll_helper;
29 mod iommu;
30 pub mod mem;
31 pub mod net;
32 mod pmem;
33 mod rng;
34 pub mod seccomp_filters;
35 mod thread_helper;
36 pub mod transport;
37 pub mod vhost_user;
38 pub mod vsock;
39 pub mod watchdog;
40 
41 pub use self::balloon::*;
42 pub use self::block::*;
43 pub use self::console::*;
44 pub use self::device::*;
45 pub use self::epoll_helper::*;
46 pub use self::iommu::*;
47 pub use self::mem::*;
48 pub use self::net::*;
49 pub use self::pmem::*;
50 pub use self::rng::*;
51 pub use self::vsock::*;
52 pub use self::watchdog::*;
53 use vm_memory::{bitmap::AtomicBitmap, GuestAddress, GuestMemory};
54 use vm_virtio::{queue::*, VirtioDeviceType};
55 
56 type GuestMemoryMmap = vm_memory::GuestMemoryMmap<AtomicBitmap>;
57 type GuestRegionMmap = vm_memory::GuestRegionMmap<AtomicBitmap>;
58 type MmapRegion = vm_memory::MmapRegion<AtomicBitmap>;
59 
60 const DEVICE_INIT: u32 = 0x00;
61 const DEVICE_ACKNOWLEDGE: u32 = 0x01;
62 const DEVICE_DRIVER: u32 = 0x02;
63 const DEVICE_DRIVER_OK: u32 = 0x04;
64 const DEVICE_FEATURES_OK: u32 = 0x08;
65 const DEVICE_FAILED: u32 = 0x80;
66 
67 const VIRTIO_F_RING_INDIRECT_DESC: u32 = 28;
68 const VIRTIO_F_RING_EVENT_IDX: u32 = 29;
69 const VIRTIO_F_VERSION_1: u32 = 32;
70 const VIRTIO_F_IOMMU_PLATFORM: u32 = 33;
71 const VIRTIO_F_IN_ORDER: u32 = 35;
72 const VIRTIO_F_ORDER_PLATFORM: u32 = 36;
73 #[allow(dead_code)]
74 const VIRTIO_F_SR_IOV: u32 = 37;
75 const VIRTIO_F_NOTIFICATION_DATA: u32 = 38;
76 
77 #[derive(Debug)]
78 pub enum ActivateError {
79     EpollCtl(std::io::Error),
80     BadActivate,
81     /// Queue number is not correct
82     BadQueueNum,
83     /// Failed to clone Kill event fd
84     CloneKillEventFd,
85     /// Failed to clone exit event fd
86     CloneExitEventFd(std::io::Error),
87     // Failed to spawn thread
88     ThreadSpawn(std::io::Error),
89     /// Failed to create Vhost-user interrupt eventfd
90     VhostIrqCreate,
91     /// Failed to setup vhost-user-fs daemon.
92     VhostUserFsSetup(vhost_user::Error),
93     /// Failed to setup vhost-user-net daemon.
94     VhostUserNetSetup(vhost_user::Error),
95     /// Failed to setup vhost-user-blk daemon.
96     VhostUserBlkSetup(vhost_user::Error),
97     /// Failed to reset vhost-user daemon.
98     VhostUserReset(vhost_user::Error),
99     /// Cannot create seccomp filter
100     CreateSeccompFilter(seccompiler::Error),
101     /// Cannot create rate limiter
102     CreateRateLimiter(std::io::Error),
103 }
104 
105 pub type ActivateResult = std::result::Result<(), ActivateError>;
106 
107 pub type DeviceEventT = u16;
108 
109 #[derive(Debug)]
110 pub enum Error {
111     FailedSignalingUsedQueue(io::Error),
112     IoError(io::Error),
113     VhostUserUpdateMemory(vhost_user::Error),
114     VhostUserAddMemoryRegion(vhost_user::Error),
115     SetShmRegionsNotSupported,
116     NetQueuePair(::net_util::NetQueuePairError),
117     ApplySeccompFilter(seccompiler::Error),
118 }
119 
120 #[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq)]
121 pub struct TokenBucketConfig {
122     pub size: u64,
123     pub one_time_burst: Option<u64>,
124     pub refill_time: u64,
125 }
126 
127 #[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq)]
128 #[serde(deny_unknown_fields)]
129 pub struct RateLimiterConfig {
130     pub bandwidth: Option<TokenBucketConfig>,
131     pub ops: Option<TokenBucketConfig>,
132 }
133 
134 impl TryInto<rate_limiter::RateLimiter> for RateLimiterConfig {
135     type Error = io::Error;
136 
137     fn try_into(self) -> std::result::Result<rate_limiter::RateLimiter, Self::Error> {
138         let bw = self.bandwidth.unwrap_or_default();
139         let ops = self.ops.unwrap_or_default();
140         rate_limiter::RateLimiter::new(
141             bw.size,
142             bw.one_time_burst.unwrap_or(0),
143             bw.refill_time,
144             ops.size,
145             ops.one_time_burst.unwrap_or(0),
146             ops.refill_time,
147         )
148     }
149 }
150 
151 /// Convert an absolute address into an address space (GuestMemory)
152 /// to a host pointer and verify that the provided size define a valid
153 /// range within a single memory region.
154 /// Return None if it is out of bounds or if addr+size overlaps a single region.
155 pub fn get_host_address_range<M: GuestMemory>(
156     mem: &M,
157     addr: GuestAddress,
158     size: usize,
159 ) -> Option<*mut u8> {
160     if mem.check_range(addr, size) {
161         Some(mem.get_host_address(addr).unwrap())
162     } else {
163         None
164     }
165 }
166