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 std::io; 19 20 use serde::{Deserialize, Serialize}; 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 use vm_memory::{bitmap::AtomicBitmap, GuestAddress, GuestMemory}; 43 use vm_virtio::VirtioDeviceType; 44 45 pub use self::balloon::Balloon; 46 pub use self::block::{Block, BlockState}; 47 pub use self::console::{Console, ConsoleResizer, Endpoint}; 48 pub use self::device::{ 49 DmaRemapping, UserspaceMapping, VirtioCommon, VirtioDevice, VirtioInterrupt, 50 VirtioInterruptType, VirtioSharedMemoryList, 51 }; 52 pub use self::epoll_helper::{ 53 EpollHelper, EpollHelperError, EpollHelperHandler, EPOLL_HELPER_EVENT_LAST, 54 }; 55 pub use self::iommu::{AccessPlatformMapping, Iommu, IommuMapping}; 56 pub use self::mem::{BlocksState, Mem, VirtioMemMappingSource, VIRTIO_MEM_ALIGN_SIZE}; 57 pub use self::net::{Net, NetCtrlEpollHandler}; 58 pub use self::pmem::Pmem; 59 pub use self::rng::Rng; 60 pub use self::vdpa::{Vdpa, VdpaDmaMapping}; 61 pub use self::vsock::Vsock; 62 pub use self::watchdog::Watchdog; 63 64 type GuestMemoryMmap = vm_memory::GuestMemoryMmap<AtomicBitmap>; 65 type GuestRegionMmap = vm_memory::GuestRegionMmap<AtomicBitmap>; 66 type MmapRegion = vm_memory::MmapRegion<AtomicBitmap>; 67 68 const DEVICE_INIT: u32 = 0x00; 69 const DEVICE_ACKNOWLEDGE: u32 = 0x01; 70 const DEVICE_DRIVER: u32 = 0x02; 71 const DEVICE_DRIVER_OK: u32 = 0x04; 72 const DEVICE_FEATURES_OK: u32 = 0x08; 73 const DEVICE_FAILED: u32 = 0x80; 74 75 const VIRTIO_F_RING_INDIRECT_DESC: u32 = 28; 76 const VIRTIO_F_RING_EVENT_IDX: u32 = 29; 77 const VIRTIO_F_VERSION_1: u32 = 32; 78 const VIRTIO_F_IOMMU_PLATFORM: u32 = 33; 79 const VIRTIO_F_IN_ORDER: u32 = 35; 80 const VIRTIO_F_ORDER_PLATFORM: u32 = 36; 81 #[allow(dead_code)] 82 const VIRTIO_F_SR_IOV: u32 = 37; 83 const VIRTIO_F_NOTIFICATION_DATA: u32 = 38; 84 85 #[derive(Error, Debug)] 86 pub enum ActivateError { 87 #[error("Failed to activate virtio device")] 88 BadActivate, 89 #[error("Failed to clone exit event fd: {0}")] 90 CloneExitEventFd(std::io::Error), 91 #[error("Failed to spawn thread: {0}")] 92 ThreadSpawn(std::io::Error), 93 #[error("Failed to setup vhost-user-fs daemon: {0}")] 94 VhostUserFsSetup(vhost_user::Error), 95 #[error("Failed to setup vhost-user daemon: {0}")] 96 VhostUserSetup(vhost_user::Error), 97 #[error("Failed to create seccomp filter: {0}")] 98 CreateSeccompFilter(seccompiler::Error), 99 #[error("Failed to create rate limiter: {0}")] 100 CreateRateLimiter(std::io::Error), 101 #[error("Failed to activate the vDPA device: {0}")] 102 ActivateVdpa(vdpa::Error), 103 } 104 105 pub type ActivateResult = std::result::Result<(), ActivateError>; 106 107 pub type DeviceEventT = u16; 108 109 #[derive(Error, Debug)] 110 pub enum Error { 111 #[error("Failed to single used queue: {0}")] 112 FailedSignalingUsedQueue(io::Error), 113 #[error("I/O Error: {0}")] 114 IoError(io::Error), 115 #[error("Failed to update memory vhost-user: {0}")] 116 VhostUserUpdateMemory(vhost_user::Error), 117 #[error("Failed to add memory region vhost-user: {0}")] 118 VhostUserAddMemoryRegion(vhost_user::Error), 119 #[error("Failed to set shared memory region")] 120 SetShmRegionsNotSupported, 121 #[error("Failed to process net queue: {0}")] 122 NetQueuePair(::net_util::NetQueuePairError), 123 #[error("Failed to : {0}")] 124 QueueAddUsed(virtio_queue::Error), 125 #[error("Failed to : {0}")] 126 QueueIterator(virtio_queue::Error), 127 } 128 129 #[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq, Eq)] 130 pub struct TokenBucketConfig { 131 pub size: u64, 132 pub one_time_burst: Option<u64>, 133 pub refill_time: u64, 134 } 135 136 #[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq, Eq)] 137 #[serde(deny_unknown_fields)] 138 pub struct RateLimiterConfig { 139 pub bandwidth: Option<TokenBucketConfig>, 140 pub ops: Option<TokenBucketConfig>, 141 } 142 143 impl TryInto<rate_limiter::RateLimiter> for RateLimiterConfig { 144 type Error = io::Error; 145 146 fn try_into(self) -> std::result::Result<rate_limiter::RateLimiter, Self::Error> { 147 let bw = self.bandwidth.unwrap_or_default(); 148 let ops = self.ops.unwrap_or_default(); 149 rate_limiter::RateLimiter::new( 150 bw.size, 151 bw.one_time_burst.unwrap_or(0), 152 bw.refill_time, 153 ops.size, 154 ops.one_time_burst.unwrap_or(0), 155 ops.refill_time, 156 ) 157 } 158 } 159 160 /// Return the host virtual address corresponding to the given guest address range 161 /// 162 /// Convert an absolute address into an address space (GuestMemory) 163 /// to a host pointer and verify that the provided size define a valid 164 /// range within a single memory region. 165 /// Return None if it is out of bounds or if addr+size overlaps a single region. 166 pub fn get_host_address_range<M: GuestMemory + ?Sized>( 167 mem: &M, 168 addr: GuestAddress, 169 size: usize, 170 ) -> Option<*mut u8> { 171 if mem.check_range(addr, size) { 172 Some(mem.get_host_address(addr).unwrap()) 173 } else { 174 None 175 } 176 } 177