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