From 0ca48453a067c7862f0bb6b85f152da826f59af7 Mon Sep 17 00:00:00 2001 From: Dimitri Staessens Date: Tue, 20 Jan 2026 22:25:41 +0100 Subject: lib: Replace rdrbuff with a proper slab allocator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a first step towards the Secure Shared Memory (SSM) infrastructure for Ouroboros, which will allow proper resource separation for non-privileged processes. This replaces the rdrbuff (random-deletion ring buffer) PoC allocator with a sharded slab allocator for the packet buffer pool to avoid the head-of-line blocking behaviour of the rdrb and reduce lock contention in multi-process scenarios. Each size class contains multiple independent shards, allowing parallel allocations without blocking. - Configurable shard count per size class (default: 4, set via SSM_POOL_SHARDS in CMake). The configured number of blocks are spread over the number of shards. As an example: SSM_POOL_512_BLOCKS = 768 blocks total These 768 blocks are shared among 4 shards (not 768 × 4 = 3072 blocks) - Lazy block distribution: all blocks initially reside in shard 0 and naturally migrate to process-local shards upon first allocation and subsequent free operations - Fallback with work stealing: processes attempt allocation from their local shard (pid % SSM_POOL_SHARDS) first, then steal from other shards if local is exhausted, eliminating fragmentation while maintaining low contention - Round-robin condvar signaling: blocking allocations cycle through all shard condition variables to ensure fairness - Blocks freed to allocator's shard: uses allocator_pid to determine target shard, enabling natural load balancing as process allocation patterns stabilize over time Maintains existing robust mutex semantics including EOWNERDEAD handling for dead process recovery. Internal structures exposed in ssm.h for testing purposes. Adds some tests (pool_test, pool_sharding_test.c. etc) verifying lazy distribution, migration, fallback stealing, and multiprocess behavior. Updates the ring buffer (rbuff) to use relaxed/acquire/release ordering on atomic indices. The ring buffer requires the (robust) mutex to ensure cross-structure synchronization between pool buffer writes and ring buffer index publication. Signed-off-by: Dimitri Staessens Signed-off-by: Sander Vrijders --- cmake/lib/common.cmake | 6 ++-- cmake/lib/lib.cmake | 19 +---------- cmake/lib/ssm.cmake | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 21 deletions(-) create mode 100644 cmake/lib/ssm.cmake (limited to 'cmake/lib') diff --git a/cmake/lib/common.cmake b/cmake/lib/common.cmake index 1e834f10..b0898950 100644 --- a/cmake/lib/common.cmake +++ b/cmake/lib/common.cmake @@ -47,9 +47,9 @@ set(SOURCE_FILES_COMMON serdes-irm.c serdes-oep.c sha3.c - shm_flow_set.c - shm_rbuff.c - shm_rdrbuff.c + ssm/flow_set.c + ssm/rbuff.c + ssm/pool.c sockets.c tpm.c utils.c diff --git a/cmake/lib/lib.cmake b/cmake/lib/lib.cmake index bb24d1b9..86b4418c 100644 --- a/cmake/lib/lib.cmake +++ b/cmake/lib/lib.cmake @@ -2,10 +2,6 @@ set(LIB_SOURCE_DIR "${CMAKE_SOURCE_DIR}/src/lib") set(LIB_BINARY_DIR "${CMAKE_BINARY_DIR}/src/lib") # Library configuration variables -set(SHM_BUFFER_SIZE 16384 CACHE STRING - "Number of blocks in packet buffer, must be a power of 2") -set(SHM_RBUFF_SIZE 1024 CACHE STRING - "Number of blocks in rbuff buffer, must be a power of 2") set(SYS_MAX_FLOWS 10240 CACHE STRING "Maximum number of total flows for this system") set(PROG_MAX_FLOWS 4096 CACHE STRING @@ -14,10 +10,6 @@ set(PROG_RES_FDS 64 CACHE STRING "Number of reserved flow descriptors per application") set(PROG_MAX_FQUEUES 32 CACHE STRING "Maximum number of flow sets per application") -set(DU_BUFF_HEADSPACE 256 CACHE STRING - "Bytes of headspace to reserve for future headers") -set(DU_BUFF_TAILSPACE 32 CACHE STRING - "Bytes of tailspace to reserve for future tails") if (NOT APPLE) set(PTHREAD_COND_CLOCK "CLOCK_MONOTONIC" CACHE STRING @@ -31,18 +23,9 @@ set(SOCKET_TIMEOUT 500 CACHE STRING "Default timeout for responses from IPCPs (ms)") set(SHM_PREFIX "ouroboros" CACHE STRING "String to prepend to POSIX shared memory filenames") -set(SHM_RBUFF_PREFIX "/${SHM_PREFIX}.rbuff." CACHE INTERNAL - "Prefix for rbuff POSIX shared memory filenames") set(SHM_LOCKFILE_NAME "/${SHM_PREFIX}.lockfile" CACHE INTERNAL "Filename for the POSIX shared memory lockfile") -set(SHM_FLOW_SET_PREFIX "/${SHM_PREFIX}.set." CACHE INTERNAL - "Prefix for the POSIX shared memory flow set") -set(SHM_RDRB_NAME "/${SHM_PREFIX}.rdrb" CACHE INTERNAL - "Name for the main POSIX shared memory buffer") -set(SHM_RDRB_BLOCK_SIZE "sysconf(_SC_PAGESIZE)" CACHE STRING - "Packet buffer block size, multiple of pagesize for performance") -set(SHM_RDRB_MULTI_BLOCK TRUE CACHE BOOL - "Packet buffer multiblock packet support") + set(QOS_DISABLE_CRC TRUE CACHE BOOL "Ignores ber setting on all QoS cubes") set(DELTA_T_MPL 60 CACHE STRING diff --git a/cmake/lib/ssm.cmake b/cmake/lib/ssm.cmake new file mode 100644 index 00000000..71790a4a --- /dev/null +++ b/cmake/lib/ssm.cmake @@ -0,0 +1,89 @@ +# Pool size configuration for Ouroboros secure shared memory +# This file defines the allocation parameters for the +# secure shared memory pool allocator + +# Shared memory pool naming configuration +set(SSM_PREFIX "o7s.ssm" CACHE STRING + "Prefix for secure shared memory pools") +set(SSM_GSMP_SUFFIX ".gsmp" CACHE STRING + "Suffix for Group Shared Memory Pool") +set(SSM_PPP_SUFFIX ".ppp" CACHE STRING + "Suffix for Process Private Pool") + +set(SSM_POOL_NAME "/${SHM_PREFIX}.pool" CACHE INTERNAL + "Name for the main POSIX shared memory pool") +set(SSM_POOL_BLOCKS 16384 CACHE STRING + "Number of blocks in SSM packet pool, must be a power of 2") +set(SSM_PK_BUFF_HEADSPACE 256 CACHE STRING + "Bytes of headspace to reserve for future headers") +set(SSM_PK_BUFF_TAILSPACE 32 CACHE STRING + "Bytes of tailspace to reserve for future tails") +set(SSM_RBUFF_SIZE 1024 CACHE STRING + "Number of blocks in rbuff buffer, must be a power of 2") +set(SSM_RBUFF_PREFIX "/${SHM_PREFIX}.rbuff." CACHE INTERNAL + "Prefix for rbuff POSIX shared memory filenames") +set(SSM_FLOW_SET_PREFIX "/${SHM_PREFIX}.set." CACHE INTERNAL + "Prefix for the POSIX shared memory flow set") + +# Pool blocks per size class +# This determines how many blocks of each size are preallocated in the pool +# Higher values reduce allocation failures but increase memory usage +set(SSM_POOL_256_BLOCKS 1024 CACHE STRING + "Number of 256B blocks in pool") +set(SSM_POOL_512_BLOCKS 768 CACHE STRING + "Number of 512B blocks in pool") +set(SSM_POOL_1K_BLOCKS 512 CACHE STRING + "Number of 1KB blocks in pool") +set(SSM_POOL_2K_BLOCKS 384 CACHE STRING + "Number of 2KB blocks in pool") +set(SSM_POOL_4K_BLOCKS 256 CACHE STRING + "Number of 4KB blocks in pool") +set(SSM_POOL_16K_BLOCKS 128 CACHE STRING + "Number of 16KB blocks in pool") +set(SSM_POOL_64K_BLOCKS 64 CACHE STRING + "Number of 64KB blocks in pool") +set(SSM_POOL_256K_BLOCKS 32 CACHE STRING + "Number of 256KB blocks in pool") +set(SSM_POOL_1M_BLOCKS 16 CACHE STRING + "Number of 1MB blocks in pool") + +# Number of shards per size class for reducing contention +set(SSM_POOL_SHARDS 4 CACHE STRING + "Number of allocator shards per size class") + +# SSM packet buffer overhead - computed at compile time via sizeof() +# Defined in config.h.in as sizeof(_ssm_memory_block) + sizeof(_ssm_pk_buff) +# This makes it portable across platforms with different pid_t sizes and padding + +# Total shared memory pool size calculation +math(EXPR SSM_POOL_TOTAL_SIZE + "(1 << 8) * ${SSM_POOL_256_BLOCKS} + \ + (1 << 9) * ${SSM_POOL_512_BLOCKS} + \ + (1 << 10) * ${SSM_POOL_1K_BLOCKS} + \ + (1 << 11) * ${SSM_POOL_2K_BLOCKS} + \ + (1 << 12) * ${SSM_POOL_4K_BLOCKS} + \ + (1 << 14) * ${SSM_POOL_16K_BLOCKS} + \ + (1 << 16) * ${SSM_POOL_64K_BLOCKS} + \ + (1 << 18) * ${SSM_POOL_256K_BLOCKS} + \ + (1 << 20) * ${SSM_POOL_1M_BLOCKS}") + +set(SSM_POOL_TOTAL_SIZE ${SSM_POOL_TOTAL_SIZE} CACHE INTERNAL + "Total shared memory pool size in bytes") + +include(utils/HumanReadable) +format_bytes_human_readable(${SSM_POOL_TOTAL_SIZE} SSM_POOL_SIZE_DISPLAY) + +# Display configuration summary +message(STATUS "Secure Shared Memory Pool Configuration:") +message(STATUS " Pool prefix: ${SSM_PREFIX}") +message(STATUS " Size classes: " + "256B, 512B, 1KiB, 2KiB, 4KiB, 16KiB, 64KiB, 256KiB, 1MiB") +message(STATUS " Max allocation: 1 MB") +message(STATUS " Total pool size: ${SSM_POOL_SIZE_DISPLAY} " + "(${SSM_POOL_TOTAL_SIZE} bytes)") +message(STATUS " Shards per class: ${SSM_POOL_SHARDS}") +message(STATUS " Blocks per class: ${SSM_POOL_256_BLOCKS}, " + "${SSM_POOL_512_BLOCKS}, ${SSM_POOL_1K_BLOCKS}, " + "${SSM_POOL_2K_BLOCKS}, ${SSM_POOL_4K_BLOCKS}, " + "${SSM_POOL_16K_BLOCKS}, ${SSM_POOL_64K_BLOCKS}, " + "${SSM_POOL_256K_BLOCKS}, ${SSM_POOL_1M_BLOCKS}") -- cgit v1.2.3