ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
stack.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 stack.h
6 * @brief Layer 3 (Network) - the idemIP stack: the cells of its units, its interfaces, and its tick.
7 *
8 * The TCP/IP stack is idemIP, under include/idemIP. This module holds one cell for each unit of idemIP.
9 * It binds each interface to its link driver. Each call of ::StackNs::tick runs one pass of the stack.
10 * Worker 0 calls the tick. The stack takes no lock, because no other context calls into it.
11 *
12 * Each cell comes from the plaintext cellblock. The cell counts to the owner of its unit, which
13 * IDEMIP_OWNERS names. worker.h puts those owners into the owner table of the program.
14 *
15 * A pass drains each receive ring one frame at a time. Dispatch.input reads the frame, the reply that it
16 * builds goes out on the same interface, and the descriptor goes back to the engine. Then the services and
17 * the flush of idemIP run in the order that Tick enforces. Last, the engine gives back the transmit
18 * descriptors that it finished.
19 *
20 * include/idemip_capabilities.h gives each IDEMIP_ENABLE_* value. idemIP sizes its tables from them and
21 * has no default for them.
22 *
23 * @author Douglas Quigg (dstroy0)
24 * @date 2026
25 */
26
27#ifndef PROTOCORE_STACK_H
28#define PROTOCORE_STACK_H
29
30#include "protocore_config.h" // the entry point: the enable gate below, and the widths
31
32#if PROTOCORE_ENABLE_IDEMIP
33
34// Only the driver table and the counts. A sketch includes this header beside protocore.h, and the whole
35// of idemIP has names that ProtoCore also has. The source of this module reads the whole of idemIP.
36#include "src/ethernet/phy.h" // IdemIpPhyDriver, and through it the counts of idemip_config.h
37
38EMBED_BEGIN_DECLS
39
40/**
41 * @brief What ::StackNs::start takes.
42 *
43 * @var StackStartArgs::isn_secret IDEMIP_TCP_ISN_SECRET_BYTES random octets, the RFC 6528 sec 3 secret
44 * of the initial sequence numbers. The stack copies them.
45 */
46typedef struct
47{
48 const uint8_t *isn_secret;
49} StackStartArgs;
50
51/**
52 * @brief What ::StackNs::bind takes: one interface and its link driver.
53 *
54 * @var StackBindArgs::driver the link driver of the interface [BORROWS]
55 * @var StackBindArgs::mac IDEMIP_MAC_LEN octets of hardware address [BORROWS]
56 * @var StackBindArgs::rx_base IDEMIP_RX_DESCRIPTORS buffers of IDEMIP_DMA_BUF_STRIDE octets, on a cache line
57 * @var StackBindArgs::tx_base IDEMIP_TX_DESCRIPTORS buffers of IDEMIP_DMA_BUF_STRIDE octets, on a cache line
58 * @var StackBindArgs::phy_addr the Clause 22 management address of the PHY, 0 through 31
59 * @var StackBindArgs::index the interface, below IDEMIP_NETIF_COUNT
60 */
61typedef struct
62{
63 const IdemIpPhyDriver *driver;
64 const uint8_t *mac;
65 uint8_t *rx_base;
66 uint8_t *tx_base;
67 uint8_t phy_addr;
68 uint8_t index;
69} StackBindArgs;
70
71/**
72 * @brief What ::StackNs::set_addr4 takes. Each address is in host order.
73 *
74 * @var StackAddr4Args::address the IPv4 address of the interface
75 * @var StackAddr4Args::mask its subnet mask
76 * @var StackAddr4Args::gateway its default gateway, 0 for none
77 * @var StackAddr4Args::index the interface
78 */
79typedef struct
80{
81 uint32_t address;
82 uint32_t mask;
83 uint32_t gateway;
84 uint8_t index;
85} StackAddr4Args;
86
87/**
88 * @brief What ::StackNs::tick takes.
89 *
90 * @var StackTickArgs::now_ms the monotonic millisecond count of the caller
91 * @var StackTickArgs::random_word a random word for this pass, which RFC 2236 sec 3 report delays draw from
92 */
93typedef struct
94{
95 uint32_t now_ms;
96 uint32_t random_word;
97} StackTickArgs;
98
99/**
100 * @brief What ::StackNs::transmit_hold and ::StackNs::transmit_post take.
101 *
102 * @var StackTransmitArgs::len the octets of the frame that ::StackNs::transmit_post sends, 1 through
103 * IDEMIP_DMA_FRAME_MAX
104 * @var StackTransmitArgs::index the interface
105 */
106typedef struct
107{
108 size_t len;
109 uint8_t index;
110} StackTransmitArgs;
111
112/**
113 * @brief The operands and the outcome.
114 *
115 * @var StackVars::start_args the secret that ::StackNs::start takes
116 * @var StackVars::bind_args the interface that ::StackNs::bind takes
117 * @var StackVars::addr4_args the address that ::StackNs::set_addr4 takes
118 * @var StackVars::tick_args the clock and the random word that ::StackNs::tick takes
119 * @var StackVars::transmit_args the interface and the frame length of a transmit entry
120 * @var StackVars::transmit_frame the buffer that the last ::StackNs::transmit_hold reported, NULL for none
121 * @var StackVars::ok the outcome of the last call
122 * @var StackVars::frames the frames that the last tick drained
123 * @var StackVars::sent the frames that the last tick put on a transmit ring
124 */
125typedef struct
126{
127 StackStartArgs start_args;
128 StackBindArgs bind_args;
129 StackAddr4Args addr4_args;
130 StackTickArgs tick_args;
131 StackTransmitArgs transmit_args;
132 uint8_t *transmit_frame;
133 embed_bool ok;
134 uint16_t frames;
135 uint16_t sent;
136} StackVars;
137
138/** @brief The operands and the outcome. */
139extern StackVars StackV;
140
141/**
142 * @brief The entries.
143 *
144 * @var StackNs::start clear each unit, seed the initial sequence numbers and bind the shared units
145 * @var StackNs::bind bind one interface to its link driver and its frame buffers
146 * @var StackNs::set_addr4 give an interface its IPv4 address
147 * @var StackNs::tick run one pass of the stack: drain, services, flush
148 * @var StackNs::transmit_hold hold the transmit buffer of an interface, and report it in
149 * ::StackVars::transmit_frame. Each interface holds one buffer at a time, and each
150 * frame of the interface goes out in it. A driver that gives its buffers out in turn
151 * thus gets each one back in turn.
152 * @var StackNs::transmit_post send the frame in the buffer that the interface holds
153 */
154typedef struct
155{
156 void (*const start)(uint8_t *work);
157 void (*const bind)(uint8_t *work);
158 void (*const set_addr4)(uint8_t *work);
159 void (*const tick)(uint8_t *work);
160 void (*const transmit_hold)(uint8_t *work);
161 void (*const transmit_post)(uint8_t *work);
162} StackNs;
163
164// The functions that the table binds. The .c defines each one, and each one takes one parameter.
165void protocore_stack_start(uint8_t *work);
166void protocore_stack_bind(uint8_t *work);
167void protocore_stack_set_addr4(uint8_t *work);
168void protocore_stack_tick(uint8_t *work);
169void protocore_stack_transmit_hold(uint8_t *work);
170void protocore_stack_transmit_post(uint8_t *work);
171
172/**
173 * @brief The cells of the unit borrows of idemIP, the region that each unit runs in.
174 *
175 * @param[in] work The borrow of this module [BORROWS].
176 * @note A caller above the network layer reads a unit through this, as the transport reads TcpPcb.
177 */
178/** @brief The units that one interface holds. */
179typedef struct
180{
181 uint8_t *phy;
182 uint8_t *dma;
183 uint8_t *nd6;
184 uint8_t *acd;
185 uint8_t *autoip;
186 uint8_t *pmtu6;
187 uint8_t *dhcp4;
188 uint8_t *dhcp6;
189 uint8_t *dad;
190 uint8_t *slaac;
191 uint8_t *rdnss;
192} StackIfaceUnits;
193
194/** @brief The units of the core layer. */
195typedef struct
196{
197 uint8_t *dispatch;
198 uint8_t *tick;
199 uint8_t *timeouts;
200 uint8_t *stats;
201} StackCoreUnits;
202
203/** @brief The units of the link layer. */
204typedef struct
205{
206 uint8_t *netif;
207 uint8_t *loopif;
208 uint8_t *arp;
209 uint8_t *vlan;
210 uint8_t *ethip6;
211} StackLinkUnits;
212
213/** @brief The units of the network layer. */
214typedef struct
215{
216 uint8_t *ip4_route;
217 uint8_t *ip4_forward;
218 uint8_t *ip6_forward;
219 uint8_t *ip4_reass;
220 uint8_t *ip4_frag;
221 uint8_t *ip6_reass;
222 uint8_t *ip6_frag;
223 uint8_t *igmp;
224 uint8_t *mld6;
225 uint8_t *icmp_in;
226 uint8_t *icmp6_in;
227 uint8_t *ip4_addr;
228 uint8_t *ip6_addr;
229 uint8_t *ip6_select;
230 uint8_t *pmtu4;
231} StackNetworkUnits;
232
233/** @brief The units of the transport layer. */
234typedef struct
235{
236 uint8_t *raw_pcb;
237 uint8_t *udp_pcb;
238 uint8_t *udplite;
239 uint8_t *tcp_pcb;
240 uint8_t *tcp_in;
241 uint8_t *tcp_out;
242 uint8_t *tcp_isn;
243} StackTransportUnits;
244
245/** @brief The units of the application layer. */
246typedef struct
247{
248 uint8_t *dns;
249} StackApplicationUnits;
250
251typedef struct
252{
253 StackCoreUnits core;
254 StackLinkUnits link;
255 StackNetworkUnits network;
256 StackTransportUnits transport;
257 StackApplicationUnits application;
258 StackIfaceUnits iface[IDEMIP_NETIF_COUNT];
259} StackUnits;
260
261/** @brief The unit borrows of the stack in @p work. */
262const StackUnits *protocore_stack_units(const uint8_t *work);
263
264// The table is `static const` with its initializer here, and not `extern` against a definition in
265// the .c. Each translation unit can see the initializer. `Stack.tick(work)` thus resolves to a named
266// function and becomes a direct call.
267static const StackNs Stack __attribute__((unused)) = {
268 .start = protocore_stack_start,
269 .bind = protocore_stack_bind,
270 .set_addr4 = protocore_stack_set_addr4,
271 .tick = protocore_stack_tick,
272 .transmit_hold = protocore_stack_transmit_hold,
273 .transmit_post = protocore_stack_transmit_post,
274};
275
276/** @brief The borrow of this module: its context, taken once from the persistent tier. */
277uint8_t *protocore_stack_span(void);
278
279EMBED_END_DECLS
280
281#endif // PROTOCORE_ENABLE_IDEMIP
282
283#endif // PROTOCORE_STACK_H