From nobody Sat Nov 8 02:50:22 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1550064988288232.88395303409015; Wed, 13 Feb 2019 05:36:28 -0800 (PST) Received: from localhost ([127.0.0.1]:56948 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtui9-0002TT-5o for importer@patchew.org; Wed, 13 Feb 2019 08:36:25 -0500 Received: from eggs.gnu.org ([209.51.188.92]:51552) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtuet-0000WB-CE for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:33:06 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gtues-0000ch-3O for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:33:03 -0500 Received: from mx1.redhat.com ([209.132.183.28]:54535) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gtuer-0000Zm-Qh for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:33:01 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3EF7181F07; Wed, 13 Feb 2019 13:26:01 +0000 (UTC) Received: from dell-per430-12.lab.eng.pek2.redhat.com (dell-per430-12.lab.eng.pek2.redhat.com [10.73.196.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id BF20E60863; Wed, 13 Feb 2019 13:25:58 +0000 (UTC) From: wexu@redhat.com To: qemu-devel@nongnu.org Date: Wed, 13 Feb 2019 08:25:36 -0500 Message-Id: <1550064346-17012-2-git-send-email-wexu@redhat.com> In-Reply-To: <1550064346-17012-1-git-send-email-wexu@redhat.com> References: <1550064346-17012-1-git-send-email-wexu@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Wed, 13 Feb 2019 13:26:01 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 01/11] virtio: rename structure for packed ring X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: tiwei.bie@intel.com, mst@redhat.com, jasowang@redhat.com, jfreiman@redhat.com, maxime.coquelin@redhat.com, wexu@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Wei Xu Redefine packed ring structure according to Qemu nomenclature. Signed-off-by: Wei Xu --- hw/virtio/virtio.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index a1ff647..eafb4cc 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -39,6 +39,13 @@ typedef struct VRingDesc uint16_t next; } VRingDesc; =20 +typedef struct VRingPackedDesc { + uint64_t addr; + uint32_t len; + uint16_t id; + uint16_t flags; +} VRingPackedDesc; + typedef struct VRingAvail { uint16_t flags; @@ -77,17 +84,25 @@ typedef struct VRing VRingMemoryRegionCaches *caches; } VRing; =20 +typedef struct VRingPackedDescEvent { + uint16_t off_wrap; + uint16_t flags; +} VRingPackedDescEvent ; + struct VirtQueue { VRing vring; =20 /* Next head to pop */ uint16_t last_avail_idx; + bool last_avail_wrap_counter; =20 /* Last avail_idx read from VQ. */ uint16_t shadow_avail_idx; + bool avail_wrap_counter; =20 uint16_t used_idx; + bool used_wrap_counter; =20 /* Last used index value we have signalled on */ uint16_t signalled_used; --=20 1.8.3.1 From nobody Sat Nov 8 02:50:22 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1550065178525649.5651747090359; Wed, 13 Feb 2019 05:39:38 -0800 (PST) Received: from localhost ([127.0.0.1]:56976 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtul9-0004vf-HZ for importer@patchew.org; Wed, 13 Feb 2019 08:39:31 -0500 Received: from eggs.gnu.org ([209.51.188.92]:52347) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtuhj-0002hq-J9 for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:36:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gtuhi-00041m-3O for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:35:59 -0500 Received: from mx1.redhat.com ([209.132.183.28]:50674) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gtuhh-0003sZ-Og for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:35:57 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A135BB212; Wed, 13 Feb 2019 13:26:08 +0000 (UTC) Received: from dell-per430-12.lab.eng.pek2.redhat.com (dell-per430-12.lab.eng.pek2.redhat.com [10.73.196.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id A8B7C60863; Wed, 13 Feb 2019 13:26:01 +0000 (UTC) From: wexu@redhat.com To: qemu-devel@nongnu.org Date: Wed, 13 Feb 2019 08:25:37 -0500 Message-Id: <1550064346-17012-3-git-send-email-wexu@redhat.com> In-Reply-To: <1550064346-17012-1-git-send-email-wexu@redhat.com> References: <1550064346-17012-1-git-send-email-wexu@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Wed, 13 Feb 2019 13:26:08 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 02/11] virtio: device/driver area size calculation helper for split ring X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: tiwei.bie@intel.com, mst@redhat.com, jasowang@redhat.com, jfreiman@redhat.com, maxime.coquelin@redhat.com, wexu@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Wei Xu There is slight size difference between split/packed rings. This is a refactor of split ring as well as a helper to expand device and driver area size calculation for packed ring. Signed-off-by: Wei Xu --- hw/virtio/virtio.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index eafb4cc..6769e54 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -155,10 +155,8 @@ static void virtio_init_region_cache(VirtIODevice *vde= v, int n) VRingMemoryRegionCaches *old =3D vq->vring.caches; VRingMemoryRegionCaches *new =3D NULL; hwaddr addr, size; - int event_size; int64_t len; =20 - event_size =3D virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_I= DX) ? 2 : 0; =20 addr =3D vq->vring.desc; if (!addr) { @@ -173,7 +171,7 @@ static void virtio_init_region_cache(VirtIODevice *vdev= , int n) goto err_desc; } =20 - size =3D virtio_queue_get_used_size(vdev, n) + event_size; + size =3D virtio_queue_get_used_size(vdev, n); len =3D address_space_cache_init(&new->used, vdev->dma_as, vq->vring.used, size, true); if (len < size) { @@ -181,7 +179,7 @@ static void virtio_init_region_cache(VirtIODevice *vdev= , int n) goto err_used; } =20 - size =3D virtio_queue_get_avail_size(vdev, n) + event_size; + size =3D virtio_queue_get_avail_size(vdev, n); len =3D address_space_cache_init(&new->avail, vdev->dma_as, vq->vring.avail, size, false); if (len < size) { @@ -2335,14 +2333,20 @@ hwaddr virtio_queue_get_desc_size(VirtIODevice *vde= v, int n) =20 hwaddr virtio_queue_get_avail_size(VirtIODevice *vdev, int n) { + int s; + + s =3D virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; return offsetof(VRingAvail, ring) + - sizeof(uint16_t) * vdev->vq[n].vring.num; + sizeof(uint16_t) * vdev->vq[n].vring.num + s; } =20 hwaddr virtio_queue_get_used_size(VirtIODevice *vdev, int n) { + int s; + + s =3D virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; return offsetof(VRingUsed, ring) + - sizeof(VRingUsedElem) * vdev->vq[n].vring.num; + sizeof(VRingUsedElem) * vdev->vq[n].vring.num + s; } =20 uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n) --=20 1.8.3.1 From nobody Sat Nov 8 02:50:22 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1550065283348397.88516779574525; Wed, 13 Feb 2019 05:41:23 -0800 (PST) Received: from localhost ([127.0.0.1]:57030 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtumq-0006O2-Am for importer@patchew.org; Wed, 13 Feb 2019 08:41:16 -0500 Received: from eggs.gnu.org ([209.51.188.92]:52310) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtuhi-0002h0-Os for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:36:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gtuhh-00040p-MC for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:35:58 -0500 Received: from mx1.redhat.com ([209.132.183.28]:26187) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gtuhh-0003sW-0F for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:35:57 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 44A127A19B; Wed, 13 Feb 2019 13:26:26 +0000 (UTC) Received: from dell-per430-12.lab.eng.pek2.redhat.com (dell-per430-12.lab.eng.pek2.redhat.com [10.73.196.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 15AA960863; Wed, 13 Feb 2019 13:26:08 +0000 (UTC) From: wexu@redhat.com To: qemu-devel@nongnu.org Date: Wed, 13 Feb 2019 08:25:38 -0500 Message-Id: <1550064346-17012-4-git-send-email-wexu@redhat.com> In-Reply-To: <1550064346-17012-1-git-send-email-wexu@redhat.com> References: <1550064346-17012-1-git-send-email-wexu@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Wed, 13 Feb 2019 13:26:26 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 03/11] virtio: initialize packed ring region X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: tiwei.bie@intel.com, mst@redhat.com, jasowang@redhat.com, jfreiman@redhat.com, maxime.coquelin@redhat.com, wexu@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Wei Xu Initialize packed ring memory region with correct size and attribute. Signed-off-by: Wei Xu --- hw/virtio/virtio.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 6769e54..1a98e61 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -156,7 +156,7 @@ static void virtio_init_region_cache(VirtIODevice *vdev= , int n) VRingMemoryRegionCaches *new =3D NULL; hwaddr addr, size; int64_t len; - + bool attr; =20 addr =3D vq->vring.desc; if (!addr) { @@ -164,8 +164,10 @@ static void virtio_init_region_cache(VirtIODevice *vde= v, int n) } new =3D g_new0(VRingMemoryRegionCaches, 1); size =3D virtio_queue_get_desc_size(vdev, n); + attr =3D virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED) ? + true : false; len =3D address_space_cache_init(&new->desc, vdev->dma_as, - addr, size, false); + addr, size, attr); if (len < size) { virtio_error(vdev, "Cannot map desc"); goto err_desc; @@ -2335,6 +2337,10 @@ hwaddr virtio_queue_get_avail_size(VirtIODevice *vde= v, int n) { int s; =20 + if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { + return sizeof(struct VRingPackedDescEvent); + } + s =3D virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; return offsetof(VRingAvail, ring) + sizeof(uint16_t) * vdev->vq[n].vring.num + s; @@ -2344,6 +2350,10 @@ hwaddr virtio_queue_get_used_size(VirtIODevice *vdev= , int n) { int s; =20 + if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { + return sizeof(struct VRingPackedDescEvent); + } + s =3D virtio_vdev_has_feature(vdev, VIRTIO_RING_F_EVENT_IDX) ? 2 : 0; return offsetof(VRingUsed, ring) + sizeof(VRingUsedElem) * vdev->vq[n].vring.num + s; --=20 1.8.3.1 From nobody Sat Nov 8 02:50:22 2025 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=temperror (zoho.com: Error in retrieving data from DNS) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1550065130399320.20315062483485; Wed, 13 Feb 2019 05:38:50 -0800 (PST) Received: from localhost ([127.0.0.1]:56972 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtukH-0004Js-9L for importer@patchew.org; Wed, 13 Feb 2019 08:38:37 -0500 Received: from eggs.gnu.org ([209.51.188.92]:52109) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtugq-0001sX-1L for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:35:09 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gtugo-00039v-Bp for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:35:04 -0500 Received: from mx1.redhat.com ([209.132.183.28]:60468) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gtugo-00035z-64 for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:35:02 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A8B95A768; Wed, 13 Feb 2019 13:27:08 +0000 (UTC) Received: from dell-per430-12.lab.eng.pek2.redhat.com (dell-per430-12.lab.eng.pek2.redhat.com [10.73.196.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id B488660863; Wed, 13 Feb 2019 13:26:26 +0000 (UTC) From: wexu@redhat.com To: qemu-devel@nongnu.org Date: Wed, 13 Feb 2019 08:25:39 -0500 Message-Id: <1550064346-17012-5-git-send-email-wexu@redhat.com> In-Reply-To: <1550064346-17012-1-git-send-email-wexu@redhat.com> References: <1550064346-17012-1-git-send-email-wexu@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Wed, 13 Feb 2019 13:27:08 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 04/11] virtio: initialize wrap counter for packed ring X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: tiwei.bie@intel.com, mst@redhat.com, jasowang@redhat.com, jfreiman@redhat.com, maxime.coquelin@redhat.com, wexu@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Wei Xu Set to 'true' by default due to spec. Signed-off-by: Wei Xu --- hw/virtio/virtio.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 1a98e61..54dc098 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -1238,6 +1238,9 @@ void virtio_reset(void *opaque) vdev->vq[i].last_avail_idx =3D 0; vdev->vq[i].shadow_avail_idx =3D 0; vdev->vq[i].used_idx =3D 0; + vdev->vq[i].last_avail_wrap_counter =3D true; + vdev->vq[i].avail_wrap_counter =3D true; + vdev->vq[i].used_wrap_counter =3D true; virtio_queue_set_vector(vdev, i, VIRTIO_NO_VECTOR); vdev->vq[i].signalled_used =3D 0; vdev->vq[i].signalled_used_valid =3D false; --=20 1.8.3.1 From nobody Sat Nov 8 02:50:22 2025 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=temperror (zoho.com: Error in retrieving data from DNS) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1550065529725612.441285290824; Wed, 13 Feb 2019 05:45:29 -0800 (PST) Received: from localhost ([127.0.0.1]:57067 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtuqk-00013D-J4 for importer@patchew.org; Wed, 13 Feb 2019 08:45:18 -0500 Received: from eggs.gnu.org ([209.51.188.92]:52797) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtuja-00045m-54 for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:37:55 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gtujY-0006OL-2f for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:37:54 -0500 Received: from mx1.redhat.com ([209.132.183.28]:52616) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gtujT-0006Gk-T1 for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:37:48 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1B85CC04B2D7; Wed, 13 Feb 2019 13:27:57 +0000 (UTC) Received: from dell-per430-12.lab.eng.pek2.redhat.com (dell-per430-12.lab.eng.pek2.redhat.com [10.73.196.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1AC1660863; Wed, 13 Feb 2019 13:27:08 +0000 (UTC) From: wexu@redhat.com To: qemu-devel@nongnu.org Date: Wed, 13 Feb 2019 08:25:40 -0500 Message-Id: <1550064346-17012-6-git-send-email-wexu@redhat.com> In-Reply-To: <1550064346-17012-1-git-send-email-wexu@redhat.com> References: <1550064346-17012-1-git-send-email-wexu@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Wed, 13 Feb 2019 13:27:57 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 05/11] virtio: queue/descriptor check helpers for packed ring X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: tiwei.bie@intel.com, mst@redhat.com, jasowang@redhat.com, jfreiman@redhat.com, maxime.coquelin@redhat.com, wexu@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Wei Xu These are descriptor available and queue empty check helpers. Signed-off-by: Wei Xu --- hw/virtio/virtio.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++= +++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 54dc098..f2ff980 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -368,6 +368,25 @@ int virtio_queue_ready(VirtQueue *vq) return vq->vring.avail !=3D 0; } =20 +static void vring_packed_desc_read_flags(VirtIODevice *vdev, + VRingPackedDesc *desc, MemoryRegionCache *cache, int i) +{ + address_space_read_cached(cache, + i * sizeof(VRingPackedDesc) + offsetof(VRingPackedDesc, flag= s), + &desc->flags, sizeof(desc->flags)); + virtio_tswap16s(vdev, &desc->flags); +} + +static inline bool is_desc_avail(struct VRingPackedDesc *desc, + bool wrap_counter) +{ + bool avail, used; + + avail =3D !!(desc->flags & (1 << VRING_PACKED_DESC_F_AVAIL)); + used =3D !!(desc->flags & (1 << VRING_PACKED_DESC_F_USED)); + return (avail !=3D used) && (avail =3D=3D wrap_counter); +} + /* Fetch avail_idx from VQ memory only when we really need to know if * guest has added some buffers. * Called within rcu_read_lock(). */ @@ -388,7 +407,7 @@ static int virtio_queue_empty_rcu(VirtQueue *vq) return vring_avail_idx(vq) =3D=3D vq->last_avail_idx; } =20 -int virtio_queue_empty(VirtQueue *vq) +static int virtio_queue_split_empty(VirtQueue *vq) { bool empty; =20 @@ -410,6 +429,41 @@ int virtio_queue_empty(VirtQueue *vq) return empty; } =20 +static int virtio_queue_packed_empty_rcu(VirtQueue *vq) +{ + struct VRingPackedDesc desc; + VRingMemoryRegionCaches *cache; + + if (unlikely(!vq->vring.desc)) { + return 1; + } + + cache =3D vring_get_region_caches(vq); + vring_packed_desc_read_flags(vq->vdev, &desc, &cache->desc, + vq->last_avail_idx); + + return !is_desc_avail(&desc, vq->last_avail_wrap_counter); +} + +static int virtio_queue_packed_empty(VirtQueue *vq) +{ + bool empty; + + rcu_read_lock(); + empty =3D virtio_queue_packed_empty_rcu(vq); + rcu_read_unlock(); + return empty; +} + +int virtio_queue_empty(VirtQueue *vq) +{ + if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) { + return virtio_queue_packed_empty(vq); + } else { + return virtio_queue_split_empty(vq); + } +} + static void virtqueue_unmap_sg(VirtQueue *vq, const VirtQueueElement *elem, unsigned int len) { --=20 1.8.3.1 From nobody Sat Nov 8 02:50:22 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1550065406142770.2284387654876; Wed, 13 Feb 2019 05:43:26 -0800 (PST) Received: from localhost ([127.0.0.1]:57049 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtuoq-00087n-4d for importer@patchew.org; Wed, 13 Feb 2019 08:43:20 -0500 Received: from eggs.gnu.org ([209.51.188.92]:52593) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtuig-0003N8-C5 for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:36:59 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gtuif-0005Pk-0K for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:36:58 -0500 Received: from mx1.redhat.com ([209.132.183.28]:39818) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gtuie-00056s-Nj for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:36:56 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DDE75369BC; Wed, 13 Feb 2019 13:28:02 +0000 (UTC) Received: from dell-per430-12.lab.eng.pek2.redhat.com (dell-per430-12.lab.eng.pek2.redhat.com [10.73.196.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8562260856; Wed, 13 Feb 2019 13:27:57 +0000 (UTC) From: wexu@redhat.com To: qemu-devel@nongnu.org Date: Wed, 13 Feb 2019 08:25:41 -0500 Message-Id: <1550064346-17012-7-git-send-email-wexu@redhat.com> In-Reply-To: <1550064346-17012-1-git-send-email-wexu@redhat.com> References: <1550064346-17012-1-git-send-email-wexu@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Wed, 13 Feb 2019 13:28:02 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 06/11] virtio: get avail bytes check for packed ring X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: tiwei.bie@intel.com, mst@redhat.com, jasowang@redhat.com, jfreiman@redhat.com, maxime.coquelin@redhat.com, wexu@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Wei Xu Add packed ring headcount check. Common part of split/packed ring are kept. Signed-off-by: Wei Xu --- hw/virtio/virtio.c | 197 ++++++++++++++++++++++++++++++++++++++++++++++++-= ---- 1 file changed, 179 insertions(+), 18 deletions(-) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index f2ff980..832287b 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -368,6 +368,17 @@ int virtio_queue_ready(VirtQueue *vq) return vq->vring.avail !=3D 0; } =20 +static void vring_packed_desc_read(VirtIODevice *vdev, VRingPackedDesc *de= sc, + MemoryRegionCache *cache, int i) +{ + address_space_read_cached(cache, i * sizeof(VRingPackedDesc), + desc, sizeof(VRingPackedDesc)); + virtio_tswap16s(vdev, &desc->flags); + virtio_tswap64s(vdev, &desc->addr); + virtio_tswap32s(vdev, &desc->len); + virtio_tswap16s(vdev, &desc->id); +} + static void vring_packed_desc_read_flags(VirtIODevice *vdev, VRingPackedDesc *desc, MemoryRegionCache *cache, int i) { @@ -667,9 +678,9 @@ static int virtqueue_read_next_desc(VirtIODevice *vdev,= VRingDesc *desc, return VIRTQUEUE_READ_DESC_MORE; } =20 -void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes, - unsigned int *out_bytes, - unsigned max_in_bytes, unsigned max_out_byt= es) +static void virtqueue_split_get_avail_bytes(VirtQueue *vq, + unsigned int *in_bytes, unsigned int *out_byte= s, + unsigned max_in_bytes, unsigned max_out_bytes) { VirtIODevice *vdev =3D vq->vdev; unsigned int max, idx; @@ -679,27 +690,12 @@ void virtqueue_get_avail_bytes(VirtQueue *vq, unsigne= d int *in_bytes, int64_t len =3D 0; int rc; =20 - if (unlikely(!vq->vring.desc)) { - if (in_bytes) { - *in_bytes =3D 0; - } - if (out_bytes) { - *out_bytes =3D 0; - } - return; - } - rcu_read_lock(); idx =3D vq->last_avail_idx; total_bufs =3D in_total =3D out_total =3D 0; =20 max =3D vq->vring.num; caches =3D vring_get_region_caches(vq); - if (caches->desc.len < max * sizeof(VRingDesc)) { - virtio_error(vdev, "Cannot map descriptor ring"); - goto err; - } - while ((rc =3D virtqueue_num_heads(vq, idx)) > 0) { MemoryRegionCache *desc_cache =3D &caches->desc; unsigned int num_bufs; @@ -792,6 +788,171 @@ err: goto done; } =20 +static void virtqueue_packed_get_avail_bytes(VirtQueue *vq, + unsigned int *in_bytes, unsigned int *out_byte= s, + unsigned max_in_bytes, unsigned max_out_bytes) +{ + VirtIODevice *vdev =3D vq->vdev; + unsigned int max, idx; + unsigned int total_bufs, in_total, out_total; + MemoryRegionCache *desc_cache; + VRingMemoryRegionCaches *caches; + MemoryRegionCache indirect_desc_cache =3D MEMORY_REGION_CACHE_INVALID; + int64_t len =3D 0; + VRingPackedDesc desc; + bool wrap_counter; + + rcu_read_lock(); + idx =3D vq->last_avail_idx; + wrap_counter =3D vq->last_avail_wrap_counter; + total_bufs =3D in_total =3D out_total =3D 0; + + max =3D vq->vring.num; + caches =3D vring_get_region_caches(vq); + desc_cache =3D &caches->desc; + vring_packed_desc_read_flags(vdev, &desc, desc_cache, idx); + while (is_desc_avail(&desc, wrap_counter)) { + unsigned int num_bufs; + unsigned int i =3D 0; + + num_bufs =3D total_bufs; + + /* Make sure flags has been read before all the fields. */ + smp_rmb(); + vring_packed_desc_read(vdev, &desc, desc_cache, idx); + + if (desc.flags & VRING_DESC_F_INDIRECT) { + if (desc.len % sizeof(VRingPackedDesc)) { + virtio_error(vdev, "Invalid size for indirect buffer table= "); + goto err; + } + + /* If we've got too many, that implies a descriptor loop. */ + if (num_bufs >=3D max) { + virtio_error(vdev, "Looped descriptor"); + goto err; + } + + /* loop over the indirect descriptor table */ + len =3D address_space_cache_init(&indirect_desc_cache, + vdev->dma_as, + desc.addr, desc.len, false); + desc_cache =3D &indirect_desc_cache; + if (len < desc.len) { + virtio_error(vdev, "Cannot map indirect buffer"); + goto err; + } + + max =3D desc.len / sizeof(VRingPackedDesc); + num_bufs =3D i =3D 0; + vring_packed_desc_read(vdev, &desc, desc_cache, i); + } + + do { + /* If we've got too many, that implies a descriptor loop. */ + if (++num_bufs > max) { + virtio_error(vdev, "Looped descriptor"); + goto err; + } + + if (desc.flags & VRING_DESC_F_WRITE) { + in_total +=3D desc.len; + } else { + out_total +=3D desc.len; + } + if (in_total >=3D max_in_bytes && out_total >=3D max_out_bytes= ) { + goto done; + } + + if (desc_cache =3D=3D &indirect_desc_cache) { + if (++i >=3D max) { + break; + } + vring_packed_desc_read(vdev, &desc, desc_cache, i); + } else { + if (++idx >=3D vq->vring.num) { + idx -=3D vq->vring.num; + wrap_counter ^=3D 1; + } + vring_packed_desc_read(vdev, &desc, desc_cache, idx); + } + /* Make sure flags has been read after all the other fields */ + smp_rmb(); + } while (desc.flags & VRING_DESC_F_NEXT); + + if (desc_cache =3D=3D &indirect_desc_cache) { + address_space_cache_destroy(&indirect_desc_cache); + total_bufs++; + /* We missed one step on for indirect desc */ + idx++; + if (++idx >=3D vq->vring.num) { + idx -=3D vq->vring.num; + wrap_counter ^=3D 1; + } + } else { + total_bufs =3D num_bufs; + } + + desc_cache =3D &caches->desc; + vring_packed_desc_read_flags(vdev, &desc, desc_cache, idx); + } + + /* Record the index and wrap counter for a kick we want */ + vq->shadow_avail_idx =3D idx; + vq->avail_wrap_counter =3D wrap_counter; +done: + address_space_cache_destroy(&indirect_desc_cache); + if (in_bytes) { + *in_bytes =3D in_total; + } + if (out_bytes) { + *out_bytes =3D out_total; + } + rcu_read_unlock(); + return; + +err: + in_total =3D out_total =3D 0; + goto done; +} + +void virtqueue_get_avail_bytes(VirtQueue *vq, unsigned int *in_bytes, + unsigned int *out_bytes, + unsigned max_in_bytes, unsigned max_out_byt= es) +{ + uint16_t desc_size; + VRingMemoryRegionCaches *caches; + + if (unlikely(!vq->vring.desc)) { + goto err; + } + + caches =3D vring_get_region_caches(vq); + desc_size =3D virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED) ? + sizeof(VRingPackedDesc) : sizeof(VRingDesc= ); + if (caches->desc.len < vq->vring.num * desc_size) { + virtio_error(vq->vdev, "Cannot map descriptor ring"); + goto err; + } + + if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) { + virtqueue_packed_get_avail_bytes(vq, in_bytes, out_bytes, + max_in_bytes, max_out_bytes); + } else { + virtqueue_split_get_avail_bytes(vq, in_bytes, out_bytes, + max_in_bytes, max_out_bytes); + } + + return; +err: + if (in_bytes) { + *in_bytes =3D 0; + } + if (out_bytes) { + *out_bytes =3D 0; + } +} + int virtqueue_avail_bytes(VirtQueue *vq, unsigned int in_bytes, unsigned int out_bytes) { --=20 1.8.3.1 From nobody Sat Nov 8 02:50:22 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1550065034961654.1444991554099; Wed, 13 Feb 2019 05:37:14 -0800 (PST) Received: from localhost ([127.0.0.1]:56950 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtuip-00036P-QT for importer@patchew.org; Wed, 13 Feb 2019 08:37:07 -0500 Received: from eggs.gnu.org ([209.51.188.92]:52107) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtugp-0001sW-UX for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:35:05 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gtugo-0003A5-Ef for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:35:03 -0500 Received: from mx1.redhat.com ([209.132.183.28]:23350) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gtugo-000362-68 for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:35:02 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 87CE4FECD; Wed, 13 Feb 2019 13:28:05 +0000 (UTC) Received: from dell-per430-12.lab.eng.pek2.redhat.com (dell-per430-12.lab.eng.pek2.redhat.com [10.73.196.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5C8B260856; Wed, 13 Feb 2019 13:28:03 +0000 (UTC) From: wexu@redhat.com To: qemu-devel@nongnu.org Date: Wed, 13 Feb 2019 08:25:42 -0500 Message-Id: <1550064346-17012-8-git-send-email-wexu@redhat.com> In-Reply-To: <1550064346-17012-1-git-send-email-wexu@redhat.com> References: <1550064346-17012-1-git-send-email-wexu@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Wed, 13 Feb 2019 13:28:05 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 07/11] virtio: fill/flush/pop for packed ring X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: tiwei.bie@intel.com, mst@redhat.com, jasowang@redhat.com, jfreiman@redhat.com, maxime.coquelin@redhat.com, wexu@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Wei Xu last_used_idx/wrap_counter should be equal to last_avail_idx/wrap_counter after a successful flush. Batching in vhost-net & dpdk testpmd is not equivalently supported in userspace backend, but a chained descriptors for Rx is similarly presented as a lightweight batch, so a write barrier is nailed only for the first(head) descriptor. Signed-off-by: Wei Xu --- hw/virtio/virtio.c | 291 +++++++++++++++++++++++++++++++++++++++++++++++++= ---- 1 file changed, 274 insertions(+), 17 deletions(-) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 832287b..7e276b4 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -379,6 +379,25 @@ static void vring_packed_desc_read(VirtIODevice *vdev,= VRingPackedDesc *desc, virtio_tswap16s(vdev, &desc->id); } =20 +static void vring_packed_desc_write_data(VirtIODevice *vdev, + VRingPackedDesc *desc, MemoryRegionCache *cache, int i) +{ + virtio_tswap32s(vdev, &desc->len); + virtio_tswap16s(vdev, &desc->id); + address_space_write_cached(cache, + i * sizeof(VRingPackedDesc) + offsetof(VRingPackedDesc, id), + &desc->id, sizeof(desc->id)); + address_space_cache_invalidate(cache, + i * sizeof(VRingPackedDesc) + offsetof(VRingPackedDesc, id), + sizeof(desc->id)); + address_space_write_cached(cache, + i * sizeof(VRingPackedDesc) + offsetof(VRingPackedDesc, len), + &desc->len, sizeof(desc->len)); + address_space_cache_invalidate(cache, + i * sizeof(VRingPackedDesc) + offsetof(VRingPackedDesc, len), + sizeof(desc->len)); +} + static void vring_packed_desc_read_flags(VirtIODevice *vdev, VRingPackedDesc *desc, MemoryRegionCache *cache, int i) { @@ -388,6 +407,18 @@ static void vring_packed_desc_read_flags(VirtIODevice = *vdev, virtio_tswap16s(vdev, &desc->flags); } =20 +static void vring_packed_desc_write_flags(VirtIODevice *vdev, + VRingPackedDesc *desc, MemoryRegionCache *cache, int i) +{ + virtio_tswap16s(vdev, &desc->flags); + address_space_write_cached(cache, + i * sizeof(VRingPackedDesc) + offsetof(VRingPackedDesc, flag= s), + &desc->flags, sizeof(desc->flags)); + address_space_cache_invalidate(cache, + i * sizeof(VRingPackedDesc) + offsetof(VRingPackedDesc, flag= s), + sizeof(desc->flags)); +} + static inline bool is_desc_avail(struct VRingPackedDesc *desc, bool wrap_counter) { @@ -554,19 +585,11 @@ bool virtqueue_rewind(VirtQueue *vq, unsigned int num) } =20 /* Called within rcu_read_lock(). */ -void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem, +static void virtqueue_split_fill(VirtQueue *vq, const VirtQueueElement *el= em, unsigned int len, unsigned int idx) { VRingUsedElem uelem; =20 - trace_virtqueue_fill(vq, elem, len, idx); - - virtqueue_unmap_sg(vq, elem, len); - - if (unlikely(vq->vdev->broken)) { - return; - } - if (unlikely(!vq->vring.used)) { return; } @@ -578,16 +601,71 @@ void virtqueue_fill(VirtQueue *vq, const VirtQueueEle= ment *elem, vring_used_write(vq, &uelem, idx); } =20 -/* Called within rcu_read_lock(). */ -void virtqueue_flush(VirtQueue *vq, unsigned int count) +static void virtqueue_packed_fill(VirtQueue *vq, const VirtQueueElement *e= lem, + unsigned int len, unsigned int idx) { - uint16_t old, new; + uint16_t head; + VRingMemoryRegionCaches *caches; + VRingPackedDesc desc =3D { + .flags =3D 0, + .id =3D elem->index, + .len =3D len, + }; + bool wrap_counter =3D vq->used_wrap_counter; + + if (unlikely(!vq->vring.desc)) { + return; + } + + head =3D vq->used_idx + idx; + if (head >=3D vq->vring.num) { + head -=3D vq->vring.num; + wrap_counter ^=3D 1; + } + if (wrap_counter) { + desc.flags |=3D (1 << VRING_PACKED_DESC_F_AVAIL); + desc.flags |=3D (1 << VRING_PACKED_DESC_F_USED); + } else { + desc.flags &=3D ~(1 << VRING_PACKED_DESC_F_AVAIL); + desc.flags &=3D ~(1 << VRING_PACKED_DESC_F_USED); + } + + caches =3D vring_get_region_caches(vq); + vring_packed_desc_write_data(vq->vdev, &desc, &caches->desc, head); + if (idx =3D=3D 0) { + /* + * Make sure descriptor id and len is written before + * flags for the first used buffer. + */ + smp_wmb(); + } + + vring_packed_desc_write_flags(vq->vdev, &desc, &caches->desc, head); +} + +void virtqueue_fill(VirtQueue *vq, const VirtQueueElement *elem, + unsigned int len, unsigned int idx) +{ + trace_virtqueue_fill(vq, elem, len, idx); + + virtqueue_unmap_sg(vq, elem, len); =20 if (unlikely(vq->vdev->broken)) { - vq->inuse -=3D count; return; } =20 + if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) { + virtqueue_packed_fill(vq, elem, len, idx); + } else { + virtqueue_split_fill(vq, elem, len, idx); + } +} + +/* Called within rcu_read_lock(). */ +static void virtqueue_split_flush(VirtQueue *vq, unsigned int count) +{ + uint16_t old, new; + if (unlikely(!vq->vring.used)) { return; } @@ -603,6 +681,31 @@ void virtqueue_flush(VirtQueue *vq, unsigned int count) vq->signalled_used_valid =3D false; } =20 +static void virtqueue_packed_flush(VirtQueue *vq, unsigned int count) +{ + if (unlikely(!vq->vring.desc)) { + return; + } + + vq->inuse -=3D count; + vq->used_idx =3D vq->last_avail_idx; + vq->used_wrap_counter =3D vq->last_avail_wrap_counter; +} + +void virtqueue_flush(VirtQueue *vq, unsigned int count) +{ + if (unlikely(vq->vdev->broken)) { + vq->inuse -=3D count; + return; + } + + if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) { + virtqueue_packed_flush(vq, count); + } else { + virtqueue_split_flush(vq, count); + } +} + void virtqueue_push(VirtQueue *vq, const VirtQueueElement *elem, unsigned int len) { @@ -1077,7 +1180,7 @@ static void *virtqueue_alloc_element(size_t sz, unsig= ned out_num, unsigned in_nu return elem; } =20 -void *virtqueue_pop(VirtQueue *vq, size_t sz) +static void *virtqueue_split_pop(VirtQueue *vq, size_t sz) { unsigned int i, head, max; VRingMemoryRegionCaches *caches; @@ -1092,9 +1195,6 @@ void *virtqueue_pop(VirtQueue *vq, size_t sz) VRingDesc desc; int rc; =20 - if (unlikely(vdev->broken)) { - return NULL; - } rcu_read_lock(); if (virtio_queue_empty_rcu(vq)) { goto done; @@ -1212,6 +1312,163 @@ err_undo_map: goto done; } =20 +static void *virtqueue_packed_pop(VirtQueue *vq, size_t sz) +{ + unsigned int i, head, max; + VRingMemoryRegionCaches *caches; + MemoryRegionCache indirect_desc_cache =3D MEMORY_REGION_CACHE_INVALID; + MemoryRegionCache *cache; + int64_t len; + VirtIODevice *vdev =3D vq->vdev; + VirtQueueElement *elem =3D NULL; + unsigned out_num, in_num, elem_entries; + hwaddr addr[VIRTQUEUE_MAX_SIZE]; + struct iovec iov[VIRTQUEUE_MAX_SIZE]; + VRingPackedDesc desc; + uint16_t id; + + rcu_read_lock(); + if (virtio_queue_packed_empty_rcu(vq)) { + goto done; + } + + /* When we start there are none of either input nor output. */ + out_num =3D in_num =3D elem_entries =3D 0; + + max =3D vq->vring.num; + + if (vq->inuse >=3D vq->vring.num) { + virtio_error(vdev, "Virtqueue size exceeded"); + goto done; + } + + head =3D vq->last_avail_idx; + i =3D head; + + caches =3D vring_get_region_caches(vq); + cache =3D &caches->desc; + + /* make sure flags is read before all the other fields */ + smp_rmb(); + vring_packed_desc_read(vdev, &desc, cache, i); + + id =3D desc.id; + if (desc.flags & VRING_DESC_F_INDIRECT) { + + if (desc.len % sizeof(VRingPackedDesc)) { + virtio_error(vdev, "Invalid size for indirect buffer table"); + goto done; + } + + /* loop over the indirect descriptor table */ + len =3D address_space_cache_init(&indirect_desc_cache, vdev->dma_a= s, + desc.addr, desc.len, false); + cache =3D &indirect_desc_cache; + if (len < desc.len) { + virtio_error(vdev, "Cannot map indirect buffer"); + goto done; + } + + max =3D desc.len / sizeof(VRingPackedDesc); + i =3D 0; + vring_packed_desc_read(vdev, &desc, cache, i); + /* Make sure we see all the fields*/ + smp_rmb(); + } + + /* Collect all the descriptors */ + while (1) { + bool map_ok; + + if (desc.flags & VRING_DESC_F_WRITE) { + map_ok =3D virtqueue_map_desc(vdev, &in_num, addr + out_num, + iov + out_num, + VIRTQUEUE_MAX_SIZE - out_num, true, + desc.addr, desc.len); + } else { + if (in_num) { + virtio_error(vdev, "Incorrect order for descriptors"); + goto err_undo_map; + } + map_ok =3D virtqueue_map_desc(vdev, &out_num, addr, iov, + VIRTQUEUE_MAX_SIZE, false, + desc.addr, desc.len); + } + if (!map_ok) { + goto err_undo_map; + } + + /* If we've got too many, that implies a descriptor loop. */ + if (++elem_entries > max) { + virtio_error(vdev, "Looped descriptor"); + goto err_undo_map; + } + + if (++i >=3D vq->vring.num) { + i -=3D vq->vring.num; + } + + if (cache =3D=3D &indirect_desc_cache) { + if (i =3D=3D max) { + break; + } + vring_packed_desc_read(vq->vdev, &desc, cache, i); + } else if (desc.flags & VRING_DESC_F_NEXT) { + vring_packed_desc_read(vq->vdev, &desc, cache, i); + } else { + break; + } + } + + /* Now copy what we have collected and mapped */ + elem =3D virtqueue_alloc_element(sz, out_num, in_num); + elem->index =3D id; + for (i =3D 0; i < out_num; i++) { + elem->out_addr[i] =3D addr[i]; + elem->out_sg[i] =3D iov[i]; + } + for (i =3D 0; i < in_num; i++) { + elem->in_addr[i] =3D addr[head + out_num + i]; + elem->in_sg[i] =3D iov[out_num + i]; + } + + vq->last_avail_idx +=3D (cache =3D=3D &indirect_desc_cache) ? + 1 : elem_entries; + if (vq->last_avail_idx >=3D vq->vring.num) { + vq->last_avail_idx -=3D vq->vring.num; + vq->last_avail_wrap_counter ^=3D 1; + } + vq->inuse++; + + vq->shadow_avail_idx =3D vq->last_avail_idx; + vq->avail_wrap_counter =3D vq->last_avail_wrap_counter; + + trace_virtqueue_pop(vq, elem, elem->in_num, elem->out_num); +done: + address_space_cache_destroy(&indirect_desc_cache); + rcu_read_unlock(); + + return elem; + +err_undo_map: + virtqueue_undo_map_desc(out_num, in_num, iov); + g_free(elem); + goto done; +} + +void *virtqueue_pop(VirtQueue *vq, size_t sz) +{ + if (unlikely(vq->vdev->broken)) { + return NULL; + } + + if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) { + return virtqueue_packed_pop(vq, sz); + } else { + return virtqueue_split_pop(vq, sz); + } +} + /* virtqueue_drop_all: * @vq: The #VirtQueue * Drops all queued buffers and indicates them to the guest --=20 1.8.3.1 From nobody Sat Nov 8 02:50:22 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 155006531456599.68197510992582; Wed, 13 Feb 2019 05:41:54 -0800 (PST) Received: from localhost ([127.0.0.1]:57032 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtunP-0006rR-GJ for importer@patchew.org; Wed, 13 Feb 2019 08:41:51 -0500 Received: from eggs.gnu.org ([209.51.188.92]:52823) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtujd-00048J-2u for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:37:58 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gtuja-0006TI-61 for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:37:56 -0500 Received: from mx1.redhat.com ([209.132.183.28]:52614) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gtujY-0006Gh-4S for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:37:53 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2A2BCC057F99; Wed, 13 Feb 2019 13:28:08 +0000 (UTC) Received: from dell-per430-12.lab.eng.pek2.redhat.com (dell-per430-12.lab.eng.pek2.redhat.com [10.73.196.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 03B7F60857; Wed, 13 Feb 2019 13:28:05 +0000 (UTC) From: wexu@redhat.com To: qemu-devel@nongnu.org Date: Wed, 13 Feb 2019 08:25:43 -0500 Message-Id: <1550064346-17012-9-git-send-email-wexu@redhat.com> In-Reply-To: <1550064346-17012-1-git-send-email-wexu@redhat.com> References: <1550064346-17012-1-git-send-email-wexu@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Wed, 13 Feb 2019 13:28:08 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 08/11] virtio: event suppression support for packed ring X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: tiwei.bie@intel.com, mst@redhat.com, jasowang@redhat.com, jfreiman@redhat.com, maxime.coquelin@redhat.com, wexu@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Wei Xu Difference between 'avail_wrap_counter' and 'last_avail_wrap_counter': For Tx(guest transmitting), they are the same after each pop of a desc. For Rx(guest receiving), they are also the same when there are enough descriptors to carry the payload for a packet(e.g. usually 16 descs are needed for a 64k packet in typical iperf tcp connection with tso enabled), however, when the ring is running out of descriptors while there are still a few free ones, e.g. 6 descriptors are available which is not enough to carry an entire packet which needs 16 descriptors, in this case the 'avail_wrap_counter' should be set as the first one pending being handled by guest driver in order to get a notification, and the 'last_avail_wrap_counter' should stay unchanged to the head of available descriptors, like below: Mark meaning: | | -- available |*| -- used A Snapshot of the queue: last_avail_idx =3D 253 last_avail_wrap_counter =3D 1 | +---------------------------------------------+ 0 | | | |*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*|*| | | | 255 +---------------------------------------------+ | shadow_avail_idx =3D 3 avail_wrap_counter =3D 0 Signed-off-by: Wei Xu --- hw/virtio/virtio.c | 137 +++++++++++++++++++++++++++++++++++++++++++++++++= ---- 1 file changed, 128 insertions(+), 9 deletions(-) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 7e276b4..8cfc7b6 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -234,6 +234,34 @@ static void vring_desc_read(VirtIODevice *vdev, VRingD= esc *desc, virtio_tswap16s(vdev, &desc->next); } =20 +static void vring_packed_event_read(VirtIODevice *vdev, + MemoryRegionCache *cache, VRingPackedDescEvent= *e) +{ + address_space_read_cached(cache, 0, e, sizeof(*e)); + virtio_tswap16s(vdev, &e->off_wrap); + virtio_tswap16s(vdev, &e->flags); +} + +static void vring_packed_off_wrap_write(VirtIODevice *vdev, + MemoryRegionCache *cache, uint16_t off_wrap) +{ + virtio_tswap16s(vdev, &off_wrap); + address_space_write_cached(cache, offsetof(VRingPackedDescEvent, off_w= rap), + &off_wrap, sizeof(off_wrap)); + address_space_cache_invalidate(cache, + offsetof(VRingPackedDescEvent, off_wrap), sizeof(off_wrap)= ); +} + +static void vring_packed_flags_write(VirtIODevice *vdev, + MemoryRegionCache *cache, uint16_t flags) +{ + virtio_tswap16s(vdev, &flags); + address_space_write_cached(cache, offsetof(VRingPackedDescEvent, flags= ), + &flags, sizeof(flags)); + address_space_cache_invalidate(cache, + offsetof(VRingPackedDescEvent, flags), sizeof(flag= s)); +} + static VRingMemoryRegionCaches *vring_get_region_caches(struct VirtQueue *= vq) { VRingMemoryRegionCaches *caches =3D atomic_rcu_read(&vq->vring.caches); @@ -340,14 +368,8 @@ static inline void vring_set_avail_event(VirtQueue *vq= , uint16_t val) address_space_cache_invalidate(&caches->used, pa, sizeof(val)); } =20 -void virtio_queue_set_notification(VirtQueue *vq, int enable) +static void virtio_queue_set_notification_split(VirtQueue *vq, int enable) { - vq->notification =3D enable; - - if (!vq->vring.desc) { - return; - } - rcu_read_lock(); if (virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX)) { vring_set_avail_event(vq, vring_avail_idx(vq)); @@ -363,6 +385,57 @@ void virtio_queue_set_notification(VirtQueue *vq, int = enable) rcu_read_unlock(); } =20 +static void virtio_queue_set_notification_packed(VirtQueue *vq, int enable) +{ + VRingPackedDescEvent e; + VRingMemoryRegionCaches *caches; + + rcu_read_lock(); + caches =3D vring_get_region_caches(vq); + vring_packed_event_read(vq->vdev, &caches->used, &e); + + if (!enable) { + if (virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX)) { + /* no need to write device area since this is outdated. */ + goto out; + } + + e.flags =3D VRING_PACKED_EVENT_FLAG_DISABLE; + goto update; + } + + e.flags =3D VRING_PACKED_EVENT_FLAG_ENABLE; + if (virtio_vdev_has_feature(vq->vdev, VIRTIO_RING_F_EVENT_IDX)) { + uint16_t off_wrap =3D vq->shadow_avail_idx | vq->avail_wrap_counte= r << 15; + + vring_packed_off_wrap_write(vq->vdev, &caches->used, off_wrap); + /* Make sure off_wrap is wrote before flags */ + smp_wmb(); + + e.flags =3D VRING_PACKED_EVENT_FLAG_DESC; + } + +update: + vring_packed_flags_write(vq->vdev, &caches->used, e.flags); +out: + rcu_read_unlock(); +} + +void virtio_queue_set_notification(VirtQueue *vq, int enable) +{ + vq->notification =3D enable; + + if (!vq->vring.desc) { + return; + } + + if (virtio_vdev_has_feature(vq->vdev, VIRTIO_F_RING_PACKED)) { + virtio_queue_set_notification_packed(vq, enable); + } else { + virtio_queue_set_notification_split(vq, enable); + } +} + int virtio_queue_ready(VirtQueue *vq) { return vq->vring.avail !=3D 0; @@ -2117,8 +2190,7 @@ static void virtio_set_isr(VirtIODevice *vdev, int va= lue) } } =20 -/* Called within rcu_read_lock(). */ -static bool virtio_should_notify(VirtIODevice *vdev, VirtQueue *vq) +static bool virtio_split_should_notify(VirtIODevice *vdev, VirtQueue *vq) { uint16_t old, new; bool v; @@ -2141,6 +2213,53 @@ static bool virtio_should_notify(VirtIODevice *vdev,= VirtQueue *vq) return !v || vring_need_event(vring_get_used_event(vq), new, old); } =20 +static bool vring_packed_need_event(VirtQueue *vq, bool wrap, + uint16_t off_wrap, uint16_t new, uint16_t old) +{ + int off =3D off_wrap & ~(1 << 15); + + if (wrap !=3D off_wrap >> 15) { + off -=3D vq->vring.num; + } + + return vring_need_event(off, new, old); +} + +static bool virtio_packed_should_notify(VirtIODevice *vdev, VirtQueue *vq) +{ + VRingPackedDescEvent e; + uint16_t old, new; + bool v; + VRingMemoryRegionCaches *caches; + + caches =3D vring_get_region_caches(vq); + vring_packed_event_read(vdev, &caches->avail, &e); + + old =3D vq->signalled_used; + new =3D vq->signalled_used =3D vq->used_idx; + v =3D vq->signalled_used_valid; + vq->signalled_used_valid =3D true; + + if (e.flags =3D=3D VRING_PACKED_EVENT_FLAG_DISABLE) { + return false; + } else if (e.flags =3D=3D VRING_PACKED_EVENT_FLAG_ENABLE) { + return true; + } + + return !v || vring_packed_need_event(vq, + vq->used_wrap_counter, e.off_wrap, new, old); +} + +/* Called within rcu_read_lock(). */ +static bool virtio_should_notify(VirtIODevice *vdev, VirtQueue *vq) +{ + if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { + return virtio_packed_should_notify(vdev, vq); + } else { + return virtio_split_should_notify(vdev, vq); + } +} + void virtio_notify_irqfd(VirtIODevice *vdev, VirtQueue *vq) { bool should_notify; --=20 1.8.3.1 From nobody Sat Nov 8 02:50:22 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1550064978789945.0129133298237; Wed, 13 Feb 2019 05:36:18 -0800 (PST) Received: from localhost ([127.0.0.1]:56946 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtuhw-0002MX-LH for importer@patchew.org; Wed, 13 Feb 2019 08:36:12 -0500 Received: from eggs.gnu.org ([209.51.188.92]:51742) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtufg-000136-PH for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:33:56 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gtufc-0001sT-Vv for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:33:51 -0500 Received: from mx1.redhat.com ([209.132.183.28]:56652) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gtufX-0001hO-SC for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:33:47 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1760E58E5B; Wed, 13 Feb 2019 13:28:11 +0000 (UTC) Received: from dell-per430-12.lab.eng.pek2.redhat.com (dell-per430-12.lab.eng.pek2.redhat.com [10.73.196.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9E48860863; Wed, 13 Feb 2019 13:28:08 +0000 (UTC) From: wexu@redhat.com To: qemu-devel@nongnu.org Date: Wed, 13 Feb 2019 08:25:44 -0500 Message-Id: <1550064346-17012-10-git-send-email-wexu@redhat.com> In-Reply-To: <1550064346-17012-1-git-send-email-wexu@redhat.com> References: <1550064346-17012-1-git-send-email-wexu@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Wed, 13 Feb 2019 13:28:11 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 09/11] virtio-net: update the head descriptor in a chain lastly X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: tiwei.bie@intel.com, mst@redhat.com, jasowang@redhat.com, jfreiman@redhat.com, maxime.coquelin@redhat.com, wexu@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Wei Xu This is a helper for packed ring. To support packed ring, the head descriptor in a chain should be updated lastly since no 'avail_idx' like in packed ring to explicitly tell the driver side that all payload is ready after having done the chain, so the head is always visible immediately. This patch fills the header after done all the other ones. Signed-off-by: Wei Xu --- hw/net/virtio-net.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c index 3f319ef..330abea 100644 --- a/hw/net/virtio-net.c +++ b/hw/net/virtio-net.c @@ -1251,6 +1251,8 @@ static ssize_t virtio_net_receive_rcu(NetClientState = *nc, const uint8_t *buf, struct virtio_net_hdr_mrg_rxbuf mhdr; unsigned mhdr_cnt =3D 0; size_t offset, i, guest_offset; + VirtQueueElement head; + int head_len =3D 0; =20 if (!virtio_net_can_receive(nc)) { return -1; @@ -1328,7 +1330,13 @@ static ssize_t virtio_net_receive_rcu(NetClientState= *nc, const uint8_t *buf, } =20 /* signal other side */ - virtqueue_fill(q->rx_vq, elem, total, i++); + if (i =3D=3D 0) { + head_len =3D total; + head =3D *elem; + } else { + virtqueue_fill(q->rx_vq, elem, len, i); + } + i++; g_free(elem); } =20 @@ -1339,6 +1347,7 @@ static ssize_t virtio_net_receive_rcu(NetClientState = *nc, const uint8_t *buf, &mhdr.num_buffers, sizeof mhdr.num_buffers); } =20 + virtqueue_fill(q->rx_vq, &head, head_len, 0); virtqueue_flush(q->rx_vq, i); virtio_notify(vdev, q->rx_vq); =20 --=20 1.8.3.1 From nobody Sat Nov 8 02:50:22 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 155006555442821.692885696223357; Wed, 13 Feb 2019 05:45:54 -0800 (PST) Received: from localhost ([127.0.0.1]:57102 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gturH-0001Uh-C4 for importer@patchew.org; Wed, 13 Feb 2019 08:45:51 -0500 Received: from eggs.gnu.org ([209.51.188.92]:52846) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtujg-0004Ae-1W for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:38:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gtujd-0006Yo-W1 for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:37:59 -0500 Received: from mx1.redhat.com ([209.132.183.28]:52620) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gtuja-0006Gg-6Y for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:37:56 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AF398C0753B2; Wed, 13 Feb 2019 13:28:13 +0000 (UTC) Received: from dell-per430-12.lab.eng.pek2.redhat.com (dell-per430-12.lab.eng.pek2.redhat.com [10.73.196.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8676F60856; Wed, 13 Feb 2019 13:28:11 +0000 (UTC) From: wexu@redhat.com To: qemu-devel@nongnu.org Date: Wed, 13 Feb 2019 08:25:45 -0500 Message-Id: <1550064346-17012-11-git-send-email-wexu@redhat.com> In-Reply-To: <1550064346-17012-1-git-send-email-wexu@redhat.com> References: <1550064346-17012-1-git-send-email-wexu@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Wed, 13 Feb 2019 13:28:13 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 10/11] virtio: migration support for packed ring X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: tiwei.bie@intel.com, mst@redhat.com, jasowang@redhat.com, jfreiman@redhat.com, maxime.coquelin@redhat.com, wexu@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Wei Xu Both userspace and vhost-net/user are supported with this patch. A new subsection is introduced for packed ring, only 'last_avail_idx' and 'last_avail_wrap_counter' are saved/loaded presumably based on all the others relevant data(inuse, used/avail index and wrap count should be the same. Signed-off-by: Wei Xu --- hw/virtio/virtio.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++= +--- 1 file changed, 66 insertions(+), 3 deletions(-) diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 8cfc7b6..7c5de07 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -2349,6 +2349,13 @@ static bool virtio_virtqueue_needed(void *opaque) return virtio_host_has_feature(vdev, VIRTIO_F_VERSION_1); } =20 +static bool virtio_packed_virtqueue_needed(void *opaque) +{ + VirtIODevice *vdev =3D opaque; + + return virtio_host_has_feature(vdev, VIRTIO_F_RING_PACKED); +} + static bool virtio_ringsize_needed(void *opaque) { VirtIODevice *vdev =3D opaque; @@ -2390,6 +2397,17 @@ static const VMStateDescription vmstate_virtqueue = =3D { } }; =20 +static const VMStateDescription vmstate_packed_virtqueue =3D { + .name =3D "packed_virtqueue_state", + .version_id =3D 1, + .minimum_version_id =3D 1, + .fields =3D (VMStateField[]) { + VMSTATE_UINT16(last_avail_idx, struct VirtQueue), + VMSTATE_BOOL(last_avail_wrap_counter, struct VirtQueue), + VMSTATE_END_OF_LIST() + } +}; + static const VMStateDescription vmstate_virtio_virtqueues =3D { .name =3D "virtio/virtqueues", .version_id =3D 1, @@ -2402,6 +2420,18 @@ static const VMStateDescription vmstate_virtio_virtq= ueues =3D { } }; =20 +static const VMStateDescription vmstate_virtio_packed_virtqueues =3D { + .name =3D "virtio/packed_virtqueues", + .version_id =3D 1, + .minimum_version_id =3D 1, + .needed =3D &virtio_packed_virtqueue_needed, + .fields =3D (VMStateField[]) { + VMSTATE_STRUCT_VARRAY_POINTER_KNOWN(vq, struct VirtIODevice, + VIRTIO_QUEUE_MAX, 0, vmstate_packed_virtqueue, VirtQ= ueue), + VMSTATE_END_OF_LIST() + } +}; + static const VMStateDescription vmstate_ringsize =3D { .name =3D "ringsize_state", .version_id =3D 1, @@ -2522,6 +2552,7 @@ static const VMStateDescription vmstate_virtio =3D { &vmstate_virtio_ringsize, &vmstate_virtio_broken, &vmstate_virtio_extra_state, + &vmstate_virtio_packed_virtqueues, NULL } }; @@ -2794,6 +2825,17 @@ int virtio_load(VirtIODevice *vdev, QEMUFile *f, int= version_id) virtio_queue_update_rings(vdev, i); } =20 + if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { + vdev->vq[i].shadow_avail_idx =3D vdev->vq[i].last_avail_id= x; + vdev->vq[i].avail_wrap_counter =3D + vdev->vq[i].last_avail_wrap_counte= r; + + vdev->vq[i].used_idx =3D vdev->vq[i].last_avail_idx; + vdev->vq[i].used_wrap_counter =3D + vdev->vq[i].last_avail_wrap_counte= r; + continue; + } + nheads =3D vring_avail_idx(&vdev->vq[i]) - vdev->vq[i].last_av= ail_idx; /* Check it isn't doing strange things with descriptor numbers= . */ if (nheads > vdev->vq[i].vring.num) { @@ -2955,17 +2997,34 @@ hwaddr virtio_queue_get_used_size(VirtIODevice *vde= v, int n) =20 uint16_t virtio_queue_get_last_avail_idx(VirtIODevice *vdev, int n) { - return vdev->vq[n].last_avail_idx; + uint16_t idx; + + if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { + idx =3D vdev->vq[n].last_avail_idx; + idx |=3D ((int)vdev->vq[n].avail_wrap_counter) << 15; + } else { + idx =3D (int)vdev->vq[n].last_avail_idx; + } + return idx; } =20 void virtio_queue_set_last_avail_idx(VirtIODevice *vdev, int n, uint16_t i= dx) { - vdev->vq[n].last_avail_idx =3D idx; - vdev->vq[n].shadow_avail_idx =3D idx; + if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { + vdev->vq[n].last_avail_idx =3D idx & 0x7fff; + vdev->vq[n].avail_wrap_counter =3D !!(idx & 0x8000); + } else { + vdev->vq[n].last_avail_idx =3D idx; + vdev->vq[n].shadow_avail_idx =3D idx; + } } =20 void virtio_queue_restore_last_avail_idx(VirtIODevice *vdev, int n) { + if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { + return; + } + rcu_read_lock(); if (vdev->vq[n].vring.desc) { vdev->vq[n].last_avail_idx =3D vring_used_idx(&vdev->vq[n]); @@ -2976,6 +3035,10 @@ void virtio_queue_restore_last_avail_idx(VirtIODevic= e *vdev, int n) =20 void virtio_queue_update_used_idx(VirtIODevice *vdev, int n) { + if (virtio_vdev_has_feature(vdev, VIRTIO_F_RING_PACKED)) { + return; + } + rcu_read_lock(); if (vdev->vq[n].vring.desc) { vdev->vq[n].used_idx =3D vring_used_idx(&vdev->vq[n]); --=20 1.8.3.1 From nobody Sat Nov 8 02:50:22 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1550065436123822.0139823432185; Wed, 13 Feb 2019 05:43:56 -0800 (PST) Received: from localhost ([127.0.0.1]:57051 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtupK-0008Oc-4j for importer@patchew.org; Wed, 13 Feb 2019 08:43:50 -0500 Received: from eggs.gnu.org ([209.51.188.92]:52845) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gtujg-0004Ad-1N for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:38:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gtuje-0006Z5-2k for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:37:59 -0500 Received: from mx1.redhat.com ([209.132.183.28]:52618) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gtujc-0006Gl-1O for qemu-devel@nongnu.org; Wed, 13 Feb 2019 08:37:57 -0500 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 94B70C0669B6; Wed, 13 Feb 2019 13:28:16 +0000 (UTC) Received: from dell-per430-12.lab.eng.pek2.redhat.com (dell-per430-12.lab.eng.pek2.redhat.com [10.73.196.55]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2F22360856; Wed, 13 Feb 2019 13:28:13 +0000 (UTC) From: wexu@redhat.com To: qemu-devel@nongnu.org Date: Wed, 13 Feb 2019 08:25:46 -0500 Message-Id: <1550064346-17012-12-git-send-email-wexu@redhat.com> In-Reply-To: <1550064346-17012-1-git-send-email-wexu@redhat.com> References: <1550064346-17012-1-git-send-email-wexu@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Wed, 13 Feb 2019 13:28:16 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 11/11] virtio: CLI and provide packed ring feature bit by default X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: tiwei.bie@intel.com, mst@redhat.com, jasowang@redhat.com, jfreiman@redhat.com, maxime.coquelin@redhat.com, wexu@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Wei Xu Add userspace and vhost kernel/user support. Add CLI "ring_packed=3Dtrue/false" to enable/disable packed ring provision. Usage: -device virtio-net-pci,netdev=3Dxx,mac=3Dxx:xx:xx:xx:xx:xx,ring_packed= =3Dfalse By default it is provided. Signed-off-by: Wei Xu --- hw/net/vhost_net.c | 2 ++ include/hw/virtio/virtio.h | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/hw/net/vhost_net.c b/hw/net/vhost_net.c index e037db6..f593086 100644 --- a/hw/net/vhost_net.c +++ b/hw/net/vhost_net.c @@ -53,6 +53,7 @@ static const int kernel_feature_bits[] =3D { VIRTIO_F_VERSION_1, VIRTIO_NET_F_MTU, VIRTIO_F_IOMMU_PLATFORM, + VIRTIO_F_RING_PACKED, VHOST_INVALID_FEATURE_BIT }; =20 @@ -78,6 +79,7 @@ static const int user_feature_bits[] =3D { VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_MTU, VIRTIO_F_IOMMU_PLATFORM, + VIRTIO_F_RING_PACKED, =20 /* This bit implies RARP isn't sent by QEMU out of band */ VIRTIO_NET_F_GUEST_ANNOUNCE, diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index 9c1fa07..2eb27d2 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -264,7 +264,9 @@ typedef struct VirtIORNGConf VirtIORNGConf; DEFINE_PROP_BIT64("any_layout", _state, _field, \ VIRTIO_F_ANY_LAYOUT, true), \ DEFINE_PROP_BIT64("iommu_platform", _state, _field, \ - VIRTIO_F_IOMMU_PLATFORM, false) + VIRTIO_F_IOMMU_PLATFORM, false), \ + DEFINE_PROP_BIT64("ring_packed", _state, _field, \ + VIRTIO_F_RING_PACKED, true) =20 hwaddr virtio_queue_get_desc_addr(VirtIODevice *vdev, int n); hwaddr virtio_queue_get_avail_addr(VirtIODevice *vdev, int n); --=20 1.8.3.1