diff mbox series

[RFC,v2,2/4] vhost-user: support programming VFIO container in master

Message ID 20180919062834.30103-3-tiwei.bie@intel.com (mailing list archive)
State New, archived
Headers show
Series Supporting programming IOMMU in QEMU (vDPA/vhost-user) | expand

Commit Message

Tiwei Bie Sept. 19, 2018, 6:28 a.m. UTC
This patch introduces a slave message to allow slave to share
its VFIO container fd to master and do the IOMMU programming
based on virtio device's DMA address space for the VFIO groups
inside this VFIO container in QEMU.

For the vhost backends which support vDPA, they can leverage
this message to ask master to do the IOMMU programming in QEMU
for the vDPA device in backend.

Signed-off-by: Tiwei Bie <tiwei.bie@intel.com>
---
 docs/interop/vhost-user.txt    | 21 ++++++++++++++++
 hw/virtio/vhost-user.c         | 45 ++++++++++++++++++++++++++++++++++
 include/hw/virtio/vhost-user.h |  2 ++
 3 files changed, 68 insertions(+)
diff mbox series

Patch

diff --git a/docs/interop/vhost-user.txt b/docs/interop/vhost-user.txt
index f59667f498..2ac250aa01 100644
--- a/docs/interop/vhost-user.txt
+++ b/docs/interop/vhost-user.txt
@@ -397,6 +397,7 @@  Protocol features
 #define VHOST_USER_PROTOCOL_F_CONFIG         9
 #define VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD  10
 #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER  11
+#define VHOST_USER_PROTOCOL_F_VFIO_CONTAINER 12
 
 Master message types
 --------------------
@@ -815,6 +816,26 @@  Slave message types
       This request should be sent only when VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
       protocol feature has been successfully negotiated.
 
+ * VHOST_USER_SLAVE_VFIO_CONTAINER_MSG
+
+      Id: 4
+      Equivalent ioctl: N/A
+      Slave payload: N/A
+      Master payload: N/A
+
+      When VHOST_USER_PROTOCOL_F_VFIO_CONTAINER is negotiated, vhost-user
+      slave could send this request to share its VFIO container fd via
+      ancillary data to master. Before sending a VFIO container fd to
+      master, slave should make sure that IOMMU type has already been
+      set correctly. After receiving this request from slave, master will
+      destroy the existing VFIO container (including clearing all the DMA
+      mappings and closing the container fd) if any and setup a new VFIO
+      container (including clearing all the existing DMA mappings in this
+      new container and setup DMA mappings for this container based on
+      virtio device's DMA address space) if the request is sent with a
+      file descriptor.
+
+
 VHOST_USER_PROTOCOL_F_REPLY_ACK:
 -------------------------------
 The original vhost-user specification only demands replies for certain
diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c
index b041343632..d53787529d 100644
--- a/hw/virtio/vhost-user.c
+++ b/hw/virtio/vhost-user.c
@@ -52,6 +52,7 @@  enum VhostUserProtocolFeature {
     VHOST_USER_PROTOCOL_F_CONFIG = 9,
     VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD = 10,
     VHOST_USER_PROTOCOL_F_HOST_NOTIFIER = 11,
+    VHOST_USER_PROTOCOL_F_VFIO_CONTAINER = 12,
     VHOST_USER_PROTOCOL_F_MAX
 };
 
@@ -97,6 +98,7 @@  typedef enum VhostUserSlaveRequest {
     VHOST_USER_SLAVE_IOTLB_MSG = 1,
     VHOST_USER_SLAVE_CONFIG_CHANGE_MSG = 2,
     VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG = 3,
+    VHOST_USER_SLAVE_VFIO_CONTAINER_MSG = 4,
     VHOST_USER_SLAVE_MAX
 }  VhostUserSlaveRequest;
 
@@ -949,6 +951,41 @@  static int vhost_user_slave_handle_vring_host_notifier(struct vhost_dev *dev,
     return 0;
 }
 
+static int vhost_user_slave_handle_vfio_container(struct vhost_dev *dev,
+                                                  int *fd)
+{
+    struct vhost_user *u = dev->opaque;
+    VhostUserState *user = u->user;
+    VirtIODevice *vdev = dev->vdev;
+    int container_fd = fd[0];
+    VFIOContainer *container;
+
+    if (!virtio_has_feature(dev->protocol_features,
+                            VHOST_USER_PROTOCOL_F_VFIO_CONTAINER) ||
+        vdev == NULL) {
+        return -1;
+    }
+
+    if (user->vfio_container) {
+        vfio_free_container(user->vfio_container);
+        user->vfio_container = NULL;
+    }
+
+    if (container_fd < 0) {
+        return 0;
+    }
+
+    container = vfio_new_container(container_fd, vdev->dma_as, NULL);
+    if (container == NULL) {
+        return -1;
+    }
+
+    user->vfio_container = container;
+    fd[0] = -1;
+
+    return 0;
+}
+
 static void slave_read(void *opaque)
 {
     struct vhost_dev *dev = opaque;
@@ -1021,6 +1058,9 @@  static void slave_read(void *opaque)
         ret = vhost_user_slave_handle_vring_host_notifier(dev, &payload.area,
                                                           fd[0]);
         break;
+    case VHOST_USER_SLAVE_VFIO_CONTAINER_MSG:
+        ret = vhost_user_slave_handle_vfio_container(dev, fd);
+        break;
     default:
         error_report("Received unexpected msg type.");
         ret = -EINVAL;
@@ -1761,6 +1801,11 @@  void vhost_user_cleanup(VhostUserState *user)
             user->notifier[i].addr = NULL;
         }
     }
+
+    if (user->vfio_container) {
+        vfio_free_container(user->vfio_container);
+        user->vfio_container = NULL;
+    }
 }
 
 const VhostOps user_ops = {
diff --git a/include/hw/virtio/vhost-user.h b/include/hw/virtio/vhost-user.h
index fd660393a0..99c6dbbbff 100644
--- a/include/hw/virtio/vhost-user.h
+++ b/include/hw/virtio/vhost-user.h
@@ -10,6 +10,7 @@ 
 
 #include "chardev/char-fe.h"
 #include "hw/virtio/virtio.h"
+#include "hw/vfio/vfio-common.h"
 
 typedef struct VhostUserHostNotifier {
     MemoryRegion mr;
@@ -20,6 +21,7 @@  typedef struct VhostUserHostNotifier {
 typedef struct VhostUserState {
     CharBackend *chr;
     VhostUserHostNotifier notifier[VIRTIO_QUEUE_MAX];
+    VFIOContainer *vfio_container;
 } VhostUserState;
 
 VhostUserState *vhost_user_init(void);