From patchwork Tue Oct 6 19:19:01 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= X-Patchwork-Id: 527021 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from eggs.gnu.org (eggs.gnu.org [IPv6:2001:4830:134:3::10]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 2A1F81402A2 for ; Wed, 7 Oct 2015 09:00:29 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=y7SA7FT7; dkim-atps=neutral Received: from lists.gnu.org ([208.118.235.17]:55633) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZjXrP-0000EH-16 for incoming@patchwork.ozlabs.org; Tue, 06 Oct 2015 15:25:15 -0400 Received: from localhost ([::1]:53333 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZjXrO-0006If-RF for incoming@patchwork.ozlabs.org; Tue, 06 Oct 2015 15:25:14 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57003) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZjXnN-0000X5-Ct for qemu-devel@nongnu.org; Tue, 06 Oct 2015 15:21:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZjXme-0002LO-Lz for qemu-devel@nongnu.org; Tue, 06 Oct 2015 15:21:05 -0400 Received: from mail-qg0-x22f.google.com ([2607:f8b0:400d:c04::22f]:34507) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZjXmd-0002Fi-SA for qemu-devel@nongnu.org; Tue, 06 Oct 2015 15:20:19 -0400 Received: by qgez77 with SMTP id z77so184216190qge.1 for ; Tue, 06 Oct 2015 12:20:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=GIbv3QHJq1hrgQn8UODyJ1pH65BMsfd7FHTldXZABvQ=; b=y7SA7FT7mxyc6sGIB309SxFE4KTHBFgl7QMgS5NnRtb4nOL1VNQ+siESr3IY8cZNA9 o1uiq+9kbFz/EkQloseVJF6KJ7fTTfS6oXVqoOL1z3wVmT+Df1K5toHKjQyRgXnLbAOi Psi+8uqbzhhXbgKu28Mr/rEZp4FdWDc/j4a/C/XiMYmHs/G20kDQ5YT38RhMJKkrIhDg MzLJnFMmO+uMGtUPN82VTQhGHapZ1UChaZloAlLJ1ivgPjkyI8eEuZJcdSN8Ao7Sp0Vv 1fZoS+jlBldKKofasexpKYJuPeBLrx6I260SLJv8d6SxF3Y8/XESnhxjoRErDc7x9mnP DU5w== X-Received: by 10.140.134.211 with SMTP id 202mr53019019qhg.51.1444159217951; Tue, 06 Oct 2015 12:20:17 -0700 (PDT) Received: from localhost (bne75-h02-31-39-163-232.dsl.sta.abo.bbox.fr. [31.39.163.232]) by smtp.gmail.com with ESMTPSA id c36sm14513803qge.49.2015.10.06.12.20.16 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Oct 2015 12:20:17 -0700 (PDT) From: marcandre.lureau@redhat.com To: peter.maydell@linaro.org Date: Tue, 6 Oct 2015 21:19:01 +0200 Message-Id: <1444159184-18153-6-git-send-email-marcandre.lureau@redhat.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1444159184-18153-1-git-send-email-marcandre.lureau@redhat.com> References: <1444159184-18153-1-git-send-email-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:400d:c04::22f Cc: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , qemu-devel@nongnu.org Subject: [Qemu-devel] [PULL 05/48] ivshmem: factor out the incoming fifo handling 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 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x From: Marc-André Lureau Make a new function fifo_update_and_get() that can be reused by other functions (in next commits). Signed-off-by: Marc-André Lureau Reviewed-by: Claudio Fontana --- hw/misc/ivshmem.c | 59 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/hw/misc/ivshmem.c b/hw/misc/ivshmem.c index 2162d02..dd15f0e 100644 --- a/hw/misc/ivshmem.c +++ b/hw/misc/ivshmem.c @@ -441,6 +441,42 @@ static int increase_dynamic_storage(IVShmemState *s, int new_min_size) return 0; } +static bool fifo_update_and_get(IVShmemState *s, const uint8_t *buf, int size, + void *data, size_t len) +{ + const uint8_t *p; + uint32_t num; + + assert(len <= sizeof(long)); /* limitation of the fifo */ + if (fifo8_is_empty(&s->incoming_fifo) && size == len) { + memcpy(data, buf, size); + return true; + } + + IVSHMEM_DPRINTF("short read of %d bytes\n", size); + + num = MIN(size, sizeof(long) - fifo8_num_used(&s->incoming_fifo)); + fifo8_push_all(&s->incoming_fifo, buf, num); + + if (fifo8_num_used(&s->incoming_fifo) < len) { + assert(num == 0); + return false; + } + + size -= num; + buf += num; + p = fifo8_pop_buf(&s->incoming_fifo, len, &num); + assert(num == len); + + memcpy(data, p, len); + + if (size > 0) { + fifo8_push_all(&s->incoming_fifo, buf, size); + } + + return true; +} + static void ivshmem_read(void *opaque, const uint8_t *buf, int size) { IVShmemState *s = opaque; @@ -448,26 +484,9 @@ static void ivshmem_read(void *opaque, const uint8_t *buf, int size) int guest_max_eventfd; long incoming_posn; - if (fifo8_is_empty(&s->incoming_fifo) && size == sizeof(incoming_posn)) { - memcpy(&incoming_posn, buf, size); - } else { - const uint8_t *p; - uint32_t num; - - IVSHMEM_DPRINTF("short read of %d bytes\n", size); - num = MIN(size, sizeof(long) - fifo8_num_used(&s->incoming_fifo)); - fifo8_push_all(&s->incoming_fifo, buf, num); - if (fifo8_num_used(&s->incoming_fifo) < sizeof(incoming_posn)) { - return; - } - size -= num; - buf += num; - p = fifo8_pop_buf(&s->incoming_fifo, sizeof(incoming_posn), &num); - g_assert(num == sizeof(incoming_posn)); - memcpy(&incoming_posn, p, sizeof(incoming_posn)); - if (size > 0) { - fifo8_push_all(&s->incoming_fifo, buf, size); - } + if (!fifo_update_and_get(s, buf, size, + &incoming_posn, sizeof(incoming_posn))) { + return; } if (incoming_posn < -1) {