2019-04-18 17:24:06 +00:00
|
|
|
// 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
|
2019-05-08 10:22:53 +00:00
|
|
|
// found in the LICENSE-BSD-3-Clause file.
|
|
|
|
//
|
|
|
|
// Copyright © 2019 Intel Corporation
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
|
2019-04-18 17:24:06 +00:00
|
|
|
|
|
|
|
//! Implements virtio devices, queues, and transport mechanisms.
|
|
|
|
extern crate epoll;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
extern crate pci;
|
|
|
|
extern crate virtio_bindings;
|
|
|
|
extern crate vm_memory;
|
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
use std::io;
|
|
|
|
|
2019-05-06 17:27:40 +00:00
|
|
|
mod block;
|
2019-04-18 17:24:06 +00:00
|
|
|
mod device;
|
2019-05-21 18:54:53 +00:00
|
|
|
pub mod fs;
|
2019-05-08 00:26:37 +00:00
|
|
|
pub mod net;
|
2019-06-18 21:40:57 +00:00
|
|
|
mod pmem;
|
2019-04-18 17:24:06 +00:00
|
|
|
mod queue;
|
2019-05-09 05:01:48 +00:00
|
|
|
mod rng;
|
2019-04-18 17:24:06 +00:00
|
|
|
|
2019-04-18 17:32:41 +00:00
|
|
|
pub mod transport;
|
|
|
|
|
2019-05-06 17:27:40 +00:00
|
|
|
pub use self::block::*;
|
2019-04-18 17:24:06 +00:00
|
|
|
pub use self::device::*;
|
2019-05-21 18:54:53 +00:00
|
|
|
pub use self::fs::*;
|
2019-05-08 00:26:37 +00:00
|
|
|
pub use self::net::*;
|
2019-06-18 21:40:57 +00:00
|
|
|
pub use self::pmem::*;
|
2019-04-18 17:24:06 +00:00
|
|
|
pub use self::queue::*;
|
2019-05-09 05:01:48 +00:00
|
|
|
pub use self::rng::*;
|
2019-04-18 17:24:06 +00:00
|
|
|
|
|
|
|
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;
|
2019-05-21 18:54:53 +00:00
|
|
|
const VIRTIO_F_VERSION_1_BITMASK: u64 = 1 << VIRTIO_F_VERSION_1;
|
2019-04-18 17:24:06 +00:00
|
|
|
|
|
|
|
// Types taken from linux/virtio_ids.h
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
#[repr(C)]
|
|
|
|
enum VirtioDeviceType {
|
|
|
|
TYPE_NET = 1,
|
|
|
|
TYPE_BLOCK = 2,
|
|
|
|
TYPE_RNG = 4,
|
|
|
|
TYPE_BALLOON = 5,
|
|
|
|
TYPE_9P = 9,
|
|
|
|
TYPE_GPU = 16,
|
|
|
|
TYPE_INPUT = 18,
|
|
|
|
TYPE_VSOCK = 19,
|
2019-05-21 18:54:53 +00:00
|
|
|
TYPE_FS = 26,
|
2019-06-18 21:40:57 +00:00
|
|
|
TYPE_PMEM = 27,
|
2019-04-18 17:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// In order to use the `{}` marker, the trait `fmt::Display` must be implemented
|
|
|
|
// manually for the type VirtioDeviceType.
|
|
|
|
impl fmt::Display for VirtioDeviceType {
|
|
|
|
// This trait requires `fmt` with this exact signature.
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let output = match *self {
|
|
|
|
VirtioDeviceType::TYPE_NET => "net",
|
|
|
|
VirtioDeviceType::TYPE_BLOCK => "block",
|
|
|
|
VirtioDeviceType::TYPE_RNG => "rng",
|
|
|
|
VirtioDeviceType::TYPE_BALLOON => "balloon",
|
|
|
|
VirtioDeviceType::TYPE_GPU => "gpu",
|
|
|
|
VirtioDeviceType::TYPE_9P => "9p",
|
|
|
|
VirtioDeviceType::TYPE_VSOCK => "vsock",
|
2019-05-21 18:54:53 +00:00
|
|
|
VirtioDeviceType::TYPE_FS => "fs",
|
2019-06-18 21:40:57 +00:00
|
|
|
VirtioDeviceType::TYPE_PMEM => "pmem",
|
2019-04-18 17:24:06 +00:00
|
|
|
_ => return Err(std::fmt::Error),
|
|
|
|
};
|
|
|
|
write!(f, "{}", output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
const INTERRUPT_STATUS_USED_RING: u32 = 0x1;
|
|
|
|
#[allow(dead_code)]
|
|
|
|
const INTERRUPT_STATUS_CONFIG_CHANGED: u32 = 0x2;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ActivateError {
|
|
|
|
EpollCtl(std::io::Error),
|
|
|
|
BadActivate,
|
2019-05-21 18:54:53 +00:00
|
|
|
|
|
|
|
/// Failed to setup vhost-user daemon.
|
|
|
|
VhostUserSetup(fs::Error),
|
2019-04-18 17:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub type ActivateResult = std::result::Result<(), ActivateError>;
|
|
|
|
|
|
|
|
pub type DeviceEventT = u16;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
FailedReadingQueue {
|
|
|
|
event_type: &'static str,
|
|
|
|
underlying: io::Error,
|
|
|
|
},
|
|
|
|
FailedReadTap,
|
|
|
|
FailedSignalingUsedQueue(io::Error),
|
|
|
|
PayloadExpected,
|
|
|
|
UnknownEvent {
|
|
|
|
device: &'static str,
|
|
|
|
event: DeviceEventT,
|
|
|
|
},
|
|
|
|
IoError(io::Error),
|
2019-05-06 17:27:40 +00:00
|
|
|
EpollCreateFd(io::Error),
|
|
|
|
EpollCtl(io::Error),
|
|
|
|
EpollWait(io::Error),
|
2019-04-18 17:24:06 +00:00
|
|
|
}
|