ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
edge_mesh.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 edge_mesh.h
6 * @brief CDN edge-cache tier - mesh (sibling-cache) wire codec + async peer-query engine
7 * (PROTOCORE_ENABLE_EDGE_MESH).
8 *
9 * Lets a fleet of edge nodes share one warm cache. On a full local miss a node queries its sibling peers
10 * (over a plaintext ProtoConn::PROTO_MESH TCP link) with a content-addressed request and pulls a fresh copy
11 * from whichever peer has it, instead of re-fetching the origin. Pull (read-through) only: no push, no
12 * invalidation. The transfer carries the object plus its freshness/age, so a sibling-fresh object serves for
13 * its remaining lifetime with zero origin contact (RFC 9111 age propagation).
14 *
15 * This file is the pure, host-testable half: the request/response frame codec, the freshness-carrying entry
16 * frame (the shared ::edge_sd_serialize body plus a fixed timing trailer), and the async requester engine
17 * over the same EdgeFetchTransport seam the origin fetch uses (protocore_client on device, a mock in host tests).
18 * The server glue (the peer table, the pre-origin query phase, and the PROTO_MESH serving listener) lives in
19 * edge_cache_proxy. Zero heap; fixed buffers.
20 *
21 * Wire format (little-endian, versioned; magic 'E','M'):
22 * request : 'E' 'M' | ver=1 | op=GET(1) | digest[32] | u16 key_len + key | u16 hdrs_len + req_hdrs
23 * response: 'E' 'M' | ver=1 | status (MISS=0 / HIT=1) | [ u16 entry_len + entry_frame ] (entry on HIT)
24 * entry : timing trailer (i64 date | i64 expires | u32 lifetime_s | u32 age_hdr | u32 current_age)
25 * followed by an ::edge_sd_serialize body (content: key/status/ct/validators/encoding/Vary/body)
26 *
27 * @author Douglas Quigg (dstroy0)
28 * @date 2026
29 */
30
31#ifndef PROTOCORE_EDGE_MESH_H
32#define PROTOCORE_EDGE_MESH_H
33
34#include "protocore_config.h" // the entry point: protocore_types.h for the widths
35
36#if PROTOCORE_ENABLE_EDGE_MESH
37
38EMBED_BEGIN_DECLS
39
40// This module holds nothing between calls, so it carves no borrow and states none. An entry
41// takes one all the same, and never reads it, so every namespace in the tree is invoked the
42// same way.
43
44#define PROTOCORE_EDGE_MESH_MAGIC0 ('E')
45
46#define PROTOCORE_EDGE_MESH_MAGIC1 ('M')
47
48#define PROTOCORE_EDGE_MESH_VERSION 1
49
50#define PROTOCORE_EDGE_MESH_OP_GET 1 ///< the only request opcode: fetch by content address
51
52#define PROTOCORE_EDGE_MESH_REQ_MAX (2 + 1 + 1 + 32 + 2 + PROTOCORE_EDGE_KEY_MAX + 2 + PROTOCORE_MESH_HDRS_MAX)
53
54/** @brief Tri-state parse result for the length-delimited frames (partial reads accumulate to complete). */
56 EdgeMeshParse,
57 EDGE_MESH_PARSE_MALFORMED = -1, ///< bad magic/version/opcode, or a field that cannot fit the destination
58 EDGE_MESH_PARSE_INCOMPLETE = 0, ///< a valid prefix so far - need more bytes
59 EDGE_MESH_PARSE_MISS = 1, ///< a complete response with no object
60 EDGE_MESH_PARSE_HIT = 2, ///< a complete request (outputs filled) / a complete response carrying an entry
61);
62
63/** @brief Peer-query progress. */
64EMBED_ENUM_BYTE(EdgeMeshStatus,
65 EDGE_MESH_STATUS_PENDING, ///< still connecting / receiving
66 EDGE_MESH_STATUS_HIT, ///< a complete entry frame arrived (entry_off / entry_len valid)
67 EDGE_MESH_STATUS_MISS, ///< the peer does not have (a fresh copy of) the object
68 EDGE_MESH_STATUS_FAILED, ///< connect / send / timeout / closed-before-complete / malformed
69);
70
71/**
72 * @brief One in-flight peer query (zero-heap). The response accumulates into a caller-owned @c buf (>=
73 * PROTOCORE_EDGE_MESH_RESP_MAX) supplied at begin - a fetch slot reuses its origin buffer, since the mesh and
74 * origin phases never run at once.
75 */
76typedef struct
77{
78 EdgeMeshStatus st;
79 int cid;
80 uint32_t start_ms;
81 size_t got; ///< response bytes accumulated
82 size_t entry_off; ///< offset of the entry frame within buf (valid on HIT)
83 size_t entry_len; ///< length of the entry frame (valid on HIT)
84 uint8_t *buf; ///< caller-owned accumulation buffer
85 size_t cap; ///< its capacity (must be >= PROTOCORE_EDGE_MESH_RESP_MAX)
86} EdgeMeshFetch;
87
88/** @brief EdgeEntry, as the caller already knows it. */
89struct EdgeEntry;
90
91/** @brief EdgeFetchTransport, as the caller already knows it. */
93
94/** @brief What build_request takes: digest, canon, req_hdrs, out, cap. */
95typedef struct
96{
97 const uint8_t *digest; ///< 32 bytes.
98 const char *canon;
99 const char
100 *req_hdrs; ///< the requester's header snapshot (name RS value US ...) so the peer can match Vary variants; ...
101 uint8_t *out;
102 size_t cap;
103} EdgeMeshBuildRequestArgs;
104
105/** @brief What parse_request takes: buf, len, digest_out, canon_out, ... */
106typedef struct
107{
108 const uint8_t *buf;
109 size_t len;
110 uint8_t *digest_out; ///< 32 bytes.
111 char *canon_out;
112 size_t canon_cap;
113 char *hdrs_out;
114 size_t hdrs_cap;
115} EdgeMeshParseRequestArgs;
116
117/** @brief What serialize_entry takes: e, current_age, out, cap. */
118typedef struct
119{
120 const struct EdgeEntry *e;
121 long current_age; ///< the sender's corrected age for e at send time (RFC 9111 sec 4.2.3), clamped >= 0
122 uint8_t *out;
123 size_t cap;
124} EdgeMeshSerializeEntryArgs;
125
126/** @brief What deserialize_entry takes: entry_buf, buf, len, e, now_ms. */
127typedef struct
128{
129 uint8_t *entry_buf;
130 const uint8_t *buf;
131 size_t len;
132 struct EdgeEntry *e;
133 uint32_t now_ms;
134} EdgeMeshDeserializeEntryArgs;
135
136/** @brief What build_response takes: hit, entry, entry_len, out, cap. */
137typedef struct
138{
139 embed_bool hit;
140 const uint8_t *entry;
141 size_t entry_len;
142 uint8_t *out;
143 size_t cap;
144} EdgeMeshBuildResponseArgs;
145
146/** @brief What parse_response takes: buf, len, entry_off, entry_len. */
147typedef struct
148{
149 const uint8_t *buf;
150 size_t len;
151 size_t *entry_off;
152 size_t *entry_len;
153} EdgeMeshParseResponseArgs;
154
155/** @brief What fetch_begin takes: m, t, host, port, request, req_len, ... */
156typedef struct
157{
158 EdgeMeshFetch *m;
159 const struct EdgeFetchTransport *t;
160 const char *host;
161 uint16_t port;
162 const uint8_t *request;
163 size_t req_len;
164 uint8_t *buf;
165 size_t cap;
166 uint32_t now_ms;
167} EdgeMeshFetchBeginArgs;
168
169/** @brief What fetch_pump takes: m, t, now_ms. */
170typedef struct
171{
172 EdgeMeshFetch *m;
173 const struct EdgeFetchTransport *t;
174 uint32_t now_ms;
175} EdgeMeshFetchPumpArgs;
176
177/** @brief What fetch_end takes: m, t. */
178typedef struct
179{
180 EdgeMeshFetch *m;
181 const struct EdgeFetchTransport *t;
182} EdgeMeshFetchEndArgs;
183
184/**
185 * @brief CDN edge-cache tier - mesh (sibling-cache) wire codec + async peer-query engine (PROTOCORE_ENABLE_EDGE_MESH).
186 * ...
187 *
188 * A caller sets the members a call takes, invokes it through ::EdgeMesh with the bytes it runs
189 * out of, and reads the outcome off the same handle.
190 *
191 * EdgeMesh.build_request_args.digest = ...;
192 * EdgeMesh.build_request_args.canon = ...;
193 * EdgeMesh.build_request_args.req_hdrs = ...;
194 * EdgeMesh.build_request_args.out = ...;
195 * EdgeMesh.build_request_args.cap = ...;
196 * EdgeMesh.build_request(work);
197 * // EdgeMesh.n is what the call reports
198 *
199 * @var EdgeMeshNs::build_request_args what build_request takes: digest, canon, req_hdrs, out, cap
200 * @var EdgeMeshNs::parse_request_args what parse_request takes: buf, len, digest_out, canon_out,
201 * @var EdgeMeshNs::serialize_entry_args what serialize_entry takes: e, current_age, out, cap
202 * @var EdgeMeshNs::deserialize_entry_args what deserialize_entry takes: entry_buf, buf, len, e, now_ms
203 * @var EdgeMeshNs::build_response_args what build_response takes: hit, entry, entry_len, out, cap
204 * @var EdgeMeshNs::parse_response_args what parse_response takes: buf, len, entry_off, entry_len
205 * @var EdgeMeshNs::fetch_begin_args what fetch_begin takes: m, t, host, port, request, req_len,
206 * @var EdgeMeshNs::fetch_pump_args what fetch_pump takes: m, t, now_ms
207 * @var EdgeMeshNs::fetch_end_args what fetch_end takes: m, t
208 * @var EdgeMeshNs::ok a call's true/false outcome
209 * @var EdgeMeshNs::n the entry-frame length, or 0 if it would not fit cap
210 * @var EdgeMeshNs::parse EDGE_MESH_PARSE_HIT with digest_out / canon_out / hdrs_out filled ...
211 * @var EdgeMeshNs::status what a call reports
212 * @var EdgeMeshNs::build_request build a GET request for digest / canon into out
213 * @var EdgeMeshNs::parse_request parse an accumulated request buffer
214 * @var EdgeMeshNs::serialize_entry serialize e (content via the shared ::edge_sd_serialize) plus a ...
215 * @var EdgeMeshNs::deserialize_entry rehydrate e from an entry frame: content via ::edge_sd_deserialize, ...
216 * @var EdgeMeshNs::build_response build a response (hit -> carry entry / entry_len; else a MISS). ...
217 * @var EdgeMeshNs::parse_response parse an accumulated response buffer
218 * @var EdgeMeshNs::fetch_begin dial host:port, send request, begin receiving into buf (cap >= ...
219 * @var EdgeMeshNs::fetch_pump drain available bytes and advance; honors PROTOCORE_MESH_QUERY_MS. ...
220 * @var EdgeMeshNs::fetch_end release the peer connection (idempotent)
221 *
222 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
223 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
224 * a caller drives every namespace the same way.
225 */
226typedef struct
227{
228 EdgeMeshBuildRequestArgs build_request_args;
229 EdgeMeshParseRequestArgs parse_request_args;
230 EdgeMeshSerializeEntryArgs serialize_entry_args;
231 EdgeMeshDeserializeEntryArgs deserialize_entry_args;
232 EdgeMeshBuildResponseArgs build_response_args;
233 EdgeMeshParseResponseArgs parse_response_args;
234 EdgeMeshFetchBeginArgs fetch_begin_args;
235 EdgeMeshFetchPumpArgs fetch_pump_args;
236 EdgeMeshFetchEndArgs fetch_end_args;
237 embed_bool ok;
238 size_t n;
239 EdgeMeshParse parse;
240 EdgeMeshStatus status;
241} EdgeMeshVars;
242
243/** @brief The operands and the outcome. */
244extern EdgeMeshVars EdgeMeshV;
245
246/** @brief The entries. */
247typedef struct
248{
249 void (*const build_request)(const uint8_t *work);
250 void (*const parse_request)(const uint8_t *work);
251 void (*const serialize_entry)(const uint8_t *work);
252 void (*const deserialize_entry)(uint8_t *work);
253 void (*const build_response)(const uint8_t *work);
254 void (*const parse_response)(const uint8_t *work);
255 void (*const fetch_begin)(const uint8_t *work);
256 void (*const fetch_pump)(const uint8_t *work);
257 void (*const fetch_end)(const uint8_t *work);
258} EdgeMeshNs;
259
260// What the table binds, defined once in the .c and taking one parameter each: everything
261// else an entry needs is an operand in EdgeMeshV or a region of the borrow at a fixed offset.
262void protocore_edge_mesh_build_request(const uint8_t *work);
263void protocore_edge_mesh_parse_request(const uint8_t *work);
264void protocore_edge_mesh_serialize_entry(const uint8_t *work);
265void protocore_edge_mesh_deserialize_entry(uint8_t *work);
266void protocore_edge_mesh_build_response(const uint8_t *work);
267void protocore_edge_mesh_parse_response(const uint8_t *work);
268void protocore_edge_mesh_fetch_begin(const uint8_t *work);
269void protocore_edge_mesh_fetch_pump(const uint8_t *work);
270void protocore_edge_mesh_fetch_end(const uint8_t *work);
271
272// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
273// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
274// `EdgeMesh.build_request(work)` resolves to a named function and becomes a DIRECT call. An extern table
275// leaves the call indirect and the symbol live at every level, -O2 -flto included.
276static const EdgeMeshNs EdgeMesh __attribute__((unused)) = {
277 .build_request = protocore_edge_mesh_build_request,
278 .parse_request = protocore_edge_mesh_parse_request,
279 .serialize_entry = protocore_edge_mesh_serialize_entry,
280 .deserialize_entry = protocore_edge_mesh_deserialize_entry,
281 .build_response = protocore_edge_mesh_build_response,
282 .parse_response = protocore_edge_mesh_parse_response,
283 .fetch_begin = protocore_edge_mesh_fetch_begin,
284 .fetch_pump = protocore_edge_mesh_fetch_pump,
285 .fetch_end = protocore_edge_mesh_fetch_end,
286};
287
288EMBED_END_DECLS
289
290#endif // PROTOCORE_ENABLE_EDGE_MESH
291
292#endif // PROTOCORE_EDGE_MESH_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.
The origin transport, bound to protocore_client on the device and a mock in host tests.
Definition edge_fetch.h:35