vhost_net: start/stop guest notifiers properly

commit a9f98bb5eb "vhost: multiqueue
support" changed the order of stopping the device. Previously
vhost_dev_stop would disable backend and only afterwards, unset guest
notifiers. We now unset guest notifiers while vhost is still
active. This can lose interrupts causing guest networking to fail. In
particular, this has been observed during migration.

To fix this, several other changes are needed:
- remove the hdev->started assertion in vhost.c since we may want to
start the guest notifiers before vhost starts and stop the guest
notifiers after vhost is stopped.
- introduce the vhost_net_set_vq_index() and call it before setting
guest notifiers. This is to guarantee vhost_net has the correct
virtqueue index when setting guest notifiers.

MST: fix up error handling.

Cc: qemu-stable@nongnu.org
Cc: Jason Wang <jasowang@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Tested-by: Andrey Korolyov <andrey@xdel.ru>
Reported-by: "Zhangjie (HZ)" <zhangjie14@huawei.com>
Tested-by: William Dauchy <william@gandi.net>
Signed-off-by: Jason Wang <jasowang@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
This commit is contained in:
Jason Wang 2014-08-19 12:56:29 +08:00 committed by Michael S. Tsirkin
parent d7efb7e08e
commit cd7d1d26b0
2 changed files with 27 additions and 16 deletions

View File

@ -188,16 +188,19 @@ bool vhost_net_query(VHostNetState *net, VirtIODevice *dev)
return vhost_dev_query(&net->dev, dev); return vhost_dev_query(&net->dev, dev);
} }
static void vhost_net_set_vq_index(struct vhost_net *net, int vq_index)
{
net->dev.vq_index = vq_index;
}
static int vhost_net_start_one(struct vhost_net *net, static int vhost_net_start_one(struct vhost_net *net,
VirtIODevice *dev, VirtIODevice *dev)
int vq_index)
{ {
struct vhost_vring_file file = { }; struct vhost_vring_file file = { };
int r; int r;
net->dev.nvqs = 2; net->dev.nvqs = 2;
net->dev.vqs = net->vqs; net->dev.vqs = net->vqs;
net->dev.vq_index = vq_index;
r = vhost_dev_enable_notifiers(&net->dev, dev); r = vhost_dev_enable_notifiers(&net->dev, dev);
if (r < 0) { if (r < 0) {
@ -286,7 +289,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev))); BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(dev)));
VirtioBusState *vbus = VIRTIO_BUS(qbus); VirtioBusState *vbus = VIRTIO_BUS(qbus);
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus); VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
int r, i = 0; int r, e, i;
if (!vhost_net_device_endian_ok(dev)) { if (!vhost_net_device_endian_ok(dev)) {
error_report("vhost-net does not support cross-endian"); error_report("vhost-net does not support cross-endian");
@ -301,11 +304,7 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
} }
for (i = 0; i < total_queues; i++) { for (i = 0; i < total_queues; i++) {
r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev, i * 2); vhost_net_set_vq_index(get_vhost_net(ncs[i].peer), i * 2);
if (r < 0) {
goto err;
}
} }
r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true); r = k->set_guest_notifiers(qbus->parent, total_queues * 2, true);
@ -314,12 +313,26 @@ int vhost_net_start(VirtIODevice *dev, NetClientState *ncs,
goto err; goto err;
} }
for (i = 0; i < total_queues; i++) {
r = vhost_net_start_one(get_vhost_net(ncs[i].peer), dev);
if (r < 0) {
goto err_start;
}
}
return 0; return 0;
err: err_start:
while (--i >= 0) { while (--i >= 0) {
vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev); vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
} }
e = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
if (e < 0) {
fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", e);
fflush(stderr);
}
err:
return r; return r;
} }
@ -331,16 +344,16 @@ void vhost_net_stop(VirtIODevice *dev, NetClientState *ncs,
VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus); VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus);
int i, r; int i, r;
for (i = 0; i < total_queues; i++) {
vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
}
r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false); r = k->set_guest_notifiers(qbus->parent, total_queues * 2, false);
if (r < 0) { if (r < 0) {
fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r); fprintf(stderr, "vhost guest notifier cleanup failed: %d\n", r);
fflush(stderr); fflush(stderr);
} }
assert(r >= 0); assert(r >= 0);
for (i = 0; i < total_queues; i++) {
vhost_net_stop_one(get_vhost_net(ncs[i].peer), dev);
}
} }
void vhost_net_cleanup(struct vhost_net *net) void vhost_net_cleanup(struct vhost_net *net)

View File

@ -976,7 +976,6 @@ void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev)
bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n) bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n)
{ {
struct vhost_virtqueue *vq = hdev->vqs + n - hdev->vq_index; struct vhost_virtqueue *vq = hdev->vqs + n - hdev->vq_index;
assert(hdev->started);
assert(n >= hdev->vq_index && n < hdev->vq_index + hdev->nvqs); assert(n >= hdev->vq_index && n < hdev->vq_index + hdev->nvqs);
return event_notifier_test_and_clear(&vq->masked_notifier); return event_notifier_test_and_clear(&vq->masked_notifier);
} }
@ -988,7 +987,6 @@ void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
struct VirtQueue *vvq = virtio_get_queue(vdev, n); struct VirtQueue *vvq = virtio_get_queue(vdev, n);
int r, index = n - hdev->vq_index; int r, index = n - hdev->vq_index;
assert(hdev->started);
assert(n >= hdev->vq_index && n < hdev->vq_index + hdev->nvqs); assert(n >= hdev->vq_index && n < hdev->vq_index + hdev->nvqs);
struct vhost_vring_file file = { struct vhost_vring_file file = {