mirror of
https://github.com/apache/nuttx.git
synced 2026-06-02 01:21:26 +08:00
Improvements in Netlink connections allocation.
This commit is contained in:
committed by
Xiang Xiao
parent
74bb921a19
commit
cf15d6b63a
+38
-3
@@ -22,11 +22,46 @@ config NET_NETLINK
|
|||||||
|
|
||||||
if NET_NETLINK
|
if NET_NETLINK
|
||||||
|
|
||||||
config NETLINK_CONNS
|
config NETLINK_PREALLOC_CONNS
|
||||||
int "Number of Netlink connections"
|
int "Preallocated Netlink connections"
|
||||||
default 4
|
default 4
|
||||||
---help---
|
---help---
|
||||||
Maximum number of Netlink connections (all tasks).
|
Number of Netlink 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).
|
||||||
|
|
||||||
|
config NETLINK_ALLOC_CONNS
|
||||||
|
int "Dynamic Netlink connections allocation"
|
||||||
|
default 0
|
||||||
|
---help---
|
||||||
|
Dynamic memory allocations for Netlink.
|
||||||
|
|
||||||
|
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 NETLINK_MAX_CONNS
|
||||||
|
int "Maximum number of Netlink connections"
|
||||||
|
default 0
|
||||||
|
depends on NETLINK_ALLOC_CONNS > 0
|
||||||
|
---help---
|
||||||
|
If dynamic connections allocation is selected (NETLINK_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.
|
||||||
|
|
||||||
menu "Netlink Protocols"
|
menu "Netlink Protocols"
|
||||||
|
|
||||||
|
|||||||
+33
-10
@@ -51,8 +51,9 @@
|
|||||||
|
|
||||||
/* The array containing all NetLink connections. */
|
/* The array containing all NetLink connections. */
|
||||||
|
|
||||||
#ifndef CONFIG_NET_ALLOC_CONNS
|
#if CONFIG_NETLINK_PREALLOC_CONNS > 0
|
||||||
static struct netlink_conn_s g_netlink_connections[CONFIG_NETLINK_CONNS];
|
static struct netlink_conn_s
|
||||||
|
g_netlink_connections[CONFIG_NETLINK_PREALLOC_CONNS];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* A list of all free NetLink connections */
|
/* A list of all free NetLink connections */
|
||||||
@@ -106,10 +107,10 @@ static void netlink_response_available(FAR void *arg)
|
|||||||
|
|
||||||
void netlink_initialize(void)
|
void netlink_initialize(void)
|
||||||
{
|
{
|
||||||
#ifndef CONFIG_NET_ALLOC_CONNS
|
#if CONFIG_NETLINK_PREALLOC_CONNS > 0
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < CONFIG_NETLINK_CONNS; i++)
|
for (i = 0; i < CONFIG_NETLINK_PREALLOC_CONNS; i++)
|
||||||
{
|
{
|
||||||
/* Mark the connection closed and move it to the free list */
|
/* Mark the connection closed and move it to the free list */
|
||||||
|
|
||||||
@@ -131,20 +132,29 @@ void netlink_initialize(void)
|
|||||||
FAR struct netlink_conn_s *netlink_alloc(void)
|
FAR struct netlink_conn_s *netlink_alloc(void)
|
||||||
{
|
{
|
||||||
FAR struct netlink_conn_s *conn;
|
FAR struct netlink_conn_s *conn;
|
||||||
#ifdef CONFIG_NET_ALLOC_CONNS
|
#if CONFIG_NETLINK_ALLOC_CONNS > 0
|
||||||
int i;
|
int i;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* The free list is protected by a mutex. */
|
/* The free list is protected by a mutex. */
|
||||||
|
|
||||||
nxmutex_lock(&g_free_lock);
|
nxmutex_lock(&g_free_lock);
|
||||||
#ifdef CONFIG_NET_ALLOC_CONNS
|
#if CONFIG_NETLINK_ALLOC_CONNS > 0
|
||||||
if (dq_peek(&g_free_netlink_connections) == NULL)
|
if (dq_peek(&g_free_netlink_connections) == NULL)
|
||||||
{
|
{
|
||||||
conn = kmm_zalloc(sizeof(*conn) * CONFIG_NETLINK_CONNS);
|
#if CONFIG_NETLINK_MAX_CONNS > 0
|
||||||
|
if (dq_count(&g_active_netlink_connections) +
|
||||||
|
CONFIG_NETLINK_ALLOC_CONNS >= CONFIG_NETLINK_MAX_CONNS)
|
||||||
|
{
|
||||||
|
nxmutex_unlock(&g_free_lock);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
conn = kmm_zalloc(sizeof(*conn) * CONFIG_NETLINK_ALLOC_CONNS);
|
||||||
if (conn != NULL)
|
if (conn != NULL)
|
||||||
{
|
{
|
||||||
for (i = 0; i < CONFIG_NETLINK_CONNS; i++)
|
for (i = 0; i < CONFIG_NETLINK_ALLOC_CONNS; i++)
|
||||||
{
|
{
|
||||||
dq_addlast(&conn[i].sconn.node, &g_free_netlink_connections);
|
dq_addlast(&conn[i].sconn.node, &g_free_netlink_connections);
|
||||||
}
|
}
|
||||||
@@ -199,9 +209,22 @@ void netlink_free(FAR struct netlink_conn_s *conn)
|
|||||||
|
|
||||||
memset(conn, 0, sizeof(*conn));
|
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_NETLINK_ALLOC_CONNS == 1
|
||||||
|
if (conn < g_netlink_connections || conn >= (g_netlink_connections +
|
||||||
|
CONFIG_NETLINK_PREALLOC_CONNS))
|
||||||
|
{
|
||||||
|
kmm_free(conn);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
dq_addlast(&conn->sconn.node, &g_free_netlink_connections);
|
||||||
|
}
|
||||||
|
|
||||||
dq_addlast(&conn->sconn.node, &g_free_netlink_connections);
|
|
||||||
nxmutex_unlock(&g_free_lock);
|
nxmutex_unlock(&g_free_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user