From patchwork Fri Jun 30 11:46:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 782821 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 3wzZXG37frz9sNS for ; Fri, 30 Jun 2017 21:47:42 +1000 (AEST) Received: from localhost ([::1]:43879 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dQuOi-0008HN-3i for incoming@patchwork.ozlabs.org; Fri, 30 Jun 2017 07:47:40 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48152) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dQuNu-0008DR-DN for qemu-devel@nongnu.org; Fri, 30 Jun 2017 07:46:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dQuNt-0004so-9D for qemu-devel@nongnu.org; Fri, 30 Jun 2017 07:46:50 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52892) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dQuNt-0004sZ-1d for qemu-devel@nongnu.org; Fri, 30 Jun 2017 07:46:49 -0400 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 1C32A80463; Fri, 30 Jun 2017 11:46:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 1C32A80463 Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=stefanha@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 1C32A80463 Received: from localhost (ovpn-116-222.ams2.redhat.com [10.36.116.222]) by smtp.corp.redhat.com (Postfix) with ESMTP id A197960A98; Fri, 30 Jun 2017 11:46:47 +0000 (UTC) From: Stefan Hajnoczi To: Date: Fri, 30 Jun 2017 12:46:31 +0100 Message-Id: <20170630114635.9286-4-stefanha@redhat.com> In-Reply-To: <20170630114635.9286-1-stefanha@redhat.com> References: <20170630114635.9286-1-stefanha@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]); Fri, 30 Jun 2017 11:46:48 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 3/7] libqos: add virtio used ring support 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: Peter Maydell , Stefan Hajnoczi Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Existing tests do not touch the virtqueue used ring. Instead they poll the virtqueue ISR register and peek into their request's device-specific status field. It turns out that the virtqueue ISR register can be set to 1 more than once for a single notification (see commit 83d768b5640946b7da55ce8335509df297e2c7cd "virtio: set ISR on dataplane notifications"). This causes problems for tests that assume a 1:1 correspondence between the ISR being 1 and request completion. Peeking at device-specific status fields is also problematic if the device has no field that can be abused for EINPROGRESS polling semantics. This is the case if all the field's values may be set by the device; there's no magic constant left for polling. It's time to process the used ring for completed requests, just like a real virtio guest driver. This patch adds the necessary APIs. Signed-off-by: Stefan Hajnoczi Reviewed-by: Michael S. Tsirkin Reviewed-by: Fam Zheng Tested-by: Eric Blake Tested-by: Kevin Wolf Message-id: 20170628184724.21378-3-stefanha@redhat.com Signed-off-by: Stefan Hajnoczi --- tests/libqos/virtio.h | 6 ++++++ tests/libqos/virtio.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/tests/libqos/virtio.h b/tests/libqos/virtio.h index 829de5e..8fbcd18 100644 --- a/tests/libqos/virtio.h +++ b/tests/libqos/virtio.h @@ -32,6 +32,7 @@ typedef struct QVirtQueue { uint32_t free_head; uint32_t num_free; uint32_t align; + uint16_t last_used_idx; bool indirect; bool event; } QVirtQueue; @@ -120,6 +121,10 @@ uint8_t qvirtio_wait_status_byte_no_isr(QVirtioDevice *d, QVirtQueue *vq, uint64_t addr, gint64 timeout_us); +void qvirtio_wait_used_elem(QVirtioDevice *d, + QVirtQueue *vq, + uint32_t desc_idx, + gint64 timeout_us); void qvirtio_wait_config_isr(QVirtioDevice *d, gint64 timeout_us); QVirtQueue *qvirtqueue_setup(QVirtioDevice *d, QGuestAllocator *alloc, uint16_t index); @@ -135,6 +140,7 @@ uint32_t qvirtqueue_add(QVirtQueue *vq, uint64_t data, uint32_t len, bool write, bool next); uint32_t qvirtqueue_add_indirect(QVirtQueue *vq, QVRingIndirectDesc *indirect); void qvirtqueue_kick(QVirtioDevice *d, QVirtQueue *vq, uint32_t free_head); +bool qvirtqueue_get_buf(QVirtQueue *vq, uint32_t *desc_idx); void qvirtqueue_set_used_event(QVirtQueue *vq, uint16_t idx); #endif diff --git a/tests/libqos/virtio.c b/tests/libqos/virtio.c index ec30cb9..9880a69 100644 --- a/tests/libqos/virtio.c +++ b/tests/libqos/virtio.c @@ -116,6 +116,35 @@ uint8_t qvirtio_wait_status_byte_no_isr(QVirtioDevice *d, return val; } +/* + * qvirtio_wait_used_elem: + * @desc_idx: The next expected vq->desc[] index in the used ring + * @timeout_us: How many microseconds to wait before failing + * + * This function waits for the next completed request on the used ring. + */ +void qvirtio_wait_used_elem(QVirtioDevice *d, + QVirtQueue *vq, + uint32_t desc_idx, + gint64 timeout_us) +{ + gint64 start_time = g_get_monotonic_time(); + + for (;;) { + uint32_t got_desc_idx; + + clock_step(100); + + if (d->bus->get_queue_isr_status(d, vq) && + qvirtqueue_get_buf(vq, &got_desc_idx)) { + g_assert_cmpint(got_desc_idx, ==, desc_idx); + return; + } + + g_assert(g_get_monotonic_time() - start_time <= timeout_us); + } +} + void qvirtio_wait_config_isr(QVirtioDevice *d, gint64 timeout_us) { gint64 start_time = g_get_monotonic_time(); @@ -272,6 +301,37 @@ void qvirtqueue_kick(QVirtioDevice *d, QVirtQueue *vq, uint32_t free_head) } } +/* + * qvirtqueue_get_buf: + * @desc_idx: A pointer that is filled with the vq->desc[] index, may be NULL + * + * This function gets the next used element if there is one ready. + * + * Returns: true if an element was ready, false otherwise + */ +bool qvirtqueue_get_buf(QVirtQueue *vq, uint32_t *desc_idx) +{ + uint16_t idx; + + idx = readw(vq->used + offsetof(struct vring_used, idx)); + if (idx == vq->last_used_idx) { + return false; + } + + if (desc_idx) { + uint64_t elem_addr; + + elem_addr = vq->used + + offsetof(struct vring_used, ring) + + (vq->last_used_idx % vq->size) * + sizeof(struct vring_used_elem); + *desc_idx = readl(elem_addr + offsetof(struct vring_used_elem, id)); + } + + vq->last_used_idx++; + return true; +} + void qvirtqueue_set_used_event(QVirtQueue *vq, uint16_t idx) { g_assert(vq->event);