From patchwork Thu Dec 6 16:59:07 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincenzo Maffione X-Patchwork-Id: 10716443 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 9EA921923 for ; Thu, 6 Dec 2018 17:10:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 817DC2F17A for ; Thu, 6 Dec 2018 17:10:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7096B2F182; Thu, 6 Dec 2018 17:10:05 +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 D0D622F17A for ; Thu, 6 Dec 2018 17:10:04 +0000 (UTC) Received: from localhost ([::1]:42158 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gUxA3-0001qu-1E for patchwork-qemu-devel@patchwork.kernel.org; Thu, 06 Dec 2018 12:10:03 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52329) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gUx1v-0003mc-C9 for qemu-devel@nongnu.org; Thu, 06 Dec 2018 12:01:43 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gUx1e-0006t6-ST for qemu-devel@nongnu.org; Thu, 06 Dec 2018 12:01:35 -0500 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]:34208) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gUx1e-0006kp-99 for qemu-devel@nongnu.org; Thu, 06 Dec 2018 12:01:22 -0500 Received: by mail-wr1-x443.google.com with SMTP id j2so1243119wrw.1 for ; Thu, 06 Dec 2018 09:01:12 -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=iUAg6dfrLALQcG1YgGKEexk7hL/v84hmTmLvr6abT14=; b=pZVPGIvsqGb+m6U/u0ubq0peSKp4DW0Hgg8vUt5p+mkkfvctRamudbUayPSsSeK0S0 Gi0mfEWrAxtYKyl/VrPk5U7Deycy0NuFpwwatlc4nqYErcCt0bWwx5N9SxeUrJh5oLpa SO3/CrdECyeEdTK2Rk79wzcHERS4ETrWTtCFaZp9cpL/arT4SApG3Lu+/8BEfgytUtZS 6UPp3UiynmuipOfFe7d9O7Gmf/KV+yZynnptO7w1OwwIXle/QGlktZE4fSlfldjggvml iHHe6muUp2og+28Oia0HRxGuYhG4xqiOTtadNsuy+KEU6Z+JlbstI+8S0cT5RUZGEXKo zgTA== 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=iUAg6dfrLALQcG1YgGKEexk7hL/v84hmTmLvr6abT14=; b=jRUkiLvagqMagcy5p7zAxih15eFowi8lWo5vvz54kwUQMMp5optxRQlJ/X6Z196WN7 /jg/0StwJ2/4sc8xmo518EF6B9l/g2VKUDLUV3fIkuL/3VXBnLEooTjoy4ZdJXeJDKw0 n1F8kUIG4S/quK5I31j1ej+4v2N3ydDQPKrI5vkqwzPpaV8QtZJDKWV1JsSNFExQeVAM 67qfHu09+3NfKP+Zt/paNhcV3wzp7KdmNXiF2mVVJ+QPB4KJYgaM4Ttj+wT2Lyd5XWRi +JENsL/V6TI+5kHQYrhDyGjE2+HHle82eR21UpE0HzygQUF4FH7GemvYmXAp1Q1oK8+H lBaw== X-Gm-Message-State: AA+aEWaCo8wwlosDhUGip9adsT7kmZcHh3/utYMVZQzbJj77UVjzNSZ/ NcqONPvhSwCSsOae9T8XWZjxoYWSiVE= X-Google-Smtp-Source: AFSGD/XYSgEGimH4SjDLyuswwpiYDeR0VqzCaTnaUcm9O4bpIYqqyiJDkahdq1b3pGpD13uA/2d1vQ== X-Received: by 2002:a5d:454f:: with SMTP id p15mr27719537wrr.39.1544115670435; Thu, 06 Dec 2018 09:01:10 -0800 (PST) Received: from localhost.localdomain (prova.iet.unipi.it. [131.114.58.86]) by smtp.gmail.com with ESMTPSA id d4sm467556wrp.89.2018.12.06.09.01.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 06 Dec 2018 09:01:09 -0800 (PST) From: Vincenzo Maffione To: qemu-devel@nongnu.org Date: Thu, 6 Dec 2018 17:59:07 +0100 Message-Id: <20181206165907.23465-4-v.maffione@gmail.com> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181206165907.23465-1-v.maffione@gmail.com> References: <20181206165907.23465-1-v.maffione@gmail.com> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::443 Subject: [Qemu-devel] [PATCH v1 3/3] net: netmap: improve netmap_receive_iov() 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: g.lettieri@iet.unipi.it, jasowang@redhat.com, Vincenzo Maffione Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Changes: - Save CPU cycles by computing the return value while scanning the input iovec, rather than calling iov_size() at the end. - Remove check for s->tx != NULL, because it cannot happen. - Cache ring->tail in a local variable and use it to check for space in the TX ring. The use of nm_ring_empty() was invalid, because nobody is updating ring->cur and ring->head at that point. - In case we run out of netmap slots in the middle of a packet, move the wake-up point by advancing ring->cur, but do not expose the incomplete packet (i.e., by updating also ring->head). Signed-off-by: Vincenzo Maffione --- net/netmap.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/net/netmap.c b/net/netmap.c index 852106af29..0cc8f545c5 100644 --- a/net/netmap.c +++ b/net/netmap.c @@ -159,21 +159,22 @@ static ssize_t netmap_receive_iov(NetClientState *nc, { NetmapState *s = DO_UPCAST(NetmapState, nc, nc); struct netmap_ring *ring = s->tx; + unsigned int tail = ring->tail; + ssize_t totlen = 0; uint32_t last; uint32_t idx; uint8_t *dst; int j; uint32_t i; - if (unlikely(!ring)) { - /* Drop the packet. */ - return iov_size(iov, iovcnt); - } - - last = i = ring->cur; + last = i = ring->head; if (nm_ring_space(ring) < iovcnt) { - /* Not enough netmap slots. */ + /* Not enough netmap slots. Tell the kernel that we have seen the new + * available slots (so that it notifies us again when it has more + * ones), but without publishing any new slots to be processed + * (e.g., we don't advance ring->head). */ + ring->cur = tail; netmap_write_poll(s, true); return 0; } @@ -183,14 +184,17 @@ static ssize_t netmap_receive_iov(NetClientState *nc, int offset = 0; int nm_frag_size; + totlen += iov_frag_size; + /* Split each iovec fragment over more netmap slots, if necessary. */ while (iov_frag_size) { nm_frag_size = MIN(iov_frag_size, ring->nr_buf_size); - if (unlikely(nm_ring_empty(ring))) { - /* We run out of netmap slots while splitting the + if (unlikely(i == tail)) { + /* We ran out of netmap slots while splitting the iovec fragments. */ + ring->cur = tail; netmap_write_poll(s, true); return 0; } @@ -212,12 +216,13 @@ static ssize_t netmap_receive_iov(NetClientState *nc, /* The last slot must not have NS_MOREFRAG set. */ ring->slot[last].flags &= ~NS_MOREFRAG; - /* Now update ring->cur and ring->head. */ - ring->cur = ring->head = i; + /* Now update ring->head and ring->cur to publish the new slots and + * the new wakeup point. */ + ring->head = ring->cur = i; ioctl(s->nmd->fd, NIOCTXSYNC, NULL); - return iov_size(iov, iovcnt); + return totlen; } static ssize_t netmap_receive(NetClientState *nc,