tcp: Split pool lookup from creating new sockets in tcp_conn_new_sock()
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> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
parent
912d37cd5b
commit
dc467d526f
3 changed files with 31 additions and 31 deletions
53
tcp.c
53
tcp.c
|
@ -1858,24 +1858,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
|
* 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 pool is empty
|
||||||
|
*/
|
||||||
|
int tcp_conn_pool_sock(int pool[])
|
||||||
|
{
|
||||||
|
int s = -1, i;
|
||||||
|
|
||||||
|
for (i = 0; i < TCP_SOCK_POOL_SIZE; i++) {
|
||||||
|
SWAP(s, pool[i]);
|
||||||
|
if (s >= 0)
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* tcp_conn_new_sock() - Open and prepare new socket for connection
|
||||||
* @c: Execution context
|
* @c: Execution context
|
||||||
* @af: Address family
|
* @af: Address family
|
||||||
*
|
*
|
||||||
* Return: socket number if available, negative code if socket creation failed
|
* 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)
|
static int tcp_conn_new_sock(const struct ctx *c, sa_family_t af)
|
||||||
{
|
{
|
||||||
int *p = af == AF_INET6 ? init_sock_pool6 : init_sock_pool4, i, s = -1;
|
int s;
|
||||||
|
|
||||||
for (i = 0; i < TCP_SOCK_POOL_SIZE; i++, p++) {
|
s = socket(af, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
|
||||||
SWAP(s, *p);
|
|
||||||
if (s >= 0)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (s < 0)
|
|
||||||
s = socket(af, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
|
|
||||||
|
|
||||||
if (s > SOCKET_MAX) {
|
if (s > SOCKET_MAX) {
|
||||||
close(s);
|
close(s);
|
||||||
|
@ -1936,6 +1947,7 @@ static void tcp_conn_from_tap(struct ctx *c, int af, const void *addr,
|
||||||
const struct tcphdr *th, const char *opts,
|
const struct tcphdr *th, const char *opts,
|
||||||
size_t optlen, const struct timespec *now)
|
size_t optlen, const struct timespec *now)
|
||||||
{
|
{
|
||||||
|
int *pool = af == AF_INET6 ? init_sock_pool6 : init_sock_pool4;
|
||||||
struct sockaddr_in addr4 = {
|
struct sockaddr_in addr4 = {
|
||||||
.sin_family = AF_INET,
|
.sin_family = AF_INET,
|
||||||
.sin_port = th->dest,
|
.sin_port = th->dest,
|
||||||
|
@ -1954,8 +1966,9 @@ static void tcp_conn_from_tap(struct ctx *c, int af, const void *addr,
|
||||||
if (c->tcp.conn_count >= TCP_MAX_CONNS)
|
if (c->tcp.conn_count >= TCP_MAX_CONNS)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if ((s = tcp_conn_new_sock(c, af)) < 0)
|
if ((s = tcp_conn_pool_sock(pool)) < 0)
|
||||||
return;
|
if ((s = tcp_conn_new_sock(c, af)) < 0)
|
||||||
|
return;
|
||||||
|
|
||||||
if (!c->no_map_gw) {
|
if (!c->no_map_gw) {
|
||||||
if (af == AF_INET && IN4_ARE_ADDR_EQUAL(addr, &c->ip4.gw))
|
if (af == AF_INET && IN4_ARE_ADDR_EQUAL(addr, &c->ip4.gw))
|
||||||
|
@ -3016,20 +3029,10 @@ void tcp_sock_refill_pool(const struct ctx *c, int pool[], int af)
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < TCP_SOCK_POOL_SIZE; i++) {
|
for (i = 0; i < TCP_SOCK_POOL_SIZE; i++) {
|
||||||
int *s = &pool[i];
|
if (pool[i] >= 0)
|
||||||
|
|
||||||
if (*s >= 0)
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
*s = socket(af, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
|
pool[i] = tcp_conn_new_sock(c, af);
|
||||||
if (*s > SOCKET_MAX) {
|
|
||||||
close(*s);
|
|
||||||
*s = -1;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (*s >= 0)
|
|
||||||
tcp_sock_set_bufsize(c, *s);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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_table_compact(struct ctx *c, union tcp_conn *hole);
|
||||||
void tcp_splice_destroy(struct ctx *c, union tcp_conn *conn_union);
|
void tcp_splice_destroy(struct ctx *c, union tcp_conn *conn_union);
|
||||||
void tcp_splice_timer(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_sock_refill_pool(const struct ctx *c, int pool[], int af);
|
||||||
void tcp_splice_refill(const struct ctx *c);
|
void tcp_splice_refill(const struct ctx *c);
|
||||||
|
|
||||||
|
|
|
@ -451,18 +451,14 @@ static int tcp_splice_connect_ns(void *arg)
|
||||||
static int tcp_splice_new(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, int outbound)
|
in_port_t port, int outbound)
|
||||||
{
|
{
|
||||||
int *p, i, s = -1;
|
int *p, s = -1;
|
||||||
|
|
||||||
if (outbound)
|
if (outbound)
|
||||||
p = CONN_V6(conn) ? init_sock_pool6 : init_sock_pool4;
|
p = CONN_V6(conn) ? init_sock_pool6 : init_sock_pool4;
|
||||||
else
|
else
|
||||||
p = CONN_V6(conn) ? ns_sock_pool6 : ns_sock_pool4;
|
p = CONN_V6(conn) ? ns_sock_pool6 : ns_sock_pool4;
|
||||||
|
|
||||||
for (i = 0; i < TCP_SOCK_POOL_SIZE; i++, p++) {
|
s = tcp_conn_pool_sock(p);
|
||||||
SWAP(s, *p);
|
|
||||||
if (s >= 0)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* No socket available in namespace: create a new one for connect() */
|
/* No socket available in namespace: create a new one for connect() */
|
||||||
if (s < 0 && !outbound) {
|
if (s < 0 && !outbound) {
|
||||||
|
|
Loading…
Reference in a new issue