From 2eee91e6cbfd046968232aeee3f935e1ed916ea3 Mon Sep 17 00:00:00 2001
From: Amos Kong <akong@redhat.com>
Date: Thu, 30 Aug 2012 03:03:44 -0300
Subject: [RHEL6 qemu-kvm PATCH 1/6] net: notify iothread after flushing queue

RH-Author: Amos Kong <akong@redhat.com>
Message-id: <1346295825-510-2-git-send-email-akong@redhat.com>
Patchwork-id: 41488
O-Subject: [RHEL-6.4 qemu-kvm PATCH 1/2] net: notify iothread after flushing queue
Bugzilla: 852665
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
RH-Acked-by: Xiao Wang <jasowang@redhat.com>

From: Paolo Bonzini <pbonzini@redhat.com>

virtio-net has code to flush the queue and notify the iothread
whenever new receive buffers are added by the guest.  That is
fine, and indeed we need to do the same in all other drivers.
However, notifying the iothread should be work for the network
subsystem.  And since we are at it we can add a little smartness:
if some of the queued packets already could not be delivered,
there is no need to notify the iothread.

Reported-by: Luigi Rizzo <rizzo@iet.unipi.it>
Cc: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Cc: Jan Kiszka <jan.kiszka@siemens.de>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 hw/virtio-net.c |    4 ----
 net.c           |    7 ++++++-
 net/queue.c     |    5 +++--
 net/queue.h     |    2 +-
 4 files changed, 10 insertions(+), 8 deletions(-)

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/virtio-net.c | 4 ----
 net.c           | 7 ++++++-
 net/queue.c     | 5 +++--
 net/queue.h     | 2 +-
 4 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/hw/virtio-net.c b/hw/virtio-net.c
index 0926cf6..6bf2b52 100644
--- a/hw/virtio-net.c
+++ b/hw/virtio-net.c
@@ -426,10 +426,6 @@ static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
     VirtIONet *n = to_virtio_net(vdev);
 
     qemu_flush_queued_packets(&n->nic->nc);
-
-    /* We now have RX buffers, signal to the IO thread to break out of the
-     * select to re-poll the tap file descriptor */
-    qemu_notify_event();
 }
 
 static int virtio_net_can_receive(VLANClientState *nc)
diff --git a/net.c b/net.c
index e10e085..4fe974a 100644
--- a/net.c
+++ b/net.c
@@ -526,7 +526,12 @@ void qemu_flush_queued_packets(VLANClientState *vc)
         queue = vc->send_queue;
     }
 
-    qemu_net_queue_flush(queue);
+    if (qemu_net_queue_flush(queue)) {
+        /* We emptied the queue successfully, signal to the IO thread to repoll
+         * the file descriptor (for tap, for example).
+         */
+        qemu_notify_event();
+    }
 }
 
 static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender,
diff --git a/net/queue.c b/net/queue.c
index 2ea6cd0..b8947a0 100644
--- a/net/queue.c
+++ b/net/queue.c
@@ -232,7 +232,7 @@ void qemu_net_queue_purge(NetQueue *queue, VLANClientState *from)
     }
 }
 
-void qemu_net_queue_flush(NetQueue *queue)
+bool qemu_net_queue_flush(NetQueue *queue)
 {
     while (!QTAILQ_EMPTY(&queue->packets)) {
         NetPacket *packet;
@@ -248,7 +248,7 @@ void qemu_net_queue_flush(NetQueue *queue)
                                      packet->size);
         if (ret == 0) {
             QTAILQ_INSERT_HEAD(&queue->packets, packet, entry);
-            break;
+            return 0;
         }
 
         if (packet->sent_cb) {
@@ -257,4 +257,5 @@ void qemu_net_queue_flush(NetQueue *queue)
 
         qemu_free(packet);
     }
+    return 1;
 }
diff --git a/net/queue.h b/net/queue.h
index a31958e..4bf6d3c 100644
--- a/net/queue.h
+++ b/net/queue.h
@@ -66,6 +66,6 @@ ssize_t qemu_net_queue_send_iov(NetQueue *queue,
                                 NetPacketSent *sent_cb);
 
 void qemu_net_queue_purge(NetQueue *queue, VLANClientState *from);
-void qemu_net_queue_flush(NetQueue *queue);
+bool qemu_net_queue_flush(NetQueue *queue);
 
 #endif /* QEMU_NET_QUEUE_H */
-- 
1.7.11.4

