Skip to content

Commit bdc6f2a

Browse files
likebreathstefano-garzarella
authored andcommitted
vhost: vdpa: Provide custom set_vring_addr() implementation
Unlike other vhost backends (e.g. vhost-net and vhost-vsock), vDPA backends can not work with host virtual address (HVA), instead they expect I/O virtual address (IOVA). The IOVA can be mapped 1:1 with guest physical address (GPA) when no IOMMU is involved. This is why the default implementation of `set_vring_addr()` from Trait `VhostBackend` is no longer working with vDPA backends. To solve this issue, this patch provides a custom `set_vring_addr()` implementation for Trait `VhostKernVdpa`. Fixes: #164 Signed-off-by: Bo Chen <[email protected]>
1 parent 03d31fa commit bdc6f2a

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

crates/vhost/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
### Changed
77

88
### Fixed
9+
- [[#165]](https://github.com/rust-vmm/vhost/pull/165) vhost: vdpa: Provide custom set_vring_addr() implementation
910

1011
### Deprecated
1112

crates/vhost/src/vhost_kern/vdpa.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,34 @@ impl<AS: GuestAddressSpace> VhostKernVdpa<AS> {
6161
backend_features_acked,
6262
}
6363
}
64+
65+
/// Set the addresses for a given vring.
66+
///
67+
/// # Arguments
68+
/// * `queue_index` - Index of the queue to set addresses for.
69+
/// * `config_data` - Vring config data, addresses of desc_table, avail_ring
70+
/// and used_ring are in the guest address space.
71+
pub fn set_vring_addr(&self, queue_index: usize, config_data: &VringConfigData) -> Result<()> {
72+
if !self.is_valid(config_data) {
73+
return Err(Error::InvalidQueue);
74+
}
75+
76+
// vDPA backends expect IOVA (that can be mapped 1:1 with
77+
// GPA when no IOMMU is involved).
78+
let vring_addr = vhost_vring_addr {
79+
index: queue_index as u32,
80+
flags: config_data.flags,
81+
desc_user_addr: config_data.desc_table_addr,
82+
used_user_addr: config_data.used_ring_addr,
83+
avail_user_addr: config_data.avail_ring_addr,
84+
log_guest_addr: config_data.get_log_addr(),
85+
};
86+
87+
// This ioctl is called on a valid vhost fd and has its
88+
// return value checked.
89+
let ret = unsafe { ioctl_with_ref(self, VHOST_SET_VRING_ADDR(), &vring_addr) };
90+
ioctl_result(ret, ())
91+
}
6492
}
6593

6694
impl<AS: GuestAddressSpace> VhostVdpa for VhostKernVdpa<AS> {

0 commit comments

Comments
 (0)