From 356c6e0677072f1a6cfe9b5d0648d89ab6fd5523 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Thu, 17 Nov 2022 16:58:51 +1100 Subject: tcp: Unify part of spliced and non-spliced conn_from_sock path In tcp_sock_handler() we split off to handle spliced sockets before checking anything else. However the first steps of the "new connection" path for each case are the same: allocate a connection entry and accept() the connection. Remove this duplication by making tcp_conn_from_sock() handle both spliced and non-spliced cases, with help from more specific tcp_tap_conn_from_sock and tcp_splice_conn_from_sock functions for the later stages which differ. Signed-off-by: David Gibson Signed-off-by: Stefano Brivio --- tcp_splice.c | 55 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 27 deletions(-) (limited to 'tcp_splice.c') diff --git a/tcp_splice.c b/tcp_splice.c index 7a06252..2fd88ec 100644 --- a/tcp_splice.c +++ b/tcp_splice.c @@ -501,6 +501,33 @@ static void tcp_splice_dir(struct tcp_splice_conn *conn, int ref_sock, *pipes = *from == conn->a ? conn->pipe_a_b : conn->pipe_b_a; } +/** + * tcp_splice_conn_from_sock() - Initialize state for spliced connection + * @c: Execution context + * @ref: epoll reference of listening socket + * @conn: connection structure to initialize + * @s: Accepted socket + * + * #syscalls:pasta setsockopt + */ +void tcp_splice_conn_from_sock(struct ctx *c, union epoll_ref ref, + struct tcp_splice_conn *conn, int s) +{ + assert(c->mode == MODE_PASTA); + + if (setsockopt(s, SOL_TCP, TCP_QUICKACK, &((int){ 1 }), sizeof(int))) + trace("TCP (spliced): failed to set TCP_QUICKACK on %i", s); + + conn->c.spliced = true; + c->tcp.splice_conn_count++; + conn->a = s; + conn->flags = ref.r.p.tcp.tcp.v6 ? SPLICE_V6 : 0; + + if (tcp_splice_new(c, conn, ref.r.p.tcp.tcp.index, + ref.r.p.tcp.tcp.outbound)) + conn_flag(c, conn, CLOSING); +} + /** * tcp_sock_handler_splice() - Handler for socket mapped to spliced connection * @c: Execution context @@ -517,33 +544,7 @@ void tcp_sock_handler_splice(struct ctx *c, union epoll_ref ref, uint32_t *seq_read, *seq_write; struct tcp_splice_conn *conn; - if (ref.r.p.tcp.tcp.listen) { - int s; - - if (c->tcp.conn_count >= TCP_MAX_CONNS) - return; - - if ((s = accept4(ref.r.s, NULL, NULL, SOCK_NONBLOCK)) < 0) - return; - - if (setsockopt(s, SOL_TCP, TCP_QUICKACK, &((int){ 1 }), - sizeof(int))) { - trace("TCP (spliced): failed to set TCP_QUICKACK on %i", - s); - } - - conn = CONN(c->tcp.conn_count++); - conn->c.spliced = true; - c->tcp.splice_conn_count++; - conn->a = s; - conn->flags = ref.r.p.tcp.tcp.v6 ? SPLICE_V6 : 0; - - if (tcp_splice_new(c, conn, ref.r.p.tcp.tcp.index, - ref.r.p.tcp.tcp.outbound)) - conn_flag(c, conn, CLOSING); - - return; - } + assert(!ref.r.p.tcp.tcp.listen); conn = CONN(ref.r.p.tcp.tcp.index); -- cgit v1.2.3