From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 029225A0278 for ; Wed, 28 Feb 2024 12:25:34 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1709119522; bh=Btnf+pUnt1M97OCFZxQcCker9+LClOBst0vxopJh3Ig=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IHW0L4VPUOovwKZqy2H5SY+qC7dCu6Jv3ovlAvIczyo96RbOQjZuRuZbp0kjrUn5Y y4TnHRWEYkXhuzuWqQp+Ei8zfPn4zPdUlAofZP6Il6KjrHGLtVhqjXu0n7eCFIvwfO OJD+WU4adl19SO1V3tsrkA8I/EcCqFCxTBSuFw7ADiSf7XGF7By4JSj25bEBYsPCLx GvUx1SmO/8wZAORvqMDL2cJnQYRouJTmuDK8E5PRXw25/Xatl0+hTOY2i2lSwnci+U iegm16BmAoz5oLuXXz15WOn1g61VLbcx9fqUnPo8LMZWf7rDdTv8sFEYm0HTX3q8DQ ijGqR4Q+Fnb/A== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4TlBq65Kxxz4wyY; Wed, 28 Feb 2024 22:25:22 +1100 (AEDT) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v3 08/20] tcp_splice: Simplify clean up logic Date: Wed, 28 Feb 2024 22:25:08 +1100 Message-ID: <20240228112520.2078220-9-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240228112520.2078220-1-david@gibson.dropbear.id.au> References: <20240228112520.2078220-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: HRPBN6OVODPVIHC3UXE7CBS4XCSJNKJE X-Message-ID-Hash: HRPBN6OVODPVIHC3UXE7CBS4XCSJNKJE 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: Currently tcp_splice_flow_defer() contains specific logic to determine if we're far enough initialised that we need to close pipes and/or sockets. This is potentially fragile if we change something about the order in which we do things. We can simplify this by initialising the pipe and socket fields to -1 very early, then close()ing them if and only if they're non-negative. This lets us remove a special case cleanup if our connect() fails. This will already trigger a CLOSING event, and the socket fd in question is populated in the connection structure. Thus we can let the new cleanup logic handle it rather than requiring an explicit close(). Signed-off-by: David Gibson --- tcp_splice.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/tcp_splice.c b/tcp_splice.c index 4828b099..8187220d 100644 --- a/tcp_splice.c +++ b/tcp_splice.c @@ -246,16 +246,14 @@ bool tcp_splice_flow_defer(union flow *flow) return false; for (side = 0; side < SIDES; side++) { - if (conn->events & SPLICE_ESTABLISHED) { - /* Flushing might need to block: don't recycle them. */ - if (conn->pipe[side][0] != -1) { - close(conn->pipe[side][0]); - close(conn->pipe[side][1]); - conn->pipe[side][0] = conn->pipe[side][1] = -1; - } + /* Flushing might need to block: don't recycle them. */ + if (conn->pipe[side][0] >= 0) { + close(conn->pipe[side][0]); + close(conn->pipe[side][1]); + conn->pipe[side][0] = conn->pipe[side][1] = -1; } - if (side == 0 || conn->events & SPLICE_CONNECT) { + if (conn->s[side] >= 0) { close(conn->s[side]); conn->s[side] = -1; } @@ -284,8 +282,6 @@ static int tcp_splice_connect_finish(const struct ctx *c, int i = 0; 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], @@ -361,12 +357,9 @@ static int tcp_splice_connect(const struct ctx *c, struct tcp_splice_conn *conn, } if (connect(conn->s[1], sa, sl)) { - if (errno != EINPROGRESS) { - int ret = -errno; + if (errno != EINPROGRESS) + return -errno; - close(sock_conn); - return ret; - } conn_event(c, conn, SPLICE_CONNECT); } else { conn_event(c, conn, SPLICE_ESTABLISHED); @@ -466,6 +459,9 @@ bool tcp_splice_conn_from_sock(const struct ctx *c, conn->f.type = FLOW_TCP_SPLICE; conn->flags = inany_v4(&aany) ? 0 : SPLICE_V6; conn->s[0] = s; + conn->s[1] = -1; + conn->pipe[0][0] = conn->pipe[0][1] = -1; + conn->pipe[1][0] = conn->pipe[1][1] = -1; if (tcp_splice_new(c, conn, ref.port, ref.pif)) conn_flag(c, conn, CLOSING); -- 2.43.2