summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDylan Reid <dgreid@chromium.org>2020-12-16 00:27:03 -0800
committerCommit Bot <commit-bot@chromium.org>2021-03-11 08:06:30 +0000
commit0323efc01ebab00f4bf8090687a6443f7a0edaa0 (patch)
tree7536e6caa6e28fd8ef0a3410ed779c89f0d5af06
parentcc3810e49b06c1c1b2311b2be232d3368d075aaf (diff)
downloadvmm_vhost-0323efc01ebab00f4bf8090687a6443f7a0edaa0.tar.gz
use sys_util and tempfile from crosvm
Adjust dependencies to use the crosvm-local crates. The tempfile interface is subtly different use the crosvm implementation to avoid a dependency on `rand`. Using a temporary directory avoids any chance of name collision with using rand to generate a string for a socket name. Some of the ScmSocket interfaces have changed and needed updating on both ends. BUG=b:181227406 TEST=cargo test Change-Id: Idee69f0c561642954ac1fa05c2951949e3b74fb5 Cq-Depend: chromium:2740533 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/rust-vmm/vhost/+/2717529 Tested-by: Dylan Reid <dgreid@chromium.org> Commit-Queue: Keiichi Watanabe <keiichiw@chromium.org> Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org> Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
-rw-r--r--Cargo.toml5
-rw-r--r--src/backend.rs4
-rw-r--r--src/lib.rs2
-rw-r--r--src/vhost_kern/mod.rs4
-rw-r--r--src/vhost_kern/vsock.rs6
-rw-r--r--src/vhost_user/connection.rs46
-rw-r--r--src/vhost_user/master.rs35
-rw-r--r--src/vhost_user/mod.rs17
8 files changed, 64 insertions, 55 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 0cd15f7..917ea25 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,5 +1,5 @@
[package]
-name = "vhost"
+name = "vmm_vhost"
version = "0.1.0"
keywords = ["vhost", "vhost-user", "virtio", "vdpa"]
description = "a pure rust library for vdpa, vhost and vhost-user"
@@ -22,7 +22,8 @@ vhost-user-slave = ["vhost-user"]
bitflags = ">=1.0.1"
libc = ">=0.2.39"
-vmm-sys-util = ">=0.3.1"
+sys_util = { path = "../../../platform/crosvm/sys_util" } # provided by ebuild
+tempfile = { path = "../../../platform/crosvm/tempfile" } # provided by ebuild
vm-memory = { version = "0.2.0", optional = true }
[dev-dependencies]
diff --git a/src/backend.rs b/src/backend.rs
index 89fde50..1ae306f 100644
--- a/src/backend.rs
+++ b/src/backend.rs
@@ -13,7 +13,7 @@ use std::cell::RefCell;
use std::os::unix::io::RawFd;
use std::sync::RwLock;
-use vmm_sys_util::eventfd::EventFd;
+use sys_util::EventFd;
use super::Result;
@@ -470,7 +470,7 @@ mod tests {
b.set_vring_base(1, 2).unwrap();
assert_eq!(b.get_vring_base(1).unwrap(), 2);
- let eventfd = EventFd::new(0).unwrap();
+ let eventfd = EventFd::new().unwrap();
b.set_vring_call(1, &eventfd).unwrap();
b.set_vring_kick(1, &eventfd).unwrap();
b.set_vring_err(1, &eventfd).unwrap();
diff --git a/src/lib.rs b/src/lib.rs
index b7ed15c..a755f4f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -35,7 +35,7 @@
#[cfg_attr(feature = "vhost-user", macro_use)]
extern crate bitflags;
#[cfg_attr(feature = "vhost-kern", macro_use)]
-extern crate vmm_sys_util;
+extern crate sys_util;
mod backend;
pub use backend::*;
diff --git a/src/vhost_kern/mod.rs b/src/vhost_kern/mod.rs
index f82cbfc..5daca51 100644
--- a/src/vhost_kern/mod.rs
+++ b/src/vhost_kern/mod.rs
@@ -13,9 +13,9 @@
use std::os::unix::io::{AsRawFd, RawFd};
+use sys_util::ioctl::{ioctl, ioctl_with_mut_ref, ioctl_with_ptr, ioctl_with_ref};
+use sys_util::EventFd;
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};
use super::{
Error, Result, VhostBackend, VhostUserMemoryRegionInfo, VringConfigData,
diff --git a/src/vhost_kern/vsock.rs b/src/vhost_kern/vsock.rs
index 65f89e4..ace5a1f 100644
--- a/src/vhost_kern/vsock.rs
+++ b/src/vhost_kern/vsock.rs
@@ -11,8 +11,8 @@ use std::fs::{File, OpenOptions};
use std::os::unix::fs::OpenOptionsExt;
use std::os::unix::io::{AsRawFd, RawFd};
+use sys_util::ioctl_with_ref;
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};
@@ -78,8 +78,8 @@ impl<AS: GuestAddressSpace> AsRawFd for Vsock<AS> {
#[cfg(test)]
mod tests {
+ use sys_util::EventFd;
use vm_memory::{GuestAddress, GuestMemory, GuestMemoryMmap};
- use vmm_sys_util::eventfd::EventFd;
use super::*;
use crate::{VhostBackend, VhostUserMemoryRegionInfo, VringConfigData};
@@ -153,7 +153,7 @@ mod tests {
vsock.set_log_base(0x4000, Some(1)).unwrap_err();
vsock.set_log_base(0x4000, None).unwrap();
- let eventfd = EventFd::new(0).unwrap();
+ let eventfd = EventFd::new().unwrap();
vsock.set_log_fd(eventfd.as_raw_fd()).unwrap();
vsock.set_vring_num(0, 32).unwrap();
diff --git a/src/vhost_user/connection.rs b/src/vhost_user/connection.rs
index fef7dac..f92db45 100644
--- a/src/vhost_user/connection.rs
+++ b/src/vhost_user/connection.rs
@@ -13,7 +13,7 @@ use std::path::{Path, PathBuf};
use std::{mem, slice};
use libc::{c_void, iovec};
-use vmm_sys_util::sock_ctrl_msg::ScmSocket;
+use sys_util::ScmSocket;
use super::message::*;
use super::{Error, Result};
@@ -126,7 +126,7 @@ impl<R: Req> Endpoint<R> {
Some(rfds) => rfds,
_ => &[],
};
- self.sock.send_with_fds(iovs, rfds).map_err(Into::into)
+ self.sock.send_bufs_with_fds(iovs, rfds).map_err(Into::into)
}
/// Sends all bytes from scatter-gather vectors over the socket with optional attached file
@@ -296,11 +296,7 @@ impl<R: Req> Endpoint<R> {
/// * - SocketError: other socket related errors.
pub fn recv_data(&mut self, len: usize) -> Result<(usize, Vec<u8>)> {
let mut rbuf = vec![0u8; len];
- let mut iovs = [iovec {
- iov_base: rbuf.as_mut_ptr() as *mut c_void,
- iov_len: len,
- }];
- let (bytes, _) = self.sock.recv_with_fds(&mut iovs, &mut [])?;
+ let (bytes, _) = self.sock.recv_with_fds(&mut rbuf[..], &mut [])?;
Ok((bytes, rbuf))
}
@@ -323,7 +319,7 @@ impl<R: Req> Endpoint<R> {
/// * - SocketError: other socket related errors.
pub fn recv_into_iovec(&mut self, iovs: &mut [iovec]) -> Result<(usize, Option<Vec<RawFd>>)> {
let mut fd_array = vec![0; MAX_ATTACHED_FD_ENTRIES];
- let (bytes, fds) = self.sock.recv_with_fds(iovs, &mut fd_array)?;
+ let (bytes, fds) = self.sock.recv_iovecs_with_fds(iovs, &mut fd_array)?;
let rfds = match fds {
0 => None,
n => {
@@ -611,19 +607,17 @@ mod tests {
use std::fs::File;
use std::io::{Read, Seek, SeekFrom, Write};
use std::os::unix::io::FromRawFd;
- use vmm_sys_util::rand::rand_alphanumerics;
- use vmm_sys_util::tempfile::TempFile;
-
- fn temp_path() -> PathBuf {
- PathBuf::from(format!(
- "/tmp/vhost_test_{}",
- rand_alphanumerics(8).to_str().unwrap()
- ))
+ use tempfile::{tempfile, Builder, TempDir};
+
+ fn temp_dir() -> TempDir {
+ Builder::new().prefix("/tmp/vhost_test").tempdir().unwrap()
}
#[test]
fn create_listener() {
- let path = temp_path();
+ let dir = temp_dir();
+ let mut path = dir.path().to_owned();
+ path.push("sock");
let listener = Listener::new(&path, true).unwrap();
assert!(listener.as_raw_fd() > 0);
@@ -631,7 +625,9 @@ mod tests {
#[test]
fn accept_connection() {
- let path = temp_path();
+ let dir = temp_dir();
+ let mut path = dir.path().to_owned();
+ path.push("sock");
let listener = Listener::new(&path, true).unwrap();
listener.set_nonblocking(true).unwrap();
@@ -642,7 +638,9 @@ mod tests {
#[test]
fn send_data() {
- let path = temp_path();
+ let dir = temp_dir();
+ let mut path = dir.path().to_owned();
+ path.push("sock");
let listener = Listener::new(&path, true).unwrap();
listener.set_nonblocking(true).unwrap();
let mut master = Endpoint::<MasterReq>::connect(&path).unwrap();
@@ -668,14 +666,16 @@ mod tests {
#[test]
fn send_fd() {
- let path = temp_path();
+ let dir = temp_dir();
+ let mut path = dir.path().to_owned();
+ path.push("sock");
let listener = Listener::new(&path, true).unwrap();
listener.set_nonblocking(true).unwrap();
let mut master = Endpoint::<MasterReq>::connect(&path).unwrap();
let sock = listener.accept().unwrap().unwrap();
let mut slave = Endpoint::<MasterReq>::from_stream(sock);
- let mut fd = TempFile::new().unwrap().into_file();
+ let mut fd = tempfile().unwrap();
write!(fd, "test").unwrap();
// Normal case for sending/receiving file descriptors
@@ -822,7 +822,9 @@ mod tests {
#[test]
fn send_recv() {
- let path = temp_path();
+ let dir = temp_dir();
+ let mut path = dir.path().to_owned();
+ path.push("sock");
let listener = Listener::new(&path, true).unwrap();
listener.set_nonblocking(true).unwrap();
let mut master = Endpoint::<MasterReq>::connect(&path).unwrap();
diff --git a/src/vhost_user/master.rs b/src/vhost_user/master.rs
index f9da535..f098982 100644
--- a/src/vhost_user/master.rs
+++ b/src/vhost_user/master.rs
@@ -9,7 +9,7 @@ use std::os::unix::net::UnixStream;
use std::path::Path;
use std::sync::{Arc, Mutex, MutexGuard};
-use vmm_sys_util::eventfd::EventFd;
+use sys_util::EventFd;
use super::connection::Endpoint;
use super::message::*;
@@ -645,15 +645,12 @@ impl MasterInternal {
mod tests {
use super::super::connection::Listener;
use super::*;
- use vmm_sys_util::rand::rand_alphanumerics;
+ use tempfile::{Builder, TempDir};
use std::path::PathBuf;
- fn temp_path() -> PathBuf {
- PathBuf::from(format!(
- "/tmp/vhost_test_{}",
- rand_alphanumerics(8).to_str().unwrap()
- ))
+ fn temp_dir() -> TempDir {
+ Builder::new().prefix("/tmp/vhost_test").tempdir().unwrap()
}
fn create_pair<P: AsRef<Path>>(path: P) -> (Master, Endpoint<MasterReq>) {
@@ -666,7 +663,9 @@ mod tests {
#[test]
fn create_master() {
- let path = temp_path();
+ let dir = temp_dir();
+ let mut path = dir.path().to_owned();
+ path.push("sock");
let listener = Listener::new(&path, true).unwrap();
listener.set_nonblocking(true).unwrap();
@@ -693,7 +692,9 @@ mod tests {
#[test]
fn test_create_failure() {
- let path = temp_path();
+ let dir = temp_dir();
+ let mut path = dir.path().to_owned();
+ path.push("sock");
let _ = Listener::new(&path, true).unwrap();
let _ = Listener::new(&path, false).is_err();
assert!(Master::connect(&path, 1).is_err());
@@ -708,7 +709,9 @@ mod tests {
#[test]
fn test_features() {
- let path = temp_path();
+ let dir = temp_dir();
+ let mut path = dir.path().to_owned();
+ path.push("sock");
let (master, mut peer) = create_pair(&path);
master.set_owner().unwrap();
@@ -743,7 +746,9 @@ mod tests {
#[test]
fn test_protocol_features() {
- let path = temp_path();
+ let dir = temp_dir();
+ let mut path = dir.path().to_owned();
+ path.push("sock");
let (mut master, mut peer) = create_pair(&path);
master.set_owner().unwrap();
@@ -794,7 +799,9 @@ mod tests {
#[test]
fn test_master_set_config_negative() {
- let path = temp_path();
+ let dir = temp_dir();
+ let mut path = dir.path().to_owned();
+ path.push("sock");
let (mut master, _peer) = create_pair(&path);
let buf = vec![0x0; MAX_MSG_SIZE + 1];
@@ -835,7 +842,9 @@ mod tests {
}
fn create_pair2() -> (Master, Endpoint<MasterReq>) {
- let path = temp_path();
+ let dir = temp_dir();
+ let mut path = dir.path().to_owned();
+ path.push("sock");
let (master, peer) = create_pair(&path);
{
diff --git a/src/vhost_user/mod.rs b/src/vhost_user/mod.rs
index 00382f4..3aef93d 100644
--- a/src/vhost_user/mod.rs
+++ b/src/vhost_user/mod.rs
@@ -130,10 +130,10 @@ impl Error {
}
}
-impl std::convert::From<vmm_sys_util::errno::Error> for Error {
+impl std::convert::From<sys_util::Error> for Error {
/// Convert raw socket errors into meaningful vhost-user errors.
///
- /// The vmm_sys_util::errno::Error is a simple wrapper over the raw errno, which doesn't means
+ /// The sys_util::Error is a simple wrapper over the raw errno, which doesn't means
/// much to the vhost-user connection manager. So convert it into meaningful errors to simplify
/// the connection manager logic.
///
@@ -142,7 +142,7 @@ impl std::convert::From<vmm_sys_util::errno::Error> for Error {
/// * - Error::SocketBroken: the underline socket is broken.
/// * - Error::SocketError: other socket related errors.
#[allow(unreachable_patterns)] // EWOULDBLOCK equals to EGAIN on linux
- fn from(err: vmm_sys_util::errno::Error) -> Self {
+ fn from(err: sys_util::Error) -> Self {
match err.errno() {
// The socket is marked nonblocking and the requested operation would block.
libc::EAGAIN => Error::SocketRetry(IOError::from_raw_os_error(libc::EAGAIN)),
@@ -184,19 +184,16 @@ mod tests {
use std::path::{Path, PathBuf};
use std::sync::{Arc, Barrier, Mutex};
use std::thread;
- use vmm_sys_util::rand::rand_alphanumerics;
use super::dummy_slave::{DummySlaveReqHandler, VIRTIO_FEATURES};
use super::message::*;
use super::*;
use crate::backend::VhostBackend;
use crate::{VhostUserMemoryRegionInfo, VringConfigData};
+ use tempfile::Builder;
fn temp_path() -> PathBuf {
- PathBuf::from(format!(
- "/tmp/vhost_test_{}",
- rand_alphanumerics(8).to_str().unwrap()
- ))
+ Builder::new().prefix("/tmp/vhost_test").path().unwrap()
}
fn create_slave<P, S>(path: P, backend: Arc<S>) -> (Master, SlaveReqHandler<S>)
@@ -351,7 +348,7 @@ mod tests {
let num = master.get_queue_num().unwrap();
assert_eq!(num, 2);
- let eventfd = vmm_sys_util::eventfd::EventFd::new(0).unwrap();
+ let eventfd = sys_util::EventFd::new().unwrap();
let mem = [VhostUserMemoryRegionInfo {
guest_phys_addr: 0,
memory_size: 0x10_0000,
@@ -419,7 +416,7 @@ mod tests {
#[test]
fn test_error_from_sys_util_error() {
- let e: Error = vmm_sys_util::errno::Error::new(libc::EAGAIN).into();
+ let e: Error = sys_util::Error::new(libc::EAGAIN).into();
if let Error::SocketRetry(e1) = e {
assert_eq!(e1.raw_os_error().unwrap(), libc::EAGAIN);
} else {