Skip to contentSkip to conversation

virtio_net: flush uncompleted TX on reset

PATCH Superseded Greg Kurz 2 participants 4 messages Mar 16, 2018 – Mar 20, 2018 Networking

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Greg Kurz

author
#0 lore raw

PATCH v2 1/1 virtio_net: flush uncompleted TX on reset

If the backend could not transmit a packet right away for some reason, the packet is queued for asynchronous sending. The corresponding vq element is tracked in the async_tx.elem field of the VirtIONetQueue, for later freeing when the transmission is complete.

If a reset happens before completion, virtio_net_tx_complete() will push async_tx.elem back to the guest anyway, and we end up with the inuse flag of the vq being equal to -1. The next call to virtqueue_pop() is then likely to fail with "Virtqueue size exceeded".

This can be reproduced easily by starting a guest with an hubport backend that is not connected to a functional network, eg,

-device virtio-net-pci,netdev=hub0 -netdev hubport,id=hub0,hubid=0

and no other -netdev hubport,hubid=0 on the command line.

The appropriate fix is to ensure that such an asynchronous transmission cannot survive a device reset. So for all queues, we first try to send the packet again, and eventually we purge it if the backend still could not deliver it.

---
v2: - make qemu_flush_or_purge_queued_packets() extern and use it
- reworded reproducer paragraph in changelog

---
 hw/net/virtio-net.c |    8 ++++++++
 include/net/net.h   |    1 +
 net/net.c           |    1 -
 3 files changed, 9 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +8 -0
index 188744e17d57..e5ed35489380 100644
@@ -422,6 +422,7 @@ static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc)
 static void virtio_net_reset(VirtIODevice *vdev)
 {
     VirtIONet *n = VIRTIO_NET(vdev);
+    int i;
 
     /* Reset back to compatibility mode */
     n->promisc = 1;
@@ -445,6 +446,13 @@ static void virtio_net_reset(VirtIODevice *vdev)
     memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
     qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
     memset(n->vlans, 0, MAX_VLAN >> 3);
+
+    /* Flush any async TX */
+    for (i = 0;  i < n->max_queues; i++) {
+        NetClientState *nc = qemu_get_subqueue(n->nic, i);
+        qemu_flush_or_purge_queued_packets(nc->peer, true);
+        assert(!virtio_net_get_subqueue(nc)->async_tx.elem);
+    }
 }
 
 static void peer_test_vnet_hdr(VirtIONet *n)
include/net/net.h +1 -0
index a943e968a3dc..1f7341e4592b 100644
@@ -153,6 +153,7 @@ ssize_t qemu_send_packet_async(NetClientState *nc, const uint8_t *buf,
                                int size, NetPacketSent *sent_cb);
 void qemu_purge_queued_packets(NetClientState *nc);
 void qemu_flush_queued_packets(NetClientState *nc);
+void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge);
 void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]);
 bool qemu_has_ufo(NetClientState *nc);
 bool qemu_has_vnet_hdr(NetClientState *nc);
net/net.c +0 -1
index 5222e450698c..29f83983e55d 100644
@@ -595,7 +595,6 @@ void qemu_purge_queued_packets(NetClientState *nc)
     qemu_net_queue_purge(nc->peer->incoming_queue, nc);
 }
 
-static
 void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
 {
     nc->receive_disabled = 0;
#1 lore raw
Quoted 80 lines · Greg Kurz

If the backend could not transmit a packet right away for some reason, the packet is queued for asynchronous sending. The corresponding vq element is tracked in the async_tx.elem field of the VirtIONetQueue, for later freeing when the transmission is complete.

If a reset happens before completion, virtio_net_tx_complete() will push async_tx.elem back to the guest anyway, and we end up with the inuse flag of the vq being equal to -1. The next call to virtqueue_pop() is then likely to fail with "Virtqueue size exceeded".

This can be reproduced easily by starting a guest with an hubport backend that is not connected to a functional network, eg,

-device virtio-net-pci,netdev=hub0 -netdev hubport,id=hub0,hubid=0

and no other -netdev hubport,hubid=0 on the command line.

The appropriate fix is to ensure that such an asynchronous transmission cannot survive a device reset. So for all queues, we first try to send the packet again, and eventually we purge it if the backend still could not deliver it.

---
v2: - make qemu_flush_or_purge_queued_packets() extern and use it
- reworded reproducer paragraph in changelog

---
  hw/net/virtio-net.c |    8 ++++++++
  include/net/net.h   |    1 +
  net/net.c           |    1 -
  3 files changed, 9 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +8 -0
index 188744e17d57..e5ed35489380 100644
@@ -422,6 +422,7 @@ static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc)
  static void virtio_net_reset(VirtIODevice *vdev)
  {
      VirtIONet *n = VIRTIO_NET(vdev);
+    int i;
 
      /* Reset back to compatibility mode */
      n->promisc = 1;
@@ -445,6 +446,13 @@ static void virtio_net_reset(VirtIODevice *vdev)
      memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
      qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
      memset(n->vlans, 0, MAX_VLAN >> 3);
+
+    /* Flush any async TX */
+    for (i = 0;  i < n->max_queues; i++) {
+        NetClientState *nc = qemu_get_subqueue(n->nic, i);
+        qemu_flush_or_purge_queued_packets(nc->peer, true);
+        assert(!virtio_net_get_subqueue(nc)->async_tx.elem);
+    }
  }
 
  static void peer_test_vnet_hdr(VirtIONet *n)
include/net/net.h +1 -0
index a943e968a3dc..1f7341e4592b 100644
@@ -153,6 +153,7 @@ ssize_t qemu_send_packet_async(NetClientState *nc, const uint8_t *buf,
                                 int size, NetPacketSent *sent_cb);
  void qemu_purge_queued_packets(NetClientState *nc);
  void qemu_flush_queued_packets(NetClientState *nc);
+void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge);
  void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]);
  bool qemu_has_ufo(NetClientState *nc);
  bool qemu_has_vnet_hdr(NetClientState *nc);
net/net.c +0 -0
index 5222e450698c..29f83983e55d 100644

net/net.c void qemu_purge_queued_packets(NetClientState *nc) · line 600

Quoted 7 lines
net/net.c +0 -1
@@ -595,7 +595,6 @@ void qemu_purge_queued_packets(NetClientState *nc)
      qemu_net_queue_purge(nc->peer->incoming_queue, nc);
  }
 
-static
  void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
  {
      nc->receive_disabled = 0;

Applied and queued for -stable.

Thanks

#2 lore raw
Quoted 8 lines
Quoted 7 lines

  -static
  void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge) {
      nc->receive_disabled = 0;

Quoted 4 lines

Applied and queued for -stable.

Thanks

Unfortunately, this breaks hotplug test:

 TEST: tests/virtio-net-test... (pid=7117)
/x86_64/virtio/net/pci/basic: OK
/x86_64/virtio/net/pci/rx_stop_cont: OK
/x86_64/virtio/net/pci/hotplug: Broken pipe
FAIL

Thanks

author
#3 lore raw
Quoted 15 lines · On Tue, 20 Mar 2018 11:27:26 +0800 Jason Wang <[email protected]> wrote:
Quoted 8 lines
Quoted 7 lines

  -static
  void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
  {
      nc->receive_disabled = 0;

Quoted 4 lines

Applied and queued for -stable.

Thanks

Unfortunately, this breaks hotplug test:

Quoted 7 lines

 TEST: tests/virtio-net-test... (pid=7117)
/x86_64/virtio/net/pci/basic: OK
/x86_64/virtio/net/pci/rx_stop_cont: OK
/x86_64/virtio/net/pci/hotplug: Broken pipe
FAIL

Thanks

Hi Jason,

Yes, I've just realized this patch assumes the virtio-net device does have an associated backend (ie, nc->peer != NULL) otherwise we segfault. This happens to be the case with the hotplug test.

I'll send a v3.

Cheers,

--
Greg