Skip to content

Commit c471ad0

Browse files
jasowangmstsirkin
authored andcommitted
vhost_net: device IOTLB support
This patches implements Device IOTLB support for vhost kernel. This is done through: 1) switch to use dma helpers when map/unmap vrings from vhost codes 2) introduce a set of VhostOps to: - setting up device IOTLB request callback - processing device IOTLB request - processing device IOTLB invalidation 2) kernel support for Device IOTLB API: - allow vhost-net to query the IOMMU IOTLB entry through eventfd - enable the ability for qemu to update a specified mapping of vhost - through ioctl. - enable the ability to invalidate a specified range of iova for the device IOTLB of vhost through ioctl. In x86/intel_iommu case this is triggered through iommu memory region notifier from device IOTLB invalidation descriptor processing routine. With all the above, kernel vhost_net can co-operate with userspace IOMMU. For vhost-user, the support could be easily done on top by implementing the VhostOps. Cc: Michael S. Tsirkin <[email protected]> Signed-off-by: Jason Wang <[email protected]> Reviewed-by: Michael S. Tsirkin <[email protected]> Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent 1448c13 commit c471ad0

File tree

6 files changed

+262
-22
lines changed

6 files changed

+262
-22
lines changed

hw/net/vhost_net.c

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ static const int kernel_feature_bits[] = {
5252
VIRTIO_NET_F_MRG_RXBUF,
5353
VIRTIO_F_VERSION_1,
5454
VIRTIO_NET_F_MTU,
55+
VIRTIO_F_IOMMU_PLATFORM,
5556
VHOST_INVALID_FEATURE_BIT
5657
};
5758

hw/virtio/vhost-backend.c

+99
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,102 @@ static int vhost_kernel_vsock_set_running(struct vhost_dev *dev, int start)
185185
}
186186
#endif /* CONFIG_VHOST_VSOCK */
187187

188+
static void vhost_kernel_iotlb_read(void *opaque)
189+
{
190+
struct vhost_dev *dev = opaque;
191+
struct vhost_msg msg;
192+
ssize_t len;
193+
194+
while ((len = read((uintptr_t)dev->opaque, &msg, sizeof msg)) > 0) {
195+
struct vhost_iotlb_msg *imsg = &msg.iotlb;
196+
if (len < sizeof msg) {
197+
error_report("Wrong vhost message len: %d", (int)len);
198+
break;
199+
}
200+
if (msg.type != VHOST_IOTLB_MSG) {
201+
error_report("Unknown vhost iotlb message type");
202+
break;
203+
}
204+
switch (imsg->type) {
205+
case VHOST_IOTLB_MISS:
206+
vhost_device_iotlb_miss(dev, imsg->iova,
207+
imsg->perm != VHOST_ACCESS_RO);
208+
break;
209+
case VHOST_IOTLB_UPDATE:
210+
case VHOST_IOTLB_INVALIDATE:
211+
error_report("Unexpected IOTLB message type");
212+
break;
213+
case VHOST_IOTLB_ACCESS_FAIL:
214+
/* FIXME: report device iotlb error */
215+
break;
216+
default:
217+
break;
218+
}
219+
}
220+
}
221+
222+
static int vhost_kernel_update_device_iotlb(struct vhost_dev *dev,
223+
uint64_t iova, uint64_t uaddr,
224+
uint64_t len,
225+
IOMMUAccessFlags perm)
226+
{
227+
struct vhost_msg msg;
228+
msg.type = VHOST_IOTLB_MSG;
229+
msg.iotlb.iova = iova;
230+
msg.iotlb.uaddr = uaddr;
231+
msg.iotlb.size = len;
232+
msg.iotlb.type = VHOST_IOTLB_UPDATE;
233+
234+
switch (perm) {
235+
case IOMMU_RO:
236+
msg.iotlb.perm = VHOST_ACCESS_RO;
237+
break;
238+
case IOMMU_WO:
239+
msg.iotlb.perm = VHOST_ACCESS_WO;
240+
break;
241+
case IOMMU_RW:
242+
msg.iotlb.perm = VHOST_ACCESS_RW;
243+
break;
244+
default:
245+
g_assert_not_reached();
246+
}
247+
248+
if (write((uintptr_t)dev->opaque, &msg, sizeof msg) != sizeof msg) {
249+
error_report("Fail to update device iotlb");
250+
return -EFAULT;
251+
}
252+
253+
return 0;
254+
}
255+
256+
static int vhost_kernel_invalidate_device_iotlb(struct vhost_dev *dev,
257+
uint64_t iova, uint64_t len)
258+
{
259+
struct vhost_msg msg;
260+
261+
msg.type = VHOST_IOTLB_MSG;
262+
msg.iotlb.iova = iova;
263+
msg.iotlb.size = len;
264+
msg.iotlb.type = VHOST_IOTLB_INVALIDATE;
265+
266+
if (write((uintptr_t)dev->opaque, &msg, sizeof msg) != sizeof msg) {
267+
error_report("Fail to invalidate device iotlb");
268+
return -EFAULT;
269+
}
270+
271+
return 0;
272+
}
273+
274+
static void vhost_kernel_set_iotlb_callback(struct vhost_dev *dev,
275+
int enabled)
276+
{
277+
if (enabled)
278+
qemu_set_fd_handler((uintptr_t)dev->opaque,
279+
vhost_kernel_iotlb_read, NULL, dev);
280+
else
281+
qemu_set_fd_handler((uintptr_t)dev->opaque, NULL, NULL, NULL);
282+
}
283+
188284
static const VhostOps kernel_ops = {
189285
.backend_type = VHOST_BACKEND_TYPE_KERNEL,
190286
.vhost_backend_init = vhost_kernel_init,
@@ -214,6 +310,9 @@ static const VhostOps kernel_ops = {
214310
.vhost_vsock_set_guest_cid = vhost_kernel_vsock_set_guest_cid,
215311
.vhost_vsock_set_running = vhost_kernel_vsock_set_running,
216312
#endif /* CONFIG_VHOST_VSOCK */
313+
.vhost_set_iotlb_callback = vhost_kernel_set_iotlb_callback,
314+
.vhost_update_device_iotlb = vhost_kernel_update_device_iotlb,
315+
.vhost_invalidate_device_iotlb = vhost_kernel_invalidate_device_iotlb,
217316
};
218317

219318
int vhost_set_backend_type(struct vhost_dev *dev, VhostBackendType backend_type)

0 commit comments

Comments
 (0)