From patchwork Thu Feb 28 08:53:51 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yongji Xie X-Patchwork-Id: 10832691 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E9BE31390 for ; Thu, 28 Feb 2019 08:56:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DAE4F2E3CE for ; Thu, 28 Feb 2019 08:56:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D936F2E412; Thu, 28 Feb 2019 08:56:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.7 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 622AE2E436 for ; Thu, 28 Feb 2019 08:56:30 +0000 (UTC) Received: from localhost ([127.0.0.1]:34486 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gzHUT-0000T4-Jf for patchwork-qemu-devel@patchwork.kernel.org; Thu, 28 Feb 2019 03:56:29 -0500 Received: from eggs.gnu.org ([209.51.188.92]:37435) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gzHT3-0000RX-6g for qemu-devel@nongnu.org; Thu, 28 Feb 2019 03:55:02 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gzHT0-0004Kr-RD for qemu-devel@nongnu.org; Thu, 28 Feb 2019 03:55:01 -0500 Received: from mail-pf1-x441.google.com ([2607:f8b0:4864:20::441]:38619) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gzHSx-0004J3-8Q for qemu-devel@nongnu.org; Thu, 28 Feb 2019 03:54:56 -0500 Received: by mail-pf1-x441.google.com with SMTP id n125so9415498pfn.5 for ; Thu, 28 Feb 2019 00:54:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Re0HyIiq6TcSY2HsM8WeDdKG2mWgNhg6qEZuo5AbfkM=; b=Y0Jt9Rmizlq+tpMcyzDLby1gnYetIyz+YdAmVGq+6eeX4gTiNl2/fPsWASeVWBqbDD nTVGAVMd8c2ikhiFFfUZ7rSuBTdqfOSJ15ohanUOxfZTf1Agzq2teQFi7seFgLxDAKxJ 2+BoR0kuT1XcKQvDnO92K60c2mlwi7KNy6GZCElNE5MIJsZ/DWFFiaZi7h7WQkX8X94O fF0PNdIsbHEBiyZagsrEOKmX5xNxyboXLQlx0Ju5Tj30KVViapvzBUj0l4ANkk/Omuwx 5RH6JHTi8xXHmgD7Y90YF9k+Nieuw25Mg8QPp8XZM2tZT7/M5HBwW6GQfo62tD//j0Pv EjIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Re0HyIiq6TcSY2HsM8WeDdKG2mWgNhg6qEZuo5AbfkM=; b=TS/wh+j8D6OiUIs6j9dQ0CECHmpvAzY2k2drKe4K1tjQ2JxGQXmassEri+4M5K6c79 Px4DUgzDrN/0nRXq4y6r0uaNIrxVIXno4NC+V3s6uOdFSaaufcMrJLYY78DXzGFus2Eg j+xItxOLF8oV+TdozgZHbjz9+ioUVtTjm94C7G48cvuZrO82SrKK/oFprSiotas9qt4K 5hFs+syhYCjDwNdH5y3nxwp/SXS6ryLIhQgA0VQ3PICAnmhD06v87d/Bi+d3G/hJMNCV KfY5+QKy9QKCbyNWJSbKfLR7AAYBhMwSldc6GAZh/h/Q53t+9jJG03G+2h9algQrXDZz FYIA== X-Gm-Message-State: AHQUAuYHh+U1aWTUrAXPE9ESWRfJKQKXxHicpy70yLqF6aNvX7bPyhm8 ZxQ7RgSjycjhMqGgkdk+LCY= X-Google-Smtp-Source: AHgI3IYa/Sf3gCPokftc6Yn6zDtLkVVgwxV7iDxyZ2MhgKO4fa283PoYEOWpc4Zy9EuACULxwha4PQ== X-Received: by 2002:a63:591f:: with SMTP id n31mr7266714pgb.304.1551344089817; Thu, 28 Feb 2019 00:54:49 -0800 (PST) Received: from localhost ([116.247.112.152]) by smtp.gmail.com with ESMTPSA id g3sm24021166pgg.41.2019.02.28.00.54.48 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 28 Feb 2019 00:54:49 -0800 (PST) From: elohimes@gmail.com X-Google-Original-From: xieyongji@baidu.com To: mst@redhat.com, stefanha@gmail.com, marcandre.lureau@redhat.com, berrange@redhat.com, jasowang@redhat.com, maxime.coquelin@redhat.com, yury-kotov@yandex-team.ru, wrfsh@yandex-team.ru Date: Thu, 28 Feb 2019 16:53:51 +0800 Message-Id: <20190228085355.9614-4-xieyongji@baidu.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190228085355.9614-1-xieyongji@baidu.com> References: <20190228085355.9614-1-xieyongji@baidu.com> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::441 Subject: [Qemu-devel] [PATCH v7 3/7] libvhost-user: Introduce vu_queue_map_desc() 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: nixun@baidu.com, qemu-devel@nongnu.org, lilin24@baidu.com, zhangyu31@baidu.com, chaiwen@baidu.com, Xie Yongji Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Xie Yongji Introduce vu_queue_map_desc() which should be independent with vu_queue_pop(); Signed-off-by: Xie Yongji Signed-off-by: Zhang Yu Reviewed-by: Marc-André Lureau --- contrib/libvhost-user/libvhost-user.c | 88 ++++++++++++++++----------- 1 file changed, 51 insertions(+), 37 deletions(-) diff --git a/contrib/libvhost-user/libvhost-user.c b/contrib/libvhost-user/libvhost-user.c index 16fec3a3fd..ea0f414b6d 100644 --- a/contrib/libvhost-user/libvhost-user.c +++ b/contrib/libvhost-user/libvhost-user.c @@ -1847,49 +1847,20 @@ virtqueue_alloc_element(size_t sz, return elem; } -void * -vu_queue_pop(VuDev *dev, VuVirtq *vq, size_t sz) +static void * +vu_queue_map_desc(VuDev *dev, VuVirtq *vq, unsigned int idx, size_t sz) { - unsigned int i, head, max, desc_len; + struct vring_desc *desc = vq->vring.desc; uint64_t desc_addr, read_len; + unsigned int desc_len; + unsigned int max = vq->vring.num; + unsigned int i = idx; VuVirtqElement *elem; - unsigned out_num, in_num; + unsigned int out_num = 0, in_num = 0; struct iovec iov[VIRTQUEUE_MAX_SIZE]; struct vring_desc desc_buf[VIRTQUEUE_MAX_SIZE]; - struct vring_desc *desc; int rc; - if (unlikely(dev->broken) || - unlikely(!vq->vring.avail)) { - return NULL; - } - - if (vu_queue_empty(dev, vq)) { - return NULL; - } - /* Needed after virtio_queue_empty(), see comment in - * virtqueue_num_heads(). */ - smp_rmb(); - - /* When we start there are none of either input nor output. */ - out_num = in_num = 0; - - max = vq->vring.num; - if (vq->inuse >= vq->vring.num) { - vu_panic(dev, "Virtqueue size exceeded"); - return NULL; - } - - if (!virtqueue_get_head(dev, vq, vq->last_avail_idx++, &head)) { - return NULL; - } - - if (vu_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) { - vring_set_avail_event(vq, vq->last_avail_idx); - } - - i = head; - desc = vq->vring.desc; if (desc[i].flags & VRING_DESC_F_INDIRECT) { if (desc[i].len % sizeof(struct vring_desc)) { vu_panic(dev, "Invalid size for indirect buffer table"); @@ -1941,12 +1912,13 @@ vu_queue_pop(VuDev *dev, VuVirtq *vq, size_t sz) } while (rc == VIRTQUEUE_READ_DESC_MORE); if (rc == VIRTQUEUE_READ_DESC_ERROR) { + vu_panic(dev, "read descriptor error"); return NULL; } /* Now copy what we have collected and mapped */ elem = virtqueue_alloc_element(sz, out_num, in_num); - elem->index = head; + elem->index = idx; for (i = 0; i < out_num; i++) { elem->out_sg[i] = iov[i]; } @@ -1954,6 +1926,48 @@ vu_queue_pop(VuDev *dev, VuVirtq *vq, size_t sz) elem->in_sg[i] = iov[out_num + i]; } + return elem; +} + +void * +vu_queue_pop(VuDev *dev, VuVirtq *vq, size_t sz) +{ + unsigned int head; + VuVirtqElement *elem; + + if (unlikely(dev->broken) || + unlikely(!vq->vring.avail)) { + return NULL; + } + + if (vu_queue_empty(dev, vq)) { + return NULL; + } + /* + * Needed after virtio_queue_empty(), see comment in + * virtqueue_num_heads(). + */ + smp_rmb(); + + if (vq->inuse >= vq->vring.num) { + vu_panic(dev, "Virtqueue size exceeded"); + return NULL; + } + + if (!virtqueue_get_head(dev, vq, vq->last_avail_idx++, &head)) { + return NULL; + } + + if (vu_has_feature(dev, VIRTIO_RING_F_EVENT_IDX)) { + vring_set_avail_event(vq, vq->last_avail_idx); + } + + elem = vu_queue_map_desc(dev, vq, head, sz); + + if (!elem) { + return NULL; + } + vq->inuse++; return elem;