From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 872345A027F for ; Thu, 12 Oct 2023 03:51:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1697075480; bh=5lAV6djVLVFm3cb32Qx2cEwof5hhRNS6jcKmKYN6Efs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YDR0qC0b4XWNEYumQCEDzNT7810Z92emEOQhO80pcXtRSEnNUEDSc+6rN32uOJKF9 vkyW8QN+9dk23nMEGPf94Tw+r0RKY3tSrB7KmRlbid8MvPQUHYopQFo3eIA+bfPRNO XB5Kx7g6u/P9ySj5/w1E5zPMPXA1zZqirkrZ9zVk= Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4S5Xfw1P2Zz4xWL; Thu, 12 Oct 2023 12:51:20 +1100 (AEDT) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 09/11] tcp_splice: Exploit side symmetry in tcp_splice_connect_finish() Date: Thu, 12 Oct 2023 12:51:12 +1100 Message-ID: <20231012015114.2612066-10-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231012015114.2612066-1-david@gibson.dropbear.id.au> References: <20231012015114.2612066-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: 52ZEMB4IMSSSGUH6ONM2JVEMAJTO5Q53 X-Message-ID-Hash: 52ZEMB4IMSSSGUH6ONM2JVEMAJTO5Q53 X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: tcp_splice_connect_finish() has two very similar blocks opening the two pipes for each direction of the connection. We can deduplicate this with a loop across the two sides. Signed-off-by: David Gibson --- tcp_splice.c | 65 ++++++++++++++++++++-------------------------------- 1 file changed, 25 insertions(+), 40 deletions(-) diff --git a/tcp_splice.c b/tcp_splice.c index 259d774..99ef8a4 100644 --- a/tcp_splice.c +++ b/tcp_splice.c @@ -299,50 +299,35 @@ void tcp_splice_destroy(struct ctx *c, union tcp_conn *conn_union) static int tcp_splice_connect_finish(const struct ctx *c, struct tcp_splice_conn *conn) { - int i; - - conn->pipe[0][0] = conn->pipe[1][0] = -1; - conn->pipe[0][1] = conn->pipe[1][1] = -1; - - for (i = 0; i < TCP_SPLICE_PIPE_POOL_SIZE; i++) { - if (splice_pipe_pool[i][0] >= 0) { - SWAP(conn->pipe[0][0], splice_pipe_pool[i][0]); - SWAP(conn->pipe[0][1], splice_pipe_pool[i][1]); - break; - } - } - if (conn->pipe[0][0] < 0) { - if (pipe2(conn->pipe[0], O_NONBLOCK | O_CLOEXEC)) { - err("TCP (spliced): cannot create 0->1 pipe: %s", - strerror(errno)); - conn_flag(c, conn, CLOSING); - return -EIO; - } + int i = 0; + int side; - if (fcntl(conn->pipe[0][0], F_SETPIPE_SZ, c->tcp.pipe_size)) { - trace("TCP (spliced): cannot set 0->1 pipe size to %lu", - c->tcp.pipe_size); + for (side = 0; side < SIDES; side++) { + conn->pipe[side][0] = conn->pipe[side][1] = -1; + + for (; i < TCP_SPLICE_PIPE_POOL_SIZE; i++) { + if (splice_pipe_pool[i][0] >= 0) { + SWAP(conn->pipe[side][0], + splice_pipe_pool[i][0]); + SWAP(conn->pipe[side][1], + splice_pipe_pool[i][1]); + break; + } } - } - for (; i < TCP_SPLICE_PIPE_POOL_SIZE; i++) { - if (splice_pipe_pool[i][0] >= 0) { - SWAP(conn->pipe[1][0], splice_pipe_pool[i][0]); - SWAP(conn->pipe[1][1], splice_pipe_pool[i][1]); - break; - } - } - if (conn->pipe[1][0] < 0) { - if (pipe2(conn->pipe[1], O_NONBLOCK | O_CLOEXEC)) { - err("TCP (spliced): cannot create 1->0 pipe: %s", - strerror(errno)); - conn_flag(c, conn, CLOSING); - return -EIO; - } + if (conn->pipe[side][0] < 0) { + if (pipe2(conn->pipe[side], O_NONBLOCK | O_CLOEXEC)) { + err("TCP (spliced): cannot create %d->%d pipe: %s", + side, !side, strerror(errno)); + conn_flag(c, conn, CLOSING); + return -EIO; + } - if (fcntl(conn->pipe[1][0], F_SETPIPE_SZ, c->tcp.pipe_size)) { - trace("TCP (spliced): cannot set 1->0 pipe size to %lu", - c->tcp.pipe_size); + if (fcntl(conn->pipe[side][0], F_SETPIPE_SZ, + c->tcp.pipe_size)) { + trace("TCP (spliced): cannot set %d->%d pipe size to %lu", + side, !side, c->tcp.pipe_size); + } } } -- 2.41.0