vhost: introduce new function helper
[dpdk.git] / lib / librte_vhost / vhost_user.c
index 7a8670c..26cfebe 100644 (file)
@@ -1,5 +1,5 @@
 /* SPDX-License-Identifier: BSD-3-Clause
- * Copyright(c) 2010-2016 Intel Corporation
+ * Copyright(c) 2010-2018 Intel Corporation
  */
 
 /* Security model
@@ -67,6 +67,8 @@ static const char *vhost_message_str[VHOST_USER_MAX] = {
        [VHOST_USER_NET_SET_MTU]  = "VHOST_USER_NET_SET_MTU",
        [VHOST_USER_SET_SLAVE_REQ_FD]  = "VHOST_USER_SET_SLAVE_REQ_FD",
        [VHOST_USER_IOTLB_MSG]  = "VHOST_USER_IOTLB_MSG",
+       [VHOST_USER_CRYPTO_CREATE_SESS] = "VHOST_USER_CRYPTO_CREATE_SESS",
+       [VHOST_USER_CRYPTO_CLOSE_SESS] = "VHOST_USER_CRYPTO_CLOSE_SESS",
 };
 
 static uint64_t
@@ -133,10 +135,7 @@ vhost_user_set_owner(void)
 static int
 vhost_user_reset_owner(struct virtio_net *dev)
 {
-       if (dev->flags & VIRTIO_DEV_RUNNING) {
-               dev->flags &= ~VIRTIO_DEV_RUNNING;
-               dev->notify_ops->destroy_device(dev->vid);
-       }
+       vhost_destroy_device_notify(dev);
 
        cleanup_device(dev, 0);
        reset_device(dev);
@@ -155,6 +154,18 @@ vhost_user_get_features(struct virtio_net *dev)
        return features;
 }
 
+/*
+ * The queue number that we support are requested.
+ */
+static uint32_t
+vhost_user_get_queue_num(struct virtio_net *dev)
+{
+       uint32_t queue_num = 0;
+
+       rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
+       return queue_num;
+}
+
 /*
  * We receive the negotiated features supported by us and the virtio device.
  */
@@ -162,6 +173,8 @@ static int
 vhost_user_set_features(struct virtio_net *dev, uint64_t features)
 {
        uint64_t vhost_features = 0;
+       struct rte_vdpa_device *vdpa_dev;
+       int did = -1;
 
        rte_vhost_driver_get_features(dev->ifname, &vhost_features);
        if (features & ~vhost_features) {
@@ -198,7 +211,7 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t features)
        } else {
                dev->vhost_hlen = sizeof(struct virtio_net_hdr);
        }
-       LOG_DEBUG(VHOST_CONFIG,
+       VHOST_LOG_DEBUG(VHOST_CONFIG,
                "(%d) mergeable RX buffers %s, virtio 1 %s\n",
                dev->vid,
                (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
@@ -224,6 +237,11 @@ vhost_user_set_features(struct virtio_net *dev, uint64_t features)
                }
        }
 
+       did = dev->vdpa_dev_id;
+       vdpa_dev = rte_vdpa_get_device(did);
+       if (vdpa_dev && vdpa_dev->ops->set_features)
+               vdpa_dev->ops->set_features(dev->vid);
+
        return 0;
 }
 
@@ -394,21 +412,26 @@ numa_realloc(struct virtio_net *dev, int index __rte_unused)
 
 /* Converts QEMU virtual address to Vhost virtual address. */
 static uint64_t
-qva_to_vva(struct virtio_net *dev, uint64_t qva)
+qva_to_vva(struct virtio_net *dev, uint64_t qva, uint64_t *len)
 {
-       struct rte_vhost_mem_region *reg;
+       struct rte_vhost_mem_region *r;
        uint32_t i;
 
        /* Find the region where the address lives. */
        for (i = 0; i < dev->mem->nregions; i++) {
-               reg = &dev->mem->regions[i];
+               r = &dev->mem->regions[i];
+
+               if (qva >= r->guest_user_addr &&
+                   qva <  r->guest_user_addr + r->size) {
+
+                       if (unlikely(*len > r->guest_user_addr + r->size - qva))
+                               *len = r->guest_user_addr + r->size - qva;
 
-               if (qva >= reg->guest_user_addr &&
-                   qva <  reg->guest_user_addr + reg->size) {
-                       return qva - reg->guest_user_addr +
-                              reg->host_user_addr;
+                       return qva - r->guest_user_addr +
+                              r->host_user_addr;
                }
        }
+       *len = 0;
 
        return 0;
 }
@@ -421,20 +444,20 @@ qva_to_vva(struct virtio_net *dev, uint64_t qva)
  */
 static uint64_t
 ring_addr_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
-               uint64_t ra, uint64_t size)
+               uint64_t ra, uint64_t *size)
 {
        if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
                uint64_t vva;
 
                vva = vhost_user_iotlb_cache_find(vq, ra,
-                                       &size, VHOST_ACCESS_RW);
+                                       size, VHOST_ACCESS_RW);
                if (!vva)
                        vhost_user_iotlb_miss(dev, ra, VHOST_ACCESS_RW);
 
                return vva;
        }
 
-       return qva_to_vva(dev, ra);
+       return qva_to_vva(dev, ra, size);
 }
 
 static struct virtio_net *
@@ -442,16 +465,18 @@ translate_ring_addresses(struct virtio_net *dev, int vq_index)
 {
        struct vhost_virtqueue *vq = dev->virtqueue[vq_index];
        struct vhost_vring_addr *addr = &vq->ring_addrs;
+       uint64_t len;
 
        /* The addresses are converted from QEMU virtual to Vhost virtual. */
        if (vq->desc && vq->avail && vq->used)
                return dev;
 
+       len = sizeof(struct vring_desc) * vq->size;
        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) {
+                       vq, addr->desc_user_addr, &len);
+       if (vq->desc == 0 || len != sizeof(struct vring_desc) * vq->size) {
                RTE_LOG(DEBUG, VHOST_CONFIG,
-                       "(%d) failed to find desc ring address.\n",
+                       "(%d) failed to map desc ring.\n",
                        dev->vid);
                return dev;
        }
@@ -460,20 +485,26 @@ translate_ring_addresses(struct virtio_net *dev, int vq_index)
        vq = dev->virtqueue[vq_index];
        addr = &vq->ring_addrs;
 
+       len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
        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) {
+                       vq, addr->avail_user_addr, &len);
+       if (vq->avail == 0 ||
+                       len != sizeof(struct vring_avail) +
+                       sizeof(uint16_t) * vq->size) {
                RTE_LOG(DEBUG, VHOST_CONFIG,
-                       "(%d) failed to find avail ring address.\n",
+                       "(%d) failed to map avail ring.\n",
                        dev->vid);
                return dev;
        }
 
+       len = sizeof(struct vring_used) +
+               sizeof(struct vring_used_elem) * vq->size;
        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) {
+                       vq, addr->used_user_addr, &len);
+       if (vq->used == 0 || len != sizeof(struct vring_used) +
+                       sizeof(struct vring_used_elem) * vq->size) {
                RTE_LOG(DEBUG, VHOST_CONFIG,
-                       "(%d) failed to find used ring address.\n",
+                       "(%d) failed to map used ring.\n",
                        dev->vid);
                return dev;
        }
@@ -489,13 +520,13 @@ translate_ring_addresses(struct virtio_net *dev, int vq_index)
 
        vq->log_guest_addr = addr->log_guest_addr;
 
-       LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address desc: %p\n",
+       VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address desc: %p\n",
                        dev->vid, vq->desc);
-       LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address avail: %p\n",
+       VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address avail: %p\n",
                        dev->vid, vq->avail);
-       LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address used: %p\n",
+       VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address used: %p\n",
                        dev->vid, vq->used);
-       LOG_DEBUG(VHOST_CONFIG, "(%d) log_guest_addr: %" PRIx64 "\n",
+       VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) log_guest_addr: %" PRIx64 "\n",
                        dev->vid, vq->log_guest_addr);
 
        return dev;
@@ -553,7 +584,7 @@ vhost_user_set_vring_base(struct virtio_net *dev,
        return 0;
 }
 
-static void
+static int
 add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
                   uint64_t host_phys_addr, uint64_t size)
 {
@@ -563,6 +594,10 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
                dev->max_guest_pages *= 2;
                dev->guest_pages = realloc(dev->guest_pages,
                                        dev->max_guest_pages * sizeof(*page));
+               if (!dev->guest_pages) {
+                       RTE_LOG(ERR, VHOST_CONFIG, "cannot realloc guest_pages\n");
+                       return -1;
+               }
        }
 
        if (dev->nr_guest_pages > 0) {
@@ -571,7 +606,7 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
                if (host_phys_addr == last_page->host_phys_addr +
                                      last_page->size) {
                        last_page->size += size;
-                       return;
+                       return 0;
                }
        }
 
@@ -579,9 +614,11 @@ add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
        page->guest_phys_addr = guest_phys_addr;
        page->host_phys_addr  = host_phys_addr;
        page->size = size;
+
+       return 0;
 }
 
-static void
+static int
 add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
                uint64_t page_size)
 {
@@ -595,7 +632,9 @@ add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
        size = page_size - (guest_phys_addr & (page_size - 1));
        size = RTE_MIN(size, reg_size);
 
-       add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size);
+       if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size) < 0)
+               return -1;
+
        host_user_addr  += size;
        guest_phys_addr += size;
        reg_size -= size;
@@ -604,12 +643,16 @@ add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
                size = RTE_MIN(reg_size, page_size);
                host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)
                                                  host_user_addr);
-               add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size);
+               if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr,
+                               size) < 0)
+                       return -1;
 
                host_user_addr  += size;
                guest_phys_addr += size;
                reg_size -= size;
        }
+
+       return 0;
 }
 
 #ifdef RTE_LIBRTE_VHOST_DEBUG
@@ -663,8 +706,9 @@ vhost_memory_changed(struct VhostUserMemory *new,
 }
 
 static int
-vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
+vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *pmsg)
 {
+       struct virtio_net *dev = *pdev;
        struct VhostUserMemory memory = pmsg->payload.memory;
        struct rte_vhost_mem_region *reg;
        void *mmap_addr;
@@ -672,6 +716,7 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
        uint64_t mmap_offset;
        uint64_t alignment;
        uint32_t i;
+       int populate;
        int fd;
 
        if (memory.nregions > VHOST_MEMORY_MAX_NREGIONS) {
@@ -730,7 +775,17 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
                reg->fd              = fd;
 
                mmap_offset = memory.regions[i].mmap_offset;
-               mmap_size   = reg->size + mmap_offset;
+
+               /* Check for memory_size + mmap_offset overflow */
+               if (mmap_offset >= -reg->size) {
+                       RTE_LOG(ERR, VHOST_CONFIG,
+                               "mmap_offset (%#"PRIx64") and memory_size "
+                               "(%#"PRIx64") overflow\n",
+                               mmap_offset, reg->size);
+                       goto err_mmap;
+               }
+
+               mmap_size = reg->size + mmap_offset;
 
                /* mmap() without flag of MAP_ANONYMOUS, should be called
                 * with length argument aligned with hugepagesz at older
@@ -748,8 +803,9 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
                }
                mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
 
+               populate = (dev->dequeue_zero_copy) ? MAP_POPULATE : 0;
                mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
-                                MAP_SHARED | MAP_POPULATE, fd, 0);
+                                MAP_SHARED | populate, fd, 0);
 
                if (mmap_addr == MAP_FAILED) {
                        RTE_LOG(ERR, VHOST_CONFIG,
@@ -763,7 +819,12 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
                                      mmap_offset;
 
                if (dev->dequeue_zero_copy)
-                       add_guest_pages(dev, reg, alignment);
+                       if (add_guest_pages(dev, reg, alignment) < 0) {
+                               RTE_LOG(ERR, VHOST_CONFIG,
+                                       "adding guest pages to region %u failed.\n",
+                                       i);
+                               goto err_mmap;
+                       }
 
                RTE_LOG(INFO, VHOST_CONFIG,
                        "guest memory region %u, size: 0x%" PRIx64 "\n"
@@ -784,6 +845,25 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
                        mmap_offset);
        }
 
+       for (i = 0; i < dev->nr_vring; i++) {
+               struct vhost_virtqueue *vq = dev->virtqueue[i];
+
+               if (vq->desc || vq->avail || vq->used) {
+                       /*
+                        * If the memory table got updated, the ring addresses
+                        * need to be translated again as virtual addresses have
+                        * changed.
+                        */
+                       vring_invalidate(dev, vq);
+
+                       dev = translate_ring_addresses(dev, i);
+                       if (!dev)
+                               return -1;
+
+                       *pdev = dev;
+               }
+       }
+
        dump_guest_pages(dev);
 
        return 0;
@@ -908,15 +988,13 @@ vhost_user_get_vring_base(struct virtio_net *dev,
        struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
 
        /* We have to stop the queue (virtio) if it is running. */
-       if (dev->flags & VIRTIO_DEV_RUNNING) {
-               dev->flags &= ~VIRTIO_DEV_RUNNING;
-               dev->notify_ops->destroy_device(dev->vid);
-       }
+       vhost_destroy_device_notify(dev);
 
        dev->flags &= ~VIRTIO_DEV_READY;
+       dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
 
-       /* Here we are safe to get the last used index */
-       msg->payload.state.num = vq->last_used_idx;
+       /* Here we are safe to get the last avail index */
+       msg->payload.state.num = vq->last_avail_idx;
 
        RTE_LOG(INFO, VHOST_CONFIG,
                "vring base idx:%d file:%d\n", msg->payload.state.index,
@@ -931,6 +1009,11 @@ vhost_user_get_vring_base(struct virtio_net *dev,
 
        vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
 
+       if (vq->callfd >= 0)
+               close(vq->callfd);
+
+       vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
+
        if (dev->dequeue_zero_copy)
                free_zmbufs(vq);
        rte_free(vq->shadow_used_ring);
@@ -951,16 +1034,24 @@ vhost_user_set_vring_enable(struct virtio_net *dev,
                            VhostUserMsg *msg)
 {
        int enable = (int)msg->payload.state.num;
+       int index = (int)msg->payload.state.index;
+       struct rte_vdpa_device *vdpa_dev;
+       int did = -1;
 
        RTE_LOG(INFO, VHOST_CONFIG,
                "set queue enable: %d to qp idx: %d\n",
-               enable, msg->payload.state.index);
+               enable, index);
+
+       did = dev->vdpa_dev_id;
+       vdpa_dev = rte_vdpa_get_device(did);
+       if (vdpa_dev && vdpa_dev->ops->set_vring_state)
+               vdpa_dev->ops->set_vring_state(dev->vid, index, enable);
 
        if (dev->notify_ops->vring_state_changed)
                dev->notify_ops->vring_state_changed(dev->vid,
-                               msg->payload.state.index, enable);
+                               index, enable);
 
-       dev->virtqueue[msg->payload.state.index]->enabled = enable;
+       dev->virtqueue[index]->enabled = enable;
 
        return 0;
 }
@@ -969,9 +1060,10 @@ static void
 vhost_user_get_protocol_features(struct virtio_net *dev,
                                 struct VhostUserMsg *msg)
 {
-       uint64_t features, protocol_features = VHOST_USER_PROTOCOL_FEATURES;
+       uint64_t features, protocol_features;
 
        rte_vhost_driver_get_features(dev->ifname, &features);
+       rte_vhost_driver_get_protocol_features(dev->ifname, &protocol_features);
 
        /*
         * REPLY_ACK protocol feature is only mandatory for now
@@ -1034,7 +1126,7 @@ vhost_user_set_log_base(struct virtio_net *dev, struct VhostUserMsg *msg)
         * mmap from 0 to workaround a hugepage mmap bug: mmap will
         * fail when offset is not page size aligned.
         */
-       addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+       addr = mmap(0, size + off, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
        close(fd);
        if (addr == MAP_FAILED) {
                RTE_LOG(ERR, VHOST_CONFIG, "mmap log base failed!\n");
@@ -1067,6 +1159,8 @@ static int
 vhost_user_send_rarp(struct virtio_net *dev, struct VhostUserMsg *msg)
 {
        uint8_t *mac = (uint8_t *)&msg->payload.u64;
+       struct rte_vdpa_device *vdpa_dev;
+       int did = -1;
 
        RTE_LOG(DEBUG, VHOST_CONFIG,
                ":: mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
@@ -1082,6 +1176,10 @@ vhost_user_send_rarp(struct virtio_net *dev, struct VhostUserMsg *msg)
         */
        rte_smp_wmb();
        rte_atomic16_set(&dev->broadcast_rarp, 1);
+       did = dev->vdpa_dev_id;
+       vdpa_dev = rte_vdpa_get_device(did);
+       if (vdpa_dev && vdpa_dev->ops->migration_done)
+               vdpa_dev->ops->migration_done(dev->vid);
 
        return 0;
 }
@@ -1174,11 +1272,12 @@ 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;
+       uint64_t vva, len;
 
        switch (imsg->type) {
        case VHOST_IOTLB_UPDATE:
-               vva = qva_to_vva(dev, imsg->uaddr);
+               len = imsg->size;
+               vva = qva_to_vva(dev, imsg->uaddr, &len);
                if (!vva)
                        return -1;
 
@@ -1186,7 +1285,7 @@ vhost_user_iotlb_msg(struct virtio_net **pdev, struct VhostUserMsg *msg)
                        struct vhost_virtqueue *vq = dev->virtqueue[i];
 
                        vhost_user_iotlb_cache_insert(vq, imsg->iova, vva,
-                                       imsg->size, imsg->perm);
+                                       len, imsg->perm);
 
                        if (is_vring_iotlb_update(vq, imsg))
                                *pdev = dev = translate_ring_addresses(dev, i);
@@ -1243,13 +1342,13 @@ read_vhost_message(int sockfd, struct VhostUserMsg *msg)
 }
 
 static int
-send_vhost_message(int sockfd, struct VhostUserMsg *msg)
+send_vhost_message(int sockfd, struct VhostUserMsg *msg, int *fds, int fd_num)
 {
        if (!msg)
                return 0;
 
        return send_fd_message(sockfd, (char *)msg,
-               VHOST_USER_HDR_SIZE + msg->size, NULL, 0);
+               VHOST_USER_HDR_SIZE + msg->size, fds, fd_num);
 }
 
 static int
@@ -1263,7 +1362,23 @@ send_vhost_reply(int sockfd, struct VhostUserMsg *msg)
        msg->flags |= VHOST_USER_VERSION;
        msg->flags |= VHOST_USER_REPLY_MASK;
 
-       return send_vhost_message(sockfd, msg);
+       return send_vhost_message(sockfd, msg, NULL, 0);
+}
+
+static int
+send_vhost_slave_message(struct virtio_net *dev, struct VhostUserMsg *msg,
+                        int *fds, int fd_num)
+{
+       int ret;
+
+       if (msg->flags & VHOST_USER_NEED_REPLY)
+               rte_spinlock_lock(&dev->slave_req_lock);
+
+       ret = send_vhost_message(dev->slave_req_fd, msg, fds, fd_num);
+       if (ret < 0 && (msg->flags & VHOST_USER_NEED_REPLY))
+               rte_spinlock_unlock(&dev->slave_req_lock);
+
+       return ret;
 }
 
 /*
@@ -1343,8 +1458,11 @@ vhost_user_msg_handler(int vid, int fd)
 {
        struct virtio_net *dev;
        struct VhostUserMsg msg;
+       struct rte_vdpa_device *vdpa_dev;
+       int did = -1;
        int ret;
        int unlock_required = 0;
+       uint32_t skip_master = 0;
 
        dev = get_device(vid);
        if (dev == NULL)
@@ -1422,6 +1540,21 @@ vhost_user_msg_handler(int vid, int fd)
 
        }
 
+       if (dev->extern_ops.pre_msg_handle) {
+               uint32_t need_reply;
+
+               ret = (*dev->extern_ops.pre_msg_handle)(dev->vid,
+                               (void *)&msg, &need_reply, &skip_master);
+               if (ret < 0)
+                       goto skip_to_reply;
+
+               if (need_reply)
+                       send_vhost_reply(fd, &msg);
+
+               if (skip_master)
+                       goto skip_to_post_handle;
+       }
+
        switch (msg.request.master) {
        case VHOST_USER_GET_FEATURES:
                msg.payload.u64 = vhost_user_get_features(dev);
@@ -1450,7 +1583,7 @@ vhost_user_msg_handler(int vid, int fd)
                break;
 
        case VHOST_USER_SET_MEM_TABLE:
-               ret = vhost_user_set_mem_table(dev, &msg);
+               ret = vhost_user_set_mem_table(&dev, &msg);
                break;
 
        case VHOST_USER_SET_LOG_BASE:
@@ -1495,7 +1628,7 @@ vhost_user_msg_handler(int vid, int fd)
                break;
 
        case VHOST_USER_GET_QUEUE_NUM:
-               msg.payload.u64 = VHOST_MAX_QUEUE_PAIRS;
+               msg.payload.u64 = (uint64_t)vhost_user_get_queue_num(dev);
                msg.size = sizeof(msg.payload.u64);
                send_vhost_reply(fd, &msg);
                break;
@@ -1522,9 +1655,22 @@ vhost_user_msg_handler(int vid, int fd)
        default:
                ret = -1;
                break;
+       }
+
+skip_to_post_handle:
+       if (dev->extern_ops.post_msg_handle) {
+               uint32_t need_reply;
 
+               ret = (*dev->extern_ops.post_msg_handle)(
+                               dev->vid, (void *)&msg, &need_reply);
+               if (ret < 0)
+                       goto skip_to_reply;
+
+               if (need_reply)
+                       send_vhost_reply(fd, &msg);
        }
 
+skip_to_reply:
        if (unlock_required)
                vhost_user_unlock_all_queue_pairs(dev);
 
@@ -1548,9 +1694,53 @@ vhost_user_msg_handler(int vid, int fd)
                }
        }
 
+       did = dev->vdpa_dev_id;
+       vdpa_dev = rte_vdpa_get_device(did);
+       if (vdpa_dev && virtio_is_ready(dev) &&
+                       !(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED) &&
+                       msg.request.master == VHOST_USER_SET_VRING_ENABLE) {
+               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;
 }
 
+static int process_slave_message_reply(struct virtio_net *dev,
+                                      const VhostUserMsg *msg)
+{
+       VhostUserMsg msg_reply;
+       int ret;
+
+       if ((msg->flags & VHOST_USER_NEED_REPLY) == 0)
+               return 0;
+
+       if (read_vhost_message(dev->slave_req_fd, &msg_reply) < 0) {
+               ret = -1;
+               goto out;
+       }
+
+       if (msg_reply.request.slave != msg->request.slave) {
+               RTE_LOG(ERR, VHOST_CONFIG,
+                       "Received unexpected msg type (%u), expected %u\n",
+                       msg_reply.request.slave, msg->request.slave);
+               ret = -1;
+               goto out;
+       }
+
+       ret = msg_reply.payload.u64 ? -1 : 0;
+
+out:
+       rte_spinlock_unlock(&dev->slave_req_lock);
+       return ret;
+}
+
 int
 vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm)
 {
@@ -1566,7 +1756,7 @@ vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm)
                },
        };
 
-       ret = send_vhost_message(dev->slave_req_fd, &msg);
+       ret = send_vhost_message(dev->slave_req_fd, &msg, NULL, 0);
        if (ret < 0) {
                RTE_LOG(ERR, VHOST_CONFIG,
                                "Failed to send IOTLB miss message (%d)\n",
@@ -1576,3 +1766,99 @@ vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm)
 
        return 0;
 }
+
+static int vhost_user_slave_set_vring_host_notifier(struct virtio_net *dev,
+                                                   int index, int fd,
+                                                   uint64_t offset,
+                                                   uint64_t size)
+{
+       int *fdp = NULL;
+       size_t fd_num = 0;
+       int ret;
+       struct VhostUserMsg msg = {
+               .request.slave = VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG,
+               .flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY,
+               .size = sizeof(msg.payload.area),
+               .payload.area = {
+                       .u64 = index & VHOST_USER_VRING_IDX_MASK,
+                       .size = size,
+                       .offset = offset,
+               },
+       };
+
+       if (fd < 0)
+               msg.payload.area.u64 |= VHOST_USER_VRING_NOFD_MASK;
+       else {
+               fdp = &fd;
+               fd_num = 1;
+       }
+
+       ret = send_vhost_slave_message(dev, &msg, fdp, fd_num);
+       if (ret < 0) {
+               RTE_LOG(ERR, VHOST_CONFIG,
+                       "Failed to set host notifier (%d)\n", ret);
+               return ret;
+       }
+
+       return process_slave_message_reply(dev, &msg);
+}
+
+int vhost_user_host_notifier_ctrl(int vid, bool enable)
+{
+       struct virtio_net *dev;
+       struct rte_vdpa_device *vdpa_dev;
+       int vfio_device_fd, did, ret = 0;
+       uint64_t offset, size;
+       unsigned int i;
+
+       dev = get_device(vid);
+       if (!dev)
+               return -ENODEV;
+
+       did = dev->vdpa_dev_id;
+       if (did < 0)
+               return -EINVAL;
+
+       if (!(dev->features & (1ULL << VIRTIO_F_VERSION_1)) ||
+           !(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) ||
+           !(dev->protocol_features &
+                       (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ)) ||
+           !(dev->protocol_features &
+                       (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD)) ||
+           !(dev->protocol_features &
+                       (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER)))
+               return -ENOTSUP;
+
+       vdpa_dev = rte_vdpa_get_device(did);
+
+       RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_vfio_device_fd, -ENOTSUP);
+       RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_notify_area, -ENOTSUP);
+
+       vfio_device_fd = vdpa_dev->ops->get_vfio_device_fd(vid);
+       if (vfio_device_fd < 0)
+               return -ENOTSUP;
+
+       if (enable) {
+               for (i = 0; i < dev->nr_vring; i++) {
+                       if (vdpa_dev->ops->get_notify_area(vid, i, &offset,
+                                       &size) < 0) {
+                               ret = -ENOTSUP;
+                               goto disable;
+                       }
+
+                       if (vhost_user_slave_set_vring_host_notifier(dev, i,
+                                       vfio_device_fd, offset, size) < 0) {
+                               ret = -EFAULT;
+                               goto disable;
+                       }
+               }
+       } else {
+disable:
+               for (i = 0; i < dev->nr_vring; i++) {
+                       vhost_user_slave_set_vring_host_notifier(dev, i, -1,
+                                       0, 0);
+               }
+       }
+
+       return ret;
+}