You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2023/02/20 01:07:03 UTC

[nuttx] 10/12: Improvements in sockets allocation.

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 9b4d784307925b39c59f0feaedeee7ade7070012
Author: Fotis Panagiotopoulos <f....@amco.gr>
AuthorDate: Mon Feb 13 18:30:21 2023 +0200

    Improvements in sockets allocation.
---
 .../lincoln60/configs/thttpd-binfs/defconfig       |  2 +-
 .../stm32/nucleo-f302r8/configs/cansock/defconfig  |  2 +-
 .../esp32/esp32-devkitc/configs/audio/defconfig    |  2 +-
 .../esp32/esp32-devkitc/configs/blewifi/defconfig  |  2 +-
 .../esp32/esp32-devkitc/configs/coex/defconfig     |  2 +-
 .../esp32/esp32-devkitc/configs/efuse/defconfig    |  2 +-
 .../esp32/esp32-devkitc/configs/nxlooper/defconfig |  2 +-
 .../esp32/esp32-devkitc/configs/wapi/defconfig     |  2 +-
 .../esp32/esp32-lyrat/configs/audio/defconfig      |  2 +-
 .../esp32/esp32-lyrat/configs/wapi/defconfig       |  2 +-
 include/nuttx/net/netconfig.h                      | 10 ------
 net/devif/devif_callback.c                         | 38 +++++++++++++++-------
 net/socket/Kconfig                                 | 31 +++++++++++++++---
 13 files changed, 63 insertions(+), 36 deletions(-)

diff --git a/boards/arm/lpc17xx_40xx/lincoln60/configs/thttpd-binfs/defconfig b/boards/arm/lpc17xx_40xx/lincoln60/configs/thttpd-binfs/defconfig
index a1f7992624..e90ee1dedc 100644
--- a/boards/arm/lpc17xx_40xx/lincoln60/configs/thttpd-binfs/defconfig
+++ b/boards/arm/lpc17xx_40xx/lincoln60/configs/thttpd-binfs/defconfig
@@ -44,7 +44,7 @@ CONFIG_NET_ARP_SEND=y
 CONFIG_NET_ICMP=y
 CONFIG_NET_ICMP_SOCKET=y
 CONFIG_NET_MAX_LISTENPORTS=8
-CONFIG_NET_NACTIVESOCKETS=12
+CONFIG_NET_PREALLOC_DEVIF_CALLBACKS=12
 CONFIG_NET_SOCKOPTS=y
 CONFIG_NET_TCP=y
 CONFIG_NET_TCPBACKLOG=y
diff --git a/boards/arm/stm32/nucleo-f302r8/configs/cansock/defconfig b/boards/arm/stm32/nucleo-f302r8/configs/cansock/defconfig
index 2553dea628..baf1572ab7 100644
--- a/boards/arm/stm32/nucleo-f302r8/configs/cansock/defconfig
+++ b/boards/arm/stm32/nucleo-f302r8/configs/cansock/defconfig
@@ -35,7 +35,7 @@ CONFIG_NETDEV_IFINDEX=y
 CONFIG_NETDEV_LATEINIT=y
 CONFIG_NET_CAN=y
 CONFIG_NET_CAN_ERRORS=y
-CONFIG_NET_NACTIVESOCKETS=16
+CONFIG_NET_PREALLOC_DEVIF_CALLBACKS=16
 CONFIG_NET_SOCKOPTS=y
 CONFIG_NET_STATISTICS=y
 CONFIG_NSH_BUILTIN_APPS=y
diff --git a/boards/xtensa/esp32/esp32-devkitc/configs/audio/defconfig b/boards/xtensa/esp32/esp32-devkitc/configs/audio/defconfig
index bf62e35fa4..8fbc8481b3 100644
--- a/boards/xtensa/esp32/esp32-devkitc/configs/audio/defconfig
+++ b/boards/xtensa/esp32/esp32-devkitc/configs/audio/defconfig
@@ -83,7 +83,7 @@ CONFIG_NET_BROADCAST=y
 CONFIG_NET_ETH_PKTSIZE=1518
 CONFIG_NET_ICMP=y
 CONFIG_NET_ICMP_SOCKET=y
-CONFIG_NET_NACTIVESOCKETS=32
+CONFIG_NET_PREALLOC_DEVIF_CALLBACKS=32
 CONFIG_NET_STATISTICS=y
 CONFIG_NET_TCP=y
 CONFIG_NET_TCP_DELAYED_ACK=y
diff --git a/boards/xtensa/esp32/esp32-devkitc/configs/blewifi/defconfig b/boards/xtensa/esp32/esp32-devkitc/configs/blewifi/defconfig
index 3b7f1bfa56..8a67773340 100644
--- a/boards/xtensa/esp32/esp32-devkitc/configs/blewifi/defconfig
+++ b/boards/xtensa/esp32/esp32-devkitc/configs/blewifi/defconfig
@@ -57,7 +57,7 @@ CONFIG_NET_BROADCAST=y
 CONFIG_NET_ETH_PKTSIZE=1518
 CONFIG_NET_ICMP=y
 CONFIG_NET_ICMP_SOCKET=y
-CONFIG_NET_NACTIVESOCKETS=32
+CONFIG_NET_PREALLOC_DEVIF_CALLBACKS=32
 CONFIG_NET_STATISTICS=y
 CONFIG_NET_TCP=y
 CONFIG_NET_TCP_DELAYED_ACK=y
diff --git a/boards/xtensa/esp32/esp32-devkitc/configs/coex/defconfig b/boards/xtensa/esp32/esp32-devkitc/configs/coex/defconfig
index e3d402c280..679db90c1f 100644
--- a/boards/xtensa/esp32/esp32-devkitc/configs/coex/defconfig
+++ b/boards/xtensa/esp32/esp32-devkitc/configs/coex/defconfig
@@ -110,7 +110,7 @@ CONFIG_NET_BROADCAST=y
 CONFIG_NET_ETH_PKTSIZE=1518
 CONFIG_NET_ICMP=y
 CONFIG_NET_ICMP_SOCKET=y
-CONFIG_NET_NACTIVESOCKETS=32
+CONFIG_NET_PREALLOC_DEVIF_CALLBACKS=32
 CONFIG_NET_STATISTICS=y
 CONFIG_NET_TCP=y
 CONFIG_NET_TCP_DELAYED_ACK=y
diff --git a/boards/xtensa/esp32/esp32-devkitc/configs/efuse/defconfig b/boards/xtensa/esp32/esp32-devkitc/configs/efuse/defconfig
index 105ac17b9a..3f847f4a3c 100644
--- a/boards/xtensa/esp32/esp32-devkitc/configs/efuse/defconfig
+++ b/boards/xtensa/esp32/esp32-devkitc/configs/efuse/defconfig
@@ -64,7 +64,7 @@ CONFIG_NET_BROADCAST=y
 CONFIG_NET_ETH_PKTSIZE=1518
 CONFIG_NET_ICMP=y
 CONFIG_NET_ICMP_SOCKET=y
-CONFIG_NET_NACTIVESOCKETS=32
+CONFIG_NET_PREALLOC_DEVIF_CALLBACKS=32
 CONFIG_NET_STATISTICS=y
 CONFIG_NET_TCP=y
 CONFIG_NET_TCP_DELAYED_ACK=y
diff --git a/boards/xtensa/esp32/esp32-devkitc/configs/nxlooper/defconfig b/boards/xtensa/esp32/esp32-devkitc/configs/nxlooper/defconfig
index b1f9aa5523..9817112f82 100644
--- a/boards/xtensa/esp32/esp32-devkitc/configs/nxlooper/defconfig
+++ b/boards/xtensa/esp32/esp32-devkitc/configs/nxlooper/defconfig
@@ -84,7 +84,7 @@ CONFIG_NET_BROADCAST=y
 CONFIG_NET_ETH_PKTSIZE=1518
 CONFIG_NET_ICMP=y
 CONFIG_NET_ICMP_SOCKET=y
-CONFIG_NET_NACTIVESOCKETS=32
+CONFIG_NET_PREALLOC_DEVIF_CALLBACKS=32
 CONFIG_NET_STATISTICS=y
 CONFIG_NET_TCP=y
 CONFIG_NET_TCP_DELAYED_ACK=y
diff --git a/boards/xtensa/esp32/esp32-devkitc/configs/wapi/defconfig b/boards/xtensa/esp32/esp32-devkitc/configs/wapi/defconfig
index b385c548ab..0cc0d9a5ea 100644
--- a/boards/xtensa/esp32/esp32-devkitc/configs/wapi/defconfig
+++ b/boards/xtensa/esp32/esp32-devkitc/configs/wapi/defconfig
@@ -52,7 +52,7 @@ CONFIG_NET_BROADCAST=y
 CONFIG_NET_ETH_PKTSIZE=1518
 CONFIG_NET_ICMP=y
 CONFIG_NET_ICMP_SOCKET=y
-CONFIG_NET_NACTIVESOCKETS=32
+CONFIG_NET_PREALLOC_DEVIF_CALLBACKS=32
 CONFIG_NET_STATISTICS=y
 CONFIG_NET_TCP=y
 CONFIG_NET_TCP_DELAYED_ACK=y
diff --git a/boards/xtensa/esp32/esp32-lyrat/configs/audio/defconfig b/boards/xtensa/esp32/esp32-lyrat/configs/audio/defconfig
index 0ada8d4578..a7bf4aafe5 100644
--- a/boards/xtensa/esp32/esp32-lyrat/configs/audio/defconfig
+++ b/boards/xtensa/esp32/esp32-lyrat/configs/audio/defconfig
@@ -83,7 +83,7 @@ CONFIG_NET_BROADCAST=y
 CONFIG_NET_ETH_PKTSIZE=1518
 CONFIG_NET_ICMP=y
 CONFIG_NET_ICMP_SOCKET=y
-CONFIG_NET_NACTIVESOCKETS=32
+CONFIG_NET_PREALLOC_DEVIF_CALLBACKS=32
 CONFIG_NET_STATISTICS=y
 CONFIG_NET_TCP=y
 CONFIG_NET_TCP_DELAYED_ACK=y
diff --git a/boards/xtensa/esp32/esp32-lyrat/configs/wapi/defconfig b/boards/xtensa/esp32/esp32-lyrat/configs/wapi/defconfig
index 85a3511a70..790ed9c53c 100644
--- a/boards/xtensa/esp32/esp32-lyrat/configs/wapi/defconfig
+++ b/boards/xtensa/esp32/esp32-lyrat/configs/wapi/defconfig
@@ -52,7 +52,7 @@ CONFIG_NET_BROADCAST=y
 CONFIG_NET_ETH_PKTSIZE=1518
 CONFIG_NET_ICMP=y
 CONFIG_NET_ICMP_SOCKET=y
-CONFIG_NET_NACTIVESOCKETS=32
+CONFIG_NET_PREALLOC_DEVIF_CALLBACKS=32
 CONFIG_NET_STATISTICS=y
 CONFIG_NET_TCP=y
 CONFIG_NET_TCP_DELAYED_ACK=y
diff --git a/include/nuttx/net/netconfig.h b/include/nuttx/net/netconfig.h
index aa04a949c0..1573a3588e 100644
--- a/include/nuttx/net/netconfig.h
+++ b/include/nuttx/net/netconfig.h
@@ -415,16 +415,6 @@
 #  define CONFIG_NET_MAX_LISTENPORTS 20
 #endif
 
-/* Define the maximum number of concurrently active UDP and TCP
- * ports.  This number must be greater than the number of open
- * sockets in order to support multi-threaded read/write operations.
- */
-
-#ifndef CONFIG_NET_NACTIVESOCKETS
-#  define CONFIG_NET_NACTIVESOCKETS (CONFIG_NET_TCP_PREALLOC_CONNS + \
-                                     CONFIG_NET_UDP_PREALLOC_CONNS)
-#endif
-
 /* The initial retransmission timeout counted in timer pulses.
  * REVISIT:  TCP RTO really should be calculated dynamically for each TCP
  * connection.
diff --git a/net/devif/devif_callback.c b/net/devif/devif_callback.c
index 68ce2aece4..fac4d5dcbf 100644
--- a/net/devif/devif_callback.c
+++ b/net/devif/devif_callback.c
@@ -43,8 +43,9 @@
  * Private Data
  ****************************************************************************/
 
-#ifndef CONFIG_NET_ALLOC_CONNS
-static struct devif_callback_s g_cbprealloc[CONFIG_NET_NACTIVESOCKETS];
+#if CONFIG_NET_PREALLOC_DEVIF_CALLBACKS > 0
+static struct devif_callback_s
+  g_cbprealloc[CONFIG_NET_PREALLOC_DEVIF_CALLBACKS];
 #endif
 static FAR struct devif_callback_s *g_cbfreelist = NULL;
 
@@ -164,11 +165,24 @@ static void devif_callback_free(FAR struct net_driver_s *dev,
             }
         }
 
-      /* Put the structure into the free list */
+      /* If this is a preallocated or a batch allocated callback store it in
+       * the free callbacks list. Else free it.
+       */
+
+#if CONFIG_NET_ALLOC_DEVIF_CALLBACKS == 1
+      if (cb < g_cbprealloc || cb >= (g_cbprealloc +
+          CONFIG_NET_PREALLOC_DEVIF_CALLBACKS))
+        {
+          kmm_free(cb);
+        }
+      else
+#endif
+        {
+          cb->nxtconn  = g_cbfreelist;
+          cb->nxtdev   = NULL;
+          g_cbfreelist = cb;
+        }
 
-      cb->nxtconn  = g_cbfreelist;
-      cb->nxtdev   = NULL;
-      g_cbfreelist = cb;
       net_unlock();
     }
 }
@@ -230,10 +244,10 @@ static bool devif_event_trigger(uint16_t events, uint16_t triggers)
 
 void devif_callback_init(void)
 {
-#ifndef CONFIG_NET_ALLOC_CONNS
+#if CONFIG_NET_PREALLOC_DEVIF_CALLBACKS > 0
   int i;
 
-  for (i = 0; i < CONFIG_NET_NACTIVESOCKETS; i++)
+  for (i = 0; i < CONFIG_NET_PREALLOC_DEVIF_CALLBACKS; i++)
     {
       g_cbprealloc[i].nxtconn = g_cbfreelist;
       g_cbfreelist = &g_cbprealloc[i];
@@ -263,7 +277,7 @@ FAR struct devif_callback_s *
                        FAR struct devif_callback_s **list_tail)
 {
   FAR struct devif_callback_s *ret;
-#ifdef CONFIG_NET_ALLOC_CONNS
+#if CONFIG_NET_ALLOC_DEVIF_CALLBACKS > 0
   int i;
 #endif
 
@@ -290,14 +304,14 @@ FAR struct devif_callback_s *
 
   /* Allocate the callback entry from heap */
 
-#ifdef CONFIG_NET_ALLOC_CONNS
+#if CONFIG_NET_ALLOC_DEVIF_CALLBACKS > 0
   if (g_cbfreelist == NULL)
     {
       ret = kmm_zalloc(sizeof(struct devif_callback_s) *
-                       CONFIG_NET_NACTIVESOCKETS);
+                       CONFIG_NET_ALLOC_DEVIF_CALLBACKS);
       if (ret != NULL)
         {
-          for (i = 0; i < CONFIG_NET_NACTIVESOCKETS; i++)
+          for (i = 0; i < CONFIG_NET_ALLOC_DEVIF_CALLBACKS; i++)
             {
               ret[i].nxtconn = g_cbfreelist;
               g_cbfreelist = &ret[i];
diff --git a/net/socket/Kconfig b/net/socket/Kconfig
index 3dc7002099..153ff79cf7 100644
--- a/net/socket/Kconfig
+++ b/net/socket/Kconfig
@@ -5,13 +5,36 @@
 
 menu "Socket Support"
 
-config NET_NACTIVESOCKETS
-	int "Max socket operations"
+config NET_PREALLOC_DEVIF_CALLBACKS
+	int "Preallocated socket callbacks"
 	default 16 if !DEFAULT_SMALL
 	default 4 if DEFAULT_SMALL
 	---help---
-		Maximum number of concurrent socket operations (recv, send,
-		connection monitoring, etc.). Default: 16
+		Number of preallocated socket callbacks (all tasks).
+		
+		This number of callbacks will be pre-allocated during system boot.
+		If dynamic callbacks allocation is enabled, more callbacks may be
+		allocated at a later time, as the system needs them. Else this
+		will be the maximum number of callbacks available to the system
+		at all times.
+
+		Set to 0 to disable (and rely only on dynamic allocations).
+
+config NET_ALLOC_DEVIF_CALLBACKS
+	int "Dynamic socket callbacks allocation"
+	default 0
+	---help---
+		Dynamic memory allocations for socket callbacks.
+
+		When set to 0 all dynamic allocations are disabled.
+
+		When set to 1 a new callback will be allocated every time, and
+		it will be free'd when no longer needed.
+
+		Setting this to 2 or more will allocate the callbacks in batches
+		(with batch size equal to this config). When a callback is no
+		longer needed, it will be returned to the free callbacks pool,
+		and it will never be deallocated!
 
 config NET_SOCKOPTS
 	bool "Socket options"