aboutgitcodebugslistschat
path: root/tcp_splice.c
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2023-11-07 13:42:48 +1100
committerStefano Brivio <sbrivio@redhat.com>2023-11-07 09:54:15 +0100
commit69db3b3b2943ab791786dccc2f35a0284e0a531d (patch)
tree80d2a59502ac1ea6dc185bea18e131378754e0e1 /tcp_splice.c
parent1b762571475476cbbee09f7dc3bce3f1a4c09f49 (diff)
downloadpasst-69db3b3b2943ab791786dccc2f35a0284e0a531d.tar
passt-69db3b3b2943ab791786dccc2f35a0284e0a531d.tar.gz
passt-69db3b3b2943ab791786dccc2f35a0284e0a531d.tar.bz2
passt-69db3b3b2943ab791786dccc2f35a0284e0a531d.tar.lz
passt-69db3b3b2943ab791786dccc2f35a0284e0a531d.tar.xz
passt-69db3b3b2943ab791786dccc2f35a0284e0a531d.tar.zst
passt-69db3b3b2943ab791786dccc2f35a0284e0a531d.zip
tcp_splice: Exploit side symmetry in tcp_splice_connect_finish()
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> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'tcp_splice.c')
-rw-r--r--tcp_splice.c65
1 files 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);
+ }
}
}