From ced80b13bfc61404088b18f5a2129e78bed5f2d3 Mon Sep 17 00:00:00 2001 From: Henrik Brix Andersen Date: Wed, 4 Sep 2024 15:42:42 +0000 Subject: [PATCH] lib: net_buf: move the network buffer implementation to lib Move the net_buf implementation from the networking subsystem to a library as they have no dependency on the networking subsystem. Network buffers are used in subsystems outside of networking (e.g. Bluetooth, USB). Fixes: #36374 Signed-off-by: Henrik Brix Andersen --- MAINTAINERS.yml | 3 +- lib/CMakeLists.txt | 1 + lib/Kconfig | 2 + lib/net_buf/CMakeLists.txt | 8 +++ lib/net_buf/Kconfig | 62 +++++++++++++++++++ {subsys/net => lib/net_buf}/buf.c | 0 {subsys/net => lib/net_buf}/buf_simple.c | 0 subsys/CMakeLists.txt | 2 +- subsys/net/CMakeLists.txt | 5 -- subsys/net/Kconfig | 58 ----------------- tests/bluetooth/audio/ascs/uut/CMakeLists.txt | 2 +- .../audio/bap_base/uut/CMakeLists.txt | 2 +- .../bap_broadcast_source/uut/CMakeLists.txt | 2 +- .../audio/cap_commander/uut/CMakeLists.txt | 2 +- .../audio/cap_initiator/uut/CMakeLists.txt | 2 +- .../bluetooth/audio/codec/uut/CMakeLists.txt | 2 +- .../host/data/bt_data_parse/CMakeLists.txt | 2 +- tests/net/buf_simple/CMakeLists.txt | 2 +- 18 files changed, 83 insertions(+), 74 deletions(-) create mode 100644 lib/net_buf/CMakeLists.txt create mode 100644 lib/net_buf/Kconfig rename {subsys/net => lib/net_buf}/buf.c (100%) rename {subsys/net => lib/net_buf}/buf_simple.c (100%) diff --git a/MAINTAINERS.yml b/MAINTAINERS.yml index c6332eb4327..42db676520a 100644 --- a/MAINTAINERS.yml +++ b/MAINTAINERS.yml @@ -2803,7 +2803,6 @@ Networking: - samples/net/lwm2m_client/ - samples/net/wifi/ - samples/net/dhcpv4_client/ - - subsys/net/buf*.c - subsys/net/l2/ethernet/gptp/ - subsys/net/l2/ieee802154/ - subsys/net/l2/wifi/ @@ -2846,7 +2845,7 @@ Networking: - jukkar files: - include/zephyr/net/buf.h - - subsys/net/buf*.c + - lib/net_buf/ - tests/net/buf/ labels: - "area: Networking Buffers" diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 7a0ee04e4ac..52aeac58300 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -14,6 +14,7 @@ add_subdirectory_ifdef(CONFIG_CPP cpp) add_subdirectory(hash) add_subdirectory(heap) add_subdirectory(mem_blocks) +add_subdirectory_ifdef(CONFIG_NET_BUF net_buf) add_subdirectory(os) add_subdirectory(utils) add_subdirectory_ifdef(CONFIG_SMF smf) diff --git a/lib/Kconfig b/lib/Kconfig index af6717bc22e..bcf8f53235e 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -15,6 +15,8 @@ source "lib/heap/Kconfig" source "lib/mem_blocks/Kconfig" +source "lib/net_buf/Kconfig" + source "lib/os/Kconfig" source "lib/posix/Kconfig" diff --git a/lib/net_buf/CMakeLists.txt b/lib/net_buf/CMakeLists.txt new file mode 100644 index 00000000000..caa47756820 --- /dev/null +++ b/lib/net_buf/CMakeLists.txt @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: Apache-2.0 + +zephyr_library() +zephyr_library_sources_ifdef( + CONFIG_NET_BUF + buf.c + buf_simple.c + ) diff --git a/lib/net_buf/Kconfig b/lib/net_buf/Kconfig new file mode 100644 index 00000000000..385bda18447 --- /dev/null +++ b/lib/net_buf/Kconfig @@ -0,0 +1,62 @@ +# Network buffer configuration options + +# Copyright (c) 2015 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +menuconfig NET_BUF + bool "Network buffer support" + help + This option enables support for generic network protocol + buffers. + +if NET_BUF + +config NET_BUF_LOG + bool "Network buffer logging" + select LOG + help + Enable logs and checks for the generic network buffers. + +module = NET_BUF +module-str = net_buf +source "subsys/logging/Kconfig.template.log_config" + +if NET_BUF_LOG + +config NET_BUF_WARN_ALLOC_INTERVAL + int "Interval of Network buffer allocation warnings" + default 1 + range 0 60 + help + Interval in seconds of Network buffer allocation warnings which are + generated when a buffer cannot immediately be allocated with K_FOREVER + which may lead to deadlocks. Setting it to 0 makes warnings to be + printed only once per allocation. + +config NET_BUF_SIMPLE_LOG + bool "Network buffer memory debugging" + select LOG + help + Enable extra debug logs and checks for the generic network buffers. + +endif # NET_BUF_LOG + +config NET_BUF_POOL_USAGE + bool "Network buffer pool usage tracking" + help + Enable network buffer pool tracking. This means that: + * amount of free buffers in the pool is remembered + * total size of the pool is calculated + * pool name is stored and can be shown in debugging prints + +config NET_BUF_ALIGNMENT + int "Network buffer alignment restriction" + default 0 + help + Alignment restriction for network buffers. This is useful for + some hardware IP with DMA that requires the buffers to be aligned + to a certain byte boundary, or dealing with cache line restrictions. + Default value of 0 means the alignment will be the size of a void pointer, + any other value will force the alignment of a net buffer in bytes. + +endif # NET_BUF diff --git a/subsys/net/buf.c b/lib/net_buf/buf.c similarity index 100% rename from subsys/net/buf.c rename to lib/net_buf/buf.c diff --git a/subsys/net/buf_simple.c b/lib/net_buf/buf_simple.c similarity index 100% rename from subsys/net/buf_simple.c rename to lib/net_buf/buf_simple.c diff --git a/subsys/CMakeLists.txt b/subsys/CMakeLists.txt index 58f0cf6ffc3..42cd98c775c 100644 --- a/subsys/CMakeLists.txt +++ b/subsys/CMakeLists.txt @@ -47,7 +47,7 @@ add_subdirectory_ifdef(CONFIG_INPUT input) add_subdirectory_ifdef(CONFIG_JWT jwt) add_subdirectory_ifdef(CONFIG_LLEXT llext) add_subdirectory_ifdef(CONFIG_MODEM_MODULES modem) -add_subdirectory_ifdef(CONFIG_NET_BUF net) +add_subdirectory_ifdef(CONFIG_NETWORKING net) add_subdirectory_ifdef(CONFIG_PROFILING profiling) add_subdirectory_ifdef(CONFIG_RETENTION retention) add_subdirectory_ifdef(CONFIG_SENSING sensing) diff --git a/subsys/net/CMakeLists.txt b/subsys/net/CMakeLists.txt index d8ef4ab0f36..e99b26f5fb6 100644 --- a/subsys/net/CMakeLists.txt +++ b/subsys/net/CMakeLists.txt @@ -1,11 +1,6 @@ # SPDX-License-Identifier: Apache-2.0 zephyr_library() -zephyr_library_sources_ifdef( - CONFIG_NET_BUF - buf.c - buf_simple.c - ) zephyr_library_sources_ifdef(CONFIG_NET_HOSTNAME_ENABLE hostname.c) if(CONFIG_NETWORKING) diff --git a/subsys/net/Kconfig b/subsys/net/Kconfig index 57706cd2f5d..e6db22d2b10 100644 --- a/subsys/net/Kconfig +++ b/subsys/net/Kconfig @@ -5,64 +5,6 @@ menu "Networking" -config NET_BUF - bool "Network buffer support" - help - This option enables support for generic network protocol - buffers. - -if NET_BUF - -config NET_BUF_LOG - bool "Network buffer logging" - select LOG - help - Enable logs and checks for the generic network buffers. - -module = NET_BUF -module-str = net_buf -source "subsys/logging/Kconfig.template.log_config" - -if NET_BUF_LOG - -config NET_BUF_WARN_ALLOC_INTERVAL - int "Interval of Network buffer allocation warnings" - default 1 - range 0 60 - help - Interval in seconds of Network buffer allocation warnings which are - generated when a buffer cannot immediately be allocated with K_FOREVER - which may lead to deadlocks. Setting it to 0 makes warnings to be - printed only once per allocation. - -config NET_BUF_SIMPLE_LOG - bool "Network buffer memory debugging" - select LOG - help - Enable extra debug logs and checks for the generic network buffers. - -endif # NET_BUF_LOG - -config NET_BUF_POOL_USAGE - bool "Network buffer pool usage tracking" - help - Enable network buffer pool tracking. This means that: - * amount of free buffers in the pool is remembered - * total size of the pool is calculated - * pool name is stored and can be shown in debugging prints - -config NET_BUF_ALIGNMENT - int "Network buffer alignment restriction" - default 0 - help - Alignment restriction for network buffers. This is useful for - some hardware IP with DMA that requires the buffers to be aligned - to a certain byte boundary, or dealing with cache line restrictions. - Default value of 0 means the alignment will be the size of a void pointer, - any other value will force the alignment of a net buffer in bytes. - -endif # NET_BUF - config NETWORKING bool "Link layer and networking (including IP)" select NET_BUF diff --git a/tests/bluetooth/audio/ascs/uut/CMakeLists.txt b/tests/bluetooth/audio/ascs/uut/CMakeLists.txt index 8153d0f9372..3c1a04c271f 100644 --- a/tests/bluetooth/audio/ascs/uut/CMakeLists.txt +++ b/tests/bluetooth/audio/ascs/uut/CMakeLists.txt @@ -16,7 +16,7 @@ add_library(uut STATIC ${ZEPHYR_BASE}/subsys/bluetooth/host/data.c ${ZEPHYR_BASE}/subsys/bluetooth/host/uuid.c ${ZEPHYR_BASE}/subsys/logging/log_minimal.c - ${ZEPHYR_BASE}/subsys/net/buf_simple.c + ${ZEPHYR_BASE}/lib/net_buf/buf_simple.c bap_unicast_client.c bap_unicast_server.c ) diff --git a/tests/bluetooth/audio/bap_base/uut/CMakeLists.txt b/tests/bluetooth/audio/bap_base/uut/CMakeLists.txt index 4891c4794d5..9a7633fa939 100644 --- a/tests/bluetooth/audio/bap_base/uut/CMakeLists.txt +++ b/tests/bluetooth/audio/bap_base/uut/CMakeLists.txt @@ -11,7 +11,7 @@ add_library(uut STATIC ${ZEPHYR_BASE}/subsys/bluetooth/host/uuid.c ${ZEPHYR_BASE}/subsys/bluetooth/common/bt_str.c ${ZEPHYR_BASE}/subsys/logging/log_minimal.c - ${ZEPHYR_BASE}/subsys/net/buf_simple.c + ${ZEPHYR_BASE}/lib/net_buf/buf_simple.c ) add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/audio/mocks mocks) diff --git a/tests/bluetooth/audio/bap_broadcast_source/uut/CMakeLists.txt b/tests/bluetooth/audio/bap_broadcast_source/uut/CMakeLists.txt index 9eb91c84fa3..879a50720b3 100644 --- a/tests/bluetooth/audio/bap_broadcast_source/uut/CMakeLists.txt +++ b/tests/bluetooth/audio/bap_broadcast_source/uut/CMakeLists.txt @@ -13,7 +13,7 @@ add_library(uut STATIC ${ZEPHYR_BASE}/subsys/bluetooth/audio/bap_broadcast_source.c ${ZEPHYR_BASE}/subsys/bluetooth/audio/codec.c ${ZEPHYR_BASE}/subsys/logging/log_minimal.c - ${ZEPHYR_BASE}/subsys/net/buf_simple.c + ${ZEPHYR_BASE}/lib/net_buf/buf_simple.c ) add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/audio/mocks mocks) diff --git a/tests/bluetooth/audio/cap_commander/uut/CMakeLists.txt b/tests/bluetooth/audio/cap_commander/uut/CMakeLists.txt index 244be9e921c..7940f3c993c 100644 --- a/tests/bluetooth/audio/cap_commander/uut/CMakeLists.txt +++ b/tests/bluetooth/audio/cap_commander/uut/CMakeLists.txt @@ -11,7 +11,7 @@ add_library(uut STATIC ${ZEPHYR_BASE}/subsys/bluetooth/audio/cap_commander.c ${ZEPHYR_BASE}/subsys/bluetooth/audio/cap_common.c ${ZEPHYR_BASE}/subsys/logging/log_minimal.c - ${ZEPHYR_BASE}/subsys/net/buf_simple.c + ${ZEPHYR_BASE}/lib/net_buf/buf_simple.c bap_broadcast_assistant.c aics.c cap_commander.c diff --git a/tests/bluetooth/audio/cap_initiator/uut/CMakeLists.txt b/tests/bluetooth/audio/cap_initiator/uut/CMakeLists.txt index dd1bf203687..a24d6cfccce 100644 --- a/tests/bluetooth/audio/cap_initiator/uut/CMakeLists.txt +++ b/tests/bluetooth/audio/cap_initiator/uut/CMakeLists.txt @@ -16,7 +16,7 @@ add_library(uut STATIC ${ZEPHYR_BASE}/subsys/bluetooth/common/bt_str.c ${ZEPHYR_BASE}/subsys/bluetooth/host/uuid.c ${ZEPHYR_BASE}/subsys/logging/log_minimal.c - ${ZEPHYR_BASE}/subsys/net/buf_simple.c + ${ZEPHYR_BASE}/lib/net_buf/buf_simple.c bap_unicast_client.c cap_initiator.c csip.c diff --git a/tests/bluetooth/audio/codec/uut/CMakeLists.txt b/tests/bluetooth/audio/codec/uut/CMakeLists.txt index d835e520f6f..f998168d417 100644 --- a/tests/bluetooth/audio/codec/uut/CMakeLists.txt +++ b/tests/bluetooth/audio/codec/uut/CMakeLists.txt @@ -11,7 +11,7 @@ add_library(uut STATIC ${ZEPHYR_BASE}/subsys/bluetooth/audio/codec.c ${ZEPHYR_BASE}/subsys/bluetooth/common/bt_str.c ${ZEPHYR_BASE}/subsys/logging/log_minimal.c - ${ZEPHYR_BASE}/subsys/net/buf_simple.c + ${ZEPHYR_BASE}/lib/net_buf/buf_simple.c ) add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/audio/mocks mocks) diff --git a/tests/bluetooth/host/data/bt_data_parse/CMakeLists.txt b/tests/bluetooth/host/data/bt_data_parse/CMakeLists.txt index 7ff79dd7108..96be82d20f7 100644 --- a/tests/bluetooth/host/data/bt_data_parse/CMakeLists.txt +++ b/tests/bluetooth/host/data/bt_data_parse/CMakeLists.txt @@ -15,7 +15,7 @@ target_sources(testbinary src/main.c ${ZEPHYR_BASE}/subsys/bluetooth/host/data.c - ${ZEPHYR_BASE}/subsys/net/buf_simple.c + ${ZEPHYR_BASE}/lib/net_buf/buf_simple.c ${ZEPHYR_BASE}/subsys/logging/log_minimal.c ${ZEPHYR_BASE}/subsys/bluetooth/common/bt_str.c ${ZEPHYR_BASE}/subsys/bluetooth/host/uuid.c diff --git a/tests/net/buf_simple/CMakeLists.txt b/tests/net/buf_simple/CMakeLists.txt index ea1cedaffc4..5631fbd5201 100644 --- a/tests/net/buf_simple/CMakeLists.txt +++ b/tests/net/buf_simple/CMakeLists.txt @@ -14,5 +14,5 @@ target_sources(testbinary PRIVATE src/main.c - ${ZEPHYR_BASE}/subsys/net/buf_simple.c + ${ZEPHYR_BASE}/lib/net_buf/buf_simple.c )