This is an automated email from the ASF dual-hosted git repository. xiaoxiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit f207072121eee08c448c5b47f177094cf1fb3935 Author: Fotis Panagiotopoulos <f.pa...@amco.gr> AuthorDate: Tue Feb 7 14:59:36 2023 +0200 Improvements in usrsock connections allocation. --- .../arm/cxd56xx/spresense/configs/wifi/defconfig | 2 +- .../cxd56xx/spresense/configs/wifi_smp/defconfig | 2 +- .../stm32/stm32f4discovery/configs/wifi/defconfig | 2 +- include/nuttx/net/netconfig.h | 12 ------ net/usrsock/Kconfig | 41 +++++++++++++++++++-- net/usrsock/usrsock_conn.c | 43 +++++++++++++++++----- 6 files changed, 74 insertions(+), 28 deletions(-) diff --git a/boards/arm/cxd56xx/spresense/configs/wifi/defconfig b/boards/arm/cxd56xx/spresense/configs/wifi/defconfig index 241819e3e8..2a280f9ec6 100644 --- a/boards/arm/cxd56xx/spresense/configs/wifi/defconfig +++ b/boards/arm/cxd56xx/spresense/configs/wifi/defconfig @@ -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 diff --git a/boards/arm/cxd56xx/spresense/configs/wifi_smp/defconfig b/boards/arm/cxd56xx/spresense/configs/wifi_smp/defconfig index 679cd4b2eb..c90d757cd9 100644 --- a/boards/arm/cxd56xx/spresense/configs/wifi_smp/defconfig +++ b/boards/arm/cxd56xx/spresense/configs/wifi_smp/defconfig @@ -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 diff --git a/boards/arm/stm32/stm32f4discovery/configs/wifi/defconfig b/boards/arm/stm32/stm32f4discovery/configs/wifi/defconfig index a4c7134d82..549f12d456 100644 --- a/boards/arm/stm32/stm32f4discovery/configs/wifi/defconfig +++ b/boards/arm/stm32/stm32f4discovery/configs/wifi/defconfig @@ -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 diff --git a/include/nuttx/net/netconfig.h b/include/nuttx/net/netconfig.h index 732bc5d00f..aa04a949c0 100644 --- a/include/nuttx/net/netconfig.h +++ b/include/nuttx/net/netconfig.h @@ -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 ****************************************************************************/ diff --git a/net/usrsock/Kconfig b/net/usrsock/Kconfig index 136b053de6..f50a366721 100644 --- a/net/usrsock/Kconfig +++ b/net/usrsock/Kconfig @@ -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 diff --git a/net/usrsock/usrsock_conn.c b/net/usrsock/usrsock_conn.c index 5b4ae2463d..675a3ca3f7 100644 --- a/net/usrsock/usrsock_conn.c +++ b/net/usrsock/usrsock_conn.c @@ -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];