From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTP id AE7EB5A026D for ; Fri, 3 Nov 2023 17:21:24 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699028483; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IDedQBNG/HJEkTAIuC6Z+cBbNLUCYskq6N9fHOLrZYQ=; b=VUWeyqXrMcPGcqW6y9nbfY/bEIudEx5YfzOOwYD6rvWzjPoa1YV5U6YIVqIY4NQzgX3L1S ipYyDWDDOF+rI80zAdGNX8EteH2l4yt3xsWzBvFk7Ckzm0sj0iA5xYrmOHDvIqx7Uqrxcb alGr7bK8eCLV+VgRnL3aYCHp7U4nBgo= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-655-QSER8cYzM5iMUU7qSSb27Q-1; Fri, 03 Nov 2023 12:21:22 -0400 X-MC-Unique: QSER8cYzM5iMUU7qSSb27Q-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F053C811E7B; Fri, 3 Nov 2023 16:21:21 +0000 (UTC) Received: from elisabeth (unknown [10.39.208.16]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 23AF32026D4C; Fri, 3 Nov 2023 16:21:20 +0000 (UTC) Date: Fri, 3 Nov 2023 17:21:12 +0100 From: Stefano Brivio To: David Gibson Subject: Re: [PATCH 11/11] tcp_splice: Simplify selection of socket and pipe sides in socket handler Message-ID: <20231103172112.3b19eda7@elisabeth> In-Reply-To: <20231012015114.2612066-12-david@gibson.dropbear.id.au> References: <20231012015114.2612066-1-david@gibson.dropbear.id.au> <20231012015114.2612066-12-david@gibson.dropbear.id.au> Organization: Red Hat MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: GKSHYE6LIQ2RB5NWC2ZUO65IJVZNXFMT X-Message-ID-Hash: GKSHYE6LIQ2RB5NWC2ZUO65IJVZNXFMT X-MailFrom: sbrivio@redhat.com 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: passt-dev@passt.top 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: On Thu, 12 Oct 2023 12:51:14 +1100 David Gibson wrote: > 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 > --- > 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; The extra whitespace had the function of making this look more "tabular", now you should add a further one (or drop it altogether depending on taste). -- Stefano