vhost: translate incoming log address to GPA
[dpdk.git] / lib / librte_vhost / vhost_user.c
index 555d09a..5ae561e 100644 (file)
@@ -284,6 +284,8 @@ vhost_user_set_features(struct virtio_net **pdev, struct VhostUserMsg *msg,
        } else {
                dev->vhost_hlen = sizeof(struct virtio_net_hdr);
        }
+       RTE_LOG(INFO, VHOST_CONFIG,
+               "negotiated Virtio features: 0x%" PRIx64 "\n", dev->features);
        VHOST_LOG_DEBUG(VHOST_CONFIG,
                "(%d) mergeable RX buffers %s, virtio 1 %s\n",
                dev->vid,
@@ -408,6 +410,9 @@ numa_realloc(struct virtio_net *dev, int index)
        struct batch_copy_elem *new_batch_copy_elems;
        int ret;
 
+       if (dev->flags & VIRTIO_DEV_RUNNING)
+               return dev;
+
        old_dev = dev;
        vq = old_vq = dev->virtqueue[index];
 
@@ -556,11 +561,13 @@ ring_addr_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
 {
        if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
                uint64_t vva;
+               uint64_t req_size = *size;
 
                vva = vhost_user_iotlb_cache_find(vq, ra,
                                        size, VHOST_ACCESS_RW);
-               if (!vva)
-                       vhost_user_iotlb_miss(dev, ra, VHOST_ACCESS_RW);
+               if (req_size != *size)
+                       vhost_user_iotlb_miss(dev, (ra + *size),
+                                             VHOST_ACCESS_RW);
 
                return vva;
        }
@@ -568,6 +575,39 @@ ring_addr_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
        return qva_to_vva(dev, ra, size);
 }
 
+/*
+ * Converts vring log address to GPA
+ * If IOMMU is enabled, the log address is IOVA
+ * If IOMMU not enabled, the log address is already GPA
+ */
+static uint64_t
+translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
+               uint64_t log_addr)
+{
+       if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
+               const uint64_t exp_size = sizeof(struct vring_used) +
+                       sizeof(struct vring_used_elem) * vq->size;
+               uint64_t hva, gpa;
+               uint64_t size = exp_size;
+
+               hva = vhost_iova_to_vva(dev, vq, log_addr,
+                                       &size, VHOST_ACCESS_RW);
+               if (size != exp_size)
+                       return 0;
+
+               gpa = hva_to_gpa(dev, hva, exp_size);
+               if (!gpa) {
+                       RTE_LOG(ERR, VHOST_CONFIG,
+                               "VQ: Failed to find GPA for log_addr: 0x%" PRIx64 " hva: 0x%" PRIx64 "\n",
+                               log_addr, hva);
+                       return 0;
+               }
+               return gpa;
+
+       } else
+               return log_addr;
+}
+
 static struct virtio_net *
 translate_ring_addresses(struct virtio_net *dev, int vq_index)
 {
@@ -617,6 +657,7 @@ translate_ring_addresses(struct virtio_net *dev, int vq_index)
                        return dev;
                }
 
+               vq->access_ok = 1;
                return dev;
        }
 
@@ -674,7 +715,15 @@ translate_ring_addresses(struct virtio_net *dev, int vq_index)
                vq->last_avail_idx = vq->used->idx;
        }
 
-       vq->log_guest_addr = addr->log_guest_addr;
+       vq->log_guest_addr =
+               translate_log_addr(dev, vq, addr->log_guest_addr);
+       if (vq->log_guest_addr == 0) {
+               RTE_LOG(DEBUG, VHOST_CONFIG,
+                       "(%d) failed to map log_guest_addr .\n",
+                       dev->vid);
+               return dev;
+       }
+       vq->access_ok = 1;
 
        VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address desc: %p\n",
                        dev->vid, vq->desc);
@@ -699,6 +748,7 @@ vhost_user_set_vring_addr(struct virtio_net **pdev, struct VhostUserMsg *msg,
        struct virtio_net *dev = *pdev;
        struct vhost_virtqueue *vq;
        struct vhost_vring_addr *addr = &msg->payload.addr;
+       bool access_ok;
 
        if (dev->mem == NULL)
                return RTE_VHOST_MSG_RESULT_ERR;
@@ -706,6 +756,8 @@ vhost_user_set_vring_addr(struct virtio_net **pdev, struct VhostUserMsg *msg,
        /* addr->index refers to the queue index. The txq 1, rxq is 0. */
        vq = dev->virtqueue[msg->payload.addr.index];
 
+       access_ok = vq->access_ok;
+
        /*
         * Rings addresses should not be interpreted as long as the ring is not
         * started and enabled
@@ -714,8 +766,9 @@ vhost_user_set_vring_addr(struct virtio_net **pdev, struct VhostUserMsg *msg,
 
        vring_invalidate(dev, vq);
 
-       if (vq->enabled && (dev->features &
-                               (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) {
+       if ((vq->enabled && (dev->features &
+                               (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) ||
+                       access_ok) {
                dev = translate_ring_addresses(dev, msg->payload.addr.index);
                if (!dev)
                        return RTE_VHOST_MSG_RESULT_ERR;
@@ -1231,8 +1284,12 @@ vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *msg,
         * the ring starts already enabled. Otherwise, it is enabled via
         * the SET_VRING_ENABLE message.
         */
-       if (!(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)))
+       if (!(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) {
                vq->enabled = 1;
+               if (dev->notify_ops->vring_state_changed)
+                       dev->notify_ops->vring_state_changed(
+                               dev->vid, file.index, 1);
+       }
 
        if (vq->kickfd >= 0)
                close(vq->kickfd);
@@ -1298,6 +1355,8 @@ vhost_user_get_vring_base(struct virtio_net **pdev,
 
        vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
 
+       vq->signalled_used_valid = false;
+
        if (dev->dequeue_zero_copy)
                free_zmbufs(vq);
        if (vq_is_packed(dev)) {
@@ -1314,6 +1373,8 @@ vhost_user_get_vring_base(struct virtio_net **pdev,
        msg->size = sizeof(msg->payload.state);
        msg->fd_num = 0;
 
+       vring_invalidate(dev, vq);
+
        return RTE_VHOST_MSG_RESULT_REPLY;
 }
 
@@ -1400,6 +1461,9 @@ vhost_user_set_protocol_features(struct virtio_net **pdev,
        }
 
        dev->protocol_features = protocol_features;
+       RTE_LOG(INFO, VHOST_CONFIG,
+               "negotiated Vhost-user protocol features: 0x%" PRIx64 "\n",
+               dev->protocol_features);
 
        return RTE_VHOST_MSG_RESULT_OK;
 }
@@ -1553,60 +1617,75 @@ vhost_user_set_req_fd(struct virtio_net **pdev, struct VhostUserMsg *msg,
                return RTE_VHOST_MSG_RESULT_ERR;
        }
 
+       if (dev->slave_req_fd >= 0)
+               close(dev->slave_req_fd);
+
        dev->slave_req_fd = fd;
 
        return RTE_VHOST_MSG_RESULT_OK;
 }
 
 static int
-is_vring_iotlb_update(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
+is_vring_iotlb_split(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
 {
        struct vhost_vring_addr *ra;
-       uint64_t start, end;
+       uint64_t start, end, len;
 
        start = imsg->iova;
        end = start + imsg->size;
 
        ra = &vq->ring_addrs;
-       if (ra->desc_user_addr >= start && ra->desc_user_addr < end)
+       len = sizeof(struct vring_desc) * vq->size;
+       if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
                return 1;
-       if (ra->avail_user_addr >= start && ra->avail_user_addr < end)
+
+       len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
+       if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
                return 1;
-       if (ra->used_user_addr >= start && ra->used_user_addr < end)
+
+       len = sizeof(struct vring_used) +
+              sizeof(struct vring_used_elem) * vq->size;
+       if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
                return 1;
 
        return 0;
 }
 
 static int
-is_vring_iotlb_invalidate(struct vhost_virtqueue *vq,
-                               struct vhost_iotlb_msg *imsg)
+is_vring_iotlb_packed(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
 {
-       uint64_t istart, iend, vstart, vend;
+       struct vhost_vring_addr *ra;
+       uint64_t start, end, len;
 
-       istart = imsg->iova;
-       iend = istart + imsg->size - 1;
+       start = imsg->iova;
+       end = start + imsg->size;
 
-       vstart = (uintptr_t)vq->desc;
-       vend = vstart + sizeof(struct vring_desc) * vq->size - 1;
-       if (vstart <= iend && istart <= vend)
+       ra = &vq->ring_addrs;
+       len = sizeof(struct vring_packed_desc) * vq->size;
+       if (ra->desc_user_addr < end && (ra->desc_user_addr + len) > start)
                return 1;
 
-       vstart = (uintptr_t)vq->avail;
-       vend = vstart + sizeof(struct vring_avail);
-       vend += sizeof(uint16_t) * vq->size - 1;
-       if (vstart <= iend && istart <= vend)
+       len = sizeof(struct vring_packed_desc_event);
+       if (ra->avail_user_addr < end && (ra->avail_user_addr + len) > start)
                return 1;
 
-       vstart = (uintptr_t)vq->used;
-       vend = vstart + sizeof(struct vring_used);
-       vend += sizeof(struct vring_used_elem) * vq->size - 1;
-       if (vstart <= iend && istart <= vend)
+       len = sizeof(struct vring_packed_desc_event);
+       if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
                return 1;
 
        return 0;
 }
 
+static int is_vring_iotlb(struct virtio_net *dev,
+                         struct vhost_virtqueue *vq,
+                         struct vhost_iotlb_msg *imsg)
+{
+       if (vq_is_packed(dev))
+               return is_vring_iotlb_packed(vq, imsg);
+       else
+               return is_vring_iotlb_split(vq, imsg);
+}
+
 static int
 vhost_user_iotlb_msg(struct virtio_net **pdev, struct VhostUserMsg *msg,
                        int main_fd __rte_unused)
@@ -1629,7 +1708,7 @@ vhost_user_iotlb_msg(struct virtio_net **pdev, struct VhostUserMsg *msg,
                        vhost_user_iotlb_cache_insert(vq, imsg->iova, vva,
                                        len, imsg->perm);
 
-                       if (is_vring_iotlb_update(vq, imsg))
+                       if (is_vring_iotlb(dev, vq, imsg))
                                *pdev = dev = translate_ring_addresses(dev, i);
                }
                break;
@@ -1640,7 +1719,7 @@ vhost_user_iotlb_msg(struct virtio_net **pdev, struct VhostUserMsg *msg,
                        vhost_user_iotlb_cache_remove(vq, imsg->iova,
                                        imsg->size);
 
-                       if (is_vring_iotlb_invalidate(vq, imsg))
+                       if (is_vring_iotlb(dev, vq, imsg))
                                vring_invalidate(dev, vq);
                }
                break;
@@ -1910,7 +1989,7 @@ vhost_user_msg_handler(int vid, int fd)
        int did = -1;
        int ret;
        int unlock_required = 0;
-       uint32_t skip_master = 0;
+       bool handled;
        int request;
 
        dev = get_device(vid);
@@ -1928,27 +2007,30 @@ vhost_user_msg_handler(int vid, int fd)
        }
 
        ret = read_vhost_message(fd, &msg);
-       if (ret <= 0 || msg.request.master >= VHOST_USER_MAX) {
+       if (ret <= 0) {
                if (ret < 0)
                        RTE_LOG(ERR, VHOST_CONFIG,
                                "vhost read message failed\n");
-               else if (ret == 0)
+               else
                        RTE_LOG(INFO, VHOST_CONFIG,
                                "vhost peer closed\n");
-               else
-                       RTE_LOG(ERR, VHOST_CONFIG,
-                               "vhost read incorrect message\n");
 
                return -1;
        }
 
        ret = 0;
-       if (msg.request.master != VHOST_USER_IOTLB_MSG)
-               RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
-                       vhost_message_str[msg.request.master]);
-       else
-               RTE_LOG(DEBUG, VHOST_CONFIG, "read message %s\n",
-                       vhost_message_str[msg.request.master]);
+       request = msg.request.master;
+       if (request > VHOST_USER_NONE && request < VHOST_USER_MAX &&
+                       vhost_message_str[request]) {
+               if (request != VHOST_USER_IOTLB_MSG)
+                       RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
+                               vhost_message_str[request]);
+               else
+                       RTE_LOG(DEBUG, VHOST_CONFIG, "read message %s\n",
+                               vhost_message_str[request]);
+       } else {
+               RTE_LOG(DEBUG, VHOST_CONFIG, "External request %d\n", request);
+       }
 
        ret = vhost_user_check_and_alloc_queue_pair(dev, &msg);
        if (ret < 0) {
@@ -1964,7 +2046,7 @@ vhost_user_msg_handler(int vid, int fd)
         * inactive, so it is safe. Otherwise taking the access_lock
         * would cause a dead lock.
         */
-       switch (msg.request.master) {
+       switch (request) {
        case VHOST_USER_SET_FEATURES:
        case VHOST_USER_SET_PROTOCOL_FEATURES:
        case VHOST_USER_SET_OWNER:
@@ -1989,19 +2071,24 @@ vhost_user_msg_handler(int vid, int fd)
 
        }
 
+       handled = false;
        if (dev->extern_ops.pre_msg_handle) {
                ret = (*dev->extern_ops.pre_msg_handle)(dev->vid,
-                               (void *)&msg, &skip_master);
-               if (ret == RTE_VHOST_MSG_RESULT_ERR)
-                       goto skip_to_reply;
-               else if (ret == RTE_VHOST_MSG_RESULT_REPLY)
+                               (void *)&msg);
+               switch (ret) {
+               case RTE_VHOST_MSG_RESULT_REPLY:
                        send_vhost_reply(fd, &msg);
-
-               if (skip_master)
+                       /* Fall-through */
+               case RTE_VHOST_MSG_RESULT_ERR:
+               case RTE_VHOST_MSG_RESULT_OK:
+                       handled = true;
                        goto skip_to_post_handle;
+               case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
+               default:
+                       break;
+               }
        }
 
-       request = msg.request.master;
        if (request > VHOST_USER_NONE && request < VHOST_USER_MAX) {
                if (!vhost_message_handlers[request])
                        goto skip_to_post_handle;
@@ -2012,40 +2099,54 @@ vhost_user_msg_handler(int vid, int fd)
                        RTE_LOG(ERR, VHOST_CONFIG,
                                "Processing %s failed.\n",
                                vhost_message_str[request]);
+                       handled = true;
                        break;
                case RTE_VHOST_MSG_RESULT_OK:
                        RTE_LOG(DEBUG, VHOST_CONFIG,
                                "Processing %s succeeded.\n",
                                vhost_message_str[request]);
+                       handled = true;
                        break;
                case RTE_VHOST_MSG_RESULT_REPLY:
                        RTE_LOG(DEBUG, VHOST_CONFIG,
                                "Processing %s succeeded and needs reply.\n",
                                vhost_message_str[request]);
                        send_vhost_reply(fd, &msg);
+                       handled = true;
+                       break;
+               default:
                        break;
                }
-       } else {
-               RTE_LOG(ERR, VHOST_CONFIG,
-                       "Requested invalid message type %d.\n", request);
-               ret = RTE_VHOST_MSG_RESULT_ERR;
        }
 
 skip_to_post_handle:
        if (ret != RTE_VHOST_MSG_RESULT_ERR &&
                        dev->extern_ops.post_msg_handle) {
-               ret = (*dev->extern_ops.post_msg_handle)(
-                               dev->vid, (void *)&msg);
-               if (ret == RTE_VHOST_MSG_RESULT_ERR)
-                       goto skip_to_reply;
-               else if (ret == RTE_VHOST_MSG_RESULT_REPLY)
+               ret = (*dev->extern_ops.post_msg_handle)(dev->vid,
+                               (void *)&msg);
+               switch (ret) {
+               case RTE_VHOST_MSG_RESULT_REPLY:
                        send_vhost_reply(fd, &msg);
+                       /* Fall-through */
+               case RTE_VHOST_MSG_RESULT_ERR:
+               case RTE_VHOST_MSG_RESULT_OK:
+                       handled = true;
+               case RTE_VHOST_MSG_RESULT_NOT_HANDLED:
+               default:
+                       break;
+               }
        }
 
-skip_to_reply:
        if (unlock_required)
                vhost_user_unlock_all_queue_pairs(dev);
 
+       /* If message was not handled at this stage, treat it as an error */
+       if (!handled) {
+               RTE_LOG(ERR, VHOST_CONFIG,
+                       "vhost message (req: %d) was not handled.\n", request);
+               ret = RTE_VHOST_MSG_RESULT_ERR;
+       }
+
        /*
         * If the request required a reply that was already sent,
         * this optional reply-ack won't be sent as the