ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
stack_tcp.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 engine.h
6 * @brief Layer 4 (Transport) - the TCP engine over idemIP: listeners, the connections of the pool, and the
7 * segments that idemIP does not send.
8 *
9 * idemIP runs RFC 9293 sec 3.10 SEGMENT ARRIVES inside Dispatch, and the engine does the rest. It sends
10 * each segment that TcpIn asks for. It copies the text of each segment into the receive ring of its pool
11 * entry. It sends the octets of the transmit ring of each pool entry, and it frees them when the peer
12 * acknowledges them. It posts one StackTcpEvent for each event of a connection. StackTcpEvent::slot_id is the index of
13 * the pool entry.
14 *
15 * Worker 0 runs the stack. The stack calls ::StackTcpNs::segment after each dispatch that reports TCP. It
16 * calls ::StackTcpNs::acknowledge in the TCP_ACK step of the flush, and ::StackTcpNs::service after the
17 * flush. Each entry on the namespace runs on worker 0.
18 *
19 * The worker that owns a pool entry calls protocore_stack_tcp_write and protocore_stack_tcp_close. The two
20 * calls put segments on the transmit ring of the entry. They change no other state. The ring is SPSC: the
21 * owner is the producer, and worker 0 is the consumer. A segment of zero octets is the close mark. The ring
22 * keeps the mark behind the data before it.
23 *
24 * @author Douglas Quigg (dstroy0)
25 * @date 2026
26 */
27
28#ifndef PROTOCORE_STACK_TCP_H
29#define PROTOCORE_STACK_TCP_H
30
31#include "protocore_config.h" // the entry point: the enable gate below, and the knobs of the stack
32
33#if PROTOCORE_ENABLE_IDEMIP
34
35#include "memoria_anularis/memoria_anularis.h" // the type of each ring of an entry
36#include "network_drivers/network/stack/stack.h" // the unit cells that the TCP of the stack calls into
37
38EMBED_BEGIN_DECLS
39
40/** @brief What happened to the connection of one entry. */
41EMBED_ENUM_BYTE(StackTcpEventKind,
42 STACK_TCP_CONNECT = 0, ///< a passive open completed on the entry
43 STACK_TCP_DATA, ///< text arrived on the receive ring of the entry
44 STACK_TCP_DISCONNECT, ///< the peer sent its FIN
45 STACK_TCP_ERROR, ///< a reset or a timeout ended the connection
46);
47
48/**
49 * @brief One event of the TCP of the stack, in the event ring.
50 *
51 * @var StackTcpEvent::data_len the octets of text that a STACK_TCP_DATA put on the receive ring
52 * @var StackTcpEvent::type what happened
53 * @var StackTcpEvent::slot_id the entry
54 * @var StackTcpEvent::reserved zeros, so that the record is a power of two in size
55 */
56typedef struct
57{
58 uint32_t data_len;
59 StackTcpEventKind type;
60 uint8_t slot_id;
61 uint8_t reserved[2];
62} StackTcpEvent;
63
64/** @brief The entries, one for each connection that the stack carries. */
65#define PROTOCORE_STACK_TCP_SLOTS PROTOCORE_STACK_TCP_ENTRIES
66
67/** @brief The index that names no pool entry. */
68#define PROTOCORE_STACK_TCP_SLOT_NONE 0xFFu
69
70/** @brief The octets in front of the text of each transmit segment: the count of its text octets. */
71#define PROTOCORE_STACK_TCP_SEGMENT_HEAD sizeof(uint32_t)
72
73static_assert(PROTOCORE_STACK_TCP_SLOTS < PROTOCORE_STACK_TCP_SLOT_NONE,
74 "a pool entry index fits one octet, below PROTOCORE_STACK_TCP_SLOT_NONE");
75static_assert((PROTOCORE_STACK_TCP_SEGMENTS & (PROTOCORE_STACK_TCP_SEGMENTS - 1u)) == 0u &&
76 PROTOCORE_STACK_TCP_SEGMENTS != 0u,
77 "PROTOCORE_STACK_TCP_SEGMENTS must be a power of two: the segment view of MMgr counts with a mask");
78
79/**
80 * @brief What ::StackTcpNs::start takes.
81 *
82 * @var StackTcpStartArgs::stack the borrow of the started stack, from protocore_stack_span [BORROWS]. The
83 * engine calls its units, and it sends each frame in the transmit buffer that
84 * the stack holds.
85 * @var StackTcpStartArgs::window the receive window of each connection: the vacant octets of an empty receive
86 * ring of a pool entry. 1 through 65535, because the engine announces no window
87 * scale.
88 */
89typedef struct
90{
91 uint8_t *stack;
92 uint32_t window;
93} StackTcpStartArgs;
94
95/**
96 * @brief What ::StackTcpNs::listen takes: one RFC 9293 sec 3.3.2 passive OPEN on each local address.
97 *
98 * @var StackTcpListenArgs::port the local port, 1 through 65535
99 * @var StackTcpListenArgs::ip_version 4 or 6
100 * @var StackTcpListenArgs::backlog the connections that the listener keeps in SYN-RECEIVED, 1 or more
101 */
102typedef struct
103{
104 uint16_t port;
105 uint8_t ip_version;
106 uint8_t backlog;
107} StackTcpListenArgs;
108
109/**
110 * @brief What ::StackTcpNs::slot_bind takes: the two rings of one pool entry.
111 *
112 * @var StackTcpSlotArgs::receive the receive ring of the entry, ready and empty. Its vacant octets are
113 * the window of ::StackTcpStartArgs at least [BORROWS].
114 * @var StackTcpSlotArgs::transmit the storage of the transmit ring, which the engine lays [BORROWS]
115 * @var StackTcpSlotArgs::transmit_buffer the octets of the transmit ring, on a 4-octet boundary [BORROWS]
116 * @var StackTcpSlotArgs::transmit_bytes the octets at transmit_buffer. The engine splits them into
117 * PROTOCORE_STACK_TCP_SEGMENTS segments of one size.
118 * @var StackTcpSlotArgs::index the entry, below PROTOCORE_STACK_TCP_SLOTS
119 */
120typedef struct
121{
122 mmgr_ring *receive;
123 mmgr_ring *transmit;
124 uint8_t *transmit_buffer;
125 size_t transmit_bytes;
126 uint8_t index;
127} StackTcpSlotArgs;
128
129/**
130 * @brief What protocore_stack_tcp_write takes.
131 *
132 * @var StackTcpWriteArgs::data the octets to send [BORROWS]
133 * @var StackTcpWriteArgs::len the count of them
134 * @var StackTcpWriteArgs::index the pool entry that sends them
135 */
136typedef struct
137{
138 const uint8_t *data;
139 size_t len;
140 uint8_t index;
141} StackTcpWriteArgs;
142
143/**
144 * @brief What protocore_stack_tcp_close takes.
145 *
146 * @var StackTcpCloseArgs::index the pool entry that the owner has no more use for
147 */
148typedef struct
149{
150 uint8_t index;
151} StackTcpCloseArgs;
152
153/**
154 * @brief What ::StackTcpNs::segment takes: the frame that the last Dispatch.input read.
155 *
156 * @var StackTcpSegmentArgs::frame the frame, where the receive descriptor holds it [BORROWS]
157 * @var StackTcpSegmentArgs::now_ms the monotonic millisecond count of the tick
158 * @var StackTcpSegmentArgs::netif the interface that the frame came in on
159 */
160typedef struct
161{
162 const uint8_t *frame;
163 uint32_t now_ms;
164 uint8_t netif;
165} StackTcpSegmentArgs;
166
167/**
168 * @brief What ::StackTcpNs::service takes.
169 *
170 * @var StackTcpServiceArgs::now_ms the monotonic millisecond count of the tick
171 */
172typedef struct
173{
174 uint32_t now_ms;
175} StackTcpServiceArgs;
176
177/**
178 * @brief The operands and the outcome of the entries on the namespace.
179 *
180 * @var StackTcpVars::start_args the stack and the window that ::StackTcpNs::start takes
181 * @var StackTcpVars::listen_args the passive OPEN that ::StackTcpNs::listen takes
182 * @var StackTcpVars::slot_args the rings that ::StackTcpNs::slot_bind takes
183 * @var StackTcpVars::segment_args the frame that ::StackTcpNs::segment takes
184 * @var StackTcpVars::service_args the clock that ::StackTcpNs::service takes
185 * @var StackTcpVars::event the record that the last ::StackTcpNs::event_take read
186 * @var StackTcpVars::sent the segments that the engine put on a transmit ring since start
187 * @var StackTcpVars::lost_events the events that found the event ring full since start
188 * @var StackTcpVars::ok the outcome of the last call
189 */
190typedef struct
191{
192 StackTcpStartArgs start_args;
193 StackTcpListenArgs listen_args;
194 StackTcpSlotArgs slot_args;
195 StackTcpSegmentArgs segment_args;
196 StackTcpServiceArgs service_args;
197 StackTcpEvent event;
198 uint32_t sent;
199 uint32_t lost_events;
200 embed_bool ok;
201} StackTcpVars;
202
203/** @brief The operands and the outcome. */
204extern StackTcpVars StackTcpV;
205
206/**
207 * @brief The entries. Each one runs on worker 0.
208 *
209 * @var StackTcpNs::start clear the pool entries and the event ring, and take the stack and the window
210 * @var StackTcpNs::listen open one listener on each local address of one IP version
211 * @var StackTcpNs::slot_bind give one pool entry its two rings
212 * @var StackTcpNs::event_take read the next event into ::StackTcpVars::event. False when none waits.
213 * @var StackTcpNs::segment act on what the last Dispatch.input decided about a TCP segment
214 * @var StackTcpNs::acknowledge send the aggregate ACK that the TCP_ACK step of the flush reports
215 * @var StackTcpNs::service send the transmit rings, open the receive windows, and end each TIME-WAIT
216 * whose 2 MSL passed
217 */
218typedef struct
219{
220 void (*const start)(uint8_t *work);
221 void (*const listen)(uint8_t *work);
222 void (*const slot_bind)(uint8_t *work);
223 void (*const event_take)(uint8_t *work);
224 void (*const segment)(uint8_t *work);
225 void (*const acknowledge)(uint8_t *work);
226 void (*const service)(uint8_t *work);
227} StackTcpNs;
228
229// The functions that the table binds. The .c defines each one, and each one takes one parameter.
230void protocore_stack_tcp_start(uint8_t *work);
231void protocore_stack_tcp_listen(uint8_t *work);
232void protocore_stack_tcp_slot_bind(uint8_t *work);
233void protocore_stack_tcp_event_take(uint8_t *work);
234void protocore_stack_tcp_segment(uint8_t *work);
235void protocore_stack_tcp_acknowledge(uint8_t *work);
236void protocore_stack_tcp_service(uint8_t *work);
237
238/**
239 * @brief Puts octets on the transmit ring of a pool entry. The worker that owns the entry calls it.
240 *
241 * @param[in] args The entry and the octets [BORROWS].
242 * @return The octets that went on the ring. Fewer than args->len where the ring has no free segment.
243 */
244size_t protocore_stack_tcp_write(const StackTcpWriteArgs *args);
245
246/**
247 * @brief Puts the close mark on the transmit ring of a pool entry. The worker that owns the entry calls it,
248 * once.
249 *
250 * @param[in] args The entry [BORROWS].
251 * @return True when the mark went on the ring. False where the ring has no free segment.
252 * @note The engine sends the FIN after the octets in front of the mark. After a reset, the mark frees the
253 * entry.
254 */
255embed_bool protocore_stack_tcp_close(const StackTcpCloseArgs *args);
256
257/**
258 * @brief The two ends of the connection of one entry.
259 *
260 * @var StackTcpEnds::local_ip the local IPv4 address, network order
261 * @var StackTcpEnds::remote_ip the IPv4 address of the peer, network order
262 * @var StackTcpEnds::local_port the local port
263 * @var StackTcpEnds::remote_port the port of the peer
264 */
265typedef struct
266{
267 uint8_t local_ip[4];
268 uint8_t remote_ip[4];
269 uint16_t local_port;
270 uint16_t remote_port;
271} StackTcpEnds;
272
273/** @brief The text octets that the transmit ring of an open entry takes now. 0 for an entry with no room. */
274size_t protocore_stack_tcp_room(uint8_t index);
275
276/** @brief The segments on the transmit ring of an entry that the peer did not acknowledge yet. */
277size_t protocore_stack_tcp_queued(uint8_t index);
278
279/** @brief The text octets of an entry that the peer acknowledged, since the connection opened. */
280uint32_t protocore_stack_tcp_released(uint8_t index);
281
282/** @brief The two ends of the connection of an entry in @p ends. False for an entry with no TCB. */
283embed_bool protocore_stack_tcp_ends(uint8_t index, StackTcpEnds *ends);
284
285/** @brief RFC 9293 sec 3.10.4 ABORT: a reset, and the entry goes back at once. No event follows. */
286void protocore_stack_tcp_abort(uint8_t index);
287
288/**
289 * @brief What an active OPEN takes.
290 *
291 * @var StackTcpConnectArgs::local_ip the local IPv4 address, network order
292 * @var StackTcpConnectArgs::remote_ip the IPv4 address of the peer, network order
293 * @var StackTcpConnectArgs::next_hop the hardware address of the next hop to the peer
294 * @var StackTcpConnectArgs::now_ms the monotonic millisecond count of the caller
295 * @var StackTcpConnectArgs::random_word a random word, which the choice of the ephemeral port takes
296 * @var StackTcpConnectArgs::remote_port the port of the peer
297 * @var StackTcpConnectArgs::netif the interface of the connection
298 */
299typedef struct
300{
301 const uint8_t *local_ip;
302 const uint8_t *remote_ip;
303 const uint8_t *next_hop;
304 uint32_t now_ms;
305 uint32_t random_word;
306 uint16_t remote_port;
307 uint8_t netif;
308} StackTcpConnectArgs;
309
310/**
311 * @brief RFC 9293 sec 3.10.1 active OPEN: the SYN goes out, and the entry waits in SYN-SENT.
312 *
313 * STACK_TCP_CONNECT follows once the handshake completes, and STACK_TCP_ERROR follows a reset or a timeout.
314 *
315 * @return the entry of the connection, or PROTOCORE_STACK_TCP_SLOT_NONE where no entry or no TCB is free
316 */
317uint8_t protocore_stack_tcp_connect(const StackTcpConnectArgs *args);
318
319// The table is `static const` with its initializer here, and not `extern` against a definition in the .c.
320// Each translation unit can see the initializer. `StackTcp.service(work)` thus resolves to a named
321// function and becomes a direct call.
322static const StackTcpNs StackTcp __attribute__((unused)) = {
323 .start = protocore_stack_tcp_start,
324 .listen = protocore_stack_tcp_listen,
325 .slot_bind = protocore_stack_tcp_slot_bind,
326 .event_take = protocore_stack_tcp_event_take,
327 .segment = protocore_stack_tcp_segment,
328 .acknowledge = protocore_stack_tcp_acknowledge,
329 .service = protocore_stack_tcp_service,
330};
331
332/** @brief The borrow of this module: its context, taken once from the persistent tier. */
333uint8_t *protocore_stack_tcp_span(void);
334
335EMBED_END_DECLS
336
337#endif // PROTOCORE_ENABLE_IDEMIP
338
339#endif // PROTOCORE_STACK_TCP_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.
Layer 3 (Network) - the idemIP stack: the cells of its units, its interfaces, and its tick.