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 74bb921a19f2ea710f53745ae428077ddd462c01 Author: Fotis Panagiotopoulos <f.pa...@amco.gr> AuthorDate: Mon Feb 13 15:05:40 2023 +0200 Improvements in packet connections allocation. --- net/pkt/Kconfig | 41 +++++++++++++++++++++++++++++++++++++++-- net/pkt/pkt_conn.c | 42 ++++++++++++++++++++++++++++++++---------- 2 files changed, 71 insertions(+), 12 deletions(-) diff --git a/net/pkt/Kconfig b/net/pkt/Kconfig index 578cf69267..282a9429a9 100644 --- a/net/pkt/Kconfig +++ b/net/pkt/Kconfig @@ -22,9 +22,46 @@ config NET_PKT if NET_PKT -config NET_PKT_CONNS - int "Max packet sockets" +config NET_PKT_PREALLOC_CONNS + int "Preallocated packet sockets" default 1 + ---help--- + Number of packet 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 NET_PKT_ALLOC_CONNS + int "Dynamic packet connections allocation" + default 0 + ---help--- + Dynamic memory allocations for packet sockets. + + 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_PKT_MAX_CONNS + int "Maximum number of packet connections" + default 0 + depends on NET_PKT_ALLOC_CONNS > 0 + ---help--- + If dynamic connections allocation is selected (NET_PKT_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. endif # NET_PKT endmenu # Raw Socket Support diff --git a/net/pkt/pkt_conn.c b/net/pkt/pkt_conn.c index 5392861d13..a7663a764e 100644 --- a/net/pkt/pkt_conn.c +++ b/net/pkt/pkt_conn.c @@ -56,8 +56,8 @@ /* The array containing all packet socket connections */ -#ifndef CONFIG_NET_ALLOC_CONNS -static struct pkt_conn_s g_pkt_connections[CONFIG_NET_PKT_CONNS]; +#if CONFIG_NET_PKT_PREALLOC_CONNS > 0 +static struct pkt_conn_s g_pkt_connections[CONFIG_NET_PKT_PREALLOC_CONNS]; #endif /* A list of all free packet socket connections */ @@ -84,10 +84,10 @@ static dq_queue_t g_active_pkt_connections; void pkt_initialize(void) { -#ifndef CONFIG_NET_ALLOC_CONNS +#if CONFIG_NET_PKT_PREALLOC_CONNS > 0 int i; - for (i = 0; i < CONFIG_NET_PKT_CONNS; i++) + for (i = 0; i < CONFIG_NET_PKT_PREALLOC_CONNS; i++) { dq_addlast(&g_pkt_connections[i].sconn.node, &g_free_pkt_connections); } @@ -106,20 +106,29 @@ void pkt_initialize(void) FAR struct pkt_conn_s *pkt_alloc(void) { FAR struct pkt_conn_s *conn; -#ifdef CONFIG_NET_ALLOC_CONNS +#if CONFIG_NET_PKT_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_NET_PKT_ALLOC_CONNS > 0 if (dq_peek(&g_free_pkt_connections) == NULL) { - conn = kmm_zalloc(sizeof(*conn) * CONFIG_NET_PKT_CONNS); +#if CONFIG_NET_PKT_MAX_CONNS > 0 + if (dq_count(&g_active_pkt_connections) + CONFIG_NET_PKT_ALLOC_CONNS + >= CONFIG_NET_PKT_MAX_CONNS) + { + nxmutex_unlock(&g_free_lock); + return NULL; + } +#endif + + conn = kmm_zalloc(sizeof(*conn) * CONFIG_NET_PKT_ALLOC_CONNS); if (conn != NULL) { - for (i = 0; i < CONFIG_NET_PKT_CONNS; i++) + for (i = 0; i < CONFIG_NET_PKT_ALLOC_CONNS; i++) { dq_addlast(&conn[i].sconn.node, &g_free_pkt_connections); } @@ -164,9 +173,22 @@ void pkt_free(FAR struct pkt_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_NET_PKT_ALLOC_CONNS == 1 + if (conn < g_pkt_connections || conn >= (g_pkt_connections + + CONFIG_NET_PKT_PREALLOC_CONNS)) + { + kmm_free(conn); + } + else +#endif + { + dq_addlast(&conn->sconn.node, &g_free_pkt_connections); + } - dq_addlast(&conn->sconn.node, &g_free_pkt_connections); nxmutex_unlock(&g_free_lock); }