Skip to content

Commit afc7b69

Browse files
bergwolfjiangliu
authored andcommitted
transport: enable virtiofs and vhost-user-fs
Bring back virtiofs and vhost-user-fs support so that consumers can use the latest code to run virtiofs and vhost-user-fs. Signed-off-by: Peng Tao <[email protected]>
1 parent 82d581a commit afc7b69

File tree

7 files changed

+67
-50
lines changed

7 files changed

+67
-50
lines changed

Cargo.toml

+8-8
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,23 @@ libc = ">=0.2.68"
2626
log = ">=0.4.6"
2727
nix = "0.22"
2828
#ringbahn = { version = "0.0.0-experimental.3", optional = true }
29-
vmm-sys-util = { version = "0.8", optional = true }
30-
vm-memory = "0.6"
31-
#vm-virtio = { git = "https://github.com/cloud-hypervisor/vm-virtio.git", branch = "dragonball", optional = true }
32-
#vhost = { version = "0.1", optional = true }
29+
vmm-sys-util = { version = ">=0.8", optional = true }
30+
vm-memory = { version = "0.6", features = ["backend-atomic"] }
31+
virtio-queue = { git = "https://github.com/rust-vmm/vm-virtio", rev = "6013dd9", optional = true }
32+
vhost = { version = "0.2", features = ["vhost-user-slave"], optional = true }
3333

3434
[dev-dependencies]
3535
futures = { version = "0.3.0", features = ["thread-pool"]}
3636
stderrlog = "0.4"
37-
vmm-sys-util = "0.8"
38-
vm-memory = { version = "0.6", features = ["backend-mmap"] }
37+
vmm-sys-util = ">=0.8"
38+
vm-memory = { version = "0.6", features = ["backend-mmap", "backend-atomic"] }
3939

4040
[features]
4141
default = ["fusedev"]
4242
#async-io = ["async-trait", "futures", "iou", "ringbahn"]
4343
fusedev = ["vmm-sys-util"]
44-
#virtiofs = ["vm-virtio"]
45-
#vhost-user-fs = ["virtiofs", "vhost-rs/vhost-user-slave"]
44+
virtiofs = ["virtio-queue"]
45+
vhost-user-fs = ["virtiofs", "vhost"]
4646

4747
[patch."registry+https://github.com/rust-lang/crates.io-index"]
4848
#ringbahn = { git = "https://github.com/jiangliu/ringbahn.git", branch = "enhance", optional = true }

Makefile

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@ current_dir := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
22

33
build:
44
cargo build --features="fusedev"
5+
cargo build --features="virtiofs"
6+
cargo build --features="vhost-user-fs"
57

68
check: build
79
cargo fmt -- --check
810
cargo clippy --features="fusedev" -- -Dclippy::all
11+
cargo clippy --features="virtiofs" -- -Dclippy::all
12+
cargo clippy --features="vhost-user-fs" -- -Dclippy::all
13+
cargo test --features="virtiofs" -- --nocapture --skip integration
914
cargo test --features="fusedev" -- --nocapture --skip integration
15+
cargo test --features="vhost-user-fs" -- --nocapture --skip integration
1016

1117
smoke: check
1218
cargo test --features="fusedev" -- --nocapture
1319

1420
docker-smoke:
15-
docker run --rm --privileged -v ${current_dir}:/fuse-rs rust:slim sh -c "apt update;apt install -y make; rustup component add clippy rustfmt; cd /fuse-rs; make smoke"
21+
docker run --rm --privileged -v ${current_dir}:/fuse-rs rust:1.52.1 sh -c "rustup component add clippy rustfmt; cd /fuse-rs; make smoke"

src/api/server/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ pub trait MetricsHook {
162162
}
163163

164164
struct SrvContext<'a, F, D: AsyncDrive = AsyncDriver, S: BitmapSlice = ()> {
165+
#[allow(dead_code)]
165166
drive: Option<D>,
166167
in_header: InHeader,
167168
context: Context,

src/lib.rs

-4
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ extern crate bitflags;
5050
extern crate libc;
5151
#[macro_use]
5252
extern crate log;
53-
#[cfg(feature = "vhost-user-fs")]
54-
extern crate vhost;
5553
extern crate vm_memory;
56-
#[cfg(feature = "virtiofs")]
57-
extern crate vm_virtio;
5854

5955
use std::ffi::{CStr, FromBytesWithNulError};
6056
use std::io::ErrorKind;

src/transport/virtiofs/mod.rs

+48-34
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,15 @@ use std::cmp;
4242
use std::collections::VecDeque;
4343
use std::fmt;
4444
use std::io::{self, IoSlice, Write};
45+
use std::ops::Deref;
4546
use std::ptr::copy_nonoverlapping;
4647

47-
use vm_memory::bitmap::{BitmapSlice, MS};
48-
use vm_memory::{ByteValued, GuestMemory, GuestMemoryError, VolatileMemoryError, VolatileSlice};
49-
use vm_virtio::{DescriptorChain, Error as QueueError};
48+
use virtio_queue::{DescriptorChain, Error as QueueError};
49+
use vm_memory::bitmap::BitmapSlice;
50+
use vm_memory::{
51+
Address, ByteValued, GuestMemory, GuestMemoryAtomic, GuestMemoryError, GuestMemoryMmap,
52+
GuestMemoryRegion, VolatileMemory, VolatileMemoryError, VolatileSlice,
53+
};
5054

5155
use super::{FileReadWriteVolatile, IoBuffers, Reader};
5256

@@ -99,19 +103,14 @@ pub type Result<T> = std::result::Result<T, Error>;
99103

100104
impl std::error::Error for Error {}
101105

102-
impl<'a, M: GuestMemory> Reader<'a, M> {
106+
impl<'a> Reader<'a> {
103107
/// Construct a new Reader wrapper over `desc_chain`.
104-
#[allow(clippy::new_ret_no_self)]
105-
pub fn new(mem: &'a M, desc_chain: DescriptorChain<'a, M>) -> Result<Reader<'a, MS<'a, M>>> {
108+
pub fn new(
109+
mem: &'a GuestMemoryMmap,
110+
desc_chain: DescriptorChain<GuestMemoryAtomic<GuestMemoryMmap>>,
111+
) -> Result<Reader<'a>> {
106112
let mut total_len: usize = 0;
107-
let chain = if desc_chain.is_indirect() {
108-
desc_chain
109-
.new_from_indirect()
110-
.map_err(Error::ConvertIndirectDescriptor)?
111-
} else {
112-
desc_chain
113-
};
114-
let buffers = chain
113+
let buffers = desc_chain
115114
.readable()
116115
.map(|desc| {
117116
// Verify that summing the descriptor sizes does not overflow.
@@ -121,10 +120,19 @@ impl<'a, M: GuestMemory> Reader<'a, M> {
121120
.checked_add(desc.len() as usize)
122121
.ok_or(Error::DescriptorChainOverflow)?;
123122

124-
mem.get_slice(desc.addr(), desc.len() as usize)
125-
.map_err(Error::GuestMemoryError)
123+
let region = mem
124+
.find_region(desc.addr())
125+
.ok_or(Error::FindMemoryRegion)?;
126+
let offset = desc
127+
.addr()
128+
.checked_sub(region.start_addr().raw_value())
129+
.unwrap();
130+
region
131+
.deref()
132+
.get_slice(offset.raw_value() as usize, desc.len() as usize)
133+
.map_err(Error::VolatileMemoryError)
126134
})
127-
.collect::<Result<VecDeque<VolatileSlice<'a, MS<M>>>>>()?;
135+
.collect::<Result<VecDeque<VolatileSlice<'a>>>>()?;
128136

129137
Ok(Reader {
130138
buffers: IoBuffers {
@@ -147,19 +155,14 @@ pub struct Writer<'a, S = ()> {
147155
buffers: IoBuffers<'a, S>,
148156
}
149157

150-
impl<'a, M: GuestMemory> Writer<'a, M> {
158+
impl<'a> Writer<'a> {
151159
/// Construct a new Writer wrapper over `desc_chain`.
152-
#[allow(clippy::new_ret_no_self)]
153-
pub fn new(mem: &'a M, desc_chain: DescriptorChain<'a, M>) -> Result<Writer<'a, MS<'a, M>>> {
160+
pub fn new(
161+
mem: &'a GuestMemoryMmap,
162+
desc_chain: DescriptorChain<GuestMemoryAtomic<GuestMemoryMmap>>,
163+
) -> Result<Writer<'a>> {
154164
let mut total_len: usize = 0;
155-
let chain = if desc_chain.is_indirect() {
156-
desc_chain
157-
.new_from_indirect()
158-
.map_err(Error::ConvertIndirectDescriptor)?
159-
} else {
160-
desc_chain
161-
};
162-
let buffers = chain
165+
let buffers = desc_chain
163166
.writable()
164167
.map(|desc| {
165168
// Verify that summing the descriptor sizes does not overflow.
@@ -169,10 +172,19 @@ impl<'a, M: GuestMemory> Writer<'a, M> {
169172
.checked_add(desc.len() as usize)
170173
.ok_or(Error::DescriptorChainOverflow)?;
171174

172-
mem.get_slice(desc.addr(), desc.len() as usize)
173-
.map_err(Error::GuestMemoryError)
175+
let region = mem
176+
.find_region(desc.addr())
177+
.ok_or(Error::FindMemoryRegion)?;
178+
let offset = desc
179+
.addr()
180+
.checked_sub(region.start_addr().raw_value())
181+
.unwrap();
182+
region
183+
.deref()
184+
.get_slice(offset.raw_value() as usize, desc.len() as usize)
185+
.map_err(Error::VolatileMemoryError)
174186
})
175-
.collect::<Result<VecDeque<VolatileSlice<'a, MS<M>>>>>()?;
187+
.collect::<Result<VecDeque<VolatileSlice<'a>>>>()?;
176188

177189
Ok(Writer {
178190
buffers: IoBuffers {
@@ -437,7 +449,9 @@ mod async_io {
437449
}
438450
}
439451

440-
#[cfg(test)]
452+
/// Disabled since vm-virtio doesn't export any DescriptorChain constructors.
453+
/// Should re-enable once it does.
454+
#[cfg(testff)]
441455
mod tests {
442456
use super::*;
443457
use std::io::{Read, Seek, SeekFrom, Write};
@@ -472,7 +486,7 @@ mod tests {
472486
mut buffers_start_addr: GuestAddress,
473487
descriptors: Vec<(DescriptorType, u32)>,
474488
spaces_between_regions: u32,
475-
) -> Result<DescriptorChain<GuestMemoryMmap>> {
489+
) -> Result<DescriptorChain<GuestMemoryAtomic<GuestMemoryMmap>>> {
476490
let descriptors_len = descriptors.len();
477491
for (index, (type_, size)) in descriptors.into_iter().enumerate() {
478492
let mut flags = 0;
@@ -504,7 +518,7 @@ mod tests {
504518
);
505519
}
506520

507-
DescriptorChain::checked_new(memory, descriptor_array_addr, 0x100, 0)
521+
DescriptorChain::<&GuestMemoryMmap>::new(memory, descriptor_array_addr, 0x100, 0)
508522
.ok_or(Error::InvalidChain)
509523
}
510524

tests/example/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
//
33
// SPDX-License-Identifier: Apache-2.0
44

5-
#[cfg(feature = "fusedev")]
5+
#[cfg(all(feature = "fusedev", not(feature = "virtiofs")))]
66
pub(crate) mod passthroughfs;

tests/smoke.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
// SPDX-License-Identifier: Apache-2.0
44
//
55

6-
#[cfg(feature = "fusedev")]
6+
#[cfg(all(feature = "fusedev", not(feature = "virtiofs")))]
77
#[macro_use]
88
extern crate log;
99

1010
mod example;
1111

12-
#[cfg(feature = "fusedev")]
12+
#[cfg(all(feature = "fusedev", not(feature = "virtiofs")))]
1313
mod fusedev_tests {
1414
extern crate stderrlog;
1515

0 commit comments

Comments
 (0)