vhost: introduce guest IOVA to backend VA helper
authorMaxime Coquelin <maxime.coquelin@redhat.com>
Thu, 5 Oct 2017 08:36:19 +0000 (10:36 +0200)
committerYuanhan Liu <yliu@fridaylinux.org>
Tue, 10 Oct 2017 13:52:27 +0000 (15:52 +0200)
This patch introduces vhost_iova_to_vva() function to translate
guest's IO virtual addresses to backend's virtual addresses.

When IOMMU is enabled, the IOTLB cache is queried to get the
translation. If missing from the IOTLB cache, an IOTLB_MISS request
is sent to Qemu, and IOTLB cache is queried again on IOTLB event
notification.

When IOMMU is disabled, the passed address is a guest's physical
address, so the legacy rte_vhost_gpa_to_vva() API is used.

Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
Acked-by: Yuanhan Liu <yliu@fridaylinux.org>
lib/librte_vhost/vhost.c
lib/librte_vhost/vhost.h

index 2493a79..6e55116 100644 (file)
 #include <rte_memory.h>
 #include <rte_malloc.h>
 #include <rte_vhost.h>
+#include <rte_rwlock.h>
 
 #include "iotlb.h"
 #include "vhost.h"
+#include "vhost_user.h"
 
 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
 
+uint64_t
+__vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
+                   uint64_t iova, uint64_t size, uint8_t perm)
+{
+       uint64_t vva, tmp_size;
+
+       if (unlikely(!size))
+               return 0;
+
+       tmp_size = size;
+
+       vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm);
+       if (tmp_size == size)
+               return vva;
+
+       if (!vhost_user_iotlb_pending_miss(vq, iova + tmp_size, perm)) {
+               vhost_user_iotlb_pending_insert(vq, iova + tmp_size, perm);
+               vhost_user_iotlb_miss(dev, iova + tmp_size, perm);
+       }
+
+       return 0;
+}
+
 struct virtio_net *
 get_device(int vid)
 {
index 7600885..2ea6c46 100644 (file)
@@ -375,4 +375,17 @@ struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
  */
 void vhost_backend_cleanup(struct virtio_net *dev);
 
+uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
+                       uint64_t iova, uint64_t size, uint8_t perm);
+
+static __rte_always_inline uint64_t
+vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
+                       uint64_t iova, uint64_t size, uint8_t perm)
+{
+       if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
+               return rte_vhost_gpa_to_vva(dev->mem, iova);
+
+       return __vhost_iova_to_vva(dev, vq, iova, size, perm);
+}
+
 #endif /* _VHOST_NET_CDEV_H_ */