public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top, Stefano Brivio <sbrivio@redhat.com>
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH v2 09/11] tcp_splice: Exploit side symmetry in tcp_splice_connect_finish()
Date: Tue,  7 Nov 2023 13:42:48 +1100	[thread overview]
Message-ID: <20231107024250.2290959-10-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20231107024250.2290959-1-david@gibson.dropbear.id.au>

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 <david@gibson.dropbear.id.au>
---
 tcp_splice.c | 65 ++++++++++++++++++++--------------------------------
 1 file changed, 25 insertions(+), 40 deletions(-)

diff --git a/tcp_splice.c b/tcp_splice.c
index cadad32..214bf22 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);
+			}
 		}
 	}
 
-- 
@@ -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


  parent reply	other threads:[~2023-11-07  2:43 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-07  2:42 [PATCH v2 00/11] tcp_splice: Better exploit symmetry between sides of connection David Gibson
2023-11-07  2:42 ` [PATCH v2 01/11] tcp_splice: Remove redundant tcp_splice_epoll_ctl() David Gibson
2023-11-07  2:42 ` [PATCH v2 02/11] tcp_splice: Correct error handling in tcp_splice_epoll_ctl() David Gibson
2023-11-07  2:42 ` [PATCH v2 03/11] tcp_splice: Don't handle EPOLL_CTL_DEL as part of tcp_splice_epoll_ctl() David Gibson
2023-11-07  2:42 ` [PATCH v2 04/11] tcp_splice: Remove unnecessary forward declaration David Gibson
2023-11-07  2:42 ` [PATCH v2 05/11] tcp_splice: Avoid awkward temporaries in tcp_splice_epoll_ctl() David Gibson
2023-11-07  2:42 ` [PATCH v2 06/11] tcp_splice: Don't pool pipes in pairs David Gibson
2023-11-07  2:42 ` [PATCH v2 07/11] tcp_splice: Rename sides of connection from a/b to 0/1 David Gibson
2023-11-07  2:42 ` [PATCH v2 08/11] tcp_splice: Exploit side symmetry in tcp_splice_timer() David Gibson
2023-11-07  2:42 ` David Gibson [this message]
2023-11-07  2:42 ` [PATCH v2 10/11] tcp_splice: Exploit side symmetry in tcp_splice_destroy() David Gibson
2023-11-07  2:42 ` [PATCH v2 11/11] tcp_splice: Simplify selection of socket and pipe sides in socket handler David Gibson
2023-11-07 12:45 ` [PATCH v2 00/11] tcp_splice: Better exploit symmetry between sides of connection Stefano Brivio

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231107024250.2290959-10-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).