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 11/11] tcp_splice: Simplify selection of socket and pipe sides in socket handler
Date: Thu, 12 Oct 2023 12:51:14 +1100	[thread overview]
Message-ID: <20231012015114.2612066-12-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20231012015114.2612066-1-david@gibson.dropbear.id.au>

tcp_splice_sock_handler() uses the tcp_splice_dir() helper to select
which of the socket, pipe and counter fields to use depending on which
side of the connection the socket event is coming from.

Now that we are using arrays for the two sides, rather than separate named
fields, we can instead just use a variable indicating the side and use
that to index the arrays whever we need a particular side's field.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 tcp_splice.c | 81 ++++++++++++++--------------------------------------
 1 file changed, 22 insertions(+), 59 deletions(-)

diff --git a/tcp_splice.c b/tcp_splice.c
index 239f6d2..822d15a 100644
--- a/tcp_splice.c
+++ b/tcp_splice.c
@@ -438,29 +438,6 @@ static int tcp_splice_new(const struct ctx *c, struct tcp_splice_conn *conn,
 	return tcp_splice_connect(c, conn, s, port);
 }
 
-/**
- * tcp_splice_dir() - Set sockets/pipe pointers reflecting flow direction
- * @conn:	Connection pointers
- * @ref_sock:	Socket returned as reference from epoll
- * @reverse:	Reverse direction: @ref_sock is used as destination
- * @from:	Destination socket pointer to set
- * @to:		Source socket pointer to set
- * @pipes:	Pipe set, assigned on return
- */
-static void tcp_splice_dir(struct tcp_splice_conn *conn, int ref_sock,
-			   int reverse, int *from, int *to, int **pipes)
-{
-	if (!reverse) {
-		*from = ref_sock;
-		*to   = (*from == conn->s[0]) ? conn->s[1] : conn->s[0];
-	} else {
-		*to   = ref_sock;
-		*from = (*to   == conn->s[0]) ? conn->s[1] : conn->s[0];
-	}
-
-	*pipes = *from == conn->s[0] ? conn->pipe[0] : conn->pipe[1];
-}
-
 /**
  * tcp_splice_conn_from_sock() - Attempt to init state for a spliced connection
  * @c:		Execution context
@@ -521,8 +498,7 @@ void tcp_splice_sock_handler(struct ctx *c, struct tcp_splice_conn *conn,
 			     int s, uint32_t events)
 {
 	uint8_t lowat_set_flag, lowat_act_flag;
-	int from, to, *pipes, eof, never_read;
-	uint32_t *seq_read, *seq_write;
+	int fromside, eof, never_read;
 
 	if (conn->events == SPLICE_CLOSED)
 		return;
@@ -538,14 +514,15 @@ void tcp_splice_sock_handler(struct ctx *c, struct tcp_splice_conn *conn,
 	}
 
 	if (events & EPOLLOUT) {
-		if (s == conn->s[0])
+		if (s == conn->s[0]) {
 			conn_event(c, conn, ~OUT_WAIT_0);
-		else
+			fromside = 1;
+		} else {
 			conn_event(c, conn, ~OUT_WAIT_1);
-
-		tcp_splice_dir(conn, s, 1, &from, &to, &pipes);
+			fromside = 0;
+		}
 	} else {
-		tcp_splice_dir(conn, s, 0, &from, &to, &pipes);
+		fromside = s == conn->s[0] ? 0 : 1;
 	}
 
 	if (events & EPOLLRDHUP) {
@@ -566,24 +543,16 @@ swap:
 	eof = 0;
 	never_read = 1;
 
-	if (from == conn->s[0]) {
-		seq_read = &conn->read[0];
-		seq_write = &conn->written[0];
-		lowat_set_flag = RCVLOWAT_SET_0;
-		lowat_act_flag = RCVLOWAT_ACT_0;
-	} else {
-		seq_read = &conn->read[1];
-		seq_write = &conn->written[1];
-		lowat_set_flag = RCVLOWAT_SET_1;
-		lowat_act_flag = RCVLOWAT_ACT_1;
-	}
+	lowat_set_flag = fromside == 0 ? RCVLOWAT_SET_0 : RCVLOWAT_SET_1;
+	lowat_act_flag = fromside == 0 ? RCVLOWAT_ACT_0 : RCVLOWAT_ACT_1;
 
 	while (1) {
 		ssize_t readlen, to_write = 0, written;
 		int more = 0;
 
 retry:
-		readlen = splice(from, NULL, pipes[1], NULL, c->tcp.pipe_size,
+		readlen = splice(conn->s[fromside], NULL,
+				 conn->pipe[fromside][1], NULL, c->tcp.pipe_size,
 				 SPLICE_F_MOVE | SPLICE_F_NONBLOCK);
 		trace("TCP (spliced): %li from read-side call", readlen);
 		if (readlen < 0) {
@@ -608,7 +577,8 @@ retry:
 		}
 
 eintr:
-		written = splice(pipes[0], NULL, to, NULL, to_write,
+		written = splice(conn->pipe[fromside][0], NULL,
+				 conn->s[!fromside], NULL, to_write,
 				 SPLICE_F_MOVE | more | SPLICE_F_NONBLOCK);
 		trace("TCP (spliced): %li from write-side call (passed %lu)",
 		      written, to_write);
@@ -622,8 +592,8 @@ eintr:
 			    readlen > (long)c->tcp.pipe_size / 10) {
 				int lowat = c->tcp.pipe_size / 4;
 
-				setsockopt(from, SOL_SOCKET, SO_RCVLOWAT,
-					   &lowat, sizeof(lowat));
+				setsockopt(conn->s[fromside], SOL_SOCKET,
+					   SO_RCVLOWAT, &lowat, sizeof(lowat));
 
 				conn_flag(c, conn, lowat_set_flag);
 				conn_flag(c, conn, lowat_act_flag);
@@ -632,8 +602,8 @@ eintr:
 			break;
 		}
 
-		*seq_read  += readlen > 0 ? readlen : 0;
-		*seq_write += written > 0 ? written : 0;
+		conn->read[fromside]  += readlen > 0 ? readlen : 0;
+		conn->written[fromside] += written > 0 ? written : 0;
 
 		if (written < 0) {
 			if (errno == EINTR)
@@ -645,10 +615,8 @@ eintr:
 			if (never_read)
 				break;
 
-			if (to == conn->s[0])
-				conn_event(c, conn, OUT_WAIT_0);
-			else
-				conn_event(c, conn, OUT_WAIT_1);
+			conn_event(c, conn,
+				   fromside == 0 ? OUT_WAIT_1 : OUT_WAIT_0);
 			break;
 		}
 
@@ -665,14 +633,14 @@ eintr:
 	}
 
 	if ((conn->events & FIN_RCVD_0) && !(conn->events & FIN_SENT_1)) {
-		if (*seq_read == *seq_write && eof) {
+		if (conn->read[fromside] == conn->written[fromside] && eof) {
 			shutdown(conn->s[1], SHUT_WR);
 			conn_event(c, conn, FIN_SENT_1);
 		}
 	}
 
 	if ((conn->events & FIN_RCVD_1) && !(conn->events & FIN_SENT_0)) {
-		if (*seq_read == *seq_write && eof) {
+		if (conn->read[fromside] == conn->written[fromside] && eof) {
 			shutdown(conn->s[0], SHUT_WR);
 			conn_event(c, conn, FIN_SENT_0);
 		}
@@ -684,12 +652,7 @@ eintr:
 	if ((events & (EPOLLIN | EPOLLOUT)) == (EPOLLIN | EPOLLOUT)) {
 		events = EPOLLIN;
 
-		SWAP(from, to);
-		if (pipes == conn->pipe[0])
-			pipes = conn->pipe[1];
-		else
-			pipes = conn->pipe[0];
-
+		fromside = !fromside;
 		goto swap;
 	}
 
-- 
@@ -438,29 +438,6 @@ static int tcp_splice_new(const struct ctx *c, struct tcp_splice_conn *conn,
 	return tcp_splice_connect(c, conn, s, port);
 }
 
-/**
- * tcp_splice_dir() - Set sockets/pipe pointers reflecting flow direction
- * @conn:	Connection pointers
- * @ref_sock:	Socket returned as reference from epoll
- * @reverse:	Reverse direction: @ref_sock is used as destination
- * @from:	Destination socket pointer to set
- * @to:		Source socket pointer to set
- * @pipes:	Pipe set, assigned on return
- */
-static void tcp_splice_dir(struct tcp_splice_conn *conn, int ref_sock,
-			   int reverse, int *from, int *to, int **pipes)
-{
-	if (!reverse) {
-		*from = ref_sock;
-		*to   = (*from == conn->s[0]) ? conn->s[1] : conn->s[0];
-	} else {
-		*to   = ref_sock;
-		*from = (*to   == conn->s[0]) ? conn->s[1] : conn->s[0];
-	}
-
-	*pipes = *from == conn->s[0] ? conn->pipe[0] : conn->pipe[1];
-}
-
 /**
  * tcp_splice_conn_from_sock() - Attempt to init state for a spliced connection
  * @c:		Execution context
@@ -521,8 +498,7 @@ void tcp_splice_sock_handler(struct ctx *c, struct tcp_splice_conn *conn,
 			     int s, uint32_t events)
 {
 	uint8_t lowat_set_flag, lowat_act_flag;
-	int from, to, *pipes, eof, never_read;
-	uint32_t *seq_read, *seq_write;
+	int fromside, eof, never_read;
 
 	if (conn->events == SPLICE_CLOSED)
 		return;
@@ -538,14 +514,15 @@ void tcp_splice_sock_handler(struct ctx *c, struct tcp_splice_conn *conn,
 	}
 
 	if (events & EPOLLOUT) {
-		if (s == conn->s[0])
+		if (s == conn->s[0]) {
 			conn_event(c, conn, ~OUT_WAIT_0);
-		else
+			fromside = 1;
+		} else {
 			conn_event(c, conn, ~OUT_WAIT_1);
-
-		tcp_splice_dir(conn, s, 1, &from, &to, &pipes);
+			fromside = 0;
+		}
 	} else {
-		tcp_splice_dir(conn, s, 0, &from, &to, &pipes);
+		fromside = s == conn->s[0] ? 0 : 1;
 	}
 
 	if (events & EPOLLRDHUP) {
@@ -566,24 +543,16 @@ swap:
 	eof = 0;
 	never_read = 1;
 
-	if (from == conn->s[0]) {
-		seq_read = &conn->read[0];
-		seq_write = &conn->written[0];
-		lowat_set_flag = RCVLOWAT_SET_0;
-		lowat_act_flag = RCVLOWAT_ACT_0;
-	} else {
-		seq_read = &conn->read[1];
-		seq_write = &conn->written[1];
-		lowat_set_flag = RCVLOWAT_SET_1;
-		lowat_act_flag = RCVLOWAT_ACT_1;
-	}
+	lowat_set_flag = fromside == 0 ? RCVLOWAT_SET_0 : RCVLOWAT_SET_1;
+	lowat_act_flag = fromside == 0 ? RCVLOWAT_ACT_0 : RCVLOWAT_ACT_1;
 
 	while (1) {
 		ssize_t readlen, to_write = 0, written;
 		int more = 0;
 
 retry:
-		readlen = splice(from, NULL, pipes[1], NULL, c->tcp.pipe_size,
+		readlen = splice(conn->s[fromside], NULL,
+				 conn->pipe[fromside][1], NULL, c->tcp.pipe_size,
 				 SPLICE_F_MOVE | SPLICE_F_NONBLOCK);
 		trace("TCP (spliced): %li from read-side call", readlen);
 		if (readlen < 0) {
@@ -608,7 +577,8 @@ retry:
 		}
 
 eintr:
-		written = splice(pipes[0], NULL, to, NULL, to_write,
+		written = splice(conn->pipe[fromside][0], NULL,
+				 conn->s[!fromside], NULL, to_write,
 				 SPLICE_F_MOVE | more | SPLICE_F_NONBLOCK);
 		trace("TCP (spliced): %li from write-side call (passed %lu)",
 		      written, to_write);
@@ -622,8 +592,8 @@ eintr:
 			    readlen > (long)c->tcp.pipe_size / 10) {
 				int lowat = c->tcp.pipe_size / 4;
 
-				setsockopt(from, SOL_SOCKET, SO_RCVLOWAT,
-					   &lowat, sizeof(lowat));
+				setsockopt(conn->s[fromside], SOL_SOCKET,
+					   SO_RCVLOWAT, &lowat, sizeof(lowat));
 
 				conn_flag(c, conn, lowat_set_flag);
 				conn_flag(c, conn, lowat_act_flag);
@@ -632,8 +602,8 @@ eintr:
 			break;
 		}
 
-		*seq_read  += readlen > 0 ? readlen : 0;
-		*seq_write += written > 0 ? written : 0;
+		conn->read[fromside]  += readlen > 0 ? readlen : 0;
+		conn->written[fromside] += written > 0 ? written : 0;
 
 		if (written < 0) {
 			if (errno == EINTR)
@@ -645,10 +615,8 @@ eintr:
 			if (never_read)
 				break;
 
-			if (to == conn->s[0])
-				conn_event(c, conn, OUT_WAIT_0);
-			else
-				conn_event(c, conn, OUT_WAIT_1);
+			conn_event(c, conn,
+				   fromside == 0 ? OUT_WAIT_1 : OUT_WAIT_0);
 			break;
 		}
 
@@ -665,14 +633,14 @@ eintr:
 	}
 
 	if ((conn->events & FIN_RCVD_0) && !(conn->events & FIN_SENT_1)) {
-		if (*seq_read == *seq_write && eof) {
+		if (conn->read[fromside] == conn->written[fromside] && eof) {
 			shutdown(conn->s[1], SHUT_WR);
 			conn_event(c, conn, FIN_SENT_1);
 		}
 	}
 
 	if ((conn->events & FIN_RCVD_1) && !(conn->events & FIN_SENT_0)) {
-		if (*seq_read == *seq_write && eof) {
+		if (conn->read[fromside] == conn->written[fromside] && eof) {
 			shutdown(conn->s[0], SHUT_WR);
 			conn_event(c, conn, FIN_SENT_0);
 		}
@@ -684,12 +652,7 @@ eintr:
 	if ((events & (EPOLLIN | EPOLLOUT)) == (EPOLLIN | EPOLLOUT)) {
 		events = EPOLLIN;
 
-		SWAP(from, to);
-		if (pipes == conn->pipe[0])
-			pipes = conn->pipe[1];
-		else
-			pipes = conn->pipe[0];
-
+		fromside = !fromside;
 		goto swap;
 	}
 
-- 
2.41.0


  parent reply	other threads:[~2023-10-12  1:51 UTC|newest]

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

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=20231012015114.2612066-12-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).