aboutgitcodebugslistschat
path: root/tcp.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.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.c')
-rw-r--r--tcp.c34
1 files changed, 29 insertions, 5 deletions
diff --git a/tcp.c b/tcp.c
index 044c57f..40a79b4 100644
--- a/tcp.c
+++ b/tcp.c
@@ -1792,7 +1792,7 @@ int tcp_conn_pool_sock(int pool[])
*
* Return: socket number on success, negative code if socket creation failed
*/
-int tcp_conn_new_sock(const struct ctx *c, sa_family_t af)
+static int tcp_conn_new_sock(const struct ctx *c, sa_family_t af)
{
int s;
@@ -1812,6 +1812,32 @@ int tcp_conn_new_sock(const struct ctx *c, sa_family_t af)
}
/**
+ * tcp_conn_sock() - Obtain a connectable socket in the host/init namespace
+ * @c: Execution context
+ * @af: Address family (AF_INET or AF_INET6)
+ *
+ * Return: Socket fd on success, -errno on failure
+ */
+int tcp_conn_sock(const struct ctx *c, sa_family_t af)
+{
+ int *pool = af == AF_INET6 ? init_sock_pool6 : init_sock_pool4;
+ int s;
+
+ if ((s = tcp_conn_pool_sock(pool)) >= 0)
+ return s;
+
+ /* If the pool is empty we just open a new one without refilling the
+ * pool to keep latency down.
+ */
+ if ((s = tcp_conn_new_sock(c, af)) >= 0)
+ return s;
+
+ err("TCP: Unable to open socket for new connection: %s",
+ strerror(-s));
+ return -1;
+}
+
+/**
* tcp_conn_tap_mss() - Get MSS value advertised by tap/guest
* @conn: Connection pointer
* @opts: Pointer to start of TCP options
@@ -1909,7 +1935,6 @@ static void tcp_conn_from_tap(struct ctx *c, sa_family_t af,
const struct tcphdr *th, const char *opts,
size_t optlen, const struct timespec *now)
{
- int *pool = af == AF_INET6 ? init_sock_pool6 : init_sock_pool4;
struct sockaddr_in addr4 = {
.sin_family = AF_INET,
.sin_port = th->dest,
@@ -1931,9 +1956,8 @@ static void tcp_conn_from_tap(struct ctx *c, sa_family_t af,
if (!(flow = flow_alloc()))
return;
- if ((s = tcp_conn_pool_sock(pool)) < 0)
- if ((s = tcp_conn_new_sock(c, af)) < 0)
- goto cancel;
+ if ((s = tcp_conn_sock(c, af)) < 0)
+ goto cancel;
if (!c->no_map_gw) {
if (af == AF_INET && IN4_ARE_ADDR_EQUAL(daddr, &c->ip4.gw))