From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top, Stefano Brivio <sbrivio@redhat.com>
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 4/5] tcp: Split pool lookup from creating new sockets in tcp_conn_new_sock()
Date: Mon, 9 Jan 2023 11:50:25 +1100 [thread overview]
Message-ID: <20230109005026.24512-5-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20230109005026.24512-1-david@gibson.dropbear.id.au>
tcp_conn_new_sock() first looks for a socket in a pre-opened pool, then if
that's empty creates a new socket in the init namespace. Both parts of
this are duplicated in other places: the pool lookup logic is duplicated in
tcp_splice_new(), and the socket opening logic is duplicated in
tcp_sock_refill_pool().
Split the function into separate parts so we can remove both these
duplications.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
tcp.c | 53 +++++++++++++++++++++++++++-------------------------
tcp_conn.h | 1 +
tcp_splice.c | 8 ++------
3 files changed, 31 insertions(+), 31 deletions(-)
diff --git a/tcp.c b/tcp.c
index 19fd17b..a540235 100644
--- a/tcp.c
+++ b/tcp.c
@@ -1825,24 +1825,35 @@ static void tcp_seq_init(const struct ctx *c, struct tcp_tap_conn *conn,
}
/**
- * tcp_conn_new_sock() - Get socket for new connection from pool or make new one
- * @c: Execution context
- * @af: Address family
+ * tcp_conn_pool_sock() - Get socket for new connection from pre-opened pool
+ * @pool: Pool of pre-opened sockets
*
- * Return: socket number if available, negative code if socket creation failed
+ * Return: socket number if available, negative code if pool is empty
*/
-static int tcp_conn_new_sock(const struct ctx *c, sa_family_t af)
+int tcp_conn_pool_sock(int pool[])
{
- int *p = af == AF_INET6 ? init_sock_pool6 : init_sock_pool4, i, s = -1;
+ int s = -1, i;
- for (i = 0; i < TCP_SOCK_POOL_SIZE; i++, p++) {
- SWAP(s, *p);
+ for (i = 0; i < TCP_SOCK_POOL_SIZE; i++) {
+ SWAP(s, pool[i]);
if (s >= 0)
- break;
+ return s;
}
+ return -1;
+}
- if (s < 0)
- s = socket(af, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
+/**
+ * tcp_conn_new_sock() - Open and prepare new socket for connection
+ * @c: Execution context
+ * @af: Address family
+ *
+ * Return: socket number on success, negative code if socket creation failed
+ */
+static int tcp_conn_new_sock(const struct ctx *c, sa_family_t af)
+{
+ int s;
+
+ s = socket(af, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
if (s > SOCKET_MAX) {
close(s);
@@ -1903,6 +1914,7 @@ static void tcp_conn_from_tap(struct ctx *c, int af, const void *addr,
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,
@@ -1921,8 +1933,9 @@ static void tcp_conn_from_tap(struct ctx *c, int af, const void *addr,
if (c->tcp.conn_count >= TCP_MAX_CONNS)
return;
- if ((s = tcp_conn_new_sock(c, af)) < 0)
- return;
+ if ((s = tcp_conn_pool_sock(pool)) < 0)
+ if ((s = tcp_conn_new_sock(c, af)) < 0)
+ return;
if (!c->no_map_gw) {
if (af == AF_INET && IN4_ARE_ADDR_EQUAL(addr, &c->ip4.gw))
@@ -2989,20 +3002,10 @@ void tcp_sock_refill_pool(const struct ctx *c, int pool[], int af)
int i;
for (i = 0; i < TCP_SOCK_POOL_SIZE; i++) {
- int *s = &pool[i];
-
- if (*s >= 0)
+ if (pool[i] >= 0)
break;
- *s = socket(af, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
- if (*s > SOCKET_MAX) {
- close(*s);
- *s = -1;
- return;
- }
-
- if (*s >= 0)
- tcp_sock_set_bufsize(c, *s);
+ pool[i] = tcp_conn_new_sock(c, af);
}
}
diff --git a/tcp_conn.h b/tcp_conn.h
index 9951b0a..c807e8b 100644
--- a/tcp_conn.h
+++ b/tcp_conn.h
@@ -192,6 +192,7 @@ void tcp_splice_conn_update(struct ctx *c, struct tcp_splice_conn *new);
void tcp_table_compact(struct ctx *c, union tcp_conn *hole);
void tcp_splice_destroy(struct ctx *c, union tcp_conn *conn_union);
void tcp_splice_timer(struct ctx *c, union tcp_conn *conn_union);
+int tcp_conn_pool_sock(int pool[]);
void tcp_sock_refill_pool(const struct ctx *c, int pool[], int af);
void tcp_splice_refill(const struct ctx *c);
diff --git a/tcp_splice.c b/tcp_splice.c
index 688d776..5a88975 100644
--- a/tcp_splice.c
+++ b/tcp_splice.c
@@ -452,18 +452,14 @@ static int tcp_splice_connect_ns(void *arg)
static int tcp_splice_new(const struct ctx *c, struct tcp_splice_conn *conn,
in_port_t port, int outbound)
{
- int *p, i, s = -1;
+ int *p, s = -1;
if (outbound)
p = CONN_V6(conn) ? init_sock_pool6 : init_sock_pool4;
else
p = CONN_V6(conn) ? ns_sock_pool6 : ns_sock_pool4;
- for (i = 0; i < TCP_SOCK_POOL_SIZE; i++, p++) {
- SWAP(s, *p);
- if (s >= 0)
- break;
- }
+ s = tcp_conn_pool_sock(p);
/* No socket available in namespace: create a new one for connect() */
if (s < 0 && !outbound) {
--
@@ -452,18 +452,14 @@ static int tcp_splice_connect_ns(void *arg)
static int tcp_splice_new(const struct ctx *c, struct tcp_splice_conn *conn,
in_port_t port, int outbound)
{
- int *p, i, s = -1;
+ int *p, s = -1;
if (outbound)
p = CONN_V6(conn) ? init_sock_pool6 : init_sock_pool4;
else
p = CONN_V6(conn) ? ns_sock_pool6 : ns_sock_pool4;
- for (i = 0; i < TCP_SOCK_POOL_SIZE; i++, p++) {
- SWAP(s, *p);
- if (s >= 0)
- break;
- }
+ s = tcp_conn_pool_sock(p);
/* No socket available in namespace: create a new one for connect() */
if (s < 0 && !outbound) {
--
2.39.0
next prev parent reply other threads:[~2023-01-09 0:50 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-09 0:50 [PATCH 0/5] Cleanups to tcp socket pool handling David Gibson
2023-01-09 0:50 ` [PATCH 1/5] tcp: Make a helper to refill each socket pool David Gibson
2023-01-12 18:09 ` Stefano Brivio
2023-01-15 0:22 ` David Gibson
2023-01-09 0:50 ` [PATCH 2/5] tcp: Split init and ns cases for tcp_sock_refill() David Gibson
2023-01-09 0:50 ` [PATCH 3/5] tcp: Move socket pool declarations around David Gibson
2023-01-12 18:09 ` Stefano Brivio
2023-01-15 0:26 ` David Gibson
2023-01-09 0:50 ` David Gibson [this message]
2023-01-09 0:50 ` [PATCH 5/5] tcp: Improve handling of fallback if socket pool is empty on new splice David Gibson
2023-01-12 18:09 ` Stefano Brivio
2023-01-15 0:31 ` David Gibson
2023-01-23 17:47 ` Stefano Brivio
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230109005026.24512-5-david@gibson.dropbear.id.au \
--to=david@gibson.dropbear.id.au \
--cc=passt-dev@passt.top \
--cc=sbrivio@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).