From patchwork Wed Aug 5 09:50:07 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Wang X-Patchwork-Id: 503947 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id DAF1814029D for ; Wed, 5 Aug 2015 19:51:03 +1000 (AEST) Received: from localhost ([::1]:39783 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZMvLi-000420-3g for incoming@patchwork.ozlabs.org; Wed, 05 Aug 2015 05:51:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49967) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZMvKz-0003A3-PC for qemu-devel@nongnu.org; Wed, 05 Aug 2015 05:50:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZMvKx-0005Db-Hd for qemu-devel@nongnu.org; Wed, 05 Aug 2015 05:50:17 -0400 Received: from mx1.redhat.com ([209.132.183.28]:43610) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZMvKx-0005Cq-9u for qemu-devel@nongnu.org; Wed, 05 Aug 2015 05:50:15 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id DF637A2C35; Wed, 5 Aug 2015 09:50:14 +0000 (UTC) Received: from jason-ThinkPad-T430s.nay.redhat.com (dhcp-14-122.nay.redhat.com [10.66.14.122]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t759o8Uo009749; Wed, 5 Aug 2015 05:50:10 -0400 From: Jason Wang To: mst@redhat.com, qemu-devel@nongnu.org Date: Wed, 5 Aug 2015 17:50:07 +0800 Message-Id: <1438768207-8439-1-git-send-email-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Cornelia Huck , Jason Wang , "Dr. David Alan Gilbert" Subject: [Qemu-devel] [PATCH for 2.4 V2] virtio: fix 1.0 virtqueue migration X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 1.0 does not requires physically-contiguous pages layout for a virtqueue. So we could not infer avail and used from desc. This means we need to migrate vring.avail and vring.used when host support virtio 1.0. This fixes malfunction of virtio 1.0 device after migration. Cc: Michael S. Tsirkin Cc: Cornelia Huck Cc: Dr. David Alan Gilbert Signed-off-by: Jason Wang Acked-by: Michael S. Tsirkin --- - Changes from V1: switch to use subsection to make debug easier --- hw/virtio/virtio.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++ include/hw/virtio/virtio.h | 6 +++++ 2 files changed, 62 insertions(+) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index ee4e07c..788b556 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -1049,6 +1049,61 @@ static bool virtio_64bit_features_needed(void *opaque) return (vdev->host_features >> 32) != 0; } +static bool virtio_virtqueue_needed(void *opaque) +{ + VirtIODevice *vdev = opaque; + + return virtio_host_has_feature(vdev, VIRTIO_F_VERSION_1); +} + +static void put_virtqueue_state(QEMUFile *f, void *pv, size_t size) +{ + VirtIODevice *vdev = pv; + int i; + + for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { + qemu_put_be64(f, vdev->vq[i].vring.avail); + qemu_put_be64(f, vdev->vq[i].vring.used); + } +} + +static int get_virtqueue_state(QEMUFile *f, void *pv, size_t size) +{ + VirtIODevice *vdev = pv; + int i; + + for (i = 0; i < VIRTIO_QUEUE_MAX; i++) { + vdev->vq[i].vring.avail = qemu_get_be64(f); + vdev->vq[i].vring.used = qemu_get_be64(f); + } + return 0; +} + +static VMStateInfo vmstate_info_virtqueue = { + .name = "virtqueue_state", + .get = get_virtqueue_state, + .put = put_virtqueue_state, +}; + +static const VMStateDescription vmstate_virtio_virtqueues = { + .name = "virtio/virtqueues", + .version_id = 1, + .minimum_version_id = 1, + .needed = &virtio_virtqueue_needed, + .fields = (VMStateField[]) { + { + .name = "virtqueues", + .version_id = 0, + .field_exists = NULL, + .size = 0, + .info = &vmstate_info_virtqueue, + .flags = VMS_SINGLE, + .offset = 0, + }, + VMSTATE_END_OF_LIST() + } +}; + static const VMStateDescription vmstate_virtio_device_endian = { .name = "virtio/device_endian", .version_id = 1, @@ -1082,6 +1137,7 @@ static const VMStateDescription vmstate_virtio = { .subsections = (const VMStateDescription*[]) { &vmstate_virtio_device_endian, &vmstate_virtio_64bit_features, + &vmstate_virtio_virtqueues, NULL } }; diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index 59f0763..cccae89 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -272,6 +272,12 @@ static inline bool virtio_has_feature(VirtIODevice *vdev, unsigned int fbit) return __virtio_has_feature(vdev->guest_features, fbit); } +static inline bool virtio_host_has_feature(VirtIODevice *vdev, + unsigned int fbit) +{ + return __virtio_has_feature(vdev->host_features, fbit); +} + static inline bool virtio_is_big_endian(VirtIODevice *vdev) { if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {