net/virtio: rationalize queue flushing
[dpdk.git] / lib / librte_vhost / vhost_user.c
index b2159e0..1dd1a61 100644 (file)
@@ -1,34 +1,5 @@
-/*-
- *   BSD LICENSE
- *
- *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
- *   All rights reserved.
- *
- *   Redistribution and use in source and binary forms, with or without
- *   modification, are permitted provided that the following conditions
- *   are met:
- *
- *     * Redistributions of source code must retain the above copyright
- *       notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above copyright
- *       notice, this list of conditions and the following disclaimer in
- *       the documentation and/or other materials provided with the
- *       distribution.
- *     * Neither the name of Intel Corporation nor the names of its
- *       contributors may be used to endorse or promote products derived
- *       from this software without specific prior written permission.
- *
- *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2010-2016 Intel Corporation
  */
 
 #include <stdint.h>
@@ -183,7 +154,22 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t features)
                return -1;
        }
 
-       if ((dev->flags & VIRTIO_DEV_RUNNING) && dev->features != features) {
+       if (dev->flags & VIRTIO_DEV_RUNNING) {
+               if (dev->features == features)
+                       return 0;
+
+               /*
+                * Error out if master tries to change features while device is
+                * in running state. The exception being VHOST_F_LOG_ALL, which
+                * is enabled when the live-migration starts.
+                */
+               if ((dev->features ^ features) & ~(1ULL << VHOST_F_LOG_ALL)) {
+                       RTE_LOG(ERR, VHOST_CONFIG,
+                               "(%d) features changed while device is running.\n",
+                               dev->vid);
+                       return -1;
+               }
+
                if (dev->notify_ops->features_changed)
                        dev->notify_ops->features_changed(dev->vid, features);
        }
@@ -201,6 +187,25 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t features)
                (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
                (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
 
+       if (!(dev->features & (1ULL << VIRTIO_NET_F_MQ))) {
+               /*
+                * Remove all but first queue pair if MQ hasn't been
+                * negotiated. This is safe because the device is not
+                * running at this stage.
+                */
+               while (dev->nr_vring > 2) {
+                       struct vhost_virtqueue *vq;
+
+                       vq = dev->virtqueue[--dev->nr_vring];
+                       if (!vq)
+                               continue;
+
+                       dev->virtqueue[dev->nr_vring] = NULL;
+                       cleanup_vq(vq, 1);
+                       free_vq(vq);
+               }
+       }
+
        return 0;
 }
 
@@ -227,6 +232,7 @@ vhost_user_set_vring_num(struct virtio_net *dev,
                                "zero copy is force disabled\n");
                        dev->dequeue_zero_copy = 0;
                }
+               TAILQ_INIT(&vq->zmbuf_list);
        }
 
        vq->shadow_used_ring = rte_malloc(NULL,
@@ -261,6 +267,9 @@ numa_realloc(struct virtio_net *dev, int index)
        int oldnode, newnode;
        struct virtio_net *old_dev;
        struct vhost_virtqueue *old_vq, *vq;
+       struct zcopy_mbuf *new_zmbuf;
+       struct vring_used_elem *new_shadow_used_ring;
+       struct batch_copy_elem *new_batch_copy_elems;
        int ret;
 
        old_dev = dev;
@@ -285,6 +294,33 @@ numa_realloc(struct virtio_net *dev, int index)
                        return dev;
 
                memcpy(vq, old_vq, sizeof(*vq));
+               TAILQ_INIT(&vq->zmbuf_list);
+
+               new_zmbuf = rte_malloc_socket(NULL, vq->zmbuf_size *
+                       sizeof(struct zcopy_mbuf), 0, newnode);
+               if (new_zmbuf) {
+                       rte_free(vq->zmbufs);
+                       vq->zmbufs = new_zmbuf;
+               }
+
+               new_shadow_used_ring = rte_malloc_socket(NULL,
+                       vq->size * sizeof(struct vring_used_elem),
+                       RTE_CACHE_LINE_SIZE,
+                       newnode);
+               if (new_shadow_used_ring) {
+                       rte_free(vq->shadow_used_ring);
+                       vq->shadow_used_ring = new_shadow_used_ring;
+               }
+
+               new_batch_copy_elems = rte_malloc_socket(NULL,
+                       vq->size * sizeof(struct batch_copy_elem),
+                       RTE_CACHE_LINE_SIZE,
+                       newnode);
+               if (new_batch_copy_elems) {
+                       rte_free(vq->batch_copy_elems);
+                       vq->batch_copy_elems = new_batch_copy_elems;
+               }
+
                rte_free(old_vq);
        }
 
@@ -314,6 +350,9 @@ out:
        dev->virtqueue[index] = vq;
        vhost_devices[dev->vid] = dev;
 
+       if (old_vq != vq)
+               vhost_user_iotlb_init(dev, index);
+
        return dev;
 }
 #else
@@ -324,10 +363,7 @@ numa_realloc(struct virtio_net *dev, int index __rte_unused)
 }
 #endif
 
-/*
- * Converts QEMU virtual address to Vhost virtual address. This function is
- * used to convert the ring addresses to our address space.
- */
+/* Converts QEMU virtual address to Vhost virtual address. */
 static uint64_t
 qva_to_vva(struct virtio_net *dev, uint64_t qva)
 {
@@ -348,29 +384,28 @@ qva_to_vva(struct virtio_net *dev, uint64_t qva)
        return 0;
 }
 
+
 /*
- * The virtio device sends us the desc, used and avail ring addresses.
- * This function then converts these to our address space.
+ * Converts ring address to Vhost virtual address.
+ * If IOMMU is enabled, the ring address is a guest IO virtual address,
+ * else it is a QEMU virtual address.
  */
-static int
-vhost_user_set_vring_addr(struct virtio_net *dev, VhostUserMsg *msg)
+static uint64_t
+ring_addr_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
+               uint64_t ra, uint64_t size)
 {
-       struct vhost_virtqueue *vq;
-       struct vhost_vring_addr *addr = &msg->payload.addr;
-
-       if (dev->mem == NULL)
-               return -1;
+       if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
+               uint64_t vva;
 
-       /* addr->index refers to the queue index. The txq 1, rxq is 0. */
-       vq = dev->virtqueue[msg->payload.addr.index];
+               vva = vhost_user_iotlb_cache_find(vq, ra,
+                                       &size, VHOST_ACCESS_RW);
+               if (!vva)
+                       vhost_user_iotlb_miss(dev, ra, VHOST_ACCESS_RW);
 
-       /*
-        * Rings addresses should not be interpreted as long as the ring is not
-        * started and enabled
-        */
-       memcpy(&vq->ring_addrs, addr, sizeof(*addr));
+               return vva;
+       }
 
-       return 0;
+       return qva_to_vva(dev, ra);
 }
 
 static struct virtio_net *
@@ -380,34 +415,38 @@ translate_ring_addresses(struct virtio_net *dev, int vq_index)
        struct vhost_vring_addr *addr = &vq->ring_addrs;
 
        /* The addresses are converted from QEMU virtual to Vhost virtual. */
-       vq->desc = (struct vring_desc *)(uintptr_t)qva_to_vva(dev,
-                       addr->desc_user_addr);
+       if (vq->desc && vq->avail && vq->used)
+               return dev;
+
+       vq->desc = (struct vring_desc *)(uintptr_t)ring_addr_to_vva(dev,
+                       vq, addr->desc_user_addr, sizeof(struct vring_desc));
        if (vq->desc == 0) {
-               RTE_LOG(ERR, VHOST_CONFIG,
+               RTE_LOG(DEBUG, VHOST_CONFIG,
                        "(%d) failed to find desc ring address.\n",
                        dev->vid);
-               return NULL;
+               return dev;
        }
 
        dev = numa_realloc(dev, vq_index);
        vq = dev->virtqueue[vq_index];
+       addr = &vq->ring_addrs;
 
-       vq->avail = (struct vring_avail *)(uintptr_t)qva_to_vva(dev,
-                       addr->avail_user_addr);
+       vq->avail = (struct vring_avail *)(uintptr_t)ring_addr_to_vva(dev,
+                       vq, addr->avail_user_addr, sizeof(struct vring_avail));
        if (vq->avail == 0) {
-               RTE_LOG(ERR, VHOST_CONFIG,
+               RTE_LOG(DEBUG, VHOST_CONFIG,
                        "(%d) failed to find avail ring address.\n",
                        dev->vid);
-               return NULL;
+               return dev;
        }
 
-       vq->used = (struct vring_used *)(uintptr_t)qva_to_vva(dev,
-                       addr->used_user_addr);
+       vq->used = (struct vring_used *)(uintptr_t)ring_addr_to_vva(dev,
+                       vq, addr->used_user_addr, sizeof(struct vring_used));
        if (vq->used == 0) {
-               RTE_LOG(ERR, VHOST_CONFIG,
+               RTE_LOG(DEBUG, VHOST_CONFIG,
                        "(%d) failed to find used ring address.\n",
                        dev->vid);
-               return NULL;
+               return dev;
        }
 
        if (vq->last_used_idx != vq->used->idx) {
@@ -433,6 +472,43 @@ translate_ring_addresses(struct virtio_net *dev, int vq_index)
        return dev;
 }
 
+/*
+ * The virtio device sends us the desc, used and avail ring addresses.
+ * This function then converts these to our address space.
+ */
+static int
+vhost_user_set_vring_addr(struct virtio_net **pdev, VhostUserMsg *msg)
+{
+       struct vhost_virtqueue *vq;
+       struct vhost_vring_addr *addr = &msg->payload.addr;
+       struct virtio_net *dev = *pdev;
+
+       if (dev->mem == NULL)
+               return -1;
+
+       /* addr->index refers to the queue index. The txq 1, rxq is 0. */
+       vq = dev->virtqueue[msg->payload.addr.index];
+
+       /*
+        * Rings addresses should not be interpreted as long as the ring is not
+        * started and enabled
+        */
+       memcpy(&vq->ring_addrs, addr, sizeof(*addr));
+
+       vring_invalidate(dev, vq);
+
+       if (vq->enabled && (dev->features &
+                               (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) {
+               dev = translate_ring_addresses(dev, msg->payload.state.index);
+               if (!dev)
+                       return -1;
+
+               *pdev = dev;
+       }
+
+       return 0;
+}
+
 /*
  * The virtio device sends us the available ring last used index.
  */
@@ -486,7 +562,7 @@ add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
        uint64_t host_phys_addr;
        uint64_t size;
 
-       host_phys_addr = rte_mem_virt2phy((void *)(uintptr_t)host_user_addr);
+       host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)host_user_addr);
        size = page_size - (guest_phys_addr & (page_size - 1));
        size = RTE_MIN(size, reg_size);
 
@@ -497,7 +573,7 @@ add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
 
        while (reg_size > 0) {
                size = RTE_MIN(reg_size, page_size);
-               host_phys_addr = rte_mem_virt2phy((void *)(uintptr_t)
+               host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)
                                                  host_user_addr);
                add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size);
 
@@ -533,6 +609,30 @@ dump_guest_pages(struct virtio_net *dev)
 #define dump_guest_pages(dev)
 #endif
 
+static bool
+vhost_memory_changed(struct VhostUserMemory *new,
+                    struct rte_vhost_memory *old)
+{
+       uint32_t i;
+
+       if (new->nregions != old->nregions)
+               return true;
+
+       for (i = 0; i < new->nregions; ++i) {
+               VhostUserMemoryRegion *new_r = &new->regions[i];
+               struct rte_vhost_mem_region *old_r = &old->regions[i];
+
+               if (new_r->guest_phys_addr != old_r->guest_phys_addr)
+                       return true;
+               if (new_r->memory_size != old_r->size)
+                       return true;
+               if (new_r->userspace_addr != old_r->guest_user_addr)
+                       return true;
+       }
+
+       return false;
+}
+
 static int
 vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
 {
@@ -545,6 +645,16 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
        uint32_t i;
        int fd;
 
+       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++)
+                       close(pmsg->fds[i]);
+
+               return 0;
+       }
+
        if (dev->mem) {
                free_mem_region(dev);
                rte_free(dev->mem);
@@ -653,7 +763,7 @@ err_mmap:
 static int
 vq_is_ready(struct vhost_virtqueue *vq)
 {
-       return vq && vq->desc   &&
+       return vq && vq->desc && vq->avail && vq->used &&
               vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
               vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD;
 }
@@ -715,15 +825,12 @@ vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *pmsg)
        RTE_LOG(INFO, VHOST_CONFIG,
                "vring kick idx:%d file:%d\n", file.index, file.fd);
 
-       /*
-        * Interpret ring addresses only when ring is started and enabled.
-        * This is now if protocol features aren't supported.
-        */
-       if (!(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) {
-               *pdev = dev = translate_ring_addresses(dev, file.index);
-               if (!dev)
-                       return;
-       }
+       /* Interpret ring addresses only when ring is started. */
+       dev = translate_ring_addresses(dev, file.index);
+       if (!dev)
+               return;
+
+       *pdev = dev;
 
        vq = dev->virtqueue[file.index];
 
@@ -805,29 +912,15 @@ vhost_user_get_vring_base(struct virtio_net *dev,
  * enable the virtio queue pair.
  */
 static int
-vhost_user_set_vring_enable(struct virtio_net **pdev,
+vhost_user_set_vring_enable(struct virtio_net *dev,
                            VhostUserMsg *msg)
 {
-       struct virtio_net *dev = *pdev;
        int enable = (int)msg->payload.state.num;
 
        RTE_LOG(INFO, VHOST_CONFIG,
                "set queue enable: %d to qp idx: %d\n",
                enable, msg->payload.state.index);
 
-       /*
-        * Interpret ring addresses only when ring is started and enabled.
-        * This is now if protocol features are supported.
-        */
-       if (enable && (dev->features &
-                               (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) {
-               dev = translate_ring_addresses(dev, msg->payload.state.index);
-               if (!dev)
-                       return -1;
-
-               *pdev = dev;
-       }
-
        if (dev->notify_ops->vring_state_changed)
                dev->notify_ops->vring_state_changed(dev->vid,
                                msg->payload.state.index, enable);
@@ -837,6 +930,27 @@ vhost_user_set_vring_enable(struct virtio_net **pdev,
        return 0;
 }
 
+static void
+vhost_user_get_protocol_features(struct virtio_net *dev,
+                                struct VhostUserMsg *msg)
+{
+       uint64_t features, protocol_features = VHOST_USER_PROTOCOL_FEATURES;
+
+       rte_vhost_driver_get_features(dev->ifname, &features);
+
+       /*
+        * REPLY_ACK protocol feature is only mandatory for now
+        * for IOMMU feature. If IOMMU is explicitly disabled by the
+        * application, disable also REPLY_ACK feature for older buggy
+        * Qemu versions (from v2.7.0 to v2.9.0).
+        */
+       if (!(features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
+               protocol_features &= ~(1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK);
+
+       msg->payload.u64 = protocol_features;
+       msg->size = sizeof(msg->payload.u64);
+}
+
 static void
 vhost_user_set_protocol_features(struct virtio_net *dev,
                                 uint64_t protocol_features)
@@ -962,8 +1076,58 @@ vhost_user_set_req_fd(struct virtio_net *dev, struct VhostUserMsg *msg)
 }
 
 static int
-vhost_user_iotlb_msg(struct virtio_net *dev, struct VhostUserMsg *msg)
+is_vring_iotlb_update(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
+{
+       struct vhost_vring_addr *ra;
+       uint64_t start, end;
+
+       start = imsg->iova;
+       end = start + imsg->size;
+
+       ra = &vq->ring_addrs;
+       if (ra->desc_user_addr >= start && ra->desc_user_addr < end)
+               return 1;
+       if (ra->avail_user_addr >= start && ra->avail_user_addr < end)
+               return 1;
+       if (ra->used_user_addr >= start && ra->used_user_addr < end)
+               return 1;
+
+       return 0;
+}
+
+static int
+is_vring_iotlb_invalidate(struct vhost_virtqueue *vq,
+                               struct vhost_iotlb_msg *imsg)
+{
+       uint64_t istart, iend, vstart, vend;
+
+       istart = imsg->iova;
+       iend = istart + imsg->size - 1;
+
+       vstart = (uintptr_t)vq->desc;
+       vend = vstart + sizeof(struct vring_desc) * vq->size - 1;
+       if (vstart <= iend && istart <= vend)
+               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)
+               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)
+               return 1;
+
+       return 0;
+}
+
+static int
+vhost_user_iotlb_msg(struct virtio_net **pdev, struct VhostUserMsg *msg)
 {
+       struct virtio_net *dev = *pdev;
        struct vhost_iotlb_msg *imsg = &msg->payload.iotlb;
        uint16_t i;
        uint64_t vva;
@@ -979,6 +1143,9 @@ vhost_user_iotlb_msg(struct virtio_net *dev, struct VhostUserMsg *msg)
 
                        vhost_user_iotlb_cache_insert(vq, imsg->iova, vva,
                                        imsg->size, imsg->perm);
+
+                       if (is_vring_iotlb_update(vq, imsg))
+                               *pdev = dev = translate_ring_addresses(dev, i);
                }
                break;
        case VHOST_IOTLB_INVALIDATE:
@@ -987,6 +1154,9 @@ vhost_user_iotlb_msg(struct virtio_net *dev, struct VhostUserMsg *msg)
 
                        vhost_user_iotlb_cache_remove(vq, imsg->iova,
                                        imsg->size);
+
+                       if (is_vring_iotlb_invalidate(vq, imsg))
+                               vring_invalidate(dev, vq);
                }
                break;
        default:
@@ -1060,7 +1230,7 @@ vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev, VhostUserMsg *msg)
 {
        uint16_t vring_idx;
 
-       switch (msg->request) {
+       switch (msg->request.master) {
        case VHOST_USER_SET_VRING_KICK:
        case VHOST_USER_SET_VRING_CALL:
        case VHOST_USER_SET_VRING_ERR:
@@ -1090,12 +1260,47 @@ vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev, VhostUserMsg *msg)
        return alloc_vring_queue(dev, vring_idx);
 }
 
+static void
+vhost_user_lock_all_queue_pairs(struct virtio_net *dev)
+{
+       unsigned int i = 0;
+       unsigned int vq_num = 0;
+
+       while (vq_num < dev->nr_vring) {
+               struct vhost_virtqueue *vq = dev->virtqueue[i];
+
+               if (vq) {
+                       rte_spinlock_lock(&vq->access_lock);
+                       vq_num++;
+               }
+               i++;
+       }
+}
+
+static void
+vhost_user_unlock_all_queue_pairs(struct virtio_net *dev)
+{
+       unsigned int i = 0;
+       unsigned int vq_num = 0;
+
+       while (vq_num < dev->nr_vring) {
+               struct vhost_virtqueue *vq = dev->virtqueue[i];
+
+               if (vq) {
+                       rte_spinlock_unlock(&vq->access_lock);
+                       vq_num++;
+               }
+               i++;
+       }
+}
+
 int
 vhost_user_msg_handler(int vid, int fd)
 {
        struct virtio_net *dev;
        struct VhostUserMsg msg;
        int ret;
+       int unlock_required = 0;
 
        dev = get_device(vid);
        if (dev == NULL)
@@ -1112,7 +1317,7 @@ vhost_user_msg_handler(int vid, int fd)
        }
 
        ret = read_vhost_message(fd, &msg);
-       if (ret <= 0 || msg.request >= VHOST_USER_MAX) {
+       if (ret <= 0 || msg.request.master >= VHOST_USER_MAX) {
                if (ret < 0)
                        RTE_LOG(ERR, VHOST_CONFIG,
                                "vhost read message failed\n");
@@ -1127,8 +1332,12 @@ vhost_user_msg_handler(int vid, int fd)
        }
 
        ret = 0;
-       RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
-               vhost_message_str[msg.request]);
+       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]);
 
        ret = vhost_user_check_and_alloc_queue_pair(dev, &msg);
        if (ret < 0) {
@@ -1137,19 +1346,52 @@ vhost_user_msg_handler(int vid, int fd)
                return -1;
        }
 
-       switch (msg.request) {
+       /*
+        * Note: we don't lock all queues on VHOST_USER_GET_VRING_BASE,
+        * since it is sent when virtio stops and device is destroyed.
+        * destroy_device waits for queues to be inactive, so it is safe.
+        * Otherwise taking the access_lock would cause a dead lock.
+        */
+       switch (msg.request.master) {
+       case VHOST_USER_SET_FEATURES:
+       case VHOST_USER_SET_PROTOCOL_FEATURES:
+       case VHOST_USER_SET_OWNER:
+       case VHOST_USER_RESET_OWNER:
+       case VHOST_USER_SET_MEM_TABLE:
+       case VHOST_USER_SET_LOG_BASE:
+       case VHOST_USER_SET_LOG_FD:
+       case VHOST_USER_SET_VRING_NUM:
+       case VHOST_USER_SET_VRING_ADDR:
+       case VHOST_USER_SET_VRING_BASE:
+       case VHOST_USER_SET_VRING_KICK:
+       case VHOST_USER_SET_VRING_CALL:
+       case VHOST_USER_SET_VRING_ERR:
+       case VHOST_USER_SET_VRING_ENABLE:
+       case VHOST_USER_SEND_RARP:
+       case VHOST_USER_NET_SET_MTU:
+       case VHOST_USER_SET_SLAVE_REQ_FD:
+               vhost_user_lock_all_queue_pairs(dev);
+               unlock_required = 1;
+               break;
+       default:
+               break;
+
+       }
+
+       switch (msg.request.master) {
        case VHOST_USER_GET_FEATURES:
                msg.payload.u64 = vhost_user_get_features(dev);
                msg.size = sizeof(msg.payload.u64);
                send_vhost_reply(fd, &msg);
                break;
        case VHOST_USER_SET_FEATURES:
-               vhost_user_set_features(dev, msg.payload.u64);
+               ret = vhost_user_set_features(dev, msg.payload.u64);
+               if (ret)
+                       return -1;
                break;
 
        case VHOST_USER_GET_PROTOCOL_FEATURES:
-               msg.payload.u64 = VHOST_USER_PROTOCOL_FEATURES;
-               msg.size = sizeof(msg.payload.u64);
+               vhost_user_get_protocol_features(dev, &msg);
                send_vhost_reply(fd, &msg);
                break;
        case VHOST_USER_SET_PROTOCOL_FEATURES:
@@ -1183,7 +1425,7 @@ vhost_user_msg_handler(int vid, int fd)
                vhost_user_set_vring_num(dev, &msg);
                break;
        case VHOST_USER_SET_VRING_ADDR:
-               vhost_user_set_vring_addr(dev, &msg);
+               vhost_user_set_vring_addr(&dev, &msg);
                break;
        case VHOST_USER_SET_VRING_BASE:
                vhost_user_set_vring_base(dev, &msg);
@@ -1215,7 +1457,7 @@ vhost_user_msg_handler(int vid, int fd)
                break;
 
        case VHOST_USER_SET_VRING_ENABLE:
-               vhost_user_set_vring_enable(&dev, &msg);
+               vhost_user_set_vring_enable(dev, &msg);
                break;
        case VHOST_USER_SEND_RARP:
                vhost_user_send_rarp(dev, &msg);
@@ -1230,7 +1472,7 @@ vhost_user_msg_handler(int vid, int fd)
                break;
 
        case VHOST_USER_IOTLB_MSG:
-               ret = vhost_user_iotlb_msg(dev, &msg);
+               ret = vhost_user_iotlb_msg(&dev, &msg);
                break;
 
        default:
@@ -1239,11 +1481,8 @@ vhost_user_msg_handler(int vid, int fd)
 
        }
 
-       /*
-        * The virtio_net struct might have been reallocated on a different
-        * NUMA node, so dev pointer might no more be valid.
-        */
-       dev = get_device(vid);
+       if (unlock_required)
+               vhost_user_unlock_all_queue_pairs(dev);
 
        if (msg.flags & VHOST_USER_NEED_REPLY) {
                msg.payload.u64 = !!ret;
@@ -1273,7 +1512,7 @@ vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm)
 {
        int ret;
        struct VhostUserMsg msg = {
-               .request = (enum VhostUserRequest)VHOST_USER_SLAVE_IOTLB_MSG,
+               .request.slave = VHOST_USER_SLAVE_IOTLB_MSG,
                .flags = VHOST_USER_VERSION,
                .size = sizeof(msg.payload.iotlb),
                .payload.iotlb = {