summaryrefslogtreecommitdiff
path: root/src/vhost_kern/vsock.rs
diff options
context:
space:
mode:
authorLiu Jiang <gerry@linux.alibaba.com>2021-02-19 22:07:48 +0800
committerSergio Lopez <slp@sinrega.org>2021-03-01 12:50:56 +0100
commit07eee11be3aa7b82fbbb23a6bf4d933e2fa4ae97 (patch)
treefee36697081e8b18f28dbe8c69c4d77abea8cad6 /src/vhost_kern/vsock.rs
parent97421f754d8d38508687926b5045c4ed1bd6c107 (diff)
downloadvmm_vhost-07eee11be3aa7b82fbbb23a6bf4d933e2fa4ae97.tar.gz
vhost_kern: add more unit test cases
Add more unit test cases for vhost-kern submodule. Signed-off-by: Liu Jiang <gerry@linux.alibaba.com>
Diffstat (limited to 'src/vhost_kern/vsock.rs')
-rw-r--r--src/vhost_kern/vsock.rs112
1 files changed, 108 insertions, 4 deletions
diff --git a/src/vhost_kern/vsock.rs b/src/vhost_kern/vsock.rs
index c4149bd..7ccf670 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};
+
const VHOST_PATH: &str = "/dev/vhost-vsock";
/// Handle for running VHOST_VSOCK ioctls.
@@ -79,3 +80,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();
+ }
+}