From nobody Wed Apr 16 11:38:49 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1551770079159527.0522017850311; Mon, 4 Mar 2019 23:14:39 -0800 (PST) Received: from localhost ([127.0.0.1]:38611 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h14Hc-0003q8-1g for importer@patchew.org; Tue, 05 Mar 2019 02:14:36 -0500 Received: from eggs.gnu.org ([209.51.188.92]:43384) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h14Fj-0002gz-Cr for qemu-devel@nongnu.org; Tue, 05 Mar 2019 02:12:40 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h14Fi-0000b2-H2 for qemu-devel@nongnu.org; Tue, 05 Mar 2019 02:12:39 -0500 Received: from mx1.redhat.com ([209.132.183.28]:46422) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1h14Fg-0000ZT-KJ for qemu-devel@nongnu.org; Tue, 05 Mar 2019 02:12:36 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id DC8A530821E0; Tue, 5 Mar 2019 07:12:34 +0000 (UTC) Received: from jason-ThinkPad-T430s.redhat.com (ovpn-12-234.pek2.redhat.com [10.72.12.234]) by smtp.corp.redhat.com (Postfix) with ESMTP id CD4E118009; Tue, 5 Mar 2019 07:12:31 +0000 (UTC) From: Jason Wang To: qemu-devel@nongnu.org, peter.maydell@linaro.org Date: Tue, 5 Mar 2019 15:12:11 +0800 Message-Id: <1551769940-22739-5-git-send-email-jasowang@redhat.com> In-Reply-To: <1551769940-22739-1-git-send-email-jasowang@redhat.com> References: <1551769940-22739-1-git-send-email-jasowang@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.47]); Tue, 05 Mar 2019 07:12:34 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL V2 04/13] 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: Jason Wang , Vincenzo Maffione Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Vincenzo Maffione 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 !=3D 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 Signed-off-by: Jason Wang --- net/netmap.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/net/netmap.c b/net/netmap.c index 852106a..0cc8f54 100644 --- a/net/netmap.c +++ b/net/netmap.c @@ -159,21 +159,22 @@ static ssize_t netmap_receive_iov(NetClientState *nc, { NetmapState *s =3D DO_UPCAST(NetmapState, nc, nc); struct netmap_ring *ring =3D s->tx; + unsigned int tail =3D ring->tail; + ssize_t totlen =3D 0; uint32_t last; uint32_t idx; uint8_t *dst; int j; uint32_t i; =20 - if (unlikely(!ring)) { - /* Drop the packet. */ - return iov_size(iov, iovcnt); - } - - last =3D i =3D ring->cur; + last =3D i =3D ring->head; =20 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 =3D tail; netmap_write_poll(s, true); return 0; } @@ -183,14 +184,17 @@ static ssize_t netmap_receive_iov(NetClientState *nc, int offset =3D 0; int nm_frag_size; =20 + totlen +=3D iov_frag_size; + /* Split each iovec fragment over more netmap slots, if necessary. */ while (iov_frag_size) { nm_frag_size =3D MIN(iov_frag_size, ring->nr_buf_size); =20 - if (unlikely(nm_ring_empty(ring))) { - /* We run out of netmap slots while splitting the + if (unlikely(i =3D=3D tail)) { + /* We ran out of netmap slots while splitting the iovec fragments. */ + ring->cur =3D 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 &=3D ~NS_MOREFRAG; =20 - /* Now update ring->cur and ring->head. */ - ring->cur =3D ring->head =3D i; + /* Now update ring->head and ring->cur to publish the new slots and + * the new wakeup point. */ + ring->head =3D ring->cur =3D i; =20 ioctl(s->nmd->fd, NIOCTXSYNC, NULL); =20 - return iov_size(iov, iovcnt); + return totlen; } =20 static ssize_t netmap_receive(NetClientState *nc, --=20 2.5.0