aboutgitcodebugslistschat
path: root/tcp_splice.c
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2024-02-19 18:56:50 +1100
committerStefano Brivio <sbrivio@redhat.com>2024-02-27 12:52:46 +0100
commitfe27ebce5c59c7fc684c5affa6ce27fdc32d362d (patch)
tree903b8e243ebc30e0ae2abe578d3fb220249107d8 /tcp_splice.c
parentfbe81decbdcdfed4b4ff336fcec5fe6ad0dfbe65 (diff)
downloadpasst-fe27ebce5c59c7fc684c5affa6ce27fdc32d362d.tar
passt-fe27ebce5c59c7fc684c5affa6ce27fdc32d362d.tar.gz
passt-fe27ebce5c59c7fc684c5affa6ce27fdc32d362d.tar.bz2
passt-fe27ebce5c59c7fc684c5affa6ce27fdc32d362d.tar.lz
passt-fe27ebce5c59c7fc684c5affa6ce27fdc32d362d.tar.xz
passt-fe27ebce5c59c7fc684c5affa6ce27fdc32d362d.tar.zst
passt-fe27ebce5c59c7fc684c5affa6ce27fdc32d362d.zip
tcp, tcp_splice: Helpers for getting sockets from the pools
We maintain pools of ready-to-connect sockets in both the original and (for pasta) guest namespace to reduce latency when starting new TCP connections. If we exhaust those pools we have to take a higher latency path to get a new socket. Currently we open-code that fallback in the places we need it. To improve clarity encapsulate that into helper functions. While we're at it, give those helpers clearer error reporting. 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.c55
1 files changed, 32 insertions, 23 deletions
diff --git a/tcp_splice.c b/tcp_splice.c
index ee68029..5b38a82 100644
--- a/tcp_splice.c
+++ b/tcp_splice.c
@@ -377,6 +377,34 @@ static int tcp_splice_connect(const struct ctx *c, struct tcp_splice_conn *conn,
}
/**
+ * tcp_conn_sock_ns() - Obtain a connectable socket in the namespace
+ * @c: Execution context
+ * @af: Address family (AF_INET or AF_INET6)
+ *
+ * Return: Socket fd in the namespace on success, -errno on failure
+ */
+static int tcp_conn_sock_ns(const struct ctx *c, sa_family_t af)
+{
+ int *p = af == AF_INET6 ? ns_sock_pool6 : ns_sock_pool4;
+ int s;
+
+ if ((s = tcp_conn_pool_sock(p)) >= 0)
+ return s;
+
+ /* If the pool is empty we have to incur the latency of entering the ns.
+ * Therefore, we might as well refill the whole pool while we're at it.
+ * This differs from tcp_conn_sock().
+ */
+ NS_CALL(tcp_sock_refill_ns, c);
+
+ if ((s = tcp_conn_pool_sock(p)) >= 0)
+ return s;
+
+ err("TCP: No available ns sockets for new connection");
+ return -1;
+}
+
+/**
* tcp_splice_new() - Handle new spliced connection
* @c: Execution context
* @conn: Connection pointer
@@ -388,38 +416,19 @@ static int tcp_splice_connect(const struct ctx *c, struct tcp_splice_conn *conn,
static int tcp_splice_new(const struct ctx *c, struct tcp_splice_conn *conn,
in_port_t port, uint8_t pif)
{
+ sa_family_t af = CONN_V6(conn) ? AF_INET6 : AF_INET;
int s = -1;
- /* If the pool is empty we take slightly different approaches
- * for init or ns sockets. For init sockets we just open a
- * new one without refilling the pool to keep latency down.
- * For ns sockets, we're going to incur the latency of
- * entering the ns anyway, so we might as well refill the
- * pool.
- */
if (pif == PIF_SPLICE) {
- int *p = CONN_V6(conn) ? init_sock_pool6 : init_sock_pool4;
- sa_family_t af = CONN_V6(conn) ? AF_INET6 : AF_INET;
-
- s = tcp_conn_pool_sock(p);
- if (s < 0)
- s = tcp_conn_new_sock(c, af);
+ s = tcp_conn_sock(c, af);
} else {
- int *p = CONN_V6(conn) ? ns_sock_pool6 : ns_sock_pool4;
-
ASSERT(pif == PIF_HOST);
- /* If pool is empty, refill it first */
- if (p[TCP_SOCK_POOL_SIZE-1] < 0)
- NS_CALL(tcp_sock_refill_ns, c);
-
- s = tcp_conn_pool_sock(p);
+ s = tcp_conn_sock_ns(c, af);
}
- if (s < 0) {
- warn("Couldn't open connectable socket for splice (%d)", s);
+ if (s < 0)
return s;
- }
return tcp_splice_connect(c, conn, s, port);
}