summaryrefslogtreecommitdiff
path: root/src/vhost_kern
diff options
context:
space:
mode:
Diffstat (limited to 'src/vhost_kern')
-rw-r--r--src/vhost_kern/mod.rs80
-rw-r--r--src/vhost_kern/vhost_binding.rs1
-rw-r--r--src/vhost_kern/vsock.rs141
3 files changed, 160 insertions, 62 deletions
diff --git a/src/vhost_kern/mod.rs b/src/vhost_kern/mod.rs
index 350e134..f82cbfc 100644
--- a/src/vhost_kern/mod.rs
+++ b/src/vhost_kern/mod.rs
@@ -13,7 +13,7 @@
use std::os::unix::io::{AsRawFd, RawFd};
-use vm_memory::GuestAddressSpace;
+use vm_memory::{Address, GuestAddress, GuestAddressSpace, GuestMemory, GuestUsize};
use vmm_sys_util::eventfd::EventFd;
use vmm_sys_util::ioctl::{ioctl, ioctl_with_mut_ref, ioctl_with_ptr, ioctl_with_ref};
@@ -39,7 +39,7 @@ fn ioctl_result<T>(rc: i32, res: T) -> Result<T> {
/// Represent an in-kernel vhost device backend.
pub trait VhostKernBackend: AsRawFd {
- /// Assoicated type to access guest memory.
+ /// Associated type to access guest memory.
type AS: GuestAddressSpace;
/// Get the object to access the guest's memory.
@@ -55,52 +55,36 @@ pub trait VhostKernBackend: AsRawFd {
return false;
}
- // TODO: the GuestMemory trait lacks of method to look up GPA by HVA,
- // so there's no way to validate HVAs. Please extend vm-memory crate
- // first.
- /*
+ let m = self.mem().memory();
let desc_table_size = 16 * u64::from(queue_size) as GuestUsize;
let avail_ring_size = 6 + 2 * u64::from(queue_size) as GuestUsize;
let used_ring_size = 6 + 8 * u64::from(queue_size) as GuestUsize;
if GuestAddress(config_data.desc_table_addr)
.checked_add(desc_table_size)
- .map_or(true, |v| !self.mem().address_in_range(v))
+ .map_or(true, |v| !m.address_in_range(v))
{
- false
- } else if GuestAddress(config_data.avail_ring_addr)
+ return false;
+ }
+ if GuestAddress(config_data.avail_ring_addr)
.checked_add(avail_ring_size)
- .map_or(true, |v| !self.mem().address_in_range(v))
+ .map_or(true, |v| !m.address_in_range(v))
{
- false
- } else if GuestAddress(config_data.used_ring_addr)
+ return false;
+ }
+ if GuestAddress(config_data.used_ring_addr)
.checked_add(used_ring_size)
- .map_or(true, |v| !self.mem().address_in_range(v))
+ .map_or(true, |v| !m.address_in_range(v))
{
- false
+ return false;
}
- */
config_data.is_log_addr_valid()
}
}
impl<T: VhostKernBackend> VhostBackend for T {
- /// Set the current process as the owner of this file descriptor.
- /// This must be run before any other vhost ioctls.
- fn set_owner(&mut self) -> Result<()> {
- // This ioctl is called on a valid vhost fd and has its return value checked.
- let ret = unsafe { ioctl(self, VHOST_SET_OWNER()) };
- ioctl_result(ret, ())
- }
-
- fn reset_owner(&mut self) -> Result<()> {
- // This ioctl is called on a valid vhost fd and has its return value checked.
- let ret = unsafe { ioctl(self, VHOST_RESET_OWNER()) };
- ioctl_result(ret, ())
- }
-
/// Get a bitmask of supported virtio/vhost features.
- fn get_features(&mut self) -> Result<u64> {
+ fn get_features(&self) -> Result<u64> {
let mut avail_features: u64 = 0;
// This ioctl is called on a valid vhost fd and has its return value checked.
let ret = unsafe { ioctl_with_mut_ref(self, VHOST_GET_FEATURES(), &mut avail_features) };
@@ -112,14 +96,28 @@ impl<T: VhostKernBackend> VhostBackend for T {
///
/// # Arguments
/// * `features` - Bitmask of features to set.
- fn set_features(&mut self, features: u64) -> Result<()> {
+ fn set_features(&self, features: u64) -> Result<()> {
// This ioctl is called on a valid vhost fd and has its return value checked.
let ret = unsafe { ioctl_with_ref(self, VHOST_SET_FEATURES(), &features) };
ioctl_result(ret, ())
}
+ /// Set the current process as the owner of this file descriptor.
+ /// This must be run before any other vhost ioctls.
+ fn set_owner(&self) -> Result<()> {
+ // This ioctl is called on a valid vhost fd and has its return value checked.
+ let ret = unsafe { ioctl(self, VHOST_SET_OWNER()) };
+ ioctl_result(ret, ())
+ }
+
+ fn reset_owner(&self) -> Result<()> {
+ // This ioctl is called on a valid vhost fd and has its return value checked.
+ let ret = unsafe { ioctl(self, VHOST_RESET_OWNER()) };
+ ioctl_result(ret, ())
+ }
+
/// Set the guest memory mappings for vhost to use.
- fn set_mem_table(&mut self, regions: &[VhostUserMemoryRegionInfo]) -> Result<()> {
+ fn set_mem_table(&self, regions: &[VhostUserMemoryRegionInfo]) -> Result<()> {
if regions.is_empty() || regions.len() > VHOST_MAX_MEMORY_REGIONS {
return Err(Error::InvalidGuestMemory);
}
@@ -148,7 +146,7 @@ impl<T: VhostKernBackend> VhostBackend for T {
///
/// # Arguments
/// * `base` - Base address for page modification logging.
- fn set_log_base(&mut self, base: u64, fd: Option<RawFd>) -> Result<()> {
+ fn set_log_base(&self, base: u64, fd: Option<RawFd>) -> Result<()> {
if fd.is_some() {
return Err(Error::LogAddress);
}
@@ -159,7 +157,7 @@ impl<T: VhostKernBackend> VhostBackend for T {
}
/// Specify an eventfd file descriptor to signal on log write.
- fn set_log_fd(&mut self, fd: RawFd) -> Result<()> {
+ fn set_log_fd(&self, fd: RawFd) -> Result<()> {
// This ioctl is called on a valid vhost fd and has its return value checked.
let val: i32 = fd;
let ret = unsafe { ioctl_with_ref(self, VHOST_SET_LOG_FD(), &val) };
@@ -171,7 +169,7 @@ impl<T: VhostKernBackend> VhostBackend for T {
/// # Arguments
/// * `queue_index` - Index of the queue to set descriptor count for.
/// * `num` - Number of descriptors in the queue.
- fn set_vring_num(&mut self, queue_index: usize, num: u16) -> Result<()> {
+ fn set_vring_num(&self, queue_index: usize, num: u16) -> Result<()> {
let vring_state = vhost_vring_state {
index: queue_index as u32,
num: u32::from(num),
@@ -187,7 +185,7 @@ impl<T: VhostKernBackend> VhostBackend for T {
/// # Arguments
/// * `queue_index` - Index of the queue to set addresses for.
/// * `config_data` - Vring config data.
- fn set_vring_addr(&mut self, queue_index: usize, config_data: &VringConfigData) -> Result<()> {
+ fn set_vring_addr(&self, queue_index: usize, config_data: &VringConfigData) -> Result<()> {
if !self.is_valid(config_data) {
return Err(Error::InvalidQueue);
}
@@ -212,7 +210,7 @@ impl<T: VhostKernBackend> VhostBackend for T {
/// # Arguments
/// * `queue_index` - Index of the queue to modify.
/// * `num` - Index where available descriptors start.
- fn set_vring_base(&mut self, queue_index: usize, base: u16) -> Result<()> {
+ fn set_vring_base(&self, queue_index: usize, base: u16) -> Result<()> {
let vring_state = vhost_vring_state {
index: queue_index as u32,
num: u32::from(base),
@@ -224,7 +222,7 @@ impl<T: VhostKernBackend> VhostBackend for T {
}
/// Get a bitmask of supported virtio/vhost features.
- fn get_vring_base(&mut self, queue_index: usize) -> Result<u32> {
+ fn get_vring_base(&self, queue_index: usize) -> Result<u32> {
let vring_state = vhost_vring_state {
index: queue_index as u32,
num: 0,
@@ -239,7 +237,7 @@ impl<T: VhostKernBackend> VhostBackend for T {
/// # Arguments
/// * `queue_index` - Index of the queue to modify.
/// * `fd` - EventFd to trigger.
- fn set_vring_call(&mut self, queue_index: usize, fd: &EventFd) -> Result<()> {
+ fn set_vring_call(&self, queue_index: usize, fd: &EventFd) -> Result<()> {
let vring_file = vhost_vring_file {
index: queue_index as u32,
fd: fd.as_raw_fd(),
@@ -256,7 +254,7 @@ impl<T: VhostKernBackend> VhostBackend for T {
/// # Arguments
/// * `queue_index` - Index of the queue to modify.
/// * `fd` - EventFd that will be signaled from guest.
- fn set_vring_kick(&mut self, queue_index: usize, fd: &EventFd) -> Result<()> {
+ fn set_vring_kick(&self, queue_index: usize, fd: &EventFd) -> Result<()> {
let vring_file = vhost_vring_file {
index: queue_index as u32,
fd: fd.as_raw_fd(),
@@ -272,7 +270,7 @@ impl<T: VhostKernBackend> VhostBackend for T {
/// # Arguments
/// * `queue_index` - Index of the queue to modify.
/// * `fd` - EventFd that will be signaled from the backend.
- fn set_vring_err(&mut self, queue_index: usize, fd: &EventFd) -> Result<()> {
+ fn set_vring_err(&self, queue_index: usize, fd: &EventFd) -> Result<()> {
let vring_file = vhost_vring_file {
index: queue_index as u32,
fd: fd.as_raw_fd(),
diff --git a/src/vhost_kern/vhost_binding.rs b/src/vhost_kern/vhost_binding.rs
index fdc5225..57ae698 100644
--- a/src/vhost_kern/vhost_binding.rs
+++ b/src/vhost_kern/vhost_binding.rs
@@ -13,6 +13,7 @@
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(missing_docs)]
+#![allow(clippy::missing_safety_doc)]
use crate::{Error, Result};
use std::os::raw;
diff --git a/src/vhost_kern/vsock.rs b/src/vhost_kern/vsock.rs
index c4149bd..65f89e4 100644
--- a/src/vhost_kern/vsock.rs
+++ b/src/vhost_kern/vsock.rs
@@ -1,22 +1,23 @@
-// Copyright (C) 2019 Alibaba Cloud Computing. All rights reserved.
+// Copyright (C) 2019 Alibaba Cloud. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause
//
// 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-Google file.
-//! Kernel-based vsock vhost backend.
+//! Kernel-based vhost-vsock backend.
use std::fs::{File, OpenOptions};
use std::os::unix::fs::OpenOptionsExt;
use std::os::unix::io::{AsRawFd, RawFd};
-use super::vhost_binding::{VHOST_VSOCK_SET_GUEST_CID, VHOST_VSOCK_SET_RUNNING};
-use super::{ioctl_result, Error, Result, VhostKernBackend};
-use libc;
use vm_memory::GuestAddressSpace;
use vmm_sys_util::ioctl::ioctl_with_ref;
+use super::vhost_binding::{VHOST_VSOCK_SET_GUEST_CID, VHOST_VSOCK_SET_RUNNING};
+use super::{ioctl_result, Error, Result, VhostKernBackend};
+use crate::vsock::VhostVsock;
+
const VHOST_PATH: &str = "/dev/vhost-vsock";
/// Handle for running VHOST_VSOCK ioctls.
@@ -39,31 +40,26 @@ impl<AS: GuestAddressSpace> Vsock<AS> {
})
}
- /// Set the CID for the guest. This number is used for routing all data destined for
- /// running in the guest. Each guest on a hypervisor must have an unique CID
- ///
- /// # Arguments
- /// * `cid` - CID to assign to the guest
- pub fn set_guest_cid(&self, cid: u64) -> Result<()> {
+ fn set_running(&self, running: bool) -> Result<()> {
+ let on: ::std::os::raw::c_int = if running { 1 } else { 0 };
+ let ret = unsafe { ioctl_with_ref(&self.fd, VHOST_VSOCK_SET_RUNNING(), &on) };
+ ioctl_result(ret, ())
+ }
+}
+
+impl<AS: GuestAddressSpace> VhostVsock for Vsock<AS> {
+ fn set_guest_cid(&self, cid: u64) -> Result<()> {
let ret = unsafe { ioctl_with_ref(&self.fd, VHOST_VSOCK_SET_GUEST_CID(), &cid) };
ioctl_result(ret, ())
}
- /// Tell the VHOST driver to start performing data transfer.
- pub fn start(&self) -> Result<()> {
+ fn start(&self) -> Result<()> {
self.set_running(true)
}
- /// Tell the VHOST driver to stop performing data transfer.
- pub fn stop(&self) -> Result<()> {
+ fn stop(&self) -> Result<()> {
self.set_running(false)
}
-
- fn set_running(&self, running: bool) -> Result<()> {
- let on: ::std::os::raw::c_int = if running { 1 } else { 0 };
- let ret = unsafe { ioctl_with_ref(&self.fd, VHOST_VSOCK_SET_RUNNING(), &on) };
- ioctl_result(ret, ())
- }
}
impl<AS: GuestAddressSpace> VhostKernBackend for Vsock<AS> {
@@ -79,3 +75,106 @@ impl<AS: GuestAddressSpace> AsRawFd for Vsock<AS> {
self.fd.as_raw_fd()
}
}
+
+#[cfg(test)]
+mod tests {
+ use vm_memory::{GuestAddress, GuestMemory, GuestMemoryMmap};
+ use vmm_sys_util::eventfd::EventFd;
+
+ use super::*;
+ use crate::{VhostBackend, VhostUserMemoryRegionInfo, VringConfigData};
+
+ #[test]
+ fn test_vsock_new_device() {
+ let m = GuestMemoryMmap::from_ranges(&[(GuestAddress(0), 0x10_0000)]).unwrap();
+ let vsock = Vsock::new(&m).unwrap();
+
+ assert!(vsock.as_raw_fd() >= 0);
+ assert!(vsock.mem().find_region(GuestAddress(0x100)).is_some());
+ assert!(vsock.mem().find_region(GuestAddress(0x10_0000)).is_none());
+ }
+
+ #[test]
+ fn test_vsock_is_valid() {
+ let m = GuestMemoryMmap::from_ranges(&[(GuestAddress(0), 0x10_0000)]).unwrap();
+ let vsock = Vsock::new(&m).unwrap();
+
+ let mut config = VringConfigData {
+ queue_max_size: 32,
+ queue_size: 32,
+ flags: 0,
+ desc_table_addr: 0x1000,
+ used_ring_addr: 0x2000,
+ avail_ring_addr: 0x3000,
+ log_addr: None,
+ };
+ assert_eq!(vsock.is_valid(&config), true);
+
+ config.queue_size = 0;
+ assert_eq!(vsock.is_valid(&config), false);
+ config.queue_size = 31;
+ assert_eq!(vsock.is_valid(&config), false);
+ config.queue_size = 33;
+ assert_eq!(vsock.is_valid(&config), false);
+ }
+
+ #[test]
+ fn test_vsock_ioctls() {
+ let m = GuestMemoryMmap::from_ranges(&[(GuestAddress(0), 0x10_0000)]).unwrap();
+ let vsock = Vsock::new(&m).unwrap();
+
+ let features = vsock.get_features().unwrap();
+ vsock.set_features(features).unwrap();
+
+ vsock.set_owner().unwrap();
+
+ vsock.set_mem_table(&[]).unwrap_err();
+
+ /*
+ let region = VhostUserMemoryRegionInfo {
+ guest_phys_addr: 0x0,
+ memory_size: 0x10_0000,
+ userspace_addr: 0,
+ mmap_offset: 0,
+ mmap_handle: -1,
+ };
+ vsock.set_mem_table(&[region]).unwrap_err();
+ */
+
+ let region = VhostUserMemoryRegionInfo {
+ guest_phys_addr: 0x0,
+ memory_size: 0x10_0000,
+ userspace_addr: m.get_host_address(GuestAddress(0x0)).unwrap() as u64,
+ mmap_offset: 0,
+ mmap_handle: -1,
+ };
+ vsock.set_mem_table(&[region]).unwrap();
+
+ vsock.set_log_base(0x4000, Some(1)).unwrap_err();
+ vsock.set_log_base(0x4000, None).unwrap();
+
+ let eventfd = EventFd::new(0).unwrap();
+ vsock.set_log_fd(eventfd.as_raw_fd()).unwrap();
+
+ vsock.set_vring_num(0, 32).unwrap();
+
+ let config = VringConfigData {
+ queue_max_size: 32,
+ queue_size: 32,
+ flags: 0,
+ desc_table_addr: 0x1000,
+ used_ring_addr: 0x2000,
+ avail_ring_addr: 0x3000,
+ log_addr: None,
+ };
+ vsock.set_vring_addr(0, &config).unwrap();
+ vsock.set_vring_base(0, 1).unwrap();
+ vsock.set_vring_call(0, &eventfd).unwrap();
+ vsock.set_vring_kick(0, &eventfd).unwrap();
+ vsock.set_vring_err(0, &eventfd).unwrap();
+ assert_eq!(vsock.get_vring_base(0).unwrap(), 1);
+ vsock.set_guest_cid(0xdead).unwrap();
+ //vsock.start().unwrap();
+ //vsock.stop().unwrap();
+ }
+}