From patchwork Thu Dec 6 06:35:49 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yongji Xie X-Patchwork-Id: 10715313 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 1DEF413AF for ; Thu, 6 Dec 2018 06:40:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0C77E2E1D4 for ; Thu, 6 Dec 2018 06:40:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 00C912E357; Thu, 6 Dec 2018 06:40:53 +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=-7.7 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 7EDB82E28D for ; Thu, 6 Dec 2018 06:40:53 +0000 (UTC) Received: from localhost ([::1]:39111 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gUnLA-0004yP-NY for patchwork-qemu-devel@patchwork.kernel.org; Thu, 06 Dec 2018 01:40:52 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52810) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gUnHJ-0001Ss-7z for qemu-devel@nongnu.org; Thu, 06 Dec 2018 01:36:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gUnHF-0003TT-A0 for qemu-devel@nongnu.org; Thu, 06 Dec 2018 01:36:51 -0500 Received: from mail-pg1-x542.google.com ([2607:f8b0:4864:20::542]:38351) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gUnHE-0003Pq-Os for qemu-devel@nongnu.org; Thu, 06 Dec 2018 01:36:48 -0500 Received: by mail-pg1-x542.google.com with SMTP id g189so10189250pgc.5 for ; Wed, 05 Dec 2018 22:36:46 -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; bh=XKl4V81WAbYlKQdyvx2f7AmT9JieubeQLT/wGHLl3FU=; b=ZwP6bI6ksSCLQbTJdW+LHrq3Mfuiwqeivi/4R4uPYZdFKpbMD4jquJqa5Q7xkxlwSL OEtEcQWVtpTA80r9Trjplc9JzUf4w0x81D+8l7Rf/1J/7qzEU6ZsWPuf8LHlKhtyNbEG x4W3C+LFg13/c4jihEbvhgCWNUpmMv0FNfsPw6BUr1h3gcbqiITllaa3wBKsUViaumTX reSm0ldnDPXHUTs9CiVpxouCRNpqyb6sbfb9tPQELT5xkIGUjs+rmtFBX4qrq0faf6kq tQh3wDytL7XiDFUB9pfyI6ewvBflVE+Tv2vr+VpQ+H1EspUrxwucJazRNtTF2PWdBD6r YCmg== 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; bh=XKl4V81WAbYlKQdyvx2f7AmT9JieubeQLT/wGHLl3FU=; b=IsoPx2h9BdAg7OEICJw0Heb6Q+NVz3RZkrVnZX5msMqG8KqEgEhyH3sW4aMhKaCGzD z4BSrKLjrROFeQaxdMP3YdoriGW0xl0DdFAuKTOCl55I1GYBZgiaiidRXxKYXWjOwuhu aEOZQZMnYF6KULyLfNo40RRm+EVt4KWte1w74EZNplbXlBR7FO0/DOR52SE0RNj6cgXj 9cGz2AtZUPr8GFLJkvb8f1Ch5OP96dQNgBcE4O0B0cyAp3AFhVxTnrwSd7lMKnNPXrmz pBEJOrLdTXDEmzgXTKwPvZ3visiZkOkJTl3LmLyv6YnM2CYUn9RaG00ddOhvu9PL4vYZ jPvg== X-Gm-Message-State: AA+aEWbsKzGiiLOfcqrBzsH3HxDePfrKyS5vXmxtnCi9dc+skBnSlvod fRwjcltVq3MK8MU5o4Ti5KfFuBal0D0= X-Google-Smtp-Source: AFSGD/UTvSvU+IpQyd29yuwQhtIUJ3F6LV1Ud7f6guM1uBNDa4QY/yb2JMfVWroUwIrCiWAMlVnWEQ== X-Received: by 2002:a63:b17:: with SMTP id 23mr23290622pgl.423.1544078205596; Wed, 05 Dec 2018 22:36:45 -0800 (PST) Received: from yongji-Ubuntu.internal.baidu.com ([116.247.112.152]) by smtp.gmail.com with ESMTPSA id x186sm26175249pfb.59.2018.12.05.22.36.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Dec 2018 22:36:45 -0800 (PST) From: elohimes@gmail.com X-Google-Original-From: xieyongji@baidu.com To: mst@redhat.com, marcandre.lureau@redhat.com Date: Thu, 6 Dec 2018 14:35:49 +0800 Message-Id: <20181206063552.6701-4-xieyongji@baidu.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181206063552.6701-1-xieyongji@baidu.com> References: <20181206063552.6701-1-xieyongji@baidu.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::542 Subject: [Qemu-devel] [PATCH for-4.0 3/6] 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 | 86 +++++++++++++++------------ 1 file changed, 49 insertions(+), 37 deletions(-) diff --git a/contrib/libvhost-user/libvhost-user.c b/contrib/libvhost-user/libvhost-user.c index a6b46cdc03..4432bd8bb4 100644 --- a/contrib/libvhost-user/libvhost-user.c +++ b/contrib/libvhost-user/libvhost-user.c @@ -1853,49 +1853,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"); @@ -1947,12 +1918,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]; } @@ -1960,6 +1932,46 @@ 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;