vhost: introduce postcopy advise message
authorMaxime Coquelin <maxime.coquelin@redhat.com>
Fri, 12 Oct 2018 12:40:39 +0000 (14:40 +0200)
committerFerruh Yigit <ferruh.yigit@intel.com>
Thu, 18 Oct 2018 08:24:39 +0000 (10:24 +0200)
This patch opens a userfaultfd and sends it back to Qemu's
VHOST_USER_POSTCOPY_ADVISE request.

Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
lib/librte_vhost/vhost.h
lib/librte_vhost/vhost_user.c
lib/librte_vhost/vhost_user.h

index c12f9d8..d81da06 100644 (file)
@@ -368,6 +368,8 @@ struct virtio_net {
        int                     slave_req_fd;
        rte_spinlock_t          slave_req_lock;
 
+       int                     postcopy_ufd;
+
        /*
         * Device id to identify a specific backend device.
         * It's set to -1 for the default software implementation.
index 99adced..970ad58 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
 #include <sys/mman.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/syscall.h>
 #include <assert.h>
 #ifdef RTE_LIBRTE_VHOST_NUMA
 #include <numaif.h>
 #endif
+#ifdef RTE_LIBRTE_VHOST_POSTCOPY
+#include <linux/userfaultfd.h>
+#endif
 
 #include <rte_common.h>
 #include <rte_malloc.h>
@@ -69,6 +75,7 @@ static const char *vhost_message_str[VHOST_USER_MAX] = {
        [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",
+       [VHOST_USER_POSTCOPY_ADVISE]  = "VHOST_USER_POSTCOPY_ADVISE",
 };
 
 static uint64_t
@@ -120,6 +127,11 @@ vhost_backend_cleanup(struct virtio_net *dev)
                close(dev->slave_req_fd);
                dev->slave_req_fd = -1;
        }
+
+       if (dev->postcopy_ufd >= 0) {
+               close(dev->postcopy_ufd);
+               dev->postcopy_ufd = -1;
+       }
 }
 
 /*
@@ -1500,6 +1512,43 @@ vhost_user_iotlb_msg(struct virtio_net **pdev, struct VhostUserMsg *msg,
        return VH_RESULT_OK;
 }
 
+static int
+vhost_user_set_postcopy_advise(struct virtio_net **pdev,
+                       struct VhostUserMsg *msg,
+                       int main_fd __rte_unused)
+{
+       struct virtio_net *dev = *pdev;
+#ifdef RTE_LIBRTE_VHOST_POSTCOPY
+       struct uffdio_api api_struct;
+
+       dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
+
+       if (dev->postcopy_ufd == -1) {
+               RTE_LOG(ERR, VHOST_CONFIG, "Userfaultfd not available: %s\n",
+                       strerror(errno));
+               return VH_RESULT_ERR;
+       }
+       api_struct.api = UFFD_API;
+       api_struct.features = 0;
+       if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) {
+               RTE_LOG(ERR, VHOST_CONFIG, "UFFDIO_API ioctl failure: %s\n",
+                       strerror(errno));
+               close(dev->postcopy_ufd);
+               dev->postcopy_ufd = -1;
+               return VH_RESULT_ERR;
+       }
+       msg->fds[0] = dev->postcopy_ufd;
+       msg->fd_num = 1;
+
+       return VH_RESULT_REPLY;
+#else
+       dev->postcopy_ufd = -1;
+       msg->fd_num = 0;
+
+       return VH_RESULT_ERR;
+#endif
+}
+
 typedef int (*vhost_message_handler_t)(struct virtio_net **pdev,
                                        struct VhostUserMsg *msg,
                                        int main_fd);
@@ -1527,6 +1576,7 @@ static vhost_message_handler_t vhost_message_handlers[VHOST_USER_MAX] = {
        [VHOST_USER_NET_SET_MTU] = vhost_user_net_set_mtu,
        [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,
 };
 
 
index dd0262f..2030b40 100644 (file)
@@ -50,7 +50,8 @@ typedef enum VhostUserRequest {
        VHOST_USER_IOTLB_MSG = 22,
        VHOST_USER_CRYPTO_CREATE_SESS = 26,
        VHOST_USER_CRYPTO_CLOSE_SESS = 27,
-       VHOST_USER_MAX = 28
+       VHOST_USER_POSTCOPY_ADVISE = 28,
+       VHOST_USER_MAX = 29
 } VhostUserRequest;
 
 typedef enum VhostUserSlaveRequest {