From c8ac3070ccdd8421696c9163323171d34f55035e Mon Sep 17 00:00:00 2001 From: Seppo Takalo Date: Fri, 28 Jul 2023 13:05:58 +0300 Subject: [PATCH] net: sockets: socketpair: Allow statically allocated socketpairs When the target board does not have heap by default, allows statically reserving the space for required socketpairs. Signed-off-by: Seppo Takalo --- subsys/net/lib/sockets/Kconfig | 24 ++++++++++++++++++++++-- subsys/net/lib/sockets/socketpair.c | 18 ++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/subsys/net/lib/sockets/Kconfig b/subsys/net/lib/sockets/Kconfig index b4c2ada0d6c031..1d8da58670136f 100644 --- a/subsys/net/lib/sockets/Kconfig +++ b/subsys/net/lib/sockets/Kconfig @@ -246,18 +246,38 @@ config NET_SOCKETS_CAN_RECEIVERS config NET_SOCKETPAIR bool "Support for socketpair" select PIPES - depends on HEAP_MEM_POOL_SIZE != 0 help Communicate over a pair of connected, unnamed UNIX domain sockets. +if NET_SOCKETPAIR + config NET_SOCKETPAIR_BUFFER_SIZE int "Size of the intermediate buffer, in bytes" default 64 range 1 4096 - depends on NET_SOCKETPAIR help Buffer size for socketpair(2) +choice + prompt "Memory management for socketpair" + default NET_SOCKETPAIR_HEAP if HEAP_MEM_POOL_SIZE != 0 + +config NET_SOCKETPAIR_STATIC + bool "Pre-allocate memory statically" + +config NET_SOCKETPAIR_HEAP + bool "Use heap for allocating socketpairs" + depends on HEAP_MEM_POOL_SIZE != 0 + +endchoice + +if NET_SOCKETPAIR_STATIC +config NET_SOCKETPAIR_MAX + int "How many socketpairs to pre-allocate" + default 1 +endif +endif + config NET_SOCKETS_NET_MGMT bool "Network management socket support [EXPERIMENTAL]" depends on NET_MGMT_EVENT diff --git a/subsys/net/lib/sockets/socketpair.c b/subsys/net/lib/sockets/socketpair.c index 3adeeab14dedf5..b16fb32d1e547d 100644 --- a/subsys/net/lib/sockets/socketpair.c +++ b/subsys/net/lib/sockets/socketpair.c @@ -56,6 +56,11 @@ __net_socket struct spair { uint8_t buf[CONFIG_NET_SOCKETPAIR_BUFFER_SIZE]; }; +#ifdef CONFIG_NET_SOCKETPAIR_STATIC +K_MEM_SLAB_DEFINE_STATIC(spair_slab, sizeof(struct spair), CONFIG_NET_SOCKETPAIR_MAX * 2, + __alignof__(struct spair)); +#endif /* CONFIG_NET_SOCKETPAIR_STATIC */ + /* forward declaration */ static const struct socket_op_vtable spair_fd_op_vtable; @@ -188,7 +193,9 @@ static void spair_delete(struct spair *spair) /* ensure no private information is released to the memory pool */ memset(spair, 0, sizeof(*spair)); -#ifdef CONFIG_USERSPACE +#ifdef CONFIG_NET_SOCKETPAIR_STATIC + k_mem_slab_free(&spair_slab, (void **) &spair); +#elif CONFIG_USERSPACE k_object_free(spair); #else k_free(spair); @@ -213,7 +220,14 @@ static struct spair *spair_new(void) struct spair *spair; int res; -#ifdef CONFIG_USERSPACE +#ifdef CONFIG_NET_SOCKETPAIR_STATIC + + res = k_mem_slab_alloc(&spair_slab, (void **) &spair, K_NO_WAIT); + if (res != 0) { + spair = NULL; + } + +#elif CONFIG_USERSPACE struct z_object *zo = z_dynamic_object_create(sizeof(*spair)); if (zo == NULL) {