From 4d17ab219cea7c63c844994306b016757334ac44 Mon Sep 17 00:00:00 2001
From: Xiao Wang <jasowang@redhat.com>
Date: Thu, 13 Jul 2017 07:20:58 +0200
Subject: [PATCH 2/2] virtio-net: fix tx queue size for !vhost-user

RH-Author: Xiao Wang <jasowang@redhat.com>
Message-id: <1499930458-9904-3-git-send-email-jasowang@redhat.com>
Patchwork-id: 75748
O-Subject: [RHEL7.4z qemu-kvm PATCH 2/2] virtio-net: fix tx queue size for !vhost-user
Bugzilla: 1471666
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
RH-Acked-by: Maxime Coquelin <maxime.coquelin@redhat.com>
RH-Acked-by: wexu@redhat.com

From: "Michael S. Tsirkin" <mst@redhat.com>

Current code segfaults when no nic peer is specified.
Fix it up - fall back to default queue size.

Fixes: 9b02e1618cf26a ("virtio-net: enable configurable tx queue size")
Cc: Wei Wang <wei.w.wang@intel.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
(cherry picked from commit 2eef278b9e6326707410eed23be40e57f6c331b7)
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
---
 hw/net/virtio-net.c | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
index b9b8449..8995703 100644
--- a/hw/net/virtio-net.c
+++ b/hw/net/virtio-net.c
@@ -497,6 +497,24 @@ static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs,
     }
 }
 
+static int virtio_net_max_tx_queue_size(VirtIONet *n)
+{
+    NetClientState *peer = n->nic_conf.peers.ncs[0];
+
+    /*
+     * Backends other than vhost-user don't support max queue size.
+     */
+    if (!peer) {
+        return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE;
+    }
+
+    if (peer->info->type != NET_CLIENT_DRIVER_VHOST_USER) {
+        return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE;
+    }
+
+    return VIRTQUEUE_MAX_SIZE;
+}
+
 static int peer_attach(VirtIONet *n, int index)
 {
     NetClientState *nc = qemu_get_subqueue(n->nic, index);
@@ -1960,14 +1978,8 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp)
         error_report("Defaulting to \"bh\"");
     }
 
-    /*
-     * Currently, backends other than vhost-user don't support 1024 queue
-     * size.
-     */
-    if (n->net_conf.tx_queue_size == VIRTQUEUE_MAX_SIZE &&
-        n->nic_conf.peers.ncs[0]->info->type != NET_CLIENT_DRIVER_VHOST_USER) {
-        n->net_conf.tx_queue_size = VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE;
-    }
+    n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n),
+                                    n->net_conf.tx_queue_size);
 
     for (i = 0; i < n->max_queues; i++) {
         virtio_net_add_queue(n, i);
-- 
1.8.3.1

