qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Wei Wang <wei.w.wang@intel.com>
To: stefanha@gmail.com, marcandre.lureau@gmail.com, mst@redhat.com,
	jasowang@redhat.com, pbonzini@redhat.com,
	virtio-dev@lists.oasis-open.org, qemu-devel@nongnu.org
Cc: Wei Wang <wei.w.wang@intel.com>
Subject: [Qemu-devel] [PATCH v2 06/16] virtio: add inter-vm notification support
Date: Fri, 12 May 2017 16:35:38 +0800	[thread overview]
Message-ID: <1494578148-102868-7-git-send-email-wei.w.wang@intel.com> (raw)
In-Reply-To: <1494578148-102868-1-git-send-email-wei.w.wang@intel.com>

This patch enables the assign of an already allocated eventfd to a notifier.
In this case, QEMU creates a new eventfd for the notifier only when the
notifier's fd equals to -1. Otherwise, it means that the notifier has been
assigned a vaild fd.

Signed-off-by: Wei Wang <wei.w.wang@intel.com>
---
 hw/net/vhost-pci-net.c            | 73 +++++++++++++++++++++++++++++++++++++++
 hw/virtio/virtio-bus.c            | 19 +++++++---
 hw/virtio/virtio-pci.c            | 22 ++++++++++--
 hw/virtio/virtio.c                | 32 ++++++++++++++---
 include/hw/virtio/vhost-pci-net.h |  6 ++++
 include/hw/virtio/virtio.h        |  2 ++
 6 files changed, 141 insertions(+), 13 deletions(-)

diff --git a/hw/net/vhost-pci-net.c b/hw/net/vhost-pci-net.c
index e36803a..0235511 100644
--- a/hw/net/vhost-pci-net.c
+++ b/hw/net/vhost-pci-net.c
@@ -18,6 +18,7 @@
 #include "qemu/error-report.h"
 #include "hw/virtio/virtio-access.h"
 #include "hw/virtio/vhost-pci-net.h"
+#include "hw/virtio/virtio-bus.h"
 
 #define VPNET_CTRLQ_SIZE 32
 #define VPNET_VQ_SIZE 256
@@ -114,12 +115,53 @@ static void vpnet_send_ctrlq_msg_remoteq(VhostPCINet *vpnet)
     g_free(msg);
 }
 
+static inline bool vq_is_txq(uint16_t id)
+{
+    return (id % 2 == 0);
+}
+
+static inline uint16_t tx2rx(uint16_t id)
+{
+    return id + 1;
+}
+
+static inline uint16_t rx2tx(uint16_t id)
+{
+    return id - 1;
+}
+
 static void vpnet_set_status(struct VirtIODevice *vdev, uint8_t status)
 {
     VhostPCINet *vpnet = VHOST_PCI_NET(vdev);
+    uint16_t vq_num = vpnet->vq_pairs * 2;
+    BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
+    VirtioBusState *vbus = VIRTIO_BUS(qbus);
+    VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
+    VirtQueue *vq;
+    int r, i;
 
     /* Send the ctrlq messages to the driver when the ctrlq is ready */
     if (status & VIRTIO_CONFIG_S_DRIVER_OK) {
+        /*
+         * Set up the callfd when the driver is ready.
+         * Crosse share the eventfds from the remoteq.
+         * Use the tx remoteq's kickfd as the rx localq's callfd.
+         * Use the rx remoteq's kickfd as the tx localq's callfd.
+         */
+        for (i = 0; i < vq_num; i++) {
+            vq = virtio_get_queue(vdev, i);
+            if (vq_is_txq(i)) {
+                virtio_queue_set_guest_notifier(vq,
+                                          vpnet->remoteq_fds[tx2rx(i)].kickfd);
+            } else {
+                virtio_queue_set_guest_notifier(vq,
+                                          vpnet->remoteq_fds[rx2tx(i)].kickfd);
+            }
+        }
+        r = k->set_guest_notifiers(qbus->parent, vq_num, true);
+        if (r < 0) {
+            error_report("Error binding guest notifier: %d", -r);
+        }
         vpnet_send_ctrlq_msg_remote_mem(vpnet);
         vpnet_send_ctrlq_msg_remoteq(vpnet);
     }
@@ -155,17 +197,29 @@ static void vpnet_set_config(VirtIODevice *vdev, const uint8_t *config)
 {
 }
 
+static void vpnet_copy_fds_from_vhostdev(VirtqueueFD *fds, Remoteq *remoteq)
+{
+    fds[remoteq->vring_num].callfd = remoteq->callfd;
+    fds[remoteq->vring_num].kickfd = remoteq->kickfd;
+}
+
 static void vpnet_device_realize(DeviceState *dev, Error **errp)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VhostPCINet *vpnet = VHOST_PCI_NET(vdev);
     uint16_t i, vq_num;
     VhostPCIDev *vp_dev = get_vhost_pci_dev();
+    Remoteq *remoteq;
 
     vq_num = vp_dev->remoteq_num;
     vpnet->vq_pairs = vq_num / 2;
     virtio_init(vdev, "vhost-pci-net", VIRTIO_ID_VHOST_PCI_NET,
                 vpnet->config_size);
+    vpnet->remoteq_fds = g_malloc(sizeof(struct VirtqueueFD) *
+                                  vq_num);
+    QLIST_FOREACH(remoteq, &vp_dev->remoteq_list, node) {
+        vpnet_copy_fds_from_vhostdev(vpnet->remoteq_fds, remoteq);
+    }
 
     /* Add local vqs */
     for (i = 0; i < vq_num; i++) {
@@ -192,6 +246,25 @@ static void vpnet_device_unrealize(DeviceState *dev, Error **errp)
 
 static void vpnet_reset(VirtIODevice *vdev)
 {
+    VhostPCINet *vpnet = VHOST_PCI_NET(vdev);
+    VirtQueue *vq;
+    uint16_t i, vq_num = vpnet->vq_pairs * 2;
+
+    for (i = 0; i < vq_num; i++) {
+        vq = virtio_get_queue(vdev, i);
+        /*
+         * Cross share the eventfds.
+         * Use the tx remoteq's callfd as the rx localq's kickfd.
+         * Use the rx remoteq's callfd as the tx localq's kickfd.
+         */
+        if (vq_is_txq(i)) {
+            virtio_queue_set_host_notifier(vq,
+                                          vpnet->remoteq_fds[tx2rx(i)].callfd);
+        } else {
+            virtio_queue_set_host_notifier(vq,
+                                          vpnet->remoteq_fds[rx2tx(i)].callfd);
+        }
+    }
 }
 
 static Property vpnet_properties[] = {
diff --git a/hw/virtio/virtio-bus.c b/hw/virtio/virtio-bus.c
index 3042232..3cf0991 100644
--- a/hw/virtio/virtio-bus.c
+++ b/hw/virtio/virtio-bus.c
@@ -274,11 +274,20 @@ int virtio_bus_set_host_notifier(VirtioBusState *bus, int n, bool assign)
     }
 
     if (assign) {
-        r = event_notifier_init(notifier, 1);
-        if (r < 0) {
-            error_report("%s: unable to init event notifier: %s (%d)",
-                         __func__, strerror(-r), r);
-            return r;
+        if (notifier->wfd == -1) {
+            r = event_notifier_init(notifier, 1);
+            if (r < 0) {
+                error_report("%s: unable to init event notifier: %s (%d)",
+                             __func__, strerror(-r), r);
+                return r;
+            }
+        } else {
+            r = event_notifier_set(notifier);
+            if (r < 0) {
+                error_report("%s: unable to set event notifier: %s (%d)",
+                             __func__, strerror(-r), r);
+                return r;
+            }
         }
         r = k->ioeventfd_assign(proxy, notifier, n, true);
         if (r < 0) {
diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c
index b60e683..3f1a198 100644
--- a/hw/virtio/virtio-pci.c
+++ b/hw/virtio/virtio-pci.c
@@ -963,11 +963,24 @@ static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign,
     VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev);
     VirtQueue *vq = virtio_get_queue(vdev, n);
     EventNotifier *notifier = virtio_queue_get_guest_notifier(vq);
+    int r = 0;
 
     if (assign) {
-        int r = event_notifier_init(notifier, 0);
-        if (r < 0) {
-            return r;
+        if (notifier->wfd == -1) {
+            r = event_notifier_init(notifier, 0);
+            if (r < 0) {
+                error_report("%s: unable to init event notifier: %s (%d)",
+                             __func__, strerror(-r), r);
+                return r;
+
+            }
+        } else {
+            r = event_notifier_set(notifier);
+            if (r < 0) {
+                error_report("%s: unable to set event notifier: %s (%d)",
+                             __func__, strerror(-r), r);
+                return r;
+            }
         }
         virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd);
     } else {
@@ -2370,6 +2383,9 @@ static const TypeInfo virtio_net_pci_info = {
 /* vhost-pci-net */
 
 static Property vpnet_pci_properties[] = {
+    DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
+                    VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
+    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 4),
     DEFINE_PROP_END_OF_LIST(),
 };
 
diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
index 03592c5..43c7273 100644
--- a/hw/virtio/virtio.c
+++ b/hw/virtio/virtio.c
@@ -1196,10 +1196,6 @@ void virtio_reset(void *opaque)
         vdev->device_endian = virtio_default_endian();
     }
 
-    if (k->reset) {
-        k->reset(vdev);
-    }
-
     vdev->broken = false;
     vdev->guest_features = 0;
     vdev->queue_sel = 0;
@@ -1222,6 +1218,14 @@ void virtio_reset(void *opaque)
         vdev->vq[i].vring.num = vdev->vq[i].vring.num_default;
         vdev->vq[i].inuse = 0;
         virtio_virtqueue_reset_region_cache(&vdev->vq[i]);
+        vdev->vq[i].host_notifier.rfd = -1;
+        vdev->vq[i].host_notifier.wfd = -1;
+        vdev->vq[i].guest_notifier.rfd = -1;
+        vdev->vq[i].guest_notifier.wfd = -1;
+    }
+
+    if (k->reset) {
+        k->reset(vdev);
     }
 }
 
@@ -2253,7 +2257,11 @@ void virtio_init(VirtIODevice *vdev, const char *name,
         vdev->vq[i].vector = VIRTIO_NO_VECTOR;
         vdev->vq[i].vdev = vdev;
         vdev->vq[i].queue_index = i;
-    }
+        vdev->vq[i].host_notifier.rfd = -1;
+        vdev->vq[i].host_notifier.wfd = -1;
+        vdev->vq[i].guest_notifier.rfd = -1;
+        vdev->vq[i].guest_notifier.wfd = -1;
+     }
 
     vdev->name = name;
     vdev->config_len = config_size;
@@ -2364,6 +2372,13 @@ EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq)
     return &vq->guest_notifier;
 }
 
+void virtio_queue_set_guest_notifier(VirtQueue *vq, int fd)
+{
+    EventNotifier *e = &vq->guest_notifier;
+    e->rfd = fd;
+    e->wfd = fd;
+}
+
 static void virtio_queue_host_notifier_aio_read(EventNotifier *n)
 {
     VirtQueue *vq = container_of(n, VirtQueue, host_notifier);
@@ -2437,6 +2452,13 @@ EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq)
     return &vq->host_notifier;
 }
 
+void virtio_queue_set_host_notifier(VirtQueue *vq, int fd)
+{
+    EventNotifier *e = &vq->host_notifier;
+    e->rfd = fd;
+    e->wfd = fd;
+}
+
 void virtio_device_set_child_bus_name(VirtIODevice *vdev, char *bus_name)
 {
     g_free(vdev->bus_name);
diff --git a/include/hw/virtio/vhost-pci-net.h b/include/hw/virtio/vhost-pci-net.h
index e3a1c8b..9776260 100644
--- a/include/hw/virtio/vhost-pci-net.h
+++ b/include/hw/virtio/vhost-pci-net.h
@@ -22,6 +22,11 @@
 #define VHOST_PCI_NET(obj) \
         OBJECT_CHECK(VhostPCINet, (obj), TYPE_VHOST_PCI_NET)
 
+typedef struct VirtqueueFD {
+    int kickfd;
+    int callfd;
+} VirtqueueFD;
+
 typedef struct VhostPCINet {
     VirtIODevice parent_obj;
     VirtQueue *ctrlq;
@@ -29,6 +34,7 @@ typedef struct VhostPCINet {
     uint16_t vq_pairs;
     size_t config_size;
     uint64_t device_features;
+    VirtqueueFD *remoteq_fds;
 } VhostPCINet;
 
 #endif
diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h
index 7b6edba..423b466 100644
--- a/include/hw/virtio/virtio.h
+++ b/include/hw/virtio/virtio.h
@@ -276,6 +276,7 @@ void virtio_queue_update_used_idx(VirtIODevice *vdev, int n);
 VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n);
 uint16_t virtio_get_queue_index(VirtQueue *vq);
 EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq);
+void virtio_queue_set_guest_notifier(VirtQueue *vq, int fd);
 void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
                                                 bool with_irqfd);
 int virtio_device_start_ioeventfd(VirtIODevice *vdev);
@@ -284,6 +285,7 @@ int virtio_device_grab_ioeventfd(VirtIODevice *vdev);
 void virtio_device_release_ioeventfd(VirtIODevice *vdev);
 bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev);
 EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq);
+void virtio_queue_set_host_notifier(VirtQueue *vq, int fd);
 void virtio_queue_host_notifier_read(EventNotifier *n);
 void virtio_queue_aio_set_host_notifier_handler(VirtQueue *vq, AioContext *ctx,
                                                 VirtIOHandleAIOOutput handle_output);
-- 
2.7.4

  parent reply	other threads:[~2017-05-12  8:41 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-12  8:35 [Qemu-devel] [PATCH v2 00/16] Vhost-pci for inter-VM communication Wei Wang
2017-05-12  8:35 ` [Qemu-devel] [PATCH v2 01/16] vhost-user: share the vhost-user protocol related structures Wei Wang
2017-05-12  8:35 ` [Qemu-devel] [PATCH v2 02/16] vl: add the vhost-pci-slave command line option Wei Wang
2017-05-12  8:35 ` [Qemu-devel] [PATCH v2 03/16] vhost-pci-slave: create a vhost-user slave to support vhost-pci Wei Wang
2017-05-12  8:35 ` [Qemu-devel] [PATCH v2 04/16] vhost-pci-net: add vhost-pci-net Wei Wang
2017-05-12  8:35 ` [Qemu-devel] [PATCH v2 05/16] vhost-pci-net-pci: add vhost-pci-net-pci Wei Wang
2017-05-12  8:35 ` Wei Wang [this message]
2017-05-15  0:21   ` [Qemu-devel] [virtio-dev] [PATCH v2 06/16] virtio: add inter-vm notification support Wei Wang
2017-05-12  8:35 ` [Qemu-devel] [PATCH v2 07/16] vhost-user: send device id to the slave Wei Wang
2017-05-12  8:35 ` [Qemu-devel] [PATCH v2 08/16] vhost-user: send guest physical address of virtqueues " Wei Wang
2017-05-12  8:35 ` [Qemu-devel] [PATCH v2 09/16] vhost-user: send VHOST_USER_SET_VHOST_PCI_START/STOP Wei Wang
2017-05-12  8:35 ` [Qemu-devel] [PATCH v2 10/16] vhost-pci-net: send the negotiated feature bits to the master Wei Wang
2017-05-12  8:35 ` [Qemu-devel] [PATCH v2 11/16] vhost-user: add asynchronous read for the vhost-user master Wei Wang
2017-05-12  8:51   ` Wei Wang
2017-05-12  8:35 ` [Qemu-devel] [PATCH v2 12/16] vhost-user: handling VHOST_USER_SET_FEATURES Wei Wang
2017-05-12  8:35 ` [Qemu-devel] [PATCH v2 13/16] vhost-pci-slave: add "reset_virtio" Wei Wang
2017-05-12  8:35 ` [Qemu-devel] [PATCH v2 14/16] vhost-pci-slave: add support to delete a vhost-pci device Wei Wang
2017-05-12  8:35 ` [Qemu-devel] [PATCH v2 15/16] vhost-pci-net: tell the driver that it is ready to send packets Wei Wang
2017-05-12  8:35 ` [Qemu-devel] [PATCH v2 16/16] vl: enable vhost-pci-slave Wei Wang
2017-05-12  9:30 ` [Qemu-devel] [PATCH v2 00/16] Vhost-pci for inter-VM communication no-reply
2017-05-16 15:21   ` Michael S. Tsirkin
2017-05-16  6:46 ` Jason Wang
2017-05-16  7:12   ` [Qemu-devel] [virtio-dev] " Wei Wang
2017-05-17  6:16     ` Jason Wang
2017-05-17  6:22       ` Jason Wang
2017-05-18  3:03         ` Wei Wang
2017-05-19  3:10           ` [Qemu-devel] [virtio-dev] " Jason Wang
2017-05-19  9:00             ` Wei Wang
2017-05-19  9:53               ` Jason Wang
2017-05-19 20:44               ` Michael S. Tsirkin
2017-05-23 11:09                 ` Wei Wang
2017-05-23 15:15                   ` Michael S. Tsirkin
2017-05-19 15:33             ` Stefan Hajnoczi
2017-05-22  2:27               ` Jason Wang
2017-05-22 11:46                 ` Wang, Wei W
2017-05-23  2:08                   ` Jason Wang
2017-05-23  5:47                     ` Wei Wang
2017-05-23  6:32                       ` Jason Wang
2017-05-23 10:48                         ` Wei Wang
2017-05-24  3:24                           ` Jason Wang
2017-05-24  8:31                             ` Wei Wang
2017-05-25  7:59                               ` Jason Wang
2017-05-25 12:01                                 ` Wei Wang
2017-05-25 12:22                                   ` Jason Wang
2017-05-25 12:31                                     ` [Qemu-devel] [virtio-dev] " Jason Wang
2017-05-25 17:57                                       ` Michael S. Tsirkin
2017-06-04 10:34                                         ` Wei Wang
2017-06-05  2:21                                           ` Michael S. Tsirkin
2017-05-25 14:35                                     ` [Qemu-devel] " Eric Blake
2017-05-26  4:26                                       ` Jason Wang
2017-05-19 16:49             ` Michael S. Tsirkin
2017-05-22  2:22               ` Jason Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1494578148-102868-7-git-send-email-wei.w.wang@intel.com \
    --to=wei.w.wang@intel.com \
    --cc=jasowang@redhat.com \
    --cc=marcandre.lureau@gmail.com \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@gmail.com \
    --cc=virtio-dev@lists.oasis-open.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).