interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / src / fabric / fabric.h

324 lines · 15833 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * fabric.h -- AIEN Fabric interface F5-0: membership, capability
19 * advertisement, leases and loss detection as typed records keyed by
20 * AienMachineId (aien_machine_id.h), over an abstract transport.
21 *
22 * Scope (F5-0). Everything here runs in one process over the loopback
23 * transport (fab_loopback.h). No socket, no real network, no placement, no
24 * topology/latency measurement, no failure recovery beyond withdrawing a lost
25 * machine's capabilities. A real transport (AIENOS M6) plugs in through
26 * FabTransport and a real per-machine signature (TRUST-1 owner key) through
27 * FabAuth; nothing else changes.
28 *
29 * Who owns what.
30 * Roster Which machines may be members at all. Supplied by the caller
31 * from the AEGIS / owner-key boundary; the Fabric only reads it
32 * and has no call that adds to it.
33 * Membership This node's view of each enrolled peer: generation, last
34 * sequence, lease, state. One writer: fab_poll / fab_tick.
35 * Capabilities The canonical Capability Graph (rx_capq.h), touched only
36 * through its public calls: cq_wire_apply to ingest a peer's
37 * record, cq_machine_advertise_id to set the peer's lease,
38 * cq_withdraw when the peer is lost or leaves. The Fabric
39 * never grants authority: an entry's auth_* is a requirement,
40 * and whether it is held is decided by the World's authority
41 * view (CqHeld), never here. The build fails if fabric.o
42 * references an authority or World operation (mk/fabric.mk).
43 *
44 * Message (FAB_HDR_BYTES + body + FAB_SIG_BYTES; integers little-endian):
45 *
46 * off len field
47 * 0 4 magic "AFAB"
48 * 4 1 version 0x02 (0x01 had a 32-byte tag; refused as FAB_E_FORMAT)
49 * 5 1 kind FAB_MSG_*
50 * 6 2 body_len must equal the kind's body size
51 * 8 44 sender AienMachineId record
52 * 52 44 dest AienMachineId record (the one receiver it is for)
53 * 96 8 generation sender's membership generation, >= 1, rises on rejoin
54 * 104 8 seq sender's sequence within that generation, >= 1, rising
55 * 112 8 sent_us sender's clock (informational; covered by the sig)
56 * 120 n body
57 * 120+n 64 sig FabAuth signature over bytes 0 .. 120+n-1, by the sender.
58 * Fixed width 64 = an Ed25519 signature (RFC 8032), so the
59 * TRUST-1 owner-key signer fits without a format change.
60 *
61 * Bodies:
62 * JOIN 40: ontology digest (cq_ontology_digest, 32) + requested lease (u64 us)
63 * RENEW 8: requested lease (u64 us)
64 * ADVERTISE 188: one rx_capq wire record (CQ_WIRE_BYTES) of any capq kind
65 * (advertise, availability, withdraw); its machine must be the
66 * sender, so a machine changes or withdraws only its own entries
67 * LEAVE 0
68 *
69 * Receive rules, in order (first failure is the verdict; nothing changes
70 * except that an authenticated message of the held generation that passed the
71 * REPLAY check consumes its seq even when refused later, so a refused message
72 * can never succeed on replay):
73 * form FAB_E_FORMAT
74 * generation > FAB_GEN_MAX FAB_E_GENERATION (never truncated)
75 * dest != self, sender = self FAB_E_MISMATCH
76 * sender not on the roster FAB_E_NOT_ENROLLED
77 * sig does not verify FAB_E_AUTH (forged identity, altered bytes)
78 * generation < held FAB_E_STALE_GEN
79 * same generation, seq <= last FAB_E_REPLAY
80 * window not saved (store) FAB_E_STATE (see Restart; nothing applied)
81 * JOIN with generation <= held FAB_E_STALE_GEN
82 * non-JOIN, not a member, or a
83 * generation it never joined with FAB_E_NOT_MEMBER
84 * non-JOIN after the lease ended FAB_E_LEASE_EXPIRED (must rejoin, new generation)
85 * JOIN with another ontology FAB_E_ONTOLOGY
86 * ADVERTISE naming another machine FAB_E_MISMATCH (nobody speaks for a peer)
87 * ADVERTISE the graph refuses FAB_E_CAPQ (capq verdict in FabVerdict.capq)
88 * Before any check the node applies fab_tick(now), so loss is a function of
89 * the clock only and replays are deterministic.
90 *
91 * Leases. The receiver grants min(requested, FabConfig.max_lease_us) from its
92 * own clock and hands the end to the graph (cq_machine_advertise_id). When
93 * now >= lease end the member is LOST: every entry it advertised is withdrawn.
94 * A lost member comes back only by a JOIN with a higher generation.
95 *
96 * Generations never move back: a JOIN is accepted only with a generation
97 * above the one held, and anything older is FAB_E_STALE_GEN, so old traffic
98 * still in flight after a restart cannot roll membership back.
99 *
100 * Restart. With a FabStore the replay window (each peer's generation and
101 * last seq) is saved before it changes: a message of the held generation is
102 * saved before its seq is consumed, and a JOIN before it is admitted. A save
103 * that fails refuses the message with FAB_E_STATE and changes nothing in
104 * memory. On restart fab_node_init restores every peer as LOST with its
105 * generation and last seq, so a captured message replayed after the restart
106 * is FAB_E_STALE_GEN or FAB_E_REPLAY exactly as before it, and a peer comes
107 * back only by a JOIN with a higher generation. A missing (without
108 * store_fresh), unreadable or corrupt record is FAB_E_STATE: the node fails
109 * closed instead of starting with an empty window.
110 * Limits: the store must resist rollback; the record's sha256 detects damage,
111 * not an attacker who restores an older record (that needs the TRUST-1
112 * sealed / owner-key storage, not done here). A node with no store keeps the
113 * window in memory only and forgets it on restart. The node's own sending
114 * generation is not saved: after a restart the caller must give a higher one
115 * (fab_node_set_generation). Every authenticated fresh message costs one
116 * save. A JOIN refused before admission (ontology, capacity) is not reliably
117 * remembered: a refusal after its save only makes the record stricter until
118 * the next save rewrites it from memory.
119
120 * Capability generations. rx_capq accepts a record only when its generation
121 * is newer than the one held. Advertisers number entries with
122 * fab_entry_generation(node, rev) = generation << 32 | rev, so a rejoin with a
123 * new generation always outranks anything from before. Generations above
124 * FAB_GEN_MAX are refused, so the shift never drops a high bit.
125 */
126#ifndef FABRIC_H
127#define FABRIC_H
128
129#include "runtime/aien_machine_id.h"
130#include "runtime/rx_capq.h"
131#include "runtime/rx_jspace.h"
132
133#include <stddef.h>
134#include <stdint.h>
135
136#define FAB_VERSION 0x02u
137#define FAB_HDR_BYTES 120u
138#define FAB_SIG_BYTES 64u /* Ed25519-sized signature, fixed width */
139#define FAB_JOIN_BODY 40u
140#define FAB_RENEW_BODY 8u
141#define FAB_ADVERTISE_BODY CQ_WIRE_BYTES
142#define FAB_MSG_MAX (FAB_HDR_BYTES + FAB_ADVERTISE_BODY + FAB_SIG_BYTES)
143
144#define FAB_MAX_MEMBERS 16u /* peers one node tracks */
145#define FAB_MAX_KEYS 64u /* capability entries one peer may advertise */
146#define FAB_EVENT_RING 256u
147
148/* Membership generations are 1 .. FAB_GEN_MAX: entry generations pack
149 * generation << 32 | rev into 64 bits, so a wider one would lose its high
150 * bits. Anything above is FAB_E_GENERATION everywhere (init, set, seal,
151 * receive), never truncated. */
152#define FAB_GEN_MAX UINT32_MAX
153
154/* Replay-window record (see FabStore): "AFST" | u32 version | self record |
155 * u32 n | n x { machine record | u64 generation | u64 last_seq } | sha256 of
156 * everything before it. */
157#define FAB_STATE_VERSION 1u
158#define FAB_STATE_ENTRY (AIEN_MID_RECORD_BYTES + 16u)
159#define FAB_STATE_MAX (12u + AIEN_MID_RECORD_BYTES + FAB_MAX_MEMBERS * FAB_STATE_ENTRY + 32u)
160
161enum { FAB_MSG_JOIN = 1, FAB_MSG_RENEW = 2, FAB_MSG_ADVERTISE = 3, FAB_MSG_LEAVE = 4 };
162
163/* Member states in this node's view. */
164enum { FAB_ST_NONE = 0, FAB_ST_JOINED = 1, FAB_ST_LOST = 2, FAB_ST_LEFT = 3 };
165
166#define FAB_OK 0
167#define FAB_E_ARG -1
168#define FAB_E_FORMAT -2
169#define FAB_E_MISMATCH -3
170#define FAB_E_NOT_ENROLLED -4
171#define FAB_E_AUTH -5
172#define FAB_E_STALE_GEN -6
173#define FAB_E_REPLAY -7
174#define FAB_E_LEASE_EXPIRED -8
175#define FAB_E_NOT_MEMBER -9
176#define FAB_E_ONTOLOGY -10
177#define FAB_E_CAPQ -11
178#define FAB_E_FULL -12
179#define FAB_E_TRANSPORT -13
180#define FAB_E_GENERATION -14 /* membership generation outside 1 .. FAB_GEN_MAX */
181#define FAB_E_STATE -15 /* replay-window store missing, corrupt or not durable */
182
183/* ---- what a real transport and a real signer must provide ---- */
184
185/* Moves opaque messages between machines. send: 0 or FAB_E_TRANSPORT.
186 * recv: 1 with a message in buf, 0 when none is waiting, <0 on error. The
187 * Fabric authenticates every message itself; the transport is not trusted. */
188typedef struct {
189 void *ctx;
190 int (*send)(void *ctx, const AienMachineId *from, const AienMachineId *to,
191 const uint8_t *msg, size_t len);
192 int (*recv)(void *ctx, const AienMachineId *self, uint8_t *buf, size_t cap, size_t *len);
193} FabTransport;
194
195/* Signs as this node; verifies a signature as coming from `claimed`. 0 = ok.
196 * The signature is always FAB_SIG_BYTES (64) bytes; a signer with a shorter
197 * native output must still define every byte (the stand-in derives 64). */
198typedef struct {
199 void *ctx;
200 int (*sign)(void *ctx, const uint8_t *msg, size_t len, uint8_t sig[FAB_SIG_BYTES]);
201 int (*verify)(void *ctx, const AienMachineId *claimed, const uint8_t *msg, size_t len,
202 const uint8_t sig[FAB_SIG_BYTES]);
203} FabAuth;
204
205/* Where the replay window survives a restart. Supplied by the caller (the
206 * Fabric itself touches no file, socket or process). save: 0 only once the
207 * record is durable, anything else is a failure. load: 1 with a record in buf
208 * (len bytes), 0 when nothing was ever saved, <0 on error. */
209typedef struct {
210 void *ctx;
211 int (*save)(void *ctx, const uint8_t *rec, size_t len);
212 int (*load)(void *ctx, uint8_t *buf, size_t cap, size_t *len);
213} FabStore;
214
215/* Enrolled machines (from the AEGIS / owner-key boundary). Read-only here. */
216typedef struct {
217 uint32_t n;
218 const AienMachineId *ids;
219} FabRoster;
220
221typedef struct {
222 AienMachineId self;
223 const FabRoster *roster;
224 CqCatalog *catalog; /* canonical catalog (cq_catalog_init_canonical) */
225 const FabTransport *transport;
226 const FabAuth *auth;
227 uint64_t max_lease_us; /* longest lease this node grants */
228 uint64_t generation; /* this node's membership generation, 1 .. FAB_GEN_MAX */
229 /* Replay window store. NULL keeps the window in memory only (loopback
230 * tests); a node that can restart must have one. store_fresh = 1 is the
231 * caller saying this is the node's first start: the store must then be
232 * empty. With store_fresh = 0 an empty, unreadable or corrupt store is
233 * FAB_E_STATE: the node does not start rather than forget. */
234 const FabStore *store;
235 uint32_t store_fresh;
236} FabConfig;
237
238/* ---- typed records ---- */
239
240typedef struct {
241 AienMachineId machine;
242 uint32_t state; /* FAB_ST_* */
243 uint32_t index; /* rx_capq machine index (0 = never joined) */
244 uint64_t generation;
245 uint64_t last_seq;
246 uint64_t lease_until_us;
247 uint32_t n_keys; /* entries it advertised and holds live or withdrawn */
248 CqKey keys[FAB_MAX_KEYS];
249} FabMember;
250
251enum { FAB_EV_JOINED = 1, FAB_EV_RENEWED, FAB_EV_ADVERTISED, FAB_EV_LOST, FAB_EV_LEFT,
252 FAB_EV_REFUSED };
253
254typedef struct {
255 uint32_t kind; /* FAB_EV_* */
256 int32_t code; /* FAB_OK or the refusal */
257 int32_t capq; /* rx_capq verdict for ADVERTISE, else 0 */
258 uint32_t msg_kind; /* FAB_MSG_* that caused it, 0 for a tick */
259 AienMachineId machine; /* the peer it is about (zero id if unparsable) */
260 uint64_t generation, seq, at_us;
261} FabEvent;
262
263typedef struct {
264 int code; /* FAB_OK or FAB_E_* */
265 int capq; /* rx_capq verdict (ADVERTISE), else 0 */
266 uint32_t msg_kind;
267 AienMachineId sender;
268} FabVerdict;
269
270typedef struct {
271 FabConfig cfg;
272 uint64_t seq; /* last sequence sent in cfg.generation */
273 uint32_t n_members;
274 FabMember members[FAB_MAX_MEMBERS];
275 uint64_t n_events;
276 FabEvent events[FAB_EVENT_RING];
277 sha256_ctx event_hash; /* every event, in order */
278 uint64_t counts[16]; /* refusals by -code */
279} FabNode;
280
281/* ---- node ---- */
282
283/* FAB_E_ARG for a bad config (generation 0 included), FAB_E_GENERATION for a
284 * generation above FAB_GEN_MAX, FAB_E_STATE when cfg->store cannot be read,
285 * holds a damaged record or one for another machine, is empty without
286 * store_fresh, or is not empty with it. On any error n holds no usable node. */
287int fab_node_init(FabNode *n, const FabConfig *cfg);
288/* Start a new membership generation (after a restart): must be higher
289 * (FAB_E_STALE_GEN) and at most FAB_GEN_MAX (FAB_E_GENERATION). */
290int fab_node_set_generation(FabNode *n, uint64_t generation);
291uint64_t fab_entry_generation(const FabNode *n, uint32_t rev);
292
293/* Seal a message as this node (next sequence) into out (FAB_MSG_MAX). */
294int fab_seal(FabNode *n, const AienMachineId *dest, uint32_t kind, const uint8_t *body,
295 size_t body_len, uint64_t now_us, uint8_t *out, size_t *out_len);
296
297/* Send to every other roster machine. */
298int fab_join(FabNode *n, uint64_t lease_us, uint64_t now_us);
299int fab_renew(FabNode *n, uint64_t lease_us, uint64_t now_us);
300int fab_leave(FabNode *n, uint64_t now_us);
301/* Advertise one of this node's own entries (capq wire kind CQ_WIRE_*). */
302int fab_advertise(FabNode *n, const CqKey *own, uint32_t wire_kind, uint64_t now_us);
303
304/* Apply the clock: members whose lease ended become LOST and their entries are
305 * withdrawn. Returns how many became lost. */
306int fab_tick(FabNode *n, uint64_t now_us);
307/* Process one raw message (normally from the transport). */
308int fab_receive(FabNode *n, const uint8_t *msg, size_t len, uint64_t now_us, FabVerdict *v);
309/* Receive and process one waiting message: 1 processed, 0 none, <0 transport. */
310int fab_poll(FabNode *n, uint64_t now_us, FabVerdict *v);
311
312/* ---- views ---- */
313
314const FabMember *fab_member(const FabNode *n, const AienMachineId *m);
315/* 1 when m is a JOINED member whose lease is live at now_us. */
316int fab_member_live(const FabNode *n, const AienMachineId *m, uint64_t now_us);
317/* J-Space placement for m: LOCAL for self, REMOTE_OWNED for a live member;
318 * FAB_E_NOT_MEMBER otherwise (J-Space must not place on a lost machine). */
319int fab_home(const FabNode *n, const AienMachineId *m, uint64_t now_us, JsHome *out);
320/* Digest of the membership view and the event history (replay check). */
321void fab_state_digest(const FabNode *n, uint8_t out[32]);
322const char *fab_strerror(int code);
323
324#endif /* FABRIC_H */