prns-runtime-embassy 0.3.6

Embassy host runtime for Personal Reticulum
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use embassy_futures::select::{select6, Either6};
use embassy_futures::yield_now;
use embassy_sync::blocking_mutex::raw::RawMutex;
use embassy_sync::channel::Receiver;
use heapless::Vec as HeaplessVec;

use crate::engine::{
    ClassifiedInboundPacket, Departure, EngineState, IngestIo, IssuedCommand, Journaled,
    ProofRequest,
};
use crate::interfaces::InterfaceIfac;
use crate::interfaces::{AttachedInterfaces, InboundPacket, InterfaceDescriptor, InterfaceId};
use crate::manifold::grant::{FrameTarget, ManifoldLaneReader};
use crate::manifold::interface_seam::{EMBEDDED_MAX_LINK_MTU, EMBEDDED_MAX_WIRE_FRAME_LEN};
use crate::manifold::kernel::{fire_due_reason, merge_wake_schedules_delta};
use crate::manifold::timers::{wait_for_due_reason, wait_for_pacer};
use crate::manifold::{AppDeciders, Host};
use crate::routing::links::resources::ResourceOffer;
use crate::runtime::{InterfaceInspectionStore, ManifoldPersistence};
use crate::storage::{DirtyInterfaceSet, StorageLayout};

use super::egress::{
    flush_due_pacers, ifac_for, route_reaction, soonest_pacer_release, InterfacePacer,
    ManifoldEgress, PooledEgress,
};
use super::packet_phy::retain_packet_phy;

/// Changes the live descriptor set without reallocating the fixed lane pool.
#[repr(C)]
pub enum InterfaceLifecycle {
    Add {
        descriptor: InterfaceDescriptor,
    },
    Remove {
        id: InterfaceId,
    },
    Update {
        descriptor: InterfaceDescriptor,
    },
    Retag {
        old_id: InterfaceId,
        new_id: InterfaceId,
        descriptor: InterfaceDescriptor,
    },
}

fn clamp_to_embedded_ceiling(mut descriptor: InterfaceDescriptor) -> InterfaceDescriptor {
    if let Some(mtu) = descriptor.hardware_mtu {
        descriptor.hardware_mtu = Some(mtu.min(EMBEDDED_MAX_LINK_MTU));
    }
    descriptor
}

fn inbound_source(
    lane_id: InterfaceId,
    stamped_source: InterfaceId,
    descriptors: &[InterfaceDescriptor],
) -> InterfaceId {
    if descriptors
        .iter()
        .any(|descriptor| descriptor.id == lane_id)
    {
        lane_id
    } else {
        stamped_source
    }
}

/// Borrowed lanes and channels for one pooled-topology manifold run.
pub struct PooledWiring<
    'run,
    M: RawMutex + 'static,
    const LANE_COUNT: usize,
    const INTERFACE_CAPACITY: usize,
    const NOTIFY: usize,
    const COMMANDS: usize,
    const LIFECYCLE: usize,
> {
    pub descriptors: &'run mut HeaplessVec<InterfaceDescriptor, INTERFACE_CAPACITY>,
    pub ifacs: &'run mut HeaplessVec<InterfaceIfac, LANE_COUNT>,
    pub inbound:
        &'run mut HeaplessVec<(InterfaceId, &'static mut dyn ManifoldLaneReader), LANE_COUNT>,
    pub egress: &'run mut PooledEgress<LANE_COUNT>,
    pub notify: Receiver<'run, M, InterfaceId, NOTIFY>,
    pub commands: Receiver<'run, M, IssuedCommand, COMMANDS>,
    pub lifecycle: Receiver<'run, M, InterfaceLifecycle, LIFECYCLE>,
}

/// Runs a mutable descriptor set over a fixed lane pool; `LANE_COUNT` bounds pacers.
pub(crate) async fn run_pooled<
    S,
    H,
    M,
    Store,
    const LANE_COUNT: usize,
    const INTERFACE_CAPACITY: usize,
    const NOTIFY: usize,
    const COMMANDS: usize,
    const LIFECYCLE: usize,
>(
    engine: &mut EngineState<S>,
    host: &mut H,
    wiring: PooledWiring<'_, M, LANE_COUNT, INTERFACE_CAPACITY, NOTIFY, COMMANDS, LIFECYCLE>,
    mut on_journaled: impl FnMut(Journaled<'_>),
    deciders: AppDeciders<impl FnMut(&ProofRequest) -> bool, impl FnMut(&ResourceOffer) -> bool>,
    store: &Store,
    persistence: &mut impl ManifoldPersistence<S>,
) where
    S: StorageLayout,
    H: Host,
    M: RawMutex + 'static,
    Store: InterfaceInspectionStore,
{
    let AppDeciders {
        mut should_prove,
        mut should_accept_resource,
    } = deciders;
    let PooledWiring {
        descriptors,
        ifacs,
        inbound,
        egress,
        notify,
        commands,
        lifecycle,
    } = wiring;
    let mut pacers: HeaplessVec<InterfacePacer, LANE_COUNT> = HeaplessVec::new();
    for descriptor in descriptors.iter_mut() {
        *descriptor = clamp_to_embedded_ceiling(*descriptor);
        engine.interface_attached(descriptor.id, host.now());
        if let Some(lane) = egress.lane_for(descriptor.id) {
            if !pacers.iter().any(|pacer| pacer.id == lane) {
                let _ = pacers.push(InterfacePacer::from_descriptor(lane, descriptor));
            }
        }
    }
    let mut wake_schedules = engine.wake_schedules(AttachedInterfaces::new(&*descriptors));
    loop {
        let wake = wake_schedules.soonest(host.now());
        let pacer_wake = soonest_pacer_release(&pacers);

        let persistence_deadline = persistence.deadline(host.now());
        match select6(
            notify.receive(),
            commands.receive(),
            wait_for_due_reason(&*host, wake),
            wait_for_pacer(&*host, pacer_wake),
            lifecycle.receive(),
            wait_for_persistence(&*host, persistence_deadline),
        )
        .await
        {
            Either6::First(_) => {
                while notify.try_receive().is_ok() {}
                for (lane_id, lane) in inbound.iter_mut() {
                    while let Some((target, packet_phy, frame)) = lane.try_read() {
                        let FrameTarget::Direct(stamped_source) = target else {
                            lane.release();
                            continue;
                        };
                        // A dedicated lane's live key is authoritative. Runtime retagging updates
                        // that key atomically with the descriptor, while the already-constructed
                        // interface seam can still have stamped a queued frame with its prior id.
                        // Fleet lanes have no descriptor of their own, so their per-member stamp
                        // remains authoritative instead.
                        let source = inbound_source(*lane_id, stamped_source, descriptors);
                        let mut unmasked = [0u8; EMBEDDED_MAX_WIRE_FRAME_LEN];
                        let bytes = match ifac_for(ifacs, *lane_id) {
                            Some(entry) => {
                                let Some(clean_len) =
                                    entry.context.unmask_inbound(frame, &mut unmasked)
                                else {
                                    lane.release();
                                    continue;
                                };
                                &mut unmasked[..clean_len]
                            }
                            None => frame,
                        };
                        let now = host.now();
                        let packet = ClassifiedInboundPacket::classify(InboundPacket {
                            arrived_at: now,
                            source_interface: source,
                            bytes,
                        });
                        retain_packet_phy(store, &packet, packet_phy);
                        let delta = engine.ingest_classified_into(
                            packet,
                            IngestIo {
                                interfaces: AttachedInterfaces::new(&*descriptors),
                                now,
                                fill_entropy: &mut |entropy| host.fill_entropy(entropy),
                                should_prove: &mut should_prove,
                                should_accept_resource: &mut should_accept_resource,
                                sink: &mut |reaction| {
                                    route_reaction(
                                        reaction,
                                        &mut *egress,
                                        ifacs,
                                        &mut pacers,
                                        now,
                                        &mut |journaled| {
                                            persistence.observe(&journaled, now);
                                            on_journaled(journaled);
                                        },
                                    )
                                },
                            },
                        );
                        lane.release();
                        merge_wake_schedules_delta(
                            &mut wake_schedules,
                            delta,
                            &*engine,
                            AttachedInterfaces::new(&*descriptors),
                        );
                    }
                }
            }
            Either6::Second(issued) => {
                let now = host.now();
                let delta = engine.ingest_command_into(
                    issued,
                    AttachedInterfaces::new(&*descriptors),
                    now,
                    &mut |entropy| host.fill_entropy(entropy),
                    &mut |reaction| {
                        route_reaction(
                            reaction,
                            &mut *egress,
                            ifacs,
                            &mut pacers,
                            now,
                            &mut |journaled| {
                                persistence.observe(&journaled, now);
                                on_journaled(journaled);
                            },
                        )
                    },
                );
                merge_wake_schedules_delta(
                    &mut wake_schedules,
                    delta,
                    &*engine,
                    AttachedInterfaces::new(&*descriptors),
                );
            }
            Either6::Third(reason) => {
                let now = host.now();
                let delta = fire_due_reason(
                    &mut *engine,
                    reason,
                    now,
                    AttachedInterfaces::new(&*descriptors),
                    &mut |bytes| host.fill_entropy(bytes),
                    &mut |reaction| {
                        route_reaction(
                            reaction,
                            &mut *egress,
                            ifacs,
                            &mut pacers,
                            now,
                            &mut |journaled| {
                                persistence.observe(&journaled, now);
                                on_journaled(journaled);
                            },
                        )
                    },
                );
                merge_wake_schedules_delta(
                    &mut wake_schedules,
                    delta,
                    &*engine,
                    AttachedInterfaces::new(&*descriptors),
                );
            }
            Either6::Fourth(()) => {
                let now = host.now();
                flush_due_pacers(&mut pacers, now, &mut *egress, ifacs);
            }
            Either6::Fifth(message) => match message {
                InterfaceLifecycle::Add { descriptor } => {
                    let descriptor = clamp_to_embedded_ceiling(descriptor);
                    let id = descriptor.id;
                    let present = descriptors.iter().any(|existing| existing.id == id);
                    if !present {
                        engine.interface_attached(id, host.now());
                        let _ = descriptors.push(descriptor);
                        if let Some(lane) = egress.lane_for(id) {
                            if !pacers.iter().any(|pacer| pacer.id == lane) {
                                let _ =
                                    pacers.push(InterfacePacer::from_descriptor(lane, &descriptor));
                            }
                        }
                        wake_schedules =
                            engine.wake_schedules(AttachedInterfaces::new(&*descriptors));
                    }
                    #[cfg(feature = "log")]
                    log::info!(
                        target: "personal_hopspot_esp32",
                        "manifold: Add kind={:?} present={present} descriptors={}",
                        id.kind(),
                        descriptors.len()
                    );
                }
                InterfaceLifecycle::Remove { id } => {
                    let now = host.now();
                    let departed_lane = egress.lane_for(id);
                    engine.interface_departed(id, Departure::Forgotten, now);
                    let found = descriptors
                        .iter()
                        .position(|descriptor| descriptor.id == id);
                    if let Some(pos) = found {
                        let _ = descriptors.swap_remove(pos);
                    }
                    #[cfg(feature = "log")]
                    log::info!(
                        target: "personal_hopspot_esp32",
                        "manifold: Remove kind={:?} found={} descriptors={}",
                        id.kind(),
                        found.is_some(),
                        descriptors.len()
                    );
                    if let Some(lane) = departed_lane {
                        let lane_still_serves_a_descriptor = descriptors
                            .iter()
                            .any(|descriptor| egress.lane_for(descriptor.id) == Some(lane));
                        if !lane_still_serves_a_descriptor {
                            if let Some(pos) = pacers.iter().position(|pacer| pacer.id == lane) {
                                let _ = pacers.swap_remove(pos);
                            }
                        }
                    }
                    engine.cull_expired_routes(
                        now,
                        AttachedInterfaces::new(&*descriptors),
                        &mut |reaction| {
                            route_reaction(
                                reaction,
                                &mut *egress,
                                ifacs,
                                &mut pacers,
                                now,
                                &mut |journaled| {
                                    persistence.observe(&journaled, now);
                                    on_journaled(journaled);
                                },
                            )
                        },
                    );
                    wake_schedules = engine.wake_schedules(AttachedInterfaces::new(&*descriptors));
                }
                InterfaceLifecycle::Update { descriptor } => {
                    let descriptor = clamp_to_embedded_ceiling(descriptor);
                    if let Some(slot) = descriptors
                        .iter()
                        .position(|existing| existing.id == descriptor.id)
                    {
                        descriptors[slot] = descriptor;
                        if let Some(lane) = egress.lane_for(descriptor.id) {
                            if let Some(pos) = pacers.iter().position(|pacer| pacer.id == lane) {
                                pacers[pos] = InterfacePacer::from_descriptor(lane, &descriptor);
                            }
                        }
                        wake_schedules =
                            engine.wake_schedules(AttachedInterfaces::new(&*descriptors));
                    }
                }
                InterfaceLifecycle::Retag {
                    old_id,
                    new_id,
                    descriptor,
                } => {
                    let descriptor = clamp_to_embedded_ceiling(descriptor);
                    let present = descriptors
                        .iter()
                        .position(|existing| existing.id == old_id);
                    let collides = descriptors.iter().any(|existing| existing.id == new_id);
                    if let (Some(slot), false) = (present, collides) {
                        let old_lane = egress.lane_for(old_id);
                        descriptors[slot] = descriptor;
                        egress.retag(old_id, new_id);
                        if let Some(entry) = inbound.iter_mut().find(|(id, _)| *id == old_id) {
                            entry.0 = new_id;
                        }
                        if let Some(entry) = ifacs.iter_mut().find(|entry| entry.id == old_id) {
                            entry.id = new_id;
                        }
                        if let (Some(old_lane), Some(new_lane)) =
                            (old_lane, egress.lane_for(new_id))
                        {
                            if let Some(pos) = pacers.iter().position(|pacer| pacer.id == old_lane)
                            {
                                pacers[pos] =
                                    InterfacePacer::from_descriptor(new_lane, &descriptor);
                            }
                        }
                        wake_schedules =
                            engine.wake_schedules(AttachedInterfaces::new(&*descriptors));
                    }
                }
            },
            Either6::Sixth(()) => {}
        }
        let now = host.now();
        if persistence
            .deadline(now)
            .is_some_and(|deadline| deadline.0 <= now.0)
        {
            persistence.progress(engine, now).await;
            yield_now().await;
        }
        if Store::RETAINS_COUNTS {
            let mut dirty = engine.take_dirty_interfaces();
            let mut changed = false;
            dirty.drain(|interface| {
                if descriptors
                    .iter()
                    .any(|descriptor| descriptor.id == interface)
                {
                    store.set_interface_counts(interface, engine.interface_counts(interface));
                } else {
                    store.forget_interface(interface);
                }
                changed = true;
            });
            if changed {
                store.signal_interface_counts_changed();
            }
        }
    }
}

async fn wait_for_persistence(host: &impl Host, deadline: Option<crate::engine::InstantMillis>) {
    match deadline {
        Some(deadline) => host.sleep_until(deadline).await,
        None => core::future::pending().await,
    }
}

#[cfg(test)]
mod tests;