Improvements in usrsock connections allocation.

This commit is contained in:
Fotis Panagiotopoulos
2023-02-07 14:59:36 +02:00
committed by Xiang Xiao
parent 7f3591b9cc
commit f207072121
6 changed files with 74 additions and 28 deletions
@@ -104,7 +104,7 @@ CONFIG_NETUTILS_TELNETD=y
CONFIG_NETUTILS_WEBCLIENT=y
CONFIG_NETUTILS_WEBSERVER=y
CONFIG_NET_USRSOCK=y
CONFIG_NET_USRSOCK_CONNS=16
CONFIG_NET_USRSOCK_PREALLOC_CONNS=16
CONFIG_NET_USRSOCK_UDP=y
CONFIG_NFS=y
CONFIG_NSH_ARCHINIT=y
@@ -107,7 +107,7 @@ CONFIG_NETUTILS_TELNETD=y
CONFIG_NETUTILS_WEBCLIENT=y
CONFIG_NETUTILS_WEBSERVER=y
CONFIG_NET_USRSOCK=y
CONFIG_NET_USRSOCK_CONNS=16
CONFIG_NET_USRSOCK_PREALLOC_CONNS=16
CONFIG_NET_USRSOCK_UDP=y
CONFIG_NFS=y
CONFIG_NSH_ARCHINIT=y
@@ -55,7 +55,7 @@ CONFIG_NETUTILS_TELNETD=y
CONFIG_NETUTILS_WEBCLIENT=y
CONFIG_NETUTILS_WEBSERVER=y
CONFIG_NET_USRSOCK=y
CONFIG_NET_USRSOCK_CONNS=16
CONFIG_NET_USRSOCK_PREALLOC_CONNS=16
CONFIG_NET_USRSOCK_UDP=y
CONFIG_NSH_ARCHINIT=y
CONFIG_NSH_BUILTIN_APPS=y
-12
View File
@@ -631,18 +631,6 @@
# define CONFIG_NET_ARP_MAXAGE 120
#endif
/* Usrsock configuration options */
/* The maximum amount of concurrent usrsock connections, Default: 6 */
#ifndef CONFIG_NET_USRSOCK_CONNS
# ifdef CONFIG_NET_USRSOCK
# define CONFIG_NET_USRSOCK_CONNS 6
# else
# define CONFIG_NET_USRSOCK_CONNS 0
# endif
#endif
/****************************************************************************
* Public Type Definitions
****************************************************************************/
+38 -3
View File
@@ -25,15 +25,50 @@ config NET_USRSOCK
if NET_USRSOCK
config NET_USRSOCK_CONNS
int "Number of usrsock connections"
config NET_USRSOCK_PREALLOC_CONNS
int "Preallocated usrsock connections"
default 6
---help---
Maximum number of usrsock connections (all tasks).
Number of usrsock connections (all tasks).
This number of connections will be pre-allocated during system boot.
If dynamic connections allocation is enabled, more connections may
be allocated at a later time, as the system needs them. Else this
will be the maximum number of connections available to the system
at all times.
Set to 0 to disable (and rely only on dynamic allocations).
Note: Usrsock daemon can impose additional restrictions for
maximum number of concurrent connections supported.
config NET_USRSOCK_ALLOC_CONNS
int "Dynamic usrsock connections allocation"
default 0
---help---
Dynamic memory allocations for usrsock.
When set to 0 all dynamic allocations are disabled.
When set to 1 a new connection will be allocated every time,
and it will be free'd when no longer needed.
Setting this to 2 or more will allocate the connections in
batches (with batch size equal to this config). When a
connection is no longer needed, it will be returned to the
free connections pool, and it will never be deallocated!
config NET_USRSOCK_MAX_CONNS
int "Maximum number of usrsock connections"
default 0
depends on NET_USRSOCK_ALLOC_CONNS > 0
---help---
If dynamic connections allocation is selected (NET_USRSOCK_ALLOC_CONNS > 0)
this will limit the number of connections that can be allocated.
This is useful in case the system is under very heavy load (or
under attack), ensuring that the heap will not be exhausted.
config NET_USRSOCK_NPOLLWAITERS
int "Number of usrsock poll waiters"
default 1
+33 -10
View File
@@ -48,8 +48,9 @@
/* The array containing all usrsock connections. */
#ifndef CONFIG_NET_ALLOC_CONNS
static struct usrsock_conn_s g_usrsock_connections[CONFIG_NET_USRSOCK_CONNS];
#if CONFIG_NET_USRSOCK_PREALLOC_CONNS > 0
static struct usrsock_conn_s
g_usrsock_connections[CONFIG_NET_USRSOCK_PREALLOC_CONNS];
#endif
/* A list of all free usrsock connections */
@@ -77,20 +78,29 @@ static dq_queue_t g_active_usrsock_connections;
FAR struct usrsock_conn_s *usrsock_alloc(void)
{
FAR struct usrsock_conn_s *conn;
#ifdef CONFIG_NET_ALLOC_CONNS
#if CONFIG_NET_USRSOCK_ALLOC_CONNS > 0
int i;
#endif
/* The free list is protected by a a mutex. */
nxmutex_lock(&g_free_lock);
#ifdef CONFIG_NET_ALLOC_CONNS
#if CONFIG_NET_USRSOCK_ALLOC_CONNS > 0
if (dq_peek(&g_free_usrsock_connections) == NULL)
{
conn = kmm_zalloc(sizeof(*conn) * CONFIG_NET_USRSOCK_CONNS);
#if CONFIG_NET_USRSOCK_MAX_CONNS > 0
if (dq_count(&g_active_usrsock_connections) +
CONFIG_NET_USRSOCK_ALLOC_CONNS >= CONFIG_NET_USRSOCK_MAX_CONNS)
{
nxmutex_unlock(&g_free_lock);
return NULL;
}
#endif
conn = kmm_zalloc(sizeof(*conn) * CONFIG_NET_USRSOCK_ALLOC_CONNS);
if (conn != NULL)
{
for (i = 0; i < CONFIG_NET_USRSOCK_CONNS; i++)
for (i = 0; i < CONFIG_NET_USRSOCK_ALLOC_CONNS; i++)
{
dq_addlast(&conn[i].sconn.node, &g_free_usrsock_connections);
}
@@ -143,9 +153,22 @@ void usrsock_free(FAR struct usrsock_conn_s *conn)
nxsem_destroy(&conn->resp.sem);
memset(conn, 0, sizeof(*conn));
/* Free the connection */
/* If this is a preallocated or a batch allocated connection store it in
* the free connections list. Else free it.
*/
#if CONFIG_NET_USRSOCK_ALLOC_CONNS == 1
if (conn < g_usrsock_connections || conn >= (g_usrsock_connections +
CONFIG_NET_USRSOCK_PREALLOC_CONNS))
{
kmm_free(conn);
}
else
#endif
{
dq_addlast(&conn->sconn.node, &g_free_usrsock_connections);
}
dq_addlast(&conn->sconn.node, &g_free_usrsock_connections);
nxmutex_unlock(&g_free_lock);
}
@@ -309,11 +332,11 @@ void usrsock_setup_datain(FAR struct usrsock_conn_s *conn,
void usrsock_initialize(void)
{
#ifndef CONFIG_NET_ALLOC_CONNS
#if CONFIG_NET_USRSOCK_PREALLOC_CONNS > 0
FAR struct usrsock_conn_s *conn;
int i;
for (i = 0; i < CONFIG_NET_USRSOCK_CONNS; i++)
for (i = 0; i < CONFIG_NET_USRSOCK_PREALLOC_CONNS; i++)
{
conn = &g_usrsock_connections[i];