tcp_splice: Exploit side symmetry in tcp_splice_destroy()

tcp_splice_destroy() has some close-to-duplicated logic handling closing of
the socket and pipes for each side of the connection.  We can use a loop
across the sides to reduce the duplication.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
David Gibson 2023-11-07 13:42:49 +11:00 committed by Stefano Brivio
parent 69db3b3b29
commit 7486cd13af

View file

@ -258,30 +258,26 @@ void tcp_splice_conn_update(const struct ctx *c, struct tcp_splice_conn *new)
void tcp_splice_destroy(struct ctx *c, union tcp_conn *conn_union) void tcp_splice_destroy(struct ctx *c, union tcp_conn *conn_union)
{ {
struct tcp_splice_conn *conn = &conn_union->splice; struct tcp_splice_conn *conn = &conn_union->splice;
int side;
if (conn->events & SPLICE_ESTABLISHED) { for (side = 0; side < SIDES; side++) {
/* Flushing might need to block: don't recycle them. */ if (conn->events & SPLICE_ESTABLISHED) {
if (conn->pipe[0][0] != -1) { /* Flushing might need to block: don't recycle them. */
close(conn->pipe[0][0]); if (conn->pipe[side][0] != -1) {
close(conn->pipe[0][1]); close(conn->pipe[side][0]);
conn->pipe[0][0] = conn->pipe[0][1] = -1; close(conn->pipe[side][1]);
conn->pipe[side][0] = conn->pipe[side][1] = -1;
}
} }
if (conn->pipe[1][0] != -1) {
close(conn->pipe[1][0]); if (side == 0 || conn->events & SPLICE_CONNECT) {
close(conn->pipe[1][1]); close(conn->s[side]);
conn->pipe[1][0] = conn->pipe[1][1] = -1; conn->s[side] = -1;
} }
}
if (conn->events & SPLICE_CONNECT) { conn->read[side] = conn->written[side] = 0;
close(conn->s[1]);
conn->s[1] = -1;
} }
close(conn->s[0]);
conn->s[0] = -1;
conn->read[0] = conn->written[0] = conn->read[1] = conn->written[1] = 0;
conn->events = SPLICE_CLOSED; conn->events = SPLICE_CLOSED;
conn->flags = 0; conn->flags = 0;
debug("TCP (spliced): index %li, CLOSED", CONN_IDX(conn)); debug("TCP (spliced): index %li, CLOSED", CONN_IDX(conn));