Skip to content

Commit

Permalink
Reorganized Virtio file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
jounathaen committed Jun 12, 2024
1 parent b987ee8 commit ba3a519
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 153 deletions.
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ pub mod params;
#[cfg(target_os = "linux")]
pub mod shared_queue;
mod vcpu;
pub mod virtio;
pub mod virtqueue;
pub mod vm;

pub use arch::*;
Expand All @@ -37,3 +35,4 @@ pub type HypervisorResult<T> = Result<T, HypervisorError>;

pub mod net;
mod pci;
mod virtio;
6 changes: 3 additions & 3 deletions src/linux/x86_64/kvm_cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ use crate::{
consts::*,
hypercall,
linux::KVM,
net::virtio::{
pci::PciDevice,
vcpu::{VcpuStopReason, VirtualCPU},
virtio::{
capabilities::{ComCfg, IsrStatus, NetDevCfg},
pci::{ConfigAddress, MEM_NOTIFY, MEM_NOTIFY_1},
},
vcpu::{VcpuStopReason, VirtualCPU},
virtio::*,
vm::UhyveVm,
HypervisorError, HypervisorResult,
};
Expand Down
6 changes: 1 addition & 5 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
pub use crate::{
consts::{UHYVE_NET_MTU, UHYVE_QUEUE_SIZE},
};
pub use crate::consts::{UHYVE_NET_MTU, UHYVE_QUEUE_SIZE};

pub const BROADCAST_MAC_ADDR: [u8; 6] = [0xff; 6];
pub const PCI_ETHERNET_CLASS_CODE: u8 = 0x2;
Expand All @@ -15,5 +13,3 @@ pub const UHYVE_PCI_CLASS_INFO: [u8; 3] = [
];

pub mod tap;

pub mod virtio;
72 changes: 0 additions & 72 deletions src/net/virtio/config.rs

This file was deleted.

11 changes: 7 additions & 4 deletions src/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ use zerocopy::AsBytes;

use crate::{
consts::GUEST_PAGE_SIZE,
net::{
virtio::{DeviceStatus, VIRTIO_VENDOR_ID},
PCI_ETHERNET_REVISION_ID, UHYVE_PCI_CLASS_INFO,
},
net::{PCI_ETHERNET_REVISION_ID, UHYVE_PCI_CLASS_INFO},
virtio::{DeviceStatus, VIRTIO_VENDOR_ID},
};

/// For now, use an address large enough to be outside of kvm_userspace,
/// as IO/MMIO writes are otherwise dismissed.
pub const IOBASE: u32 = 0xFE000000;

pub trait PciDevice {
fn handle_read(&self, address: u32, dest: &mut [u8]);
fn handle_write(&mut self, address: u32, src: &[u8]);
}

#[derive(Error, Debug)]
pub enum PciError {
#[error("Trying to write to read_only memory (PCI space address: {0:#p})")]
Expand Down
20 changes: 10 additions & 10 deletions src/net/virtio/capabilities.rs → src/virtio/capabilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ use bitflags::bitflags;
use zerocopy::AsBytes;

use crate::{
virtqueue::VirtqueueNotification,
net::{
virtio::pci::{
get_offset, ConfigAddress, COMMON_CFG_START, DEVICE_CFG_START, ISR_CFG_START,
},
BROADCAST_MAC_ADDR, UHYVE_NET_MTU, UHYVE_QUEUE_SIZE,
},
net::{BROADCAST_MAC_ADDR, UHYVE_NET_MTU, UHYVE_QUEUE_SIZE},
pci::PciError,
virtio::{
pci::{get_offset, ConfigAddress, COMMON_CFG_START, DEVICE_CFG_START, ISR_CFG_START},
virtqueue::VirtqueueNotification,
},
};

/// Virtio capability type IDs. See section 4.1.4 virtio v1.2
#[derive(Debug, Clone, Copy, AsBytes, PartialEq, Eq)]
#[repr(u8)]
#[allow(non_camel_case_types)]
#[allow(non_camel_case_types, dead_code)]
pub enum CfgType {
INVALID_CFG = 0x00,
/// Common configuration
Expand Down Expand Up @@ -134,7 +132,7 @@ bitflags! {
}

// TODO: Replace with virtio_bindings::Virtio_net_config?
/// Virtio device configuration layout.
/// Virtio device configuration layout. Virtio v1.2 Section 5.1.4
#[derive(Clone, Debug)]
#[repr(C)]
pub struct NetDevCfg {
Expand Down Expand Up @@ -209,7 +207,6 @@ impl IsrStatus {
/// See section 4.1.4.4.1 virtio v1.2
#[derive(AsBytes, Clone, Debug)]
#[repr(C)]
// TODO: Rename Notif -> Notify
pub struct NotifyCap {
pub cap: PciCap,
/// Combind with queue_notify_off to derive the Queue Notify address
Expand All @@ -234,6 +231,7 @@ impl NotifyCap {
_ => Err(PciError::InvalidAddress(address as u32)),
}
}
#[allow(dead_code)]
pub fn write(&mut self, address: u8, _data: &[u8]) -> Result<(), PciError> {
Err(PciError::ReadOnlyAccess(address as u32))
}
Expand All @@ -259,6 +257,7 @@ impl Default for NotifyCap {
/// All data should be treated as little-endian.
#[derive(AsBytes, Clone, Copy, Debug)]
#[repr(C)]
#[allow(dead_code)]
pub struct ComCfg {
/// **read-write**: The driver uses this to select device_feature.
///
Expand Down Expand Up @@ -340,6 +339,7 @@ pub struct ComCfg {

_padding: [u8; 4],
}
#[allow(dead_code)]
impl ComCfg {
pub const DEVICE_FEATURE_SELECT: ConfigAddress =
get_offset!(COMMON_CFG_START, Self, device_feature_select);
Expand Down
13 changes: 5 additions & 8 deletions src/net/virtio/mod.rs → src/virtio/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
pub mod capabilities;
pub mod pci;
//! Virtio Datastructures and constants.


pub use virtio_bindings::{
bindings::virtio_net::{VIRTIO_NET_F_MAC, VIRTIO_NET_F_MTU, VIRTIO_NET_F_STATUS},
virtio_config::{VIRTIO_F_RING_RESET, VIRTIO_F_VERSION_1},
};
pub(crate) mod capabilities;
pub(crate) mod net;
pub(crate) mod pci;
pub(crate) mod virtqueue;

pub mod features {
use virtio_bindings::{
Expand All @@ -19,7 +17,6 @@ pub mod features {
}

use bitflags::bitflags;
pub use virtio_bindings::bindings::virtio_net::{VIRTIO_NET_HDR_GSO_NONE, VIRTIO_NET_S_LINK_UP};
use zerocopy::AsBytes;

/// Virtio device status field. See section 2.1 virtio v1.2
Expand Down
36 changes: 9 additions & 27 deletions src/virtio.rs → src/virtio/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,23 @@ use vmm_sys_util::eventfd::EventFd;

use crate::{
consts::{UHYVE_IRQ_NET, UHYVE_NET_MTU},
net::{
tap::Tap,
virtio::{
capabilities::IsrStatus,
features::{UHYVE_NET_FEATURES_HIGH, UHYVE_NET_FEATURES_LOW},
pci::{HeaderConf, MEM_NOTIFY, MEM_NOTIFY_1},
DeviceStatus, IOBASE, NET_DEVICE_ID,
},
net::tap::Tap,
pci::{MemoryBar64, PciDevice},
virtio::{
capabilities::IsrStatus,
features::{UHYVE_NET_FEATURES_HIGH, UHYVE_NET_FEATURES_LOW},
pci::{HeaderConf, MEM_NOTIFY, MEM_NOTIFY_1},
virtqueue::{self, QUEUE_LIMIT},
DeviceStatus, IOBASE, NET_DEVICE_ID,
},
virtqueue::{self, QUEUE_LIMIT},
pci::MemoryBar64,
};

const VIRTIO_NET_HEADER_SZ: usize = mem::size_of::<virtio_net_hdr_v1>();

const RX_QUEUE: u16 = 0;
const TX_QUEUE: u16 = 1;
pub const VIRTIO_PCI_MEM_BAR_PFN: u16 = 1 << 3;

use crate::net::virtio::capabilities::{FeatureSelector, NetDevStatus};

pub trait PciDevice {
fn handle_read(&self, address: u32, dest: &mut [u8]);
fn handle_write(&mut self, address: u32, src: &[u8]);
}
use crate::virtio::capabilities::{FeatureSelector, NetDevStatus};

/// Struct to manage uhyve's network device.
pub struct VirtioNetPciDevice {
Expand Down Expand Up @@ -495,22 +487,12 @@ impl VirtioNetPciDevice {

impl PciDevice for VirtioNetPciDevice {
fn handle_read(&self, address: u32, dest: &mut [u8]) {
// println!(
// "VirtioPCI: reading {} bytes from {address:#x}: {:?}",
// dest.len(),
// &self.config_space.slice[address as usize..(address as usize + dest.len())]
// );
if let Err(e) = self.header_caps.read(address, dest) {
error!("PCI Read error: {e:?}");
}
}

fn handle_write(&mut self, address: u32, data: &[u8]) {
// println!(
// "VirtioPCI: writing {} bytes to {address:#x}: {:?}",
// data.len(),
// data
// );
if let Err(e) = self.header_caps.write(address, data) {
error!("PCI Write error: {e:?}");
}
Expand Down
18 changes: 3 additions & 15 deletions src/net/virtio/pci.rs → src/virtio/pci.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Configuration Structures for Virtio PCI devices

use std::{mem::size_of, ops::Add};

/// Configuration Structures for Virtio PCI devices
use crate::{
net::virtio::capabilities::*,
pci::{PciError, PciType0ConfigSpaceHeader, IOBASE},
virtqueue::VirtqueueNotification,
virtio::{capabilities::*, virtqueue::VirtqueueNotification},
};

pub const HDR_END: u8 = size_of::<PciType0ConfigSpaceHeader>() as u8 - 1;
Expand Down Expand Up @@ -40,18 +40,6 @@ pub const NOTIFY_REGION_END: u8 =
pub const DEVICE_CFG_START: u8 = NOTIFY_REGION_END + 1;
pub const DEVICE_CFG_END: u8 = DEVICE_CFG_START + size_of::<NetDevCfg>() as u8 - 1;

pub const MAC_ADDRESS: ConfigAddress =
ConfigAddress::from_configuration_address(DEVICE_CAP_START as u32);

/// Contains immutable offsets of uhyve's virtio configuration.
// pub mod offsets {
// use super::capabilities::{
// ComCfg, IsrStatus, NetDevCfg,
// };
// use crate::net::virtio::ConfigAddress;

// mod offsets {

/// An address in the PCI configuration space.
#[derive(Eq, PartialEq, Clone, Copy, Debug)]
pub struct ConfigAddress(pub(crate) u32);
Expand Down
11 changes: 5 additions & 6 deletions src/virtqueue.rs → src/virtio/virtqueue.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// TODO: replace with https://crates.io/crates/virtio-queue
#![allow(dead_code)]

use std::{
marker::PhantomData,
mem,
Expand All @@ -9,14 +12,10 @@ use uhyve_interface::GuestPhysAddr;

use crate::consts::PAGE_SIZE;

pub const QUEUE_LIMIT: usize = 256;
pub const VIRTQ_DESC_F_AVAIL: u16 = 1 << 7;
pub const VIRTQ_DESC_F_USED: u16 = 1 << 15;
pub(crate) const QUEUE_LIMIT: usize = 256;

use virtio_bindings::bindings::virtio_ring::VRING_AVAIL_F_NO_INTERRUPT;
pub use virtio_bindings::bindings::virtio_ring::{
VRING_DESC_F_INDIRECT, VRING_DESC_F_NEXT, VRING_DESC_F_WRITE,
};
pub use virtio_bindings::bindings::virtio_ring::VRING_DESC_F_WRITE;

// A virtqueue notification as described in the Virtio standard v1.2 Sec. 2.9 & 4.1.5.2.
#[repr(C)]
Expand Down
5 changes: 4 additions & 1 deletion src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ use crate::arch::x86_64::{
};
#[cfg(all(target_arch = "x86_64", target_os = "linux"))]
use crate::linux::x86_64::kvm_cpu::initialize_kvm;
use crate::{arch, consts::*, os::HypervisorError, params::Params, vcpu::VirtualCPU, virtio::*};
use crate::{
arch, consts::*, os::HypervisorError, params::Params, vcpu::VirtualCPU,
virtio::net::VirtioNetPciDevice,
};

pub type HypervisorResult<T> = Result<T, HypervisorError>;

Expand Down

0 comments on commit ba3a519

Please sign in to comment.