diff --git a/net/netlink/Kconfig b/net/netlink/Kconfig index d92efeff03..0a07a04364 100644 --- a/net/netlink/Kconfig +++ b/net/netlink/Kconfig @@ -22,11 +22,46 @@ config NET_NETLINK if NET_NETLINK -config NETLINK_CONNS - int "Number of Netlink connections" +config NETLINK_PREALLOC_CONNS + int "Preallocated Netlink connections" default 4 ---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" diff --git a/net/netlink/netlink_conn.c b/net/netlink/netlink_conn.c index 772b733eb9..443e004b0b 100644 --- a/net/netlink/netlink_conn.c +++ b/net/netlink/netlink_conn.c @@ -51,8 +51,9 @@ /* The array containing all NetLink connections. */ -#ifndef CONFIG_NET_ALLOC_CONNS -static struct netlink_conn_s g_netlink_connections[CONFIG_NETLINK_CONNS]; +#if CONFIG_NETLINK_PREALLOC_CONNS > 0 +static struct netlink_conn_s + g_netlink_connections[CONFIG_NETLINK_PREALLOC_CONNS]; #endif /* A list of all free NetLink connections */ @@ -106,10 +107,10 @@ static void netlink_response_available(FAR void *arg) void netlink_initialize(void) { -#ifndef CONFIG_NET_ALLOC_CONNS +#if CONFIG_NETLINK_PREALLOC_CONNS > 0 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 */ @@ -131,20 +132,29 @@ void netlink_initialize(void) FAR struct netlink_conn_s *netlink_alloc(void) { FAR struct netlink_conn_s *conn; -#ifdef CONFIG_NET_ALLOC_CONNS +#if CONFIG_NETLINK_ALLOC_CONNS > 0 int i; #endif /* The free list is protected by a mutex. */ nxmutex_lock(&g_free_lock); -#ifdef CONFIG_NET_ALLOC_CONNS +#if CONFIG_NETLINK_ALLOC_CONNS > 0 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) { - 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); } @@ -199,9 +209,22 @@ void netlink_free(FAR struct netlink_conn_s *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); }