mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
To support I/O throttling on virt-net devices, we need to use the 'rate_limiter' module from the 'net_utils' crate. Given the 'virtio-devices' crate has dependency on the 'net_utils', we will need to move the 'rate_limiter' module out of the 'virtio-devices' crate to avoid circular dependency issue. Considering the 'rate_limiter' is not virtio specific and could be reused for non virtio devices, we move it to its own crate. Signed-off-by: Bo Chen <chen.bo@intel.com>
178 lines
4.7 KiB
Rust
178 lines
4.7 KiB
Rust
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
//
|
|
// Portions Copyright 2017 The Chromium OS Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE-BSD-3-Clause file.
|
|
//
|
|
// Copyright © 2019 Intel Corporation
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
|
|
|
|
//! Implements virtio devices, queues, and transport mechanisms.
|
|
|
|
extern crate arc_swap;
|
|
extern crate epoll;
|
|
#[macro_use]
|
|
extern crate event_monitor;
|
|
#[macro_use]
|
|
extern crate log;
|
|
extern crate pci;
|
|
extern crate serde;
|
|
#[macro_use]
|
|
extern crate serde_derive;
|
|
extern crate serde_json;
|
|
extern crate vhost;
|
|
extern crate virtio_bindings;
|
|
extern crate vm_device;
|
|
extern crate vm_memory;
|
|
|
|
use std::convert::TryInto;
|
|
use std::io;
|
|
|
|
#[macro_use]
|
|
mod device;
|
|
pub mod balloon;
|
|
pub mod block;
|
|
mod console;
|
|
pub mod epoll_helper;
|
|
mod iommu;
|
|
pub mod mem;
|
|
pub mod net;
|
|
pub mod net_util;
|
|
mod pmem;
|
|
mod rng;
|
|
pub mod seccomp_filters;
|
|
pub mod transport;
|
|
pub mod vhost_user;
|
|
pub mod vsock;
|
|
pub mod watchdog;
|
|
|
|
pub use self::balloon::*;
|
|
pub use self::block::*;
|
|
pub use self::console::*;
|
|
pub use self::device::*;
|
|
pub use self::epoll_helper::*;
|
|
pub use self::iommu::*;
|
|
pub use self::mem::*;
|
|
pub use self::net::*;
|
|
pub use self::net_util::*;
|
|
pub use self::pmem::*;
|
|
pub use self::rng::*;
|
|
pub use self::vsock::*;
|
|
pub use self::watchdog::*;
|
|
use vm_memory::{GuestAddress, GuestMemory};
|
|
use vm_virtio::{queue::*, VirtioDeviceType};
|
|
|
|
const DEVICE_INIT: u32 = 0x00;
|
|
const DEVICE_ACKNOWLEDGE: u32 = 0x01;
|
|
const DEVICE_DRIVER: u32 = 0x02;
|
|
const DEVICE_DRIVER_OK: u32 = 0x04;
|
|
const DEVICE_FEATURES_OK: u32 = 0x08;
|
|
const DEVICE_FAILED: u32 = 0x80;
|
|
|
|
const VIRTIO_F_VERSION_1: u32 = 32;
|
|
const VIRTIO_F_IOMMU_PLATFORM: u32 = 33;
|
|
const VIRTIO_F_IN_ORDER: u32 = 35;
|
|
|
|
#[derive(Debug)]
|
|
pub enum ActivateError {
|
|
EpollCtl(std::io::Error),
|
|
BadActivate,
|
|
/// Queue number is not correct
|
|
BadQueueNum,
|
|
/// Failed to clone Kill event
|
|
CloneKillEventFd,
|
|
/// Failed to create Vhost-user interrupt eventfd
|
|
VhostIrqCreate,
|
|
/// Failed to setup vhost-user-fs daemon.
|
|
VhostUserFsSetup(vhost_user::Error),
|
|
/// Failed to setup vhost-user-net daemon.
|
|
VhostUserNetSetup(vhost_user::Error),
|
|
/// Failed to setup vhost-user-blk daemon.
|
|
VhostUserBlkSetup(vhost_user::Error),
|
|
/// Failed to reset vhost-user daemon.
|
|
VhostUserReset(vhost_user::Error),
|
|
/// Cannot create seccomp filter
|
|
CreateSeccompFilter(seccomp::SeccompError),
|
|
/// Cannot create rate limiter
|
|
CreateRateLimiter(std::io::Error),
|
|
}
|
|
|
|
pub type ActivateResult = std::result::Result<(), ActivateError>;
|
|
|
|
pub type DeviceEventT = u16;
|
|
|
|
#[derive(Debug)]
|
|
pub enum Error {
|
|
FailedReadingQueue {
|
|
event_type: &'static str,
|
|
underlying: io::Error,
|
|
},
|
|
FailedSignalingUsedQueue(io::Error),
|
|
PayloadExpected,
|
|
UnknownEvent {
|
|
device: &'static str,
|
|
event: DeviceEventT,
|
|
},
|
|
IoError(io::Error),
|
|
EpollCreateFd(io::Error),
|
|
EpollCtl(io::Error),
|
|
EpollWait(io::Error),
|
|
FailedSignalingDriver(io::Error),
|
|
VhostUserUpdateMemory(vhost_user::Error),
|
|
VhostUserAddMemoryRegion(vhost_user::Error),
|
|
EventfdError(io::Error),
|
|
SetShmRegionsNotSupported,
|
|
EpollHander(String),
|
|
NoMemoryConfigured,
|
|
NetQueuePair(::net_util::NetQueuePairError),
|
|
ApplySeccompFilter(seccomp::Error),
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq)]
|
|
pub struct TokenBucketConfig {
|
|
pub size: u64,
|
|
pub one_time_burst: Option<u64>,
|
|
pub refill_time: u64,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct RateLimiterConfig {
|
|
pub bandwidth: Option<TokenBucketConfig>,
|
|
pub ops: Option<TokenBucketConfig>,
|
|
}
|
|
|
|
impl TryInto<rate_limiter::RateLimiter> for RateLimiterConfig {
|
|
type Error = io::Error;
|
|
|
|
fn try_into(self) -> std::result::Result<rate_limiter::RateLimiter, Self::Error> {
|
|
let bw = self.bandwidth.unwrap_or_default();
|
|
let ops = self.ops.unwrap_or_default();
|
|
rate_limiter::RateLimiter::new(
|
|
bw.size,
|
|
bw.one_time_burst.unwrap_or(0),
|
|
bw.refill_time,
|
|
ops.size,
|
|
ops.one_time_burst.unwrap_or(0),
|
|
ops.refill_time,
|
|
)
|
|
}
|
|
}
|
|
|
|
/// Convert an absolute address into an address space (GuestMemory)
|
|
/// to a host pointer and verify that the provided size define a valid
|
|
/// range within a single memory region.
|
|
/// Return None if it is out of bounds or if addr+size overlaps a single region.
|
|
pub fn get_host_address_range<M: GuestMemory>(
|
|
mem: &M,
|
|
addr: GuestAddress,
|
|
size: usize,
|
|
) -> Option<*mut u8> {
|
|
if mem.check_range(addr, size) {
|
|
Some(mem.get_host_address(addr).unwrap())
|
|
} else {
|
|
None
|
|
}
|
|
}
|