prns-runtime-tokio 0.3.6

Tokio 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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
#[cfg(feature = "runtime-metrics")]
use crate::engine::AnnounceOrigin;
use crate::engine::{EngineReaction, FanTarget, InstantMillis, Journaled};
use crate::interfaces::InterfaceIfac;
use crate::interfaces::{ConnectionView, InterfaceDescriptor, InterfaceId, InterfaceKind};
#[cfg(feature = "runtime-metrics")]
use crate::manifold::announce_pacer::PacerOffer;
use crate::manifold::announce_pacer::{AnnouncePacer, HeapPacerQueue};
use crate::manifold::interface_seam::MAX_WIRE_FRAME_LEN;
use crate::manifold::kernel::{
    route_reaction as route_engine_reaction, AnnounceDirective, DirectiveEgress,
};
#[cfg(feature = "runtime-metrics")]
use crate::runtime::{AnnounceEgressOutcome, EgressMetricsSnapshot};

use super::TokioGrantProducer;

pub struct Egress {
    lanes: std::vec::Vec<EgressLane>,

    #[cfg(feature = "runtime-metrics")]
    metrics: EgressMetricsSnapshot,
}

struct EgressLane {
    id: InterfaceId,
    producer: TokioGrantProducer,
    connection: Option<ConnectionView>,

    #[cfg(feature = "runtime-metrics")]
    logical_interface: InterfaceId,
}

impl EgressLane {
    fn is_available(&self) -> bool {
        self.connection
            .as_ref()
            .is_none_or(|connection| connection.connection().is_online())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum EgressEnqueueOutcome {
    Enqueued,
    LaneFull,
    LaneMissing,
}

impl Egress {
    #[must_use]
    pub fn new(lanes: std::vec::Vec<(InterfaceId, TokioGrantProducer)>) -> Self {
        let lanes = lanes
            .into_iter()
            .map(|(id, producer)| EgressLane {
                id,
                producer,
                connection: None,
                #[cfg(feature = "runtime-metrics")]
                logical_interface: id,
            })
            .collect::<std::vec::Vec<_>>();

        #[cfg(feature = "runtime-metrics")]
        let metrics = {
            let mut metrics = EgressMetricsSnapshot::default();
            for lane in &lanes {
                metrics.announces.register_interface(lane.logical_interface);
            }
            metrics
        };

        Self {
            lanes,
            #[cfg(feature = "runtime-metrics")]
            metrics,
        }
    }

    pub(super) fn enqueue(&mut self, target: InterfaceId, bytes: &[u8]) -> EgressEnqueueOutcome {
        for lane in &mut self.lanes {
            if lane.id != target {
                continue;
            }

            match lane.producer.try_grant() {
                None => {
                    #[cfg(feature = "runtime-metrics")]
                    {
                        self.metrics.full_lane_drops =
                            self.metrics.full_lane_drops.saturating_add(1);
                    }

                    return EgressEnqueueOutcome::LaneFull;
                }
                Some(slot) => {
                    slot.fill(bytes);
                    lane.producer.commit();

                    #[cfg(feature = "runtime-metrics")]
                    {
                        self.metrics.enqueued_frames =
                            self.metrics.enqueued_frames.saturating_add(1);
                    }

                    return EgressEnqueueOutcome::Enqueued;
                }
            }
        }

        #[cfg(feature = "runtime-metrics")]
        {
            self.metrics.missing_lane_drops = self.metrics.missing_lane_drops.saturating_add(1);
        }

        EgressEnqueueOutcome::LaneMissing
    }

    fn skip_unavailable(&mut self, target: InterfaceId) -> bool {
        let unavailable = self
            .lanes
            .iter()
            .find(|lane| lane.id == target)
            .is_some_and(|lane| !lane.is_available());

        #[cfg(feature = "runtime-metrics")]
        if unavailable {
            self.metrics.unavailable_frame_skips =
                self.metrics.unavailable_frame_skips.saturating_add(1);
        }

        unavailable
    }

    #[cfg(feature = "runtime-metrics")]
    fn record_announce(
        &mut self,
        target: InterfaceId,
        bytes: usize,
        origin: AnnounceOrigin,
        outcome: AnnounceEgressOutcome,
    ) {
        let logical_interface = self
            .lanes
            .iter()
            .find(|lane| lane.id == target)
            .map_or(target, |lane| lane.logical_interface);
        self.metrics
            .announces
            .record(origin, logical_interface, outcome, bytes);
    }

    /// Every lane of the supervisor's member kind that `fan` selects.
    fn broadcast_targets(
        &self,
        supervisor: InterfaceKind,
        fan: FanTarget,
    ) -> std::vec::Vec<InterfaceId> {
        let member = supervisor.member_kind();
        self.lanes
            .iter()
            .map(|lane| lane.id)
            .filter(|id| id.kind() == member)
            .filter(|id| match fan {
                FanTarget::All => true,
                FanTarget::Only(only) => *id == only,
                FanTarget::AllExcept(except) => *id != except,
            })
            .collect()
    }

    fn emit(
        &mut self,
        target: InterfaceId,
        size_hint: usize,
        fill: &mut dyn FnMut(&mut [u8]) -> Option<usize>,
        discard: &mut [u8],
    ) {
        for lane in &mut self.lanes {
            if lane.id != target {
                continue;
            }
            match lane.producer.try_grant() {
                Some(slot) => {
                    let hint = size_hint.clamp(1, MAX_WIRE_FRAME_LEN);
                    if slot.bytes.len() < hint {
                        slot.bytes.resize(hint, 0);
                    }
                    if let Some(len) = fill(&mut slot.bytes[..hint]) {
                        slot.len = len.min(hint);
                        lane.producer.commit();
                        #[cfg(feature = "runtime-metrics")]
                        {
                            self.metrics.enqueued_frames =
                                self.metrics.enqueued_frames.saturating_add(1);
                        }
                    }
                }
                None => {
                    let _fill_result = fill(discard);
                    #[cfg(feature = "runtime-metrics")]
                    if _fill_result.is_some() {
                        self.metrics.full_lane_drops =
                            self.metrics.full_lane_drops.saturating_add(1);
                    }
                }
            }
            return;
        }
        let _fill_result = fill(discard);
        #[cfg(feature = "runtime-metrics")]
        if _fill_result.is_some() {
            self.metrics.missing_lane_drops = self.metrics.missing_lane_drops.saturating_add(1);
        }
    }

    pub(super) fn add_lane(
        &mut self,
        id: InterfaceId,
        logical_interface: InterfaceId,
        producer: TokioGrantProducer,
        connection: Option<ConnectionView>,
    ) {
        #[cfg(not(feature = "runtime-metrics"))]
        let _ = logical_interface;

        self.lanes.push(EgressLane {
            id,
            producer,
            connection,
            #[cfg(feature = "runtime-metrics")]
            logical_interface,
        });

        #[cfg(feature = "runtime-metrics")]
        self.metrics.announces.register_interface(logical_interface);
    }

    pub(super) fn remove_lane(&mut self, id: InterfaceId) {
        self.lanes.retain(|lane| lane.id != id);
    }

    #[cfg(feature = "runtime-metrics")]
    pub(super) fn metrics_snapshot(&self, pacers: &[InterfacePacer]) -> EgressMetricsSnapshot {
        let mut snapshot = self.metrics.clone();
        snapshot.announces.reset_pacer_depths();
        for entry in pacers {
            snapshot
                .announces
                .add_pacer_depth(entry.logical_interface, entry.pacer.queued_len());
        }
        snapshot
    }
}

#[cfg(feature = "runtime-metrics")]
pub(super) type TokioAnnouncePacer = AnnouncePacer<HeapPacerQueue<AnnounceOrigin>, AnnounceOrigin>;
#[cfg(not(feature = "runtime-metrics"))]
pub(super) type TokioAnnouncePacer = AnnouncePacer<HeapPacerQueue>;

pub(super) struct InterfacePacer {
    pub(super) id: InterfaceId,
    #[cfg(feature = "runtime-metrics")]
    pub(super) logical_interface: InterfaceId,
    pub(super) pacer: TokioAnnouncePacer,
}

impl InterfacePacer {
    pub(super) fn from_descriptor(
        descriptor: &InterfaceDescriptor,
        logical_interface: InterfaceId,
    ) -> Self {
        #[cfg(not(feature = "runtime-metrics"))]
        let _ = logical_interface;

        Self {
            id: descriptor.id,
            #[cfg(feature = "runtime-metrics")]
            logical_interface,
            pacer: AnnouncePacer::new(descriptor.announce_bandwidth_cap, descriptor.bitrate),
        }
    }
}

/// Heap-parked wire scratch for every emission that can't land straight in a granted slot: `emit` carries a discarded or pre-mask frame, `masked` the IFAC mask output. Boxed once per manifold — wire-sized buffers never live on a task stack.
pub(super) struct WireScratch {
    emit: std::boxed::Box<[u8]>,
    masked: std::boxed::Box<[u8]>,
}

impl WireScratch {
    pub(super) fn new(cap: usize) -> Self {
        Self {
            emit: std::vec![0u8; cap].into_boxed_slice(),
            masked: std::vec![0u8; cap].into_boxed_slice(),
        }
    }

    pub(super) fn grow(&mut self, cap: usize) {
        if self.emit.len() < cap {
            self.emit = std::vec![0u8; cap].into_boxed_slice();
            self.masked = std::vec![0u8; cap].into_boxed_slice();
        }
    }
}

pub(super) fn route_reaction<A: FnMut(Journaled<'_>)>(
    reaction: EngineReaction<'_>,
    egress: &mut Egress,
    ifacs: &[InterfaceIfac],
    pacers: &mut [InterfacePacer],
    scratch: &mut WireScratch,
    now: InstantMillis,
    app: &mut A,
) {
    let mut directive_egress = TokioDirectiveEgress {
        egress,
        ifacs,
        pacers,
        scratch,
        now,
    };
    route_engine_reaction(reaction, &mut directive_egress, app);
}

struct TokioDirectiveEgress<'a> {
    egress: &'a mut Egress,
    ifacs: &'a [InterfaceIfac],
    pacers: &'a mut [InterfacePacer],
    scratch: &'a mut WireScratch,
    now: InstantMillis,
}

impl DirectiveEgress for TokioDirectiveEgress<'_> {
    fn send(&mut self, target: InterfaceId, bytes: &[u8]) {
        enqueue_for_wire(
            self.egress,
            self.ifacs,
            target,
            bytes,
            &mut self.scratch.masked,
        );
    }

    fn send_if_online(&mut self, target: InterfaceId, bytes: &[u8], on_send: &mut dyn FnMut()) {
        if self.egress.skip_unavailable(target) {
            return;
        }
        on_send();
        self.send(target, bytes);
    }

    fn send_announce(&mut self, target: InterfaceId, announce: AnnounceDirective<'_>) {
        offer_to_pacer(
            self.pacers,
            target,
            PacedAnnounce {
                bytes: announce.bytes(),
                hops: announce.hops(),
                #[cfg(feature = "runtime-metrics")]
                origin: announce.origin(),
            },
            self.now,
            self.egress,
            self.ifacs,
        );
    }

    fn send_to_fleet(&mut self, supervisor: InterfaceKind, fan: FanTarget, bytes: &[u8]) {
        for target in self.egress.broadcast_targets(supervisor, fan) {
            enqueue_for_wire(
                self.egress,
                self.ifacs,
                target,
                bytes,
                &mut self.scratch.masked,
            );
        }
    }

    fn send_announce_to_fleet(
        &mut self,
        supervisor: InterfaceKind,
        fan: FanTarget,
        announce: AnnounceDirective<'_>,
    ) {
        let bytes = announce.bytes();
        let hops = announce.hops();
        #[cfg(feature = "runtime-metrics")]
        let origin = announce.origin();
        for target in self.egress.broadcast_targets(supervisor, fan) {
            offer_to_pacer(
                self.pacers,
                target,
                PacedAnnounce {
                    bytes,
                    hops,
                    #[cfg(feature = "runtime-metrics")]
                    origin,
                },
                self.now,
                self.egress,
                self.ifacs,
            );
        }
    }

    fn emit_frame(
        &mut self,
        target: InterfaceId,
        size_hint: usize,
        fill: &mut dyn FnMut(&mut [u8]) -> Option<usize>,
    ) {
        emit_for_wire(
            self.egress,
            self.ifacs,
            target,
            size_hint,
            fill,
            self.scratch,
        );
    }

    #[cfg(feature = "runtime-metrics")]
    fn send_measured_local_announce(&mut self, target: InterfaceId, bytes: &[u8]) {
        enqueue_announce_for_wire(
            self.egress,
            self.ifacs,
            target,
            bytes,
            &mut self.scratch.masked,
            AnnounceOrigin::Local,
        );
    }

    #[cfg(feature = "runtime-metrics")]
    fn send_measured_local_announce_to_fleet(
        &mut self,
        supervisor: InterfaceKind,
        fan: FanTarget,
        bytes: &[u8],
    ) {
        for target in self.egress.broadcast_targets(supervisor, fan) {
            enqueue_announce_for_wire(
                self.egress,
                self.ifacs,
                target,
                bytes,
                &mut self.scratch.masked,
                AnnounceOrigin::Local,
            );
        }
    }
}

/// Grant-first emission: with no IFAC in the way the engine seals straight into the granted slot, zero copy. An IFAC'd target builds in scratch and masks into the slot (the mask is the copy), and a full lane runs `fill` against scratch and discards, so the engine's bookkeeping runs exactly once on every path.
fn emit_for_wire(
    egress: &mut Egress,
    ifacs: &[InterfaceIfac],
    target: InterfaceId,
    size_hint: usize,
    fill: &mut dyn FnMut(&mut [u8]) -> Option<usize>,
    scratch: &mut WireScratch,
) {
    match ifac_for(ifacs, target) {
        Some(entry) => {
            if let Some(len) = fill(&mut scratch.emit) {
                if let Some(masked_len) = entry
                    .context
                    .mask_outbound(&scratch.emit[..len], &mut scratch.masked)
                {
                    egress.enqueue(target, &scratch.masked[..masked_len]);
                }
            }
        }
        None => egress.emit(target, size_hint, fill, &mut scratch.emit),
    }
}

pub(super) fn ifac_for(ifacs: &[InterfaceIfac], id: InterfaceId) -> Option<&InterfaceIfac> {
    if ifacs.is_empty() {
        return None;
    }
    ifacs.iter().find(|entry| entry.id == id)
}

/// The one egress choke: a target with an access code never sees clean bytes on its wire, and a frame the mask refuses (oversize) is dropped rather than leaked open.
fn enqueue_for_wire(
    egress: &mut Egress,
    ifacs: &[InterfaceIfac],
    target: InterfaceId,
    bytes: &[u8],
    masked: &mut [u8],
) {
    match ifac_for(ifacs, target) {
        Some(entry) => {
            if let Some(masked_len) = entry.context.mask_outbound(bytes, masked) {
                egress.enqueue(target, &masked[..masked_len]);
            }
        }
        None => {
            egress.enqueue(target, bytes);
        }
    }
}

#[cfg(feature = "runtime-metrics")]
pub(super) fn enqueue_announce_for_wire(
    egress: &mut Egress,
    ifacs: &[InterfaceIfac],
    target: InterfaceId,
    bytes: &[u8],
    masked: &mut [u8],
    origin: AnnounceOrigin,
) {
    let (outcome, wire_bytes) = match ifac_for(ifacs, target) {
        Some(entry) => {
            let Some(masked_len) = entry.context.mask_outbound(bytes, masked) else {
                egress.record_announce(
                    target,
                    bytes.len(),
                    origin,
                    AnnounceEgressOutcome::IfacRejected,
                );
                return;
            };
            (egress.enqueue(target, &masked[..masked_len]), masked_len)
        }
        None => (egress.enqueue(target, bytes), bytes.len()),
    };
    let outcome = match outcome {
        EgressEnqueueOutcome::Enqueued => AnnounceEgressOutcome::Enqueued,
        EgressEnqueueOutcome::LaneFull => AnnounceEgressOutcome::LaneFull,
        EgressEnqueueOutcome::LaneMissing => AnnounceEgressOutcome::LaneMissing,
    };
    egress.record_announce(target, wire_bytes, origin, outcome);
}

/// A paced announce is broadcast-sized by construction, so its mask scratch fits on the stack — the wire-sized [`WireScratch`] is reserved for the frame paths.
const PACED_MASK_LEN: usize = crate::wire::BROADCAST_MTU + crate::interfaces::IFAC_MAX_SIZE;

pub(super) struct PacedAnnounce<'a> {
    pub(super) bytes: &'a [u8],
    pub(super) hops: u8,
    #[cfg(feature = "runtime-metrics")]
    pub(super) origin: AnnounceOrigin,
}

#[cfg(feature = "runtime-metrics")]
pub(super) fn offer_to_pacer(
    pacers: &mut [InterfacePacer],
    target: InterfaceId,
    announce: PacedAnnounce<'_>,
    now: InstantMillis,
    egress: &mut Egress,
    ifacs: &[InterfaceIfac],
) {
    if egress.skip_unavailable(target) {
        egress.record_announce(
            target,
            announce.bytes.len(),
            announce.origin,
            AnnounceEgressOutcome::InterfaceUnavailable,
        );
        return;
    }
    let offer = match pacers.iter_mut().find(|entry| entry.id == target) {
        Some(entry) => entry.pacer.offer_tagged(
            announce.bytes,
            announce.hops,
            now,
            announce.origin,
            |frame, frame_origin| {
                let mut masked = [0u8; PACED_MASK_LEN];
                enqueue_announce_for_wire(egress, ifacs, target, frame, &mut masked, frame_origin);
            },
        ),
        None => {
            let mut masked = [0u8; PACED_MASK_LEN];
            enqueue_announce_for_wire(
                egress,
                ifacs,
                target,
                announce.bytes,
                &mut masked,
                announce.origin,
            );
            PacerOffer::Sent
        }
    };
    if matches!(offer, PacerOffer::Rejected(_)) {
        egress.record_announce(
            target,
            announce.bytes.len(),
            announce.origin,
            AnnounceEgressOutcome::PacerRejected,
        );
    }
}

#[cfg(not(feature = "runtime-metrics"))]
pub(super) fn offer_to_pacer(
    pacers: &mut [InterfacePacer],
    target: InterfaceId,
    announce: PacedAnnounce<'_>,
    now: InstantMillis,
    egress: &mut Egress,
    ifacs: &[InterfaceIfac],
) {
    if egress.skip_unavailable(target) {
        return;
    }
    match pacers.iter_mut().find(|entry| entry.id == target) {
        Some(entry) => {
            entry
                .pacer
                .offer(announce.bytes, announce.hops, now, |frame| {
                    let mut masked = [0u8; PACED_MASK_LEN];
                    enqueue_for_wire(egress, ifacs, target, frame, &mut masked);
                });
        }
        None => {
            let mut masked = [0u8; PACED_MASK_LEN];
            enqueue_for_wire(egress, ifacs, target, announce.bytes, &mut masked);
        }
    }
}

#[cfg(feature = "runtime-metrics")]
pub(super) fn flush_due_pacers(
    pacers: &mut [InterfacePacer],
    now: InstantMillis,
    egress: &mut Egress,
    ifacs: &[InterfaceIfac],
) {
    for entry in pacers.iter_mut() {
        let target = entry.id;
        entry.pacer.release_due_tagged(now, |frame, origin| {
            if egress.skip_unavailable(target) {
                egress.record_announce(
                    target,
                    frame.len(),
                    origin,
                    AnnounceEgressOutcome::InterfaceUnavailable,
                );
                return;
            }
            let mut masked = [0u8; PACED_MASK_LEN];
            enqueue_announce_for_wire(egress, ifacs, target, frame, &mut masked, origin);
        });
    }
}

#[cfg(not(feature = "runtime-metrics"))]
pub(super) fn flush_due_pacers(
    pacers: &mut [InterfacePacer],
    now: InstantMillis,
    egress: &mut Egress,
    ifacs: &[InterfaceIfac],
) {
    for entry in pacers.iter_mut() {
        let target = entry.id;
        entry.pacer.release_due(now, |frame| {
            if egress.skip_unavailable(target) {
                return;
            }
            let mut masked = [0u8; PACED_MASK_LEN];
            enqueue_for_wire(egress, ifacs, target, frame, &mut masked);
        });
    }
}

pub(super) fn soonest_pacer_release(pacers: &[InterfacePacer]) -> Option<InstantMillis> {
    pacers
        .iter()
        .filter_map(|entry| entry.pacer.next_release())
        .min_by_key(|deadline| deadline.0)
}

pub(super) fn clear_announce_queues(pacers: &mut [InterfacePacer]) -> usize {
    pacers.iter_mut().fold(0, |dropped, entry| {
        dropped.saturating_add(entry.pacer.clear_queue())
    })
}

#[cfg(test)]
mod tests;