vhost: fix possible dead loop in vector filling
[dpdk.git] / lib / librte_vhost / vhost_user.c
index 970ad58..8fec773 100644 (file)
@@ -76,8 +76,13 @@ static const char *vhost_message_str[VHOST_USER_MAX] = {
        [VHOST_USER_CRYPTO_CREATE_SESS] = "VHOST_USER_CRYPTO_CREATE_SESS",
        [VHOST_USER_CRYPTO_CLOSE_SESS] = "VHOST_USER_CRYPTO_CLOSE_SESS",
        [VHOST_USER_POSTCOPY_ADVISE]  = "VHOST_USER_POSTCOPY_ADVISE",
+       [VHOST_USER_POSTCOPY_LISTEN]  = "VHOST_USER_POSTCOPY_LISTEN",
+       [VHOST_USER_POSTCOPY_END]  = "VHOST_USER_POSTCOPY_END",
 };
 
+static int send_vhost_reply(int sockfd, struct VhostUserMsg *msg);
+static int read_vhost_message(int sockfd, struct VhostUserMsg *msg);
+
 static uint64_t
 get_blk_size(int fd)
 {
@@ -132,6 +137,8 @@ vhost_backend_cleanup(struct virtio_net *dev)
                close(dev->postcopy_ufd);
                dev->postcopy_ufd = -1;
        }
+
+       dev->postcopy_listening = 0;
 }
 
 /*
@@ -482,6 +489,9 @@ qva_to_vva(struct virtio_net *dev, uint64_t qva, uint64_t *len)
        struct rte_vhost_mem_region *r;
        uint32_t i;
 
+       if (unlikely(!dev || !dev->mem))
+               goto out_error;
+
        /* Find the region where the address lives. */
        for (i = 0; i < dev->mem->nregions; i++) {
                r = &dev->mem->regions[i];
@@ -496,6 +506,7 @@ qva_to_vva(struct virtio_net *dev, uint64_t qva, uint64_t *len)
                               r->host_user_addr;
                }
        }
+out_error:
        *len = 0;
 
        return 0;
@@ -689,10 +700,27 @@ vhost_user_set_vring_base(struct virtio_net **pdev,
                        int main_fd __rte_unused)
 {
        struct virtio_net *dev = *pdev;
-       dev->virtqueue[msg->payload.state.index]->last_used_idx  =
-                       msg->payload.state.num;
-       dev->virtqueue[msg->payload.state.index]->last_avail_idx =
-                       msg->payload.state.num;
+       struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
+       uint64_t val = msg->payload.state.num;
+
+       if (vq_is_packed(dev)) {
+               /*
+                * Bit[0:14]: avail index
+                * Bit[15]: avail wrap counter
+                */
+               vq->last_avail_idx = val & 0x7fff;
+               vq->avail_wrap_counter = !!(val & (0x1 << 15));
+               /*
+                * Set used index to same value as available one, as
+                * their values should be the same since ring processing
+                * was stopped at get time.
+                */
+               vq->last_used_idx = vq->last_avail_idx;
+               vq->used_wrap_counter = vq->avail_wrap_counter;
+       } else {
+               vq->last_used_idx = msg->payload.state.num;
+               vq->last_avail_idx = msg->payload.state.num;
+       }
 
        return VH_RESULT_OK;
 }
@@ -820,10 +848,10 @@ vhost_memory_changed(struct VhostUserMemory *new,
 
 static int
 vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
-                       int main_fd __rte_unused)
+                       int main_fd)
 {
        struct virtio_net *dev = *pdev;
-       struct VhostUserMemory memory = msg->payload.memory;
+       struct VhostUserMemory *memory = &msg->payload.memory;
        struct rte_vhost_mem_region *reg;
        void *mmap_addr;
        uint64_t mmap_size;
@@ -833,17 +861,17 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
        int populate;
        int fd;
 
-       if (memory.nregions > VHOST_MEMORY_MAX_NREGIONS) {
+       if (memory->nregions > VHOST_MEMORY_MAX_NREGIONS) {
                RTE_LOG(ERR, VHOST_CONFIG,
-                       "too many memory regions (%u)\n", memory.nregions);
+                       "too many memory regions (%u)\n", memory->nregions);
                return VH_RESULT_ERR;
        }
 
-       if (dev->mem && !vhost_memory_changed(&memory, dev->mem)) {
+       if (dev->mem && !vhost_memory_changed(memory, dev->mem)) {
                RTE_LOG(INFO, VHOST_CONFIG,
                        "(%d) memory regions not changed\n", dev->vid);
 
-               for (i = 0; i < memory.nregions; i++)
+               for (i = 0; i < memory->nregions; i++)
                        close(msg->fds[i]);
 
                return VH_RESULT_OK;
@@ -875,25 +903,25 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
        }
 
        dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct rte_vhost_memory) +
-               sizeof(struct rte_vhost_mem_region) * memory.nregions, 0);
+               sizeof(struct rte_vhost_mem_region) * memory->nregions, 0);
        if (dev->mem == NULL) {
                RTE_LOG(ERR, VHOST_CONFIG,
                        "(%d) failed to allocate memory for dev->mem\n",
                        dev->vid);
                return VH_RESULT_ERR;
        }
-       dev->mem->nregions = memory.nregions;
+       dev->mem->nregions = memory->nregions;
 
-       for (i = 0; i < memory.nregions; i++) {
+       for (i = 0; i < memory->nregions; i++) {
                fd  = msg->fds[i];
                reg = &dev->mem->regions[i];
 
-               reg->guest_phys_addr = memory.regions[i].guest_phys_addr;
-               reg->guest_user_addr = memory.regions[i].userspace_addr;
-               reg->size            = memory.regions[i].memory_size;
+               reg->guest_phys_addr = memory->regions[i].guest_phys_addr;
+               reg->guest_user_addr = memory->regions[i].userspace_addr;
+               reg->size            = memory->regions[i].memory_size;
                reg->fd              = fd;
 
-               mmap_offset = memory.regions[i].mmap_offset;
+               mmap_offset = memory->regions[i].mmap_offset;
 
                /* Check for memory_size + mmap_offset overflow */
                if (mmap_offset >= -reg->size) {
@@ -962,6 +990,70 @@ vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *msg,
                        mmap_size,
                        alignment,
                        mmap_offset);
+
+               if (dev->postcopy_listening) {
+                       /*
+                        * We haven't a better way right now than sharing
+                        * DPDK's virtual address with Qemu, so that Qemu can
+                        * retrieve the region offset when handling userfaults.
+                        */
+                       memory->regions[i].userspace_addr =
+                               reg->host_user_addr;
+               }
+       }
+       if (dev->postcopy_listening) {
+               /* Send the addresses back to qemu */
+               msg->fd_num = 0;
+               send_vhost_reply(main_fd, msg);
+
+               /* Wait for qemu to acknolwedge it's got the addresses
+                * we've got to wait before we're allowed to generate faults.
+                */
+               VhostUserMsg ack_msg;
+               if (read_vhost_message(main_fd, &ack_msg) <= 0) {
+                       RTE_LOG(ERR, VHOST_CONFIG,
+                               "Failed to read qemu ack on postcopy set-mem-table\n");
+                       goto err_mmap;
+               }
+               if (ack_msg.request.master != VHOST_USER_SET_MEM_TABLE) {
+                       RTE_LOG(ERR, VHOST_CONFIG,
+                               "Bad qemu ack on postcopy set-mem-table (%d)\n",
+                               ack_msg.request.master);
+                       goto err_mmap;
+               }
+
+               /* Now userfault register and we can use the memory */
+               for (i = 0; i < memory->nregions; i++) {
+#ifdef RTE_LIBRTE_VHOST_POSTCOPY
+                       reg = &dev->mem->regions[i];
+                       struct uffdio_register reg_struct;
+
+                       /*
+                        * Let's register all the mmap'ed area to ensure
+                        * alignment on page boundary.
+                        */
+                       reg_struct.range.start =
+                               (uint64_t)(uintptr_t)reg->mmap_addr;
+                       reg_struct.range.len = reg->mmap_size;
+                       reg_struct.mode = UFFDIO_REGISTER_MODE_MISSING;
+
+                       if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER,
+                                               &reg_struct)) {
+                               RTE_LOG(ERR, VHOST_CONFIG,
+                                       "Failed to register ufd for region %d: (ufd = %d) %s\n",
+                                       i, dev->postcopy_ufd,
+                                       strerror(errno));
+                               goto err_mmap;
+                       }
+                       RTE_LOG(INFO, VHOST_CONFIG,
+                               "\t userfaultfd registered for range : %llx - %llx\n",
+                               reg_struct.range.start,
+                               reg_struct.range.start +
+                               reg_struct.range.len - 1);
+#else
+                       goto err_mmap;
+#endif
+               }
        }
 
        for (i = 0; i < dev->nr_vring; i++) {
@@ -1137,6 +1229,7 @@ vhost_user_get_vring_base(struct virtio_net **pdev,
 {
        struct virtio_net *dev = *pdev;
        struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
+       uint64_t val;
 
        /* We have to stop the queue (virtio) if it is running. */
        vhost_destroy_device_notify(dev);
@@ -1144,8 +1237,18 @@ vhost_user_get_vring_base(struct virtio_net **pdev,
        dev->flags &= ~VIRTIO_DEV_READY;
        dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
 
-       /* Here we are safe to get the last avail index */
-       msg->payload.state.num = vq->last_avail_idx;
+       /* Here we are safe to get the indexes */
+       if (vq_is_packed(dev)) {
+               /*
+                * Bit[0:14]: avail index
+                * Bit[15]: avail wrap counter
+                */
+               val = vq->last_avail_idx & 0x7fff;
+               val |= vq->avail_wrap_counter << 15;
+               msg->payload.state.num = val;
+       } else {
+               msg->payload.state.num = vq->last_avail_idx;
+       }
 
        RTE_LOG(INFO, VHOST_CONFIG,
                "vring base idx:%d file:%d\n", msg->payload.state.index,
@@ -1251,7 +1354,11 @@ vhost_user_set_protocol_features(struct virtio_net **pdev,
 {
        struct virtio_net *dev = *pdev;
        uint64_t protocol_features = msg->payload.u64;
-       if (protocol_features & ~VHOST_USER_PROTOCOL_FEATURES) {
+       uint64_t slave_protocol_features = 0;
+
+       rte_vhost_driver_get_protocol_features(dev->ifname,
+                       &slave_protocol_features);
+       if (protocol_features & ~slave_protocol_features) {
                RTE_LOG(ERR, VHOST_CONFIG,
                        "(%d) received invalid protocol features.\n",
                        dev->vid);
@@ -1549,6 +1656,42 @@ vhost_user_set_postcopy_advise(struct virtio_net **pdev,
 #endif
 }
 
+static int
+vhost_user_set_postcopy_listen(struct virtio_net **pdev,
+                       struct VhostUserMsg *msg __rte_unused,
+                       int main_fd __rte_unused)
+{
+       struct virtio_net *dev = *pdev;
+
+       if (dev->mem && dev->mem->nregions) {
+               RTE_LOG(ERR, VHOST_CONFIG,
+                       "Regions already registered at postcopy-listen\n");
+               return VH_RESULT_ERR;
+       }
+       dev->postcopy_listening = 1;
+
+       return VH_RESULT_OK;
+}
+
+static int
+vhost_user_postcopy_end(struct virtio_net **pdev, struct VhostUserMsg *msg,
+                       int main_fd __rte_unused)
+{
+       struct virtio_net *dev = *pdev;
+
+       dev->postcopy_listening = 0;
+       if (dev->postcopy_ufd >= 0) {
+               close(dev->postcopy_ufd);
+               dev->postcopy_ufd = -1;
+       }
+
+       msg->payload.u64 = 0;
+       msg->size = sizeof(msg->payload.u64);
+       msg->fd_num = 0;
+
+       return VH_RESULT_REPLY;
+}
+
 typedef int (*vhost_message_handler_t)(struct virtio_net **pdev,
                                        struct VhostUserMsg *msg,
                                        int main_fd);
@@ -1577,6 +1720,8 @@ static vhost_message_handler_t vhost_message_handlers[VHOST_USER_MAX] = {
        [VHOST_USER_SET_SLAVE_REQ_FD] = vhost_user_set_req_fd,
        [VHOST_USER_IOTLB_MSG] = vhost_user_iotlb_msg,
        [VHOST_USER_POSTCOPY_ADVISE] = vhost_user_set_postcopy_advise,
+       [VHOST_USER_POSTCOPY_LISTEN] = vhost_user_set_postcopy_listen,
+       [VHOST_USER_POSTCOPY_END] = vhost_user_postcopy_end,
 };
 
 
@@ -1591,7 +1736,7 @@ read_vhost_message(int sockfd, struct VhostUserMsg *msg)
        if (ret <= 0)
                return ret;
 
-       if (msg && msg->size) {
+       if (msg->size) {
                if (msg->size > sizeof(msg->payload)) {
                        RTE_LOG(ERR, VHOST_CONFIG,
                                "invalid msg size: %d\n", msg->size);
@@ -1904,11 +2049,6 @@ skip_to_reply:
                if (vdpa_dev->ops->dev_conf)
                        vdpa_dev->ops->dev_conf(dev->vid);
                dev->flags |= VIRTIO_DEV_VDPA_CONFIGURED;
-               if (vhost_user_host_notifier_ctrl(dev->vid, true) != 0) {
-                       RTE_LOG(INFO, VHOST_CONFIG,
-                               "(%d) software relay is used for vDPA, performance may be low.\n",
-                               dev->vid);
-               }
        }
 
        return 0;
@@ -2003,7 +2143,7 @@ static int vhost_user_slave_set_vring_host_notifier(struct virtio_net *dev,
        return process_slave_message_reply(dev, &msg);
 }
 
-int vhost_user_host_notifier_ctrl(int vid, bool enable)
+int rte_vhost_host_notifier_ctrl(int vid, bool enable)
 {
        struct virtio_net *dev;
        struct rte_vdpa_device *vdpa_dev;