Skip to contentSkip to conversation

net: Flush queued packets when guest resumes

PATCH Fam Zheng 2 participants 2 messages Jul 10, 2015 – Jul 13, 2015

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw
Quoted 62 lines · Jason Wang
Quoted 59 lines · Fam Zheng
Quoted 50 lines · Jason Wang
Quoted 35 lines · Fam Zheng

Since commit 6e99c63 "net/socket: Drop net_socket_can_send" and friends, net queues need to be explicitly flushed after qemu_can_send_packet() returns false, because the netdev side will disable the polling of fd.

This fixes the case of "cont" after "stop" (or migration).

---

v2: Unify with VM stop handler. (Stefan)

---
 net/net.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
net/net.c +4 -7
index 6ff7fec..28a5597 100644
@@ -1257,14 +1257,19 @@ void qmp_set_link(const char *name, bool up, Error **errp)
 static void net_vm_change_state_handler(void *opaque, int running,
                                         RunState state)
 {
-    /* Complete all queued packets, to guarantee we don't modify
-     * state later when VM is not running.
-     */
-    if (!running) {
-        NetClientState *nc;
-        NetClientState *tmp;
+    NetClientState *nc;
+    NetClientState *tmp;
 
-        QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
+    QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
+        if (running) {

net/net.c void qmp_set_link(const char *name, bool up, Error **errp) · line 1274

Quoted 10 lines
net/net.c +8 -0
+            /* Flush queued packets and wake up backends. */
+            if (nc->peer && qemu_can_send_packet(nc)) {
+                qemu_flush_queued_packets(nc->peer);
+            }
+        } else {
+            /* Complete all queued packets, to guarantee we don't modify
+             * state later when VM is not running.
+             */
             qemu_flush_or_purge_queued_packets(nc, true);
         }

Looks like qemu_can_send_packet() checks both nc->peer and runstate. So probably, we can simplify this to:

Quoted 6 lines

if (qemu_can_send_packet(nc))

    qemu_flush_queued_packets(nc->peer);
else
    qemu_flush_or_purge_queued_packets(nc, true);
Quoted 1 line
net/net.c +0 -0
     }

qemu_can_send_packet returns 1 if !nc->peer, so this doesn't work.

Quoted 8 lines
Quoted 1 line

Fam

Yes, I was wrong.

Btw, instead of depending on vm handler (which seems racy with other state change handler). Can we do this in places like vm_start() and vm_stop(). Like we drain and flush block queue during vm stop.

Because that's a bit hacky.

It won't be racy if we replace vdev->vm_running with runstate_is_running() in virtio-net, and/or don't check it in virtio_net_can_receive(). Is that OK?

Fam

Reviewed-by
#1 lore raw
Quoted 64 lines · Fam Zheng
Quoted 63 lines · Jason Wang
Quoted 55 lines · Fam Zheng
Quoted 54 lines · Jason Wang
Quoted 35 lines · Fam Zheng

Since commit 6e99c63 "net/socket: Drop net_socket_can_send" and friends, net queues need to be explicitly flushed after qemu_can_send_packet() returns false, because the netdev side will disable the polling of fd.

This fixes the case of "cont" after "stop" (or migration).

---

v2: Unify with VM stop handler. (Stefan)

---
 net/net.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
net/net.c +4 -7
index 6ff7fec..28a5597 100644
@@ -1257,14 +1257,19 @@ void qmp_set_link(const char *name, bool up, Error **errp)
 static void net_vm_change_state_handler(void *opaque, int running,
                                         RunState state)
 {
-    /* Complete all queued packets, to guarantee we don't modify
-     * state later when VM is not running.
-     */
-    if (!running) {
-        NetClientState *nc;
-        NetClientState *tmp;
+    NetClientState *nc;
+    NetClientState *tmp;
 
-        QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
+    QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
+        if (running) {

net/net.c void qmp_set_link(const char *name, bool up, Error **errp) · line 1274

Quoted 10 lines
net/net.c +8 -0
+            /* Flush queued packets and wake up backends. */
+            if (nc->peer && qemu_can_send_packet(nc)) {
+                qemu_flush_queued_packets(nc->peer);
+            }
+        } else {
+            /* Complete all queued packets, to guarantee we don't modify
+             * state later when VM is not running.
+             */
             qemu_flush_or_purge_queued_packets(nc, true);
         }

Looks like qemu_can_send_packet() checks both nc->peer and runstate. So probably, we can simplify this to:

if (qemu_can_send_packet(nc))

    qemu_flush_queued_packets(nc->peer);
else
    qemu_flush_or_purge_queued_packets(nc, true);
Quoted 4 lines
Quoted 1 line

net/net.c void qmp_set_link(const char *name, bool up, Error **errp) · line 1275

Quoted 1 line
net/net.c +0 -0
     }

qemu_can_send_packet returns 1 if !nc->peer, so this doesn't work.

Fam

Yes, I was wrong.

Quoted 8 lines
Quoted 4 lines

Btw, instead of depending on vm handler (which seems racy with other state change handler). Can we do this in places like vm_start() and vm_stop(). Like we drain and flush block queue during vm stop.

Because that's a bit hacky.

It won't be racy if we replace vdev->vm_running with runstate_is_running() in virtio-net, and/or don't check it in virtio_net_can_receive(). Is that OK?

Ok but not necessary. I believe this patch will be used with the patch that drops virtio_net_can_receive(). So at any order two vmstate handlers were called, the sent_cb will be called and poll will be enabled again. I'm ok with the patch. So

Thanks

Quoted 2 lines

Fam