virtio-net: Fix TX data discard on backend disconnection
cover virtio-net: Fix TX data discard on backend disconnection
This series fixes TX data discard when backend disconnects.
On backend disconnection, QEMU cannot retrieve its internal avail index, which makes the virtio queue internal state inconsistent (last_avail_idx will always be 0, whereas used_idx is correct).
It results in discarding TX data to corrupt the vring state, by completing descriptors already completed by the backend.
This series fixes this by restoring last_avail_idx to the vring used idx, as some backends used to do on reconnection.
Maxime Coquelin (2):
virtio: Add queue interface to restore avail index from vring used
index
vhost: restore avail index from vring used index on disconnection
hw/virtio/vhost.c | 4 ++++ hw/virtio/virtio.c | 10 ++++++++++ include/hw/virtio/virtio.h | 1 + 3 files changed, 15 insertions(+)
Signature · 1 line
2.14.3
PATCH 1/2 virtio: Add queue interface to restore avail index from vring used index
In case of backend crash, it is not possible to restore internal avail index from the backend value as vhost_get_vring_base callback fails.
This patch provides a new interface to restore internal avail index from the vring used index, as done by some vhost-user backend on reconnection.
--- hw/virtio/virtio.c | 10 ++++++++++ include/hw/virtio/virtio.h | 1 + 2 files changed, 11 insertions(+)
hw/virtio/virtio.c +10 -0
@@ -2310,6 +2310,16 @@ void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t idx) vdev->vq[n].shadow_avail_idx = idx; } +void virtio_queue_restore_last_avail_idx (VirtIODevice *vdev,int n) +{ +rcu_read_lock (); +if (vdev->vq[n].vring.desc) { + vdev->vq[n].last_avail_idx =vring_used_idx (&vdev->vq[n]); + vdev->vq[n].shadow_avail_idx = vdev->vq[n].last_avail_idx; + } +rcu_read_unlock (); +} +void virtio_queue_update_used_idx (VirtIODevice *vdev,int n) {rcu_read_lock ();
include/hw/virtio/virtio.h +1 -0
@@ -272,6 +272,7 @@ hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n);hwaddr virtio_queue_get_used_size (VirtIODevice *vdev,int n);uint16_t virtio_queue_get_last_avail_idx (VirtIODevice *vdev,int n);void virtio_queue_set_last_avail_idx (VirtIODevice *vdev,int n,uint16_t idx); +void virtio_queue_restore_last_avail_idx (VirtIODevice *vdev,int n);void virtio_queue_invalidate_signalled_used (VirtIODevice *vdev,int n);void virtio_queue_update_used_idx (VirtIODevice *vdev,int n);VirtQueue *virtio_get_queue (VirtIODevice *vdev,int n);
Signature · 1 line
2.14.3
Re: [Qemu-devel] [PATCH 1/2] virtio: Add queue interface to restore avail index from vring used index
Quoted 8 lines · Maxime Coquelin
In case of backend crash, it is not possible to restore internal avail index from the backend value as
vhost_get_vring_basecallback fails.This patch provides a new interface to restore internal avail index from the vring used index, as done by some vhost-user backend on reconnection.
Quoted 6 lines
Signed-off-by Maxime Coquelin <[email protected]>---
hw/virtio/virtio.c| 10 ++++++++++include/hw/virtio/virtio.h| 1 +2 files changed, 11 insertions(+)
PATCH 2/2 vhost: restore avail index from vring used index on disconnection
In reply to Maxime Coquelinvhost_virtqueue_stop() gets avail index value from the backend, except if the backend is not responding.
It happens when the backend crashes, and in this case, internal state of the virtio queue is inconsistent, making packets to corrupt the vring state.
With a Linux guest, it results in following error message on backend reconnection:
[ 22.444905] virtio_net virtio0: output.0:id 0 is not a head! [ 22.446746] net enp0s3: Unexpected TXQ (0) queue failure: -5 [ 22.476360] net enp0s3: Unexpected TXQ (0) queue failure: -5
--- hw/virtio/vhost.c | 4 ++++ 1 file changed, 4 insertions(+)
hw/virtio/vhost.c +4 -0
@@ -1138,6 +1138,10 @@ static void vhost_virtqueue_stop(struct vhost_dev *dev, r = dev->vhost_ops->vhost_get_vring_base (dev, &state);if (r <0 ) {VHOST_OPS_DEBUG ("vhost VQ %d ring restore failed: %d" , idx, r); +/* Connection to the backend is broken, so let's sync internal +* last avail idx to the device used idx. +*/ +virtio_queue_restore_last_avail_idx (vdev, idx); }else {virtio_queue_set_last_avail_idx (vdev, idx, state.num); }
Signature · 1 line
2.14.3
Re: [Qemu-devel] [PATCH 2/2] vhost: restore avail index from vring used index on disconnection
Quoted 14 lines · Maxime Coquelin
vhost_virtqueue_stop()gets avail index value from the backend, except if the backend is not responding.It happens when the backend crashes, and in this case, internal state of the virtio queue is inconsistent, making packets to corrupt the vring state.
With a Linux guest, it results in following error message on backend reconnection:
[ 22.444905] virtio_net virtio0: output.0:id 0 is not a head! [ 22.446746] net enp0s3: Unexpected TXQ (0) queue failure: -5 [ 22.476360] net enp0s3: Unexpected TXQ (0) queue failure: -5
Quoted 7 lines
Fixes 283e2c2adcb8 ("net: virtio-net discards TX data after link down")Signed-off-by Maxime Coquelin <[email protected]>---
hw/virtio/vhost.c| 4 ++++1 file changed, 4 insertions(+)
Quoted 18 lines · Maxime Coquelin
This series fixes TX data discard when backend disconnects.
On backend disconnection, QEMU cannot retrieve its internal avail index, which makes the virtio queue internal state inconsistent (
last_avail_idxwill always be 0, whereasused_idxis correct).It results in discarding TX data to corrupt the vring state, by completing descriptors already completed by the backend.
This series fixes this by restoring
last_avail_idxto the vring used idx, as some backends used to do on reconnection.Maxime Coquelin (2):
virtio: Add queue interface to restore avail index from vring used
index
vhost: restore avail index from vring used index on disconnection
Quoted 5 lines
hw/virtio/vhost.c | 4 ++++ hw/virtio/virtio.c | 10 ++++++++++ include/hw/virtio/virtio.h | 1 + 3 files changed, 15 insertions(+)