From patchwork Tue Jul 28 00:32:17 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: 500948 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 7E894140D19 for ; Tue, 28 Jul 2015 10:34:02 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=mETwwIl9; dkim-atps=neutral Received: from localhost ([::1]:56337 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZJsqG-0007mP-4a for incoming@patchwork.ozlabs.org; Mon, 27 Jul 2015 20:34:00 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47242) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZJsph-0006i5-QP for qemu-devel@nongnu.org; Mon, 27 Jul 2015 20:33:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZJspg-0004sR-U3 for qemu-devel@nongnu.org; Mon, 27 Jul 2015 20:33:25 -0400 Received: from mail-qg0-x229.google.com ([2607:f8b0:400d:c04::229]:32893) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZJspg-0004sN-Qw for qemu-devel@nongnu.org; Mon, 27 Jul 2015 20:33:24 -0400 Received: by qged69 with SMTP id d69so64559277qge.0 for ; Mon, 27 Jul 2015 17:33:24 -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=ZvnJDYlvMyH5LXEQlaSIdgwbjcZn6FVOMDN715zK72s=; b=mETwwIl9gXhsfX7T3zyzZtHXKL1nHfrWsjvHgVllMSdZ7MIGRikJbTLdDVm1E+2Jov yHNEwGyiLOf2XhTwewdEtP0aroK9dry4RU6zXq8FeF0P2cZdo6LYO4xTnaa3KJpeOQZ5 nBiU555WLxsD/jVrEeC0aSL6ecp56bvzfv/Y6mvPCyvMgjbdyOZe8SQdjr9apUyEDX5F q22Kzjr8SHI+lyOhV2iG7OKLMF3cUrZNzKS0ge/jsfAWsltyAqHHTL/9zT7Ept0ukAtA awe94sWdQQTCeuCVrFDMzOZFLvq7op2fnmb9eUvHNip6rRPopM7fD7KnaefijaA5vZdr vwsg== X-Received: by 10.140.144.144 with SMTP id 138mr46230363qhq.60.1438043604436; Mon, 27 Jul 2015 17:33:24 -0700 (PDT) Received: from localhost (APoitiers-257-1-37-212.w90-38.abo.wanadoo.fr. [90.38.212.212]) by smtp.gmail.com with ESMTPSA id d10sm10197068qhc.9.2015.07.27.17.33.22 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 27 Jul 2015 17:33:23 -0700 (PDT) From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= To: qemu-devel@nongnu.org Date: Tue, 28 Jul 2015 02:32:17 +0200 Message-Id: <1438043577-28636-6-git-send-email-marcandre.lureau@redhat.com> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1438043577-28636-1-git-send-email-marcandre.lureau@redhat.com> References: <1438043577-28636-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::229 Cc: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , drjones@redhat.com, cam@cs.ualberta.ca, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , stefanha@redhat.com Subject: [Qemu-devel] [PATCH v2 05/45] 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 From: Marc-André Lureau Make a new function fifo_update_and_get() that can be reused by other part of the functions. Signed-off-by: Marc-André Lureau --- 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) {