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 18 use serde::{Deserialize, Serialize}; 19 use std::convert::TryInto; 20 use std::io; 21 use thiserror::Error; 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(Error, Debug)] 80 pub enum ActivateError { 81 #[error("Failed to activate virtio device")] 82 BadActivate, 83 #[error("Failed to clone exit event fd: {0}")] 84 CloneExitEventFd(std::io::Error), 85 #[error("Failed to spawn thread: {0}")] 86 ThreadSpawn(std::io::Error), 87 #[error("Failed to setup vhost-user-fs daemon: {0}")] 88 VhostUserFsSetup(vhost_user::Error), 89 #[error("Failed to setup vhost-user-blk daemon: {0}")] 90 VhostUserBlkSetup(vhost_user::Error), 91 #[error("Failed to create seccomp filter: {0}")] 92 CreateSeccompFilter(seccompiler::Error), 93 #[error("Failed to create rate limiter: {0}")] 94 CreateRateLimiter(std::io::Error), 95 #[error("Failed to activate the vDPA device: {0}")] 96 ActivateVdpa(vdpa::Error), 97 } 98 99 pub type ActivateResult = std::result::Result<(), ActivateError>; 100 101 pub type DeviceEventT = u16; 102 103 #[derive(Error, Debug)] 104 pub enum Error { 105 #[error("Failed to single used queue: {0}")] 106 FailedSignalingUsedQueue(io::Error), 107 #[error("I/O Error: {0}")] 108 IoError(io::Error), 109 #[error("Failed to update memory vhost-user: {0}")] 110 VhostUserUpdateMemory(vhost_user::Error), 111 #[error("Failed to add memory region vhost-user: {0}")] 112 VhostUserAddMemoryRegion(vhost_user::Error), 113 #[error("Failed to set shared memory region")] 114 SetShmRegionsNotSupported, 115 #[error("Failed to process net queue: {0}")] 116 NetQueuePair(::net_util::NetQueuePairError), 117 #[error("Failed to : {0}")] 118 QueueAddUsed(virtio_queue::Error), 119 #[error("Failed to : {0}")] 120 QueueIterator(virtio_queue::Error), 121 } 122 123 #[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq, Eq)] 124 pub struct TokenBucketConfig { 125 pub size: u64, 126 pub one_time_burst: Option<u64>, 127 pub refill_time: u64, 128 } 129 130 #[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq, Eq)] 131 #[serde(deny_unknown_fields)] 132 pub struct RateLimiterConfig { 133 pub bandwidth: Option<TokenBucketConfig>, 134 pub ops: Option<TokenBucketConfig>, 135 } 136 137 impl TryInto<rate_limiter::RateLimiter> for RateLimiterConfig { 138 type Error = io::Error; 139 140 fn try_into(self) -> std::result::Result<rate_limiter::RateLimiter, Self::Error> { 141 let bw = self.bandwidth.unwrap_or_default(); 142 let ops = self.ops.unwrap_or_default(); 143 rate_limiter::RateLimiter::new( 144 bw.size, 145 bw.one_time_burst.unwrap_or(0), 146 bw.refill_time, 147 ops.size, 148 ops.one_time_burst.unwrap_or(0), 149 ops.refill_time, 150 ) 151 } 152 } 153 154 /// Convert an absolute address into an address space (GuestMemory) 155 /// to a host pointer and verify that the provided size define a valid 156 /// range within a single memory region. 157 /// Return None if it is out of bounds or if addr+size overlaps a single region. 158 pub fn get_host_address_range<M: GuestMemory + ?Sized>( 159 mem: &M, 160 addr: GuestAddress, 161 size: usize, 162 ) -> Option<*mut u8> { 163 if mem.check_range(addr, size) { 164 Some(mem.get_host_address(addr).unwrap()) 165 } else { 166 None 167 } 168 } 169