vhost: convert buffer addresses to GPA for logging
[dpdk.git] / lib / librte_vhost / vhost.h
index 884befa..cfea138 100644 (file)
@@ -344,7 +344,7 @@ vq_is_packed(struct virtio_net *dev)
 static inline bool
 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
 {
-       uint16_t flags = *((volatile uint16_t *) &desc->flags);
+       uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
 
        return wrap_counter == !!(flags & VRING_DESC_F_AVAIL) &&
                wrap_counter != !!(flags & VRING_DESC_F_USED);
@@ -353,9 +353,14 @@ desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
 void __vhost_log_cache_write(struct virtio_net *dev,
                struct vhost_virtqueue *vq,
                uint64_t addr, uint64_t len);
+void __vhost_log_cache_write_iova(struct virtio_net *dev,
+               struct vhost_virtqueue *vq,
+               uint64_t iova, uint64_t len);
 void __vhost_log_cache_sync(struct virtio_net *dev,
                struct vhost_virtqueue *vq);
 void __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len);
+void __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
+                           uint64_t iova, uint64_t len);
 
 static __rte_always_inline void
 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
@@ -393,6 +398,32 @@ vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
        vhost_log_write(dev, vq->log_guest_addr + offset, len);
 }
 
+static __rte_always_inline void
+vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
+                          uint64_t iova, uint64_t len)
+{
+       if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
+               return;
+
+       if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
+               __vhost_log_cache_write_iova(dev, vq, iova, len);
+       else
+               __vhost_log_cache_write(dev, vq, iova, len);
+}
+
+static __rte_always_inline void
+vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
+                          uint64_t iova, uint64_t len)
+{
+       if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
+               return;
+
+       if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
+               __vhost_log_write_iova(dev, vq, iova, len);
+       else
+               __vhost_log_write(dev, iova, len);
+}
+
 /* Macros for printing using RTE_LOG */
 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
 #define RTE_LOGTYPE_VHOST_DATA   RTE_LOGTYPE_USER1
@@ -447,6 +478,26 @@ gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
        return 0;
 }
 
+static __rte_always_inline uint64_t
+hva_to_gpa(struct virtio_net *dev, uint64_t vva, uint64_t len)
+{
+       struct rte_vhost_mem_region *r;
+       uint32_t i;
+
+       if (unlikely(!dev || !dev->mem))
+               return 0;
+
+       for (i = 0; i < dev->mem->nregions; i++) {
+               r = &dev->mem->regions[i];
+
+               if (vva >= r->host_user_addr &&
+                   vva + len <  r->host_user_addr + r->size) {
+                       return r->guest_phys_addr + vva - r->host_user_addr;
+               }
+       }
+       return 0;
+}
+
 static __rte_always_inline struct virtio_net *
 get_device(int vid)
 {
@@ -543,13 +594,19 @@ vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
 
                if ((vhost_need_event(vhost_used_event(vq), new, old) &&
                                        (vq->callfd >= 0)) ||
-                               unlikely(!signalled_used_valid))
+                               unlikely(!signalled_used_valid)) {
                        eventfd_write(vq->callfd, (eventfd_t) 1);
+                       if (dev->notify_ops->guest_notified)
+                               dev->notify_ops->guest_notified(dev->vid);
+               }
        } else {
                /* Kick the guest if necessary. */
                if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
-                               && (vq->callfd >= 0))
+                               && (vq->callfd >= 0)) {
                        eventfd_write(vq->callfd, (eventfd_t)1);
+                       if (dev->notify_ops->guest_notified)
+                               dev->notify_ops->guest_notified(dev->vid);
+               }
        }
 }
 
@@ -600,8 +657,11 @@ vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
        if (vhost_need_event(off, new, old))
                kick = true;
 kick:
-       if (kick)
+       if (kick) {
                eventfd_write(vq->callfd, (eventfd_t)1);
+               if (dev->notify_ops->guest_notified)
+                       dev->notify_ops->guest_notified(dev->vid);
+       }
 }
 
 static __rte_always_inline void