Skip to content

Commit 78f062a

Browse files
vireshkjiangliu
authored andcommitted
vhost-user-backend: Add support for Xen memory mappings
Migrate to a newer version of the vhost and other dependencies and add support for xen memory mappings. Add a corresponding xen feature for vhost-user-backend crate. Signed-off-by: Viresh Kumar <[email protected]>
1 parent b128901 commit 78f062a

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

crates/vhost-user-backend/Cargo.toml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,20 @@ repository = "https://github.com/rust-vmm/vhost"
88
edition = "2018"
99
license = "Apache-2.0"
1010

11+
[features]
12+
xen = ["vm-memory/xen", "vhost/xen"]
13+
1114
[dependencies]
1215
libc = "0.2.39"
1316
log = "0.4.17"
14-
vhost = { path = "../vhost", version = "0.7", features = ["vhost-user-slave"] }
15-
virtio-bindings = "0.2.0"
16-
virtio-queue = "0.8.0"
17-
vm-memory = { version = "0.11.0", features = ["backend-mmap", "backend-atomic"] }
17+
vhost = { path = "../vhost", version = "0.8", features = ["vhost-user-slave"] }
18+
virtio-bindings = "0.2.1"
19+
virtio-queue = "0.9.0"
20+
vm-memory = { version = "0.12.0", features = ["backend-mmap", "backend-atomic"] }
1821
vmm-sys-util = "0.11.0"
1922

2023
[dev-dependencies]
2124
nix = "0.26"
22-
vhost = { path = "../vhost", version = "0.7", features = ["test-utils", "vhost-user-master", "vhost-user-slave"] }
23-
vm-memory = { version = "0.11.0", features = ["backend-mmap", "backend-atomic", "backend-bitmap"] }
25+
vhost = { path = "../vhost", version = "0.8", features = ["test-utils", "vhost-user-master", "vhost-user-slave"] }
26+
vm-memory = { version = "0.12.0", features = ["backend-mmap", "backend-atomic", "backend-bitmap"] }
2427
tempfile = "3.2.0"

crates/vhost-user-backend/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ impl VhostUserBackendMut for VhostUserService {
9898
}
9999
```
100100

101+
## Xen support
102+
103+
Supporting Xen requires special handling while mapping the guest memory. The
104+
`vm-memory` crate implements xen memory mapping support via a separate feature
105+
`xen`, and this crate uses the same feature name to enable Xen support.
106+
107+
Also, for xen mappings, the memory regions passed by the frontend contains few
108+
extra fields as described in the vhost-user protocol documentation.
109+
110+
It was decided by the `rust-vmm` maintainers to keep the interface simple and
111+
build the crate for either standard Unix memory mapping or Xen, and not both.
112+
101113
## License
102114

103115
This project is licensed under

crates/vhost-user-backend/src/handler.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use virtio_bindings::bindings::virtio_ring::VIRTIO_RING_F_EVENT_IDX;
2222
use virtio_queue::{Error as VirtQueError, QueueT};
2323
use vm_memory::bitmap::Bitmap;
2424
use vm_memory::mmap::NewBitmap;
25-
use vm_memory::{FileOffset, GuestAddress, GuestAddressSpace, GuestMemoryMmap, GuestRegionMmap};
25+
use vm_memory::{GuestAddress, GuestAddressSpace, GuestMemoryMmap, GuestRegionMmap};
2626
use vmm_sys_util::epoll::EventSet;
2727

2828
use super::backend::VhostUserBackend;
@@ -277,23 +277,27 @@ where
277277
) -> VhostUserResult<()> {
278278
// We need to create tuple of ranges from the list of VhostUserMemoryRegion
279279
// that we get from the caller.
280-
let mut regions: Vec<(GuestAddress, usize, Option<FileOffset>)> = Vec::new();
280+
let mut regions = Vec::new();
281281
let mut mappings: Vec<AddrMapping> = Vec::new();
282282

283283
for (region, file) in ctx.iter().zip(files) {
284-
let g_addr = GuestAddress(region.guest_phys_addr);
285-
let len = region.memory_size as usize;
286-
let f_off = FileOffset::new(file, region.mmap_offset);
287-
288-
regions.push((g_addr, len, Some(f_off)));
284+
regions.push(
285+
GuestRegionMmap::new(
286+
region.mmap_region(file)?,
287+
GuestAddress(region.guest_phys_addr),
288+
)
289+
.map_err(|e| {
290+
VhostUserError::ReqHandlerError(io::Error::new(io::ErrorKind::Other, e))
291+
})?,
292+
);
289293
mappings.push(AddrMapping {
290294
vmm_addr: region.user_addr,
291295
size: region.memory_size,
292296
gpa_base: region.guest_phys_addr,
293297
});
294298
}
295299

296-
let mem = GuestMemoryMmap::from_ranges_with_files(regions).map_err(|e| {
300+
let mem = GuestMemoryMmap::from_regions(regions).map_err(|e| {
297301
VhostUserError::ReqHandlerError(io::Error::new(io::ErrorKind::Other, e))
298302
})?;
299303

@@ -526,10 +530,9 @@ where
526530
file: File,
527531
) -> VhostUserResult<()> {
528532
let guest_region = Arc::new(
529-
GuestRegionMmap::from_range(
533+
GuestRegionMmap::new(
534+
region.mmap_region(file)?,
530535
GuestAddress(region.guest_phys_addr),
531-
region.memory_size as usize,
532-
Some(FileOffset::new(file, region.mmap_offset)),
533536
)
534537
.map_err(|e| {
535538
VhostUserError::ReqHandlerError(io::Error::new(io::ErrorKind::Other, e))

0 commit comments

Comments
 (0)