Skip to content

Commit fc2b99d

Browse files
vireshkstefano-garzarella
authored andcommitted
vhost: Add VhostUserMemoryRegionInfo::new() for tests
A lot of tests are creating objects of this structure directly, it would be better if they all use a function instead to do so. This will also be beneficial for future commits where more fields will be added to this structure. Signed-off-by: Viresh Kumar <[email protected]>
1 parent 26fd3ce commit fc2b99d

File tree

8 files changed

+61
-50
lines changed

8 files changed

+61
-50
lines changed

crates/vhost-user-backend/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ vmm-sys-util = "0.11.0"
1919

2020
[dev-dependencies]
2121
nix = "0.26"
22-
vhost = { path = "../vhost", version = "0.7", features = ["vhost-user-master", "vhost-user-slave"] }
22+
vhost = { path = "../vhost", version = "0.7", features = ["test-utils", "vhost-user-master", "vhost-user-slave"] }
2323
vm-memory = { version = "0.11.0", features = ["backend-mmap", "backend-atomic", "backend-bitmap"] }
2424
tempfile = "3.2.0"

crates/vhost-user-backend/tests/vhost-user-server.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,13 @@ fn vhost_user_client(path: &Path, barrier: Arc<Barrier>) {
166166
let addr = mem.get_host_address(GuestAddress(0x100000)).unwrap() as u64;
167167
let reg = mem.find_region(GuestAddress(0x100000)).unwrap();
168168
let fd = reg.file_offset().unwrap();
169-
let regions = [VhostUserMemoryRegionInfo {
170-
guest_phys_addr: 0x100000,
171-
memory_size: 0x100000,
172-
userspace_addr: addr,
173-
mmap_offset: 0,
174-
mmap_handle: fd.file().as_raw_fd(),
175-
}];
169+
let regions = [VhostUserMemoryRegionInfo::new(
170+
0x100000,
171+
0x100000,
172+
addr,
173+
0,
174+
fd.file().as_raw_fd(),
175+
)];
176176
master.set_mem_table(&regions).unwrap();
177177

178178
master.set_vring_num(0, 256).unwrap();
@@ -210,13 +210,7 @@ fn vhost_user_client(path: &Path, barrier: Arc<Barrier>) {
210210
master.set_vring_base(0, state as u16).unwrap();
211211

212212
assert_eq!(master.get_max_mem_slots().unwrap(), 32);
213-
let region = VhostUserMemoryRegionInfo {
214-
guest_phys_addr: 0x800000,
215-
memory_size: 0x100000,
216-
userspace_addr: addr,
217-
mmap_offset: 0,
218-
mmap_handle: fd.file().as_raw_fd(),
219-
};
213+
let region = VhostUserMemoryRegionInfo::new(0x800000, 0x100000, addr, 0, fd.file().as_raw_fd());
220214
master.add_mem_region(&region).unwrap();
221215
master.remove_mem_region(&region).unwrap();
222216
}

crates/vhost/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ all-features = true
1515

1616
[features]
1717
default = []
18+
test-utils = []
1819
vhost-vsock = []
1920
vhost-kern = []
2021
vhost-vdpa = ["vhost-kern"]

crates/vhost/src/backend.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,27 @@ impl<T: VhostBackendMut> VhostBackend for RefCell<T> {
443443
self.borrow_mut().set_vring_err(queue_index, fd)
444444
}
445445
}
446+
447+
#[cfg(any(test, feature = "test-utils"))]
448+
impl VhostUserMemoryRegionInfo {
449+
/// creates instance of `VhostUserMemoryRegionInfo`.
450+
pub fn new(
451+
guest_phys_addr: u64,
452+
memory_size: u64,
453+
userspace_addr: u64,
454+
mmap_offset: u64,
455+
mmap_handle: RawFd,
456+
) -> Self {
457+
Self {
458+
guest_phys_addr,
459+
memory_size,
460+
userspace_addr,
461+
mmap_offset,
462+
mmap_handle,
463+
}
464+
}
465+
}
466+
446467
#[cfg(test)]
447468
mod tests {
448469
use super::*;

crates/vhost/src/vhost_kern/net.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,13 @@ mod tests {
130130

131131
net.set_mem_table(&[]).unwrap_err();
132132

133-
let region = VhostUserMemoryRegionInfo {
134-
guest_phys_addr: 0x0,
135-
memory_size: 0x10_0000,
136-
userspace_addr: m.get_host_address(GuestAddress(0x0)).unwrap() as u64,
137-
mmap_offset: 0,
138-
mmap_handle: -1,
139-
};
133+
let region = VhostUserMemoryRegionInfo::new(
134+
0x0,
135+
0x10_0000,
136+
m.get_host_address(GuestAddress(0x0)).unwrap() as u64,
137+
0,
138+
-1,
139+
);
140140
net.set_mem_table(&[region]).unwrap();
141141

142142
net.set_log_base(

crates/vhost/src/vhost_kern/vdpa.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -436,13 +436,13 @@ mod tests {
436436

437437
vdpa.set_mem_table(&[]).unwrap_err();
438438

439-
let region = VhostUserMemoryRegionInfo {
440-
guest_phys_addr: 0x0,
441-
memory_size: 0x10_0000,
442-
userspace_addr: m.get_host_address(GuestAddress(0x0)).unwrap() as u64,
443-
mmap_offset: 0,
444-
mmap_handle: -1,
445-
};
439+
let region = VhostUserMemoryRegionInfo::new(
440+
0x0,
441+
0x10_0000,
442+
m.get_host_address(GuestAddress(0x0)).unwrap() as u64,
443+
0,
444+
-1,
445+
);
446446
vdpa.set_mem_table(&[region]).unwrap();
447447

448448
let device_id = vdpa.get_device_id().unwrap();

crates/vhost/src/vhost_kern/vsock.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,13 @@ mod tests {
148148
vsock.set_mem_table(&[region]).unwrap_err();
149149
*/
150150

151-
let region = VhostUserMemoryRegionInfo {
152-
guest_phys_addr: 0x0,
153-
memory_size: 0x10_0000,
154-
userspace_addr: m.get_host_address(GuestAddress(0x0)).unwrap() as u64,
155-
mmap_offset: 0,
156-
mmap_handle: -1,
157-
};
151+
let region = VhostUserMemoryRegionInfo::new(
152+
0x0,
153+
0x10_0000,
154+
m.get_host_address(GuestAddress(0x0)).unwrap() as u64,
155+
0,
156+
-1,
157+
);
158158
vsock.set_mem_table(&[region]).unwrap();
159159

160160
vsock

crates/vhost/src/vhost_user/mod.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -424,13 +424,13 @@ mod tests {
424424
assert_eq!(num, 2);
425425

426426
let eventfd = vmm_sys_util::eventfd::EventFd::new(0).unwrap();
427-
let mem = [VhostUserMemoryRegionInfo {
428-
guest_phys_addr: 0,
429-
memory_size: 0x10_0000,
430-
userspace_addr: 0,
431-
mmap_offset: 0,
432-
mmap_handle: eventfd.as_raw_fd(),
433-
}];
427+
let mem = [VhostUserMemoryRegionInfo::new(
428+
0,
429+
0x10_0000,
430+
0,
431+
0,
432+
eventfd.as_raw_fd(),
433+
)];
434434
master.set_mem_table(&mem).unwrap();
435435

436436
master
@@ -479,13 +479,8 @@ mod tests {
479479
assert_eq!(max_mem_slots, 32);
480480

481481
let region_file: File = TempFile::new().unwrap().into_file();
482-
let region = VhostUserMemoryRegionInfo {
483-
guest_phys_addr: 0x10_0000,
484-
memory_size: 0x10_0000,
485-
userspace_addr: 0,
486-
mmap_offset: 0,
487-
mmap_handle: region_file.as_raw_fd(),
488-
};
482+
let region =
483+
VhostUserMemoryRegionInfo::new(0x10_0000, 0x10_0000, 0, 0, region_file.as_raw_fd());
489484
master.add_mem_region(&region).unwrap();
490485

491486
master.remove_mem_region(&region).unwrap();

0 commit comments

Comments
 (0)