From nobody Sun Feb 8 18:29:35 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.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 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 150771674826479.95467026270603; Wed, 11 Oct 2017 03:12:28 -0700 (PDT) Received: from localhost ([::1]:40002 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e2Dzy-00060c-Qi for importer@patchew.org; Wed, 11 Oct 2017 06:12:22 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60368) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e2Dxt-0004nW-7C for qemu-devel@nongnu.org; Wed, 11 Oct 2017 06:10:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e2Dxr-0001ys-LA for qemu-devel@nongnu.org; Wed, 11 Oct 2017 06:10:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:45994) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e2Dxr-0001w0-Am for qemu-devel@nongnu.org; Wed, 11 Oct 2017 06:10:11 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 01DEE7EA85 for ; Wed, 11 Oct 2017 10:10:09 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.42.22.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id 313B868B2B; Wed, 11 Oct 2017 10:10:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 01DEE7EA85 Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=berrange@redhat.com From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Wed, 11 Oct 2017 11:09:57 +0100 Message-Id: <20171011100959.29326-6-berrange@redhat.com> In-Reply-To: <20171011100959.29326-1-berrange@redhat.com> References: <20171011100959.29326-1-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Wed, 11 Oct 2017 10:10:10 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 5/7] io: get rid of bounce buffering in websock write path 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Currently most outbound I/O on the websock channel gets copied into the rawoutput buffer, and then immediately copied again into the encoutput buffer, with a header prepended. Now that qio_channel_websock_encode accepts a struct iovec, we can trivially remove this bounce buffering and write directly to encoutput. In doing so, we also now correctly validate the encoutput size against the QIO_CHANNEL_WEBSOCK_MAX_BUFFER limit. Signed-off-by: Daniel P. Berrange Reviewed-by: Eric Blake --- include/io/channel-websock.h | 1 - io/channel-websock.c | 64 +++++++++++++++++++---------------------= ---- 2 files changed, 28 insertions(+), 37 deletions(-) diff --git a/include/io/channel-websock.h b/include/io/channel-websock.h index 3762707b9c..a7e5e92e61 100644 --- a/include/io/channel-websock.h +++ b/include/io/channel-websock.h @@ -59,7 +59,6 @@ struct QIOChannelWebsock { Buffer encinput; Buffer encoutput; Buffer rawinput; - Buffer rawoutput; size_t payload_remain; size_t pong_remain; QIOChannelWebsockMask mask; diff --git a/io/channel-websock.c b/io/channel-websock.c index c29abb53f0..4c62b5eafc 100644 --- a/io/channel-websock.c +++ b/io/channel-websock.c @@ -24,6 +24,7 @@ #include "io/channel-websock.h" #include "crypto/hash.h" #include "trace.h" +#include "qemu/iov.h" =20 #include =20 @@ -631,19 +632,22 @@ static ssize_t qio_channel_websock_write_wire(QIOChan= nelWebsock *, Error **); static void qio_channel_websock_write_close(QIOChannelWebsock *ioc, uint16_t code, const char *rea= son) { - struct iovec iov; - buffer_reserve(&ioc->rawoutput, 2 + (reason ? strlen(reason) : 0)); - *(uint16_t *)(ioc->rawoutput.buffer + ioc->rawoutput.offset) =3D - cpu_to_be16(code); - ioc->rawoutput.offset +=3D 2; + struct iovec iov[2] =3D { + { .iov_base =3D &code, .iov_len =3D sizeof(code) }, + }; + size_t niov =3D 1; + size_t size =3D iov[0].iov_len; + + cpu_to_be16s(&code); + if (reason) { - buffer_append(&ioc->rawoutput, reason, strlen(reason)); + iov[1].iov_base =3D (void *)reason; + iov[1].iov_len =3D strlen(reason); + size +=3D iov[1].iov_len; + niov++; } - iov.iov_base =3D ioc->rawoutput.buffer; - iov.iov_len =3D ioc->rawoutput.offset; qio_channel_websock_encode(ioc, QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE, - &iov, 1, iov.iov_len); - buffer_reset(&ioc->rawoutput); + iov, niov, size); qio_channel_websock_write_wire(ioc, NULL); qio_channel_shutdown(ioc->master, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); } @@ -891,7 +895,6 @@ static void qio_channel_websock_finalize(Object *obj) buffer_free(&ioc->encinput); buffer_free(&ioc->encoutput); buffer_free(&ioc->rawinput); - buffer_free(&ioc->rawoutput); object_unref(OBJECT(ioc->master)); if (ioc->io_tag) { g_source_remove(ioc->io_tag); @@ -1101,8 +1104,8 @@ static ssize_t qio_channel_websock_writev(QIOChannel = *ioc, Error **errp) { QIOChannelWebsock *wioc =3D QIO_CHANNEL_WEBSOCK(ioc); - size_t i; - ssize_t done =3D 0; + ssize_t want =3D iov_size(iov, niov); + ssize_t avail; ssize_t ret; =20 if (wioc->io_err) { @@ -1115,32 +1118,21 @@ static ssize_t qio_channel_websock_writev(QIOChanne= l *ioc, return -1; } =20 - for (i =3D 0; i < niov; i++) { - size_t want =3D iov[i].iov_len; - if ((want + wioc->rawoutput.offset) > QIO_CHANNEL_WEBSOCK_MAX_BUFF= ER) { - want =3D (QIO_CHANNEL_WEBSOCK_MAX_BUFFER - wioc->rawoutput.off= set); - } - if (want =3D=3D 0) { - goto done; - } - - buffer_reserve(&wioc->rawoutput, want); - buffer_append(&wioc->rawoutput, iov[i].iov_base, want); - done +=3D want; - if (want < iov[i].iov_len) { - break; - } + avail =3D wioc->encoutput.offset >=3D QIO_CHANNEL_WEBSOCK_MAX_BUFFER ? + 0 : (QIO_CHANNEL_WEBSOCK_MAX_BUFFER - wioc->encoutput.offset); + if (want > avail) { + want =3D avail; } =20 - done: - if (wioc->rawoutput.offset) { - struct iovec iov =3D { .iov_base =3D wioc->rawoutput.buffer, - .iov_len =3D wioc->rawoutput.offset }; + if (want) { qio_channel_websock_encode(wioc, QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME, - &iov, 1, iov.iov_len); - buffer_reset(&wioc->rawoutput); + iov, niov, want); } + + /* Even if want =3D=3D 0, we'll try write_wire in case there's + * pending data we could usefully flush out + */ ret =3D qio_channel_websock_write_wire(wioc, errp); if (ret < 0 && ret !=3D QIO_CHANNEL_ERR_BLOCK) { @@ -1150,11 +1142,11 @@ static ssize_t qio_channel_websock_writev(QIOChanne= l *ioc, =20 qio_channel_websock_set_watch(wioc); =20 - if (done =3D=3D 0) { + if (want =3D=3D 0) { return QIO_CHANNEL_ERR_BLOCK; } =20 - return done; + return want; } =20 static int qio_channel_websock_set_blocking(QIOChannel *ioc, --=20 2.13.5