render / rpms / qemu

Forked from rpms/qemu 9 months ago
Clone

Blame qemu-virtio-net-vhost-net-support.patch

Justin M. Forbes 272dfe
This connects virtio-net to vhost net backend.
Justin M. Forbes 272dfe
The code is structured in a way analogous to what we have with vnet
Justin M. Forbes 272dfe
header capability in tap.  We start/stop backend on driver start/stop as
Justin M. Forbes 272dfe
well as on save and vm start (for migration).
Justin M. Forbes 272dfe
Justin M. Forbes 272dfe
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Justin M. Forbes 272dfe
---
Justin M. Forbes 272dfe
 hw/virtio-net.c |   67 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
Justin M. Forbes 272dfe
 1 files changed, 65 insertions(+), 2 deletions(-)
Justin M. Forbes 272dfe
Justin M. Forbes 272dfe
diff --git a/hw/virtio-net.c b/hw/virtio-net.c
Justin M. Forbes 272dfe
index 02d9180..088029b 100644
Justin M. Forbes 272dfe
--- a/hw/virtio-net.c
Justin M. Forbes 272dfe
+++ b/hw/virtio-net.c
Justin M. Forbes 272dfe
@@ -17,6 +17,7 @@
Justin M. Forbes 272dfe
 #include "net/tap.h"
Justin M. Forbes 272dfe
 #include "qemu-timer.h"
Justin M. Forbes 272dfe
 #include "virtio-net.h"
Justin M. Forbes 272dfe
+#include "vhost_net.h"
Justin M. Forbes 272dfe
 
Justin M. Forbes 272dfe
 #define VIRTIO_NET_VM_VERSION    11
Justin M. Forbes 272dfe
 
Justin M. Forbes 272dfe
@@ -47,6 +48,8 @@ typedef struct VirtIONet
Justin M. Forbes 272dfe
     uint8_t nomulti;
Justin M. Forbes 272dfe
     uint8_t nouni;
Justin M. Forbes 272dfe
     uint8_t nobcast;
Justin M. Forbes 272dfe
+    uint8_t vhost_started;
Justin M. Forbes 272dfe
+    VMChangeStateEntry *vmstate;
Justin M. Forbes 272dfe
     struct {
Justin M. Forbes 272dfe
         int in_use;
Justin M. Forbes 272dfe
         int first_multi;
Justin M. Forbes 272dfe
@@ -114,6 +117,10 @@ static void virtio_net_reset(VirtIODevice *vdev)
Justin M. Forbes 272dfe
     n->nomulti = 0;
Justin M. Forbes 272dfe
     n->nouni = 0;
Justin M. Forbes 272dfe
     n->nobcast = 0;
Justin M. Forbes 272dfe
+    if (n->vhost_started) {
Justin M. Forbes 272dfe
+        vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), vdev);
Justin M. Forbes 272dfe
+        n->vhost_started = 0;
Justin M. Forbes 272dfe
+    }
Justin M. Forbes 272dfe
 
Justin M. Forbes 272dfe
     /* Flush any MAC and VLAN filter table state */
Justin M. Forbes 272dfe
     n->mac_table.in_use = 0;
Justin M. Forbes 272dfe
@@ -172,7 +179,10 @@ static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
Justin M. Forbes 272dfe
         features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
Justin M. Forbes 272dfe
     }
Justin M. Forbes 272dfe
 
Justin M. Forbes 272dfe
-    return features;
Justin M. Forbes 272dfe
+    if (!tap_get_vhost_net(n->nic->nc.peer)) {
Justin M. Forbes 272dfe
+        return features;
Justin M. Forbes 272dfe
+    }
Justin M. Forbes 272dfe
+    return vhost_net_get_features(tap_get_vhost_net(n->nic->nc.peer), features);
Justin M. Forbes 272dfe
 }
Justin M. Forbes 272dfe
 
Justin M. Forbes 272dfe
 static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
Justin M. Forbes 272dfe
@@ -690,6 +700,12 @@ static void virtio_net_save(QEMUFile *f, void *opaque)
Justin M. Forbes 272dfe
 {
Justin M. Forbes 272dfe
     VirtIONet *n = opaque;
Justin M. Forbes 272dfe
 
Justin M. Forbes 272dfe
+    if (n->vhost_started) {
Justin M. Forbes 272dfe
+	/* TODO: should we really stop the backend?
Justin M. Forbes 272dfe
+	 * If we don't, it might keep writing to memory. */
Justin M. Forbes 272dfe
+        vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
Justin M. Forbes 272dfe
+	n->vhost_started = 0;
Justin M. Forbes 272dfe
+    }
Justin M. Forbes 272dfe
     virtio_save(&n->vdev, f);
Justin M. Forbes 272dfe
 
Justin M. Forbes 272dfe
     qemu_put_buffer(f, n->mac, ETH_ALEN);
Justin M. Forbes 272dfe
@@ -802,7 +818,6 @@ static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
Justin M. Forbes 272dfe
         qemu_mod_timer(n->tx_timer,
Justin M. Forbes 272dfe
                        qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL);
Justin M. Forbes 272dfe
     }
Justin M. Forbes 272dfe
-
Justin M. Forbes 272dfe
     return 0;
Justin M. Forbes 272dfe
 }
Justin M. Forbes 272dfe
 
Justin M. Forbes 272dfe
@@ -822,6 +837,47 @@ static NetClientInfo net_virtio_info = {
Justin M. Forbes 272dfe
     .link_status_changed = virtio_net_set_link_status,
Justin M. Forbes 272dfe
 };
Justin M. Forbes 272dfe
 
Justin M. Forbes 272dfe
+static void virtio_net_set_status(struct VirtIODevice *vdev)
Justin M. Forbes 272dfe
+{
Justin M. Forbes 272dfe
+    VirtIONet *n = to_virtio_net(vdev);
Justin M. Forbes 272dfe
+    if (!n->nic->nc.peer) {
Justin M. Forbes 272dfe
+        return;
Justin M. Forbes 272dfe
+    }
Justin M. Forbes 272dfe
+    if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
Justin M. Forbes 272dfe
+        return;
Justin M. Forbes 272dfe
+    }
Justin M. Forbes 272dfe
+
Justin M. Forbes 272dfe
+    if (!tap_get_vhost_net(n->nic->nc.peer)) {
Justin M. Forbes 272dfe
+        return;
Justin M. Forbes 272dfe
+    }
Justin M. Forbes 272dfe
+    if (!!n->vhost_started == !!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
Justin M. Forbes 272dfe
+        return;
Justin M. Forbes 272dfe
+    }
Justin M. Forbes 272dfe
+    if (vdev->status & VIRTIO_CONFIG_S_DRIVER_OK) {
Justin M. Forbes 272dfe
+        int r = vhost_net_start(tap_get_vhost_net(n->nic->nc.peer), vdev);
Justin M. Forbes 272dfe
+        if (r < 0) {
Justin M. Forbes 272dfe
+            fprintf(stderr, "unable to start vhost net: %d: "
Justin M. Forbes 272dfe
+                    "falling back on userspace virtio\n", -r);
Justin M. Forbes 272dfe
+        } else {
Justin M. Forbes 272dfe
+            n->vhost_started = 1;
Justin M. Forbes 272dfe
+        }
Justin M. Forbes 272dfe
+    } else {
Justin M. Forbes 272dfe
+        vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), vdev);
Justin M. Forbes 272dfe
+        n->vhost_started = 0;
Justin M. Forbes 272dfe
+    }
Justin M. Forbes 272dfe
+}
Justin M. Forbes 272dfe
+
Justin M. Forbes 272dfe
+static void virtio_net_vmstate_change(void *opaque, int running, int reason)
Justin M. Forbes 272dfe
+{
Justin M. Forbes 272dfe
+	VirtIONet *n = opaque;
Justin M. Forbes 272dfe
+	if (!running) {
Justin M. Forbes 272dfe
+		return;
Justin M. Forbes 272dfe
+	}
Justin M. Forbes 272dfe
+	/* This is called when vm is started, it will start vhost backend if it
Justin M. Forbes 272dfe
+	 * appropriate e.g. after migration. */
Justin M. Forbes 272dfe
+	virtio_net_set_status(&n->vdev);
Justin M. Forbes 272dfe
+}
Justin M. Forbes 272dfe
+
Justin M. Forbes 272dfe
 VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf)
Justin M. Forbes 272dfe
 {
Justin M. Forbes 272dfe
     VirtIONet *n;
Justin M. Forbes 272dfe
@@ -837,6 +893,7 @@ VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf)
Justin M. Forbes 272dfe
     n->vdev.set_features = virtio_net_set_features;
Justin M. Forbes 272dfe
     n->vdev.bad_features = virtio_net_bad_features;
Justin M. Forbes 272dfe
     n->vdev.reset = virtio_net_reset;
Justin M. Forbes 272dfe
+    n->vdev.set_status = virtio_net_set_status;
Justin M. Forbes 272dfe
     n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
Justin M. Forbes 272dfe
     n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
Justin M. Forbes 272dfe
     n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
Justin M. Forbes 272dfe
@@ -859,6 +916,7 @@ VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf)
Justin M. Forbes 272dfe
 
Justin M. Forbes 272dfe
     register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION,
Justin M. Forbes 272dfe
                     virtio_net_save, virtio_net_load, n);
Justin M. Forbes 272dfe
+    n->vmstate = qemu_add_vm_change_state_handler(virtio_net_vmstate_change, n);
Justin M. Forbes 272dfe
 
Justin M. Forbes 272dfe
     return &n->vdev;
Justin M. Forbes 272dfe
 }
Justin M. Forbes 272dfe
@@ -866,6 +924,11 @@ VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf)
Justin M. Forbes 272dfe
 void virtio_net_exit(VirtIODevice *vdev)
Justin M. Forbes 272dfe
 {
Justin M. Forbes 272dfe
     VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
Justin M. Forbes 272dfe
+    qemu_del_vm_change_state_handler(n->vmstate);
Justin M. Forbes 272dfe
+
Justin M. Forbes 272dfe
+    if (n->vhost_started) {
Justin M. Forbes 272dfe
+        vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), vdev);
Justin M. Forbes 272dfe
+    }
Justin M. Forbes 272dfe
 
Justin M. Forbes 272dfe
     qemu_purge_queued_packets(&n->nic->nc);
Justin M. Forbes 272dfe
 
Justin M. Forbes 272dfe
-- 
Justin M. Forbes 272dfe
1.6.6.144.g5c3af