net: Add buffer pool to replace connection allocation

Our net socket connection allocations are powerful but redundant
because they're implemented once in each protocol.  This is not good for
further optimizing and extending to other allocations, so maybe we can
add a common implementation for the usage.

Impact:
1. We add a `struct net_bufpool_s` as pool descriptor, which may use a
   little bit more memory than previous implementation (~28Bytes).
2. We share same functions between pools, so code size may shrink under
   some scenarios.

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
This commit is contained in:
Zhe Weng
2024-12-17 18:29:14 +08:00
committed by Alan C. Assis
parent badb5c5ac6
commit 1fe07d0838
15 changed files with 437 additions and 724 deletions
+15 -90
View File
@@ -74,19 +74,23 @@
#include "udp/udp.h"
#include "utils/utils.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifndef CONFIG_NET_UDP_MAX_CONNS
# define CONFIG_NET_UDP_MAX_CONNS 0
#endif
/****************************************************************************
* Private Data
****************************************************************************/
/* The array containing all UDP connections. */
#if CONFIG_NET_UDP_PREALLOC_CONNS > 0
static struct udp_conn_s g_udp_connections[CONFIG_NET_UDP_PREALLOC_CONNS];
#endif
/* A list of all free UDP connections */
static dq_queue_t g_free_udp_connections;
NET_BUFPOOL_DECLARE(g_udp_connections, sizeof(struct udp_conn_s),
CONFIG_NET_UDP_PREALLOC_CONNS,
CONFIG_NET_UDP_ALLOC_CONNS, CONFIG_NET_UDP_MAX_CONNS);
static mutex_t g_free_lock = NXMUTEX_INITIALIZER;
/* A list of all allocated UDP connections */
@@ -460,54 +464,6 @@ udp_ipv6_active(FAR struct net_driver_s *dev, FAR struct udp_conn_s *conn,
}
#endif /* CONFIG_NET_IPv6 */
/****************************************************************************
* Name: udp_alloc_conn
*
* Description:
* Allocate a uninitialized UDP connection structure.
*
****************************************************************************/
#if CONFIG_NET_UDP_ALLOC_CONNS > 0
static FAR struct udp_conn_s *udp_alloc_conn(void)
{
FAR struct udp_conn_s *conn;
int i;
/* Return the entry from the head of the free list */
if (dq_peek(&g_free_udp_connections) == NULL)
{
#if CONFIG_NET_UDP_MAX_CONNS > 0
if (dq_count(&g_active_udp_connections) +
CONFIG_NET_UDP_ALLOC_CONNS > CONFIG_NET_UDP_MAX_CONNS)
{
return NULL;
}
#endif
conn = kmm_zalloc(sizeof(struct udp_conn_s) *
CONFIG_NET_UDP_ALLOC_CONNS);
if (conn == NULL)
{
return conn;
}
/* Now initialize each connection structure */
for (i = 0; i < CONFIG_NET_UDP_ALLOC_CONNS; i++)
{
/* Mark the connection closed and move it to the free list */
conn[i].lport = 0;
dq_addlast(&conn[i].sconn.node, &g_free_udp_connections);
}
}
return (FAR struct udp_conn_s *)dq_remfirst(&g_free_udp_connections);
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -593,17 +549,7 @@ errout:
void udp_initialize(void)
{
#if CONFIG_NET_UDP_PREALLOC_CONNS > 0
int i;
for (i = 0; i < CONFIG_NET_UDP_PREALLOC_CONNS; i++)
{
/* Mark the connection closed and move it to the free list */
g_udp_connections[i].lport = 0;
dq_addlast(&g_udp_connections[i].sconn.node, &g_free_udp_connections);
}
#endif
NET_BUFPOOL_INIT(g_udp_connections);
}
/****************************************************************************
@@ -623,14 +569,7 @@ FAR struct udp_conn_s *udp_alloc(uint8_t domain)
nxmutex_lock(&g_free_lock);
conn = (FAR struct udp_conn_s *)dq_remfirst(&g_free_udp_connections);
#if CONFIG_NET_UDP_ALLOC_CONNS > 0
if (conn == NULL)
{
conn = udp_alloc_conn();
}
#endif
conn = NET_BUFPOOL_TRYALLOC(g_udp_connections);
if (conn)
{
@@ -712,23 +651,9 @@ void udp_free(FAR struct udp_conn_s *conn)
#endif
/* Free the connection.
* If this is a preallocated or a batch allocated connection store it in
* the free connections list. Else free it.
*/
/* Free the connection. */
#if CONFIG_NET_UDP_ALLOC_CONNS == 1
if (conn < g_udp_connections || conn >= (g_udp_connections +
CONFIG_NET_UDP_PREALLOC_CONNS))
{
kmm_free(conn);
}
else
#endif
{
memset(conn, 0, sizeof(*conn));
dq_addlast(&conn->sconn.node, &g_free_udp_connections);
}
NET_BUFPOOL_FREE(g_udp_connections, conn);
nxmutex_unlock(&g_free_lock);
}