ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
sockpool.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file sockpool.h
6 * @brief Dynamic socket recycling: an LRU connection-slot pool (PROTOCORE_ENABLE_SOCKPOOL).
7 *
8 * A device serves a bounded number of concurrent connections. When the pool saturates, the right move is
9 * not to drop the new connection but to *recycle* the least-recently-active slot (the one most likely to
10 * be a dead / idle keep-alive) and hand it to the new peer, returning the evicted id so the transport can
11 * close it cleanly. This is the transport-pool half left open by `services/netadapt`.
12 *
13 * This is that pure policy: a fixed table of connection slots (each an id + last-used tick), with acquire
14 * (free slot, else LRU-recycle), touch (mark active), release, and find. The app owns the real sockets;
15 * this owns *which* slot a connection lives in and which to reclaim under pressure. No heap, no stdlib,
16 * host-testable.
17 */
18
19#ifndef PROTOCORE_SOCKPOOL_H
20#define PROTOCORE_SOCKPOOL_H
21
22#include "protocore_config.h" // the entry point: protocore_types.h for the widths
23
24#if PROTOCORE_ENABLE_SOCKPOOL
25
26EMBED_BEGIN_DECLS
27
28// This module holds nothing between calls, so it carves no borrow and states none. An entry
29// takes one all the same, and never reads it, so every namespace in the tree is invoked the
30// same way.
31
32/** @brief One connection slot. */
33typedef struct
34{
35 embed_bool in_use;
36 uint32_t id; ///< application connection id (e.g. a socket fd / handle).
37 uint32_t last_used; ///< tick of the last acquire/touch (for LRU).
38} SockSlot;
39
40/** @brief A fixed pool of connection slots (storage is caller-owned). */
41typedef struct
42{
43 SockSlot *slots;
44 size_t n;
45} SockPool;
46
47/** @brief Acquire outcome (the sole return of protocore_sockpool_acquire). */
48EMBED_ENUM_BYTE(SockAcq,
49 SOCK_ACQ_FREE = 0, ///< a free slot was used.
50 SOCK_ACQ_RECYCLED = 1, ///< the pool was full; the LRU slot was recycled (see evicted_id).
51 SOCK_ACQ_FAIL = 2 ///< the pool has zero slots / bad args.
52);
53
54/** @brief What init takes: p, slots, n. */
55typedef struct
56{
57 SockPool *p;
58 SockSlot *slots;
59 size_t n;
60} SockpoolInitArgs;
61
62/** @brief What acquire takes: p, id, now, idx, evicted_id. */
63typedef struct
64{
65 SockPool *p;
66 uint32_t id;
67 uint32_t now;
68 size_t *idx; ///< (may be null) receives the chosen slot index
69 uint32_t *evicted_id;
70} SockpoolAcquireArgs;
71
72/** @brief What touch takes: p, idx, now. */
73typedef struct
74{
75 SockPool *p;
76 size_t idx;
77 uint32_t now;
78} SockpoolTouchArgs;
79
80/** @brief What release takes: p, idx. */
81typedef struct
82{
83 SockPool *p;
84 size_t idx;
85} SockpoolReleaseArgs;
86
87/** @brief What find takes: p, id, idx. */
88typedef struct
89{
90 const SockPool *p;
91 uint32_t id;
92 size_t *idx;
93} SockpoolFindArgs;
94
95/** @brief What in_use takes: p. */
96typedef struct
97{
98 const SockPool *p;
99} SockpoolInUseArgs;
100
101/**
102 * @brief Dynamic socket recycling: an LRU connection-slot pool (PROTOCORE_ENABLE_SOCKPOOL). A device serves a bounded
103 * ...
104 *
105 * A caller sets the members a call takes, invokes it through ::Sockpool with the bytes it runs
106 * out of, and reads the outcome off the same handle.
107 *
108 * Sockpool.init_args.p = ...;
109 * Sockpool.init_args.slots = ...;
110 * Sockpool.init_args.n = ...;
111 * Sockpool.init(work);
112 *
113 * @var SockpoolNs::init_args what init takes: p, slots, n
114 * @var SockpoolNs::acquire_args what acquire takes: p, id, now, idx, evicted_id
115 * @var SockpoolNs::touch_args what touch takes: p, idx, now
116 * @var SockpoolNs::release_args what release takes: p, idx
117 * @var SockpoolNs::find_args what find takes: p, id, idx
118 * @var SockpoolNs::in_use_args what in_use takes: p
119 * @var SockpoolNs::ok a call's true/false outcome
120 * @var SockpoolNs::acq SOCK_ACQ_FREE / SOCK_ACQ_RECYCLED / SOCK_ACQ_FAIL
121 * @var SockpoolNs::n the count a call reports
122 * @var SockpoolNs::init initialize a pool over caller storage; all slots start free
123 * @var SockpoolNs::acquire acquire a slot for connection id at tick now. Uses a free slot if ...
124 * @var SockpoolNs::touch mark slot idx active at tick now (refreshes its LRU position)
125 * @var SockpoolNs::release free slot idx. true if it was a valid, in-use slot
126 * @var SockpoolNs::find find the slot holding connection id. idx (may be null) gets the ...
127 * @var SockpoolNs::in_use count of in-use slots
128 *
129 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
130 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
131 * a caller drives every namespace the same way.
132 */
133typedef struct
134{
135 SockpoolInitArgs init_args;
136 SockpoolAcquireArgs acquire_args;
137 SockpoolTouchArgs touch_args;
138 SockpoolReleaseArgs release_args;
139 SockpoolFindArgs find_args;
140 SockpoolInUseArgs in_use_args;
141 embed_bool ok;
142 SockAcq acq;
143 size_t n;
144} SockpoolVars;
145
146/** @brief The operands and the outcome. */
147extern SockpoolVars SockpoolV;
148
149/** @brief The entries. */
150typedef struct
151{
152 void (*const init)(const uint8_t *work);
153 void (*const acquire)(const uint8_t *work);
154 void (*const touch)(const uint8_t *work);
155 void (*const release)(const uint8_t *work);
156 void (*const find)(const uint8_t *work);
157 void (*const in_use)(const uint8_t *work);
158} SockpoolNs;
159
160// What the table binds, defined once in the .c and taking one parameter each: everything
161// else an entry needs is an operand in SockpoolV or a region of the borrow at a fixed offset.
162void protocore_sockpool_init(const uint8_t *work);
163void protocore_sockpool_acquire(const uint8_t *work);
164void protocore_sockpool_touch(const uint8_t *work);
165void protocore_sockpool_release(const uint8_t *work);
166void protocore_sockpool_find(const uint8_t *work);
167void protocore_sockpool_in_use(const uint8_t *work);
168
169// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
170// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
171// `Sockpool.init(work)` resolves to a named function and becomes a DIRECT call. An extern table
172// leaves the call indirect and the symbol live at every level, -O2 -flto included.
173static const SockpoolNs Sockpool __attribute__((unused)) = {
174 .init = protocore_sockpool_init,
175 .acquire = protocore_sockpool_acquire,
176 .touch = protocore_sockpool_touch,
177 .release = protocore_sockpool_release,
178 .find = protocore_sockpool_find,
179 .in_use = protocore_sockpool_in_use,
180};
181
182EMBED_END_DECLS
183
184#endif // PROTOCORE_ENABLE_SOCKPOOL
185
186#endif // PROTOCORE_SOCKPOOL_H
EMBED_ENUM_BYTE(ProtoConn, PROTO_NONE=0, PROTO_HTTP=1, PROTO_TELNET=2, PROTO_SSH=3, PROTO_MODBUS=4, PROTO_OPCUA=5, PROTO_SSH_RFWD=6, PROTO_RELAY=7, PROTO_BRIDGE=8, PROTO_NTRIP_CASTER=9, PROTO_MESH=10, PROTO_UDP=11,)
Application protocol spoken on a listener port or connection slot.