netring 0.24.0

High-performance zero-copy packet I/O for Linux (AF_PACKET TPACKET_V3 + AF_XDP)
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
//! Run loop for the 0.20 [`Monitor`].
//!
//! Phase F.1 added multi-interface fan-in (N [`AsyncCapture`]s
//! round-robin'd into one driver+dispatcher); F.2 added the
//! tick handler firing path. F.3 (per-CPU sharding) lives
//! separately and isn't reached from this run loop. Each
//! iteration:
//!
//! 1. await *either* the next packet batch (across all N
//!    interfaces, fair-round-robin) *or* the next tick from any
//!    registered tick handler,
//! 2. on packet: feed it to the flowscope driver and translate
//!    the resulting lifecycle events into typed `FlowStarted<P>`
//!    / `FlowEnded<P>` / `FlowEstablished<P>` / `AnyFlowAnomaly`
//!    payloads dispatched through the handler table — with
//!    `ctx.source` set to the interface's SourceIdx,
//! 3. on packet: drain each protocol-slot's typed parser
//!    messages and dispatch them,
//! 4. on tick: invoke the registered `.tick(period, handler)`
//!    closure *and* dispatch the typed `Tick` event so users
//!    who registered via `.on::<Tick>(...)` see it too.

use std::task::{Context, Poll};
use std::time::{Duration, Instant};

use flowscope::L4Proto;
use flowscope::driver::Event as FsEvent;
use flowscope::extract::FiveTuple;

use crate::AsyncCapture;
use crate::anomaly::sink::AnomalySink;
use crate::ctx::{CounterRegistry, Ctx, SourceIdx, StateMap};
use crate::error::Result;
use crate::monitor::backend::AnyBackend;
use crate::monitor::dispatcher::Dispatcher;
use crate::monitor::{BackendErrorPolicy, HandlerErrorPolicy, Monitor};
use crate::protocol::FlowKey;
#[cfg(feature = "icmp")]
use crate::protocol::builtin::Icmp;
use crate::protocol::builtin::{Tcp, Udp};
use crate::protocol::event_typed::{
    AnyFlowAnomaly, FlowEnded, FlowEstablished, FlowPacket, FlowStarted, FlowTick, ParserClosed,
    TcpRst, Tick,
};
use std::time::SystemTime;

/// How long to keep the run loop alive.
pub(crate) enum StopCondition {
    /// Stop when wall-clock reaches this deadline.
    Deadline(Instant),
    /// Stop on Ctrl-C / SIGTERM. Available only when the tokio
    /// `signal` feature is on; today that's transitively enabled
    /// by netring's `tokio` feature.
    Signal,
    /// 0.21 E.2: stop after `window` of inactivity. The run loop
    /// resets a deadline each time a packet batch arrives; if the
    /// deadline expires before the next batch, the loop exits.
    /// Useful for pcap replay (auto-stop after EOF + grace) and
    /// one-shot scans where the upstream traffic stops cleanly.
    Idle(Duration),
}

pub(crate) async fn run_loop(monitor: Monitor, stop: StopCondition) -> Result<()> {
    let Monitor {
        interfaces,
        #[cfg(feature = "af-xdp")]
        xdp_interfaces,
        mut driver,
        mut dispatcher,
        mut protocol_slots,
        mut state_map,
        mut counters,
        mut sink,
        mut tick_handlers,
        detector_names: _,
        monitor_name,
        drain_timeout,
        broadcast_handles: _,
        #[cfg(all(feature = "pcap", feature = "tokio"))]
            pcap_source_path: _,
        #[cfg(all(feature = "pcap", feature = "tokio"))]
            pcap_speed_factor: _,
        mut flow_states,
        fanout,
        label_table,
        mut merge_rx,
        handler_error_policy,
        backend_error_policy,
        mut capture_stats,
        health,
        mut flow_exporters,
    } = monitor;
    // Borrow the monitor name as `&str` for the run loop's
    // dispatch sites. The owned `Box<str>` lives in this stack
    // frame so the borrow is valid for the run loop's lifetime.
    let monitor_name_borrow: Option<&str> = monitor_name.as_deref();

    // Phase F.1: open one AsyncCapture per interface. The order
    // matches the builder's `.interfaces([...])` order; each event
    // gets the corresponding `SourceIdx`. A single-interface
    // monitor (the common case) opens exactly one ring — the
    // round-robin select reduces to a one-armed select with the
    // same latency as the prior single-cap path.
    // 0.24 Phase B: each capture source is an `AnyBackend` (AF_PACKET today,
    // AF_XDP behind `af-xdp`), drained through one backend-agnostic path. The
    // run loop holds the backend directly (not an owned `PacketStream`) so it
    // can drain **borrowed** zero-copy batches in place — no per-packet
    // `to_owned` copy. The future stays `Send` because the only borrow held
    // across an `.await` lives inside `drain_batch`, and `AnyBackend` is
    // `Send`; all dispatch runs *after* the batch is dropped.
    let cap_count = {
        #[cfg(feature = "af-xdp")]
        {
            interfaces.len() + xdp_interfaces.len()
        }
        #[cfg(not(feature = "af-xdp"))]
        {
            interfaces.len()
        }
    };
    let mut caps: Vec<AnyBackend> = Vec::with_capacity(cap_count);
    for iface in &interfaces {
        // 0.21 C: when the user set a fanout (single-shard or
        // sharded via ShardedRunner), open each ring with the
        // configured fanout group. Plain `.interface(iface)` with
        // no fanout falls back to `AsyncCapture::open`.
        let cap = match fanout {
            Some((mode, group_id)) => {
                let rx = crate::Capture::builder()
                    .interface(iface)
                    .fanout(mode, group_id)
                    .build()?;
                AsyncCapture::new(rx)?
            }
            None => AsyncCapture::open(iface)?,
        };
        caps.push(AnyBackend::AfPacket(cap));
    }
    // 0.24 Phase B: AF_XDP backends (in builder-registration order, after the
    // AF_PACKET ones). Needs an attached XDP redirect program to see traffic.
    #[cfg(feature = "af-xdp")]
    for iface in &xdp_interfaces {
        let xdp = crate::AsyncXdpSocket::open(iface)?;
        caps.push(AnyBackend::Xdp(xdp));
    }
    // 0.24 Phase C4: all sockets are open and the loop is about to run —
    // readiness flips true. `mark_started` stamps the uptime/liveness
    // clock now (not at build time).
    health.mark_started();
    health.mark_sockets_open();

    let mut events: Vec<FsEvent<FlowKey>> = Vec::with_capacity(64);
    let mut shutdown = ShutdownSignal::new(stop);
    let mut rr_anchor: usize = 0;
    // 0.24 Phase B: consecutive backend-error count for the SkipSource circuit
    // breaker. Reset on every successful readable wake.
    let mut backend_errors: u64 = 0;
    // 0.21 E.2: bumped on every packet batch + every tick. Idle
    // mode computes its deadline as `last_event_at + window`,
    // so refreshing this resets the idle timer. Initialized to
    // "now" so the loop has the full window of grace before the
    // first event arrives.
    let mut last_event_at = Instant::now();

    // Phase F.2: one tokio interval per registered tick handler.
    // First tick fires after `period` (interval_at with deadline =
    // now + period), not immediately. `Skip` missed-tick behaviour
    // so a slow tick handler doesn't pile up backlog ticks.
    let mut tick_intervals: Vec<tokio::time::Interval> = tick_handlers
        .iter()
        .map(|t| {
            let mut int =
                tokio::time::interval_at(tokio::time::Instant::now() + t.period, t.period);
            int.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
            int
        })
        .collect();

    // 0.24 Phase C: capture-telemetry sampling. Only armed when an
    // `on_capture_stats` handler is registered — otherwise the `Option`
    // is `None` and the `select!` branch is gated off at zero cost (same
    // pattern as the tick / merge branches). The sampler keeps per-source
    // cumulative state so each sample's `drop_rate` is windowed.
    let mut telemetry_interval = capture_stats.as_ref().map(|reg| {
        let mut int =
            tokio::time::interval_at(tokio::time::Instant::now() + reg.period, reg.period);
        int.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
        int
    });
    // Allocate per-source sampler slots only when telemetry is armed — an
    // empty `Vec` doesn't allocate, so an unconfigured monitor pays nothing.
    let mut telemetry_sampler =
        crate::monitor::telemetry::TelemetrySampler::new(if capture_stats.is_some() {
            caps.len()
        } else {
            0
        });

    loop {
        // tokio::select! waits on shutdown, the next packet, OR
        // the next tick. The `if !tick_intervals.is_empty()`
        // gate keeps the tick branch from being polled when no
        // handlers are registered (saves one cx wake per loop).
        let ready = tokio::select! {
            biased;
            _ = shutdown.recv(last_event_at) => break,
            idx = ready_capture(&mut caps, &mut rr_anchor) => idx,
            tick_idx = next_tick(&mut tick_intervals), if !tick_intervals.is_empty() => {
                // Reset idle timer on every tick — periodic
                // tick fires are intended user activity, not
                // dead air. Without this, a 1s idle timeout +
                // 500ms tick handler would never resolve.
                last_event_at = Instant::now();
                fire_tick(
                    tick_idx,
                    &mut tick_handlers,
                    &mut dispatcher,
                    sink.as_mut(),
                    &mut state_map,
                    &mut counters,
                    monitor_name_borrow,
                    &mut flow_states,
                    &label_table,
                )
                .await?;
                // 0.24 Phase C4: a tick is progress too — keeps liveness
                // alive on a quiet link with a registered heartbeat tick.
                health.record_event(driver.tracker().flow_count());
                continue;
            }
            // 0.22 §5.1: cross-shard merge probe. Gated so non-merged
            // monitors never poll it (zero cost, like the tick branch).
            // Out-of-band — doesn't touch the idle timer.
            req = recv_merge(&mut merge_rx), if merge_rx.is_some() => {
                if let Some(req) = req {
                    let taken = state_map.take_dyn(req.type_id);
                    let _ = req.reply.send(taken);
                }
                continue;
            }
            // 0.24 Phase C: capture-telemetry sample. Gated on the
            // `on_capture_stats` registration so monitors without it
            // never poll the interval. Out-of-band like the merge probe:
            // sampling is observability, not traffic, so it must NOT reset
            // the idle timer (else `on_capture_stats` + `run_until_idle`
            // would never idle-stop). The sampling itself runs in the
            // branch body — after the `select!` drops the other branch
            // futures, so the `&caps` read here can't alias the
            // `ready_capture` branch's `&mut caps`.
            _ = next_telemetry_sample(&mut telemetry_interval),
                if telemetry_interval.is_some() =>
            {
                if let Some(reg) = capture_stats.as_mut() {
                    sample_and_fire_capture_stats(
                        &caps,
                        &mut telemetry_sampler,
                        reg,
                        sink.as_mut(),
                        &mut state_map,
                        &mut counters,
                        monitor_name_borrow,
                        &mut flow_states,
                        &label_table,
                        &health,
                    )?;
                }
                continue;
            }
        };
        let i = match ready {
            Some(Ok(i)) => i,
            Some(Err(e)) => match backend_error_policy {
                BackendErrorPolicy::FailFast => return Err(e),
                BackendErrorPolicy::SkipSource => {
                    backend_errors += 1;
                    health.record_backend_error();
                    tracing::warn!(error = %e, count = backend_errors, "capture backend error (SkipSource)");
                    // Circuit breaker: a persistently-failing fd would otherwise
                    // spin the readiness select. Back off, and after many
                    // consecutive failures give up rather than burn a core.
                    if backend_errors > 64 {
                        return Err(e);
                    }
                    tokio::time::sleep(Duration::from_millis(50)).await;
                    continue;
                }
            },
            None => break, // all captures exhausted (AF_PACKET never reports this)
        };
        backend_errors = 0; // a successful wake clears the circuit breaker
        let source = SourceIdx(i as u8);
        // Reset idle timer on every readable wake.
        last_event_at = Instant::now();

        // IN-BORROW: drain every retired block now ready on this capture and
        // feed each packet's zero-copy view to the tracker. `track_into` copies
        // only the metadata it needs into the owned `events` buffer (and feeds
        // the L7 parsers, which buffer owned messages) — no packet-data copy.
        events.clear();
        // IN-BORROW: drain the ready batches on this backend, feeding each
        // packet's zero-copy view to the tracker. `drain_batch` holds the
        // ring/UMEM borrow only across this synchronous callback loop and
        // drops it before returning — no borrow crosses the dispatch
        // `.await` below, which is what keeps the run loop's future `Send`.
        // `track_into` copies only the metadata it needs into `events`; no
        // packet-data copy.
        let last_ts = caps[i]
            .drain_batch(|view| driver.track_into(view, &mut events))
            .await?;

        // A spurious wake (no retired block) leaves `last_ts == None`.
        let Some(ts) = last_ts else { continue };

        // AFTER BORROW: dispatch on owned data (sync + async, Send-safe).
        dispatch_tracked_events(
            &mut dispatcher,
            sink.as_mut(),
            &mut state_map,
            &mut counters,
            &mut events,
            source,
            monitor_name_borrow,
            &mut flow_states,
            &label_table,
            handler_error_policy,
            &mut flow_exporters,
            &health,
        )
        .await?;
        drain_protocol_slots(
            &mut dispatcher,
            &mut protocol_slots,
            &driver,
            sink.as_mut(),
            &mut state_map,
            &mut counters,
            &mut flow_states,
            ts,
            source,
            monitor_name_borrow,
            &label_table,
            handler_error_policy,
            &health,
        )?;

        // 0.24 Phase C4: record progress for the health handle — a packet
        // batch was processed; snapshot the tracker's active-flow count.
        health.record_event(driver.tracker().flow_count());
    }

    // 0.21 D.2: graceful drain phase. After the stop condition
    // fires, flush in-flight flows out of the central tracker,
    // drain each protocol slot's queued messages, and flush the
    // sink. Skipped entirely when `drain_timeout` is zero — useful
    // for fail-fast smoke tests that don't care about residual
    // events.
    if !drain_timeout.is_zero() {
        let deadline = Instant::now() + drain_timeout;
        drain_phase(
            &mut driver,
            &mut dispatcher,
            sink.as_mut(),
            &mut state_map,
            &mut counters,
            &mut protocol_slots,
            monitor_name_borrow,
            deadline,
            &mut flow_states,
            &label_table,
            handler_error_policy,
            &mut flow_exporters,
            &health,
        )
        .await?;
    }

    // 0.24 Phase D: flush exporters (NDJSON/IPFIX writers may buffer).
    for exporter in flow_exporters.iter_mut() {
        let _ = exporter.flush();
    }

    Ok(())
}

/// 0.21 E.1: drive a monitor from an offline pcap file.
///
/// Single-source by design: pcap replay doesn't need
/// multi-interface fan-in or tick handlers (pcap timestamps
/// jitter relative to wall-clock; tick scheduling against them
/// is ambiguous). Runs to EOF, then calls the shared drain phase
/// so trailing flow ends + sink flushes still land.
///
/// On parse error from the pcap source, propagates the error
/// up — no partial-replay recovery.
#[cfg(all(feature = "pcap", feature = "tokio"))]
pub(crate) async fn replay_loop(
    monitor: Monitor,
    path: std::path::PathBuf,
    config: crate::pcap_source::AsyncPcapConfig,
) -> Result<()> {
    use std::pin::Pin;

    use futures_core::Stream;

    let Monitor {
        interfaces: _,
        #[cfg(feature = "af-xdp")]
            xdp_interfaces: _, // pcap replay has no live backend

        mut driver,
        mut dispatcher,
        mut protocol_slots,
        mut state_map,
        mut counters,
        mut sink,
        tick_handlers: _,
        detector_names: _,
        monitor_name,
        drain_timeout,
        broadcast_handles: _,
        pcap_source_path: _,
        pcap_speed_factor: _,
        mut flow_states,
        fanout: _,
        label_table,
        merge_rx: _, // replay is single-shard; no cross-shard merge
        handler_error_policy,
        backend_error_policy: _, // replay has no live capture backend
        capture_stats: _,        // pcap replay has no kernel ring to sample
        health,
        mut flow_exporters,
    } = monitor;
    let monitor_name_borrow: Option<&str> = monitor_name.as_deref();

    let mut source = crate::pcap_source::AsyncPcapSource::open_with_config(&path, config).await?;
    let mut events: Vec<FsEvent<FlowKey>> = Vec::with_capacity(64);

    // 0.24 Phase C4: the pcap source is open and replay is starting — the
    // same readiness/liveness handle works for offline replay.
    health.mark_started();
    health.mark_sockets_open();

    loop {
        // Pin the stream + poll the next packet. The source's
        // `Stream` impl drives the underlying spawn_blocking
        // reader task; `None` = EOF.
        let next = std::future::poll_fn(|cx| Pin::new(&mut source).poll_next(cx)).await;
        let pkt = match next {
            Some(Ok(p)) => p,
            Some(Err(e)) => return Err(e),
            None => break,
        };

        let view = flowscope::PacketView::new(&pkt.data, pkt.timestamp);

        events.clear();
        driver.track_into(view, &mut events);
        dispatch_tracked_events(
            &mut dispatcher,
            sink.as_mut(),
            &mut state_map,
            &mut counters,
            &mut events,
            SourceIdx(0),
            monitor_name_borrow,
            &mut flow_states,
            &label_table,
            handler_error_policy,
            &mut flow_exporters,
            &health,
        )
        .await?;

        drain_protocol_slots(
            &mut dispatcher,
            &mut protocol_slots,
            &driver,
            sink.as_mut(),
            &mut state_map,
            &mut counters,
            &mut flow_states,
            pkt.timestamp,
            SourceIdx(0),
            monitor_name_borrow,
            &label_table,
            handler_error_policy,
            &health,
        )?;

        // 0.24 Phase C4: record replay progress for the health handle.
        health.record_event(driver.tracker().flow_count());
    }

    // EOF reached. Run the drain phase to land any trailing
    // events (flowscope's `finish()` synthesises FlowEnded
    // events for in-flight flows).
    if !drain_timeout.is_zero() {
        let deadline = Instant::now() + drain_timeout;
        drain_phase(
            &mut driver,
            &mut dispatcher,
            sink.as_mut(),
            &mut state_map,
            &mut counters,
            &mut protocol_slots,
            monitor_name_borrow,
            deadline,
            &mut flow_states,
            &label_table,
            handler_error_policy,
            &mut flow_exporters,
            &health,
        )
        .await?;
    }

    // 0.24 Phase D: flush exporters after replay drain.
    for exporter in flow_exporters.iter_mut() {
        let _ = exporter.flush();
    }

    Ok(())
}

/// 0.21 D.2: drain residual events after the run loop's stop
/// condition fires.
///
/// Steps, each guarded by the `deadline`:
///
/// 1. `driver.finish()` — flush in-flight flows out of the central
///    tracker (synthesizes `FlowEnded` events for anything still
///    alive). Dispatches each through the same lifecycle path the
///    run loop uses, so handlers see end-of-stream events the
///    same way they see live ones.
/// 2. For each protocol slot, drain queued typed messages.
/// 3. `sink.flush()` — give a chance for buffered writes (eve-sink,
///    json sink, etc.) to land on disk.
///
/// Best-effort: a slow handler can push past `deadline`. The
/// deadline check sits between steps, not inside them. If
/// step 1 already overran, steps 2 and 3 are skipped to bound
/// total shutdown time.
#[allow(clippy::too_many_arguments)]
async fn drain_phase(
    driver: &mut flowscope::driver::Driver<FiveTuple>,
    dispatcher: &mut Dispatcher,
    sink: &mut dyn AnomalySink,
    state_map: &mut StateMap,
    counters: &mut CounterRegistry,
    protocol_slots: &mut [Box<dyn crate::monitor::ProtocolSlot>],
    monitor_name: Option<&str>,
    deadline: Instant,
    flow_states: &mut crate::ctx::FlowStateRegistry,
    label_table: &flowscope::well_known::LabelTable,
    policy: HandlerErrorPolicy,
    flow_exporters: &mut [Box<dyn crate::export::FlowExporter>],
    health: &crate::monitor::health::HealthState,
) -> Result<()> {
    // Step 1: drain the central tracker.
    let mut leftover: Vec<FsEvent<FlowKey>> = Vec::new();
    driver.finish_into(&mut leftover);
    for evt in leftover.drain(..) {
        if Instant::now() >= deadline {
            return Ok(());
        }
        // 0.24 Phase D: export the flows finalized by `finish_into` (flows
        // still open at shutdown get a synthesized FlowEnded here).
        if !flow_exporters.is_empty()
            && let FsEvent::FlowEnded {
                key, stats, reason, ..
            } = &evt
        {
            let record = crate::export::FlowRecord::from_ended(key, stats, *reason);
            for exporter in flow_exporters.iter_mut() {
                exporter.export(&record);
            }
        }
        let res = match dispatch_lifecycle(
            dispatcher,
            sink,
            state_map,
            counters,
            evt.clone(),
            SourceIdx(0),
            monitor_name,
            flow_states,
            label_table,
        ) {
            Ok(()) => dispatch_lifecycle_async(dispatcher, evt).await,
            Err(e) => Err(e),
        };
        if let Err(e) = res {
            match policy {
                HandlerErrorPolicy::Propagate => return Err(e),
                HandlerErrorPolicy::Isolate => {
                    health.record_handler_error();
                    tracing::warn!(error = %e, "handler error isolated (drain)")
                }
            }
        }
    }

    if Instant::now() >= deadline {
        return Ok(());
    }

    // Step 2: drain each protocol slot's typed messages.
    let ts = flowscope::Timestamp::from_system_time(SystemTime::now());
    for slot in protocol_slots.iter_mut() {
        if Instant::now() >= deadline {
            return Ok(());
        }
        let mut ctx = Ctx::new(
            None,
            ts,
            SourceIdx(0),
            state_map,
            sink,
            counters,
            flow_states,
        );
        ctx.monitor_name = monitor_name;
        ctx.label_table = label_table;
        ctx.tracker = Some(driver.tracker());
        if let Err(e) = slot.drain_and_dispatch(dispatcher, &mut ctx) {
            match policy {
                HandlerErrorPolicy::Propagate => return Err(e),
                HandlerErrorPolicy::Isolate => {
                    health.record_handler_error();
                    tracing::warn!(error = %e, "handler error isolated (drain slot)")
                }
            }
        }
    }

    if Instant::now() >= deadline {
        return Ok(());
    }

    // Step 3: flush the sink. The `AnomalySink::flush` default is
    // `Ok(())`; impls that buffer (eve-sink, json sink) actually
    // do work here. Errors propagate as `io::Error`; the cast
    // through netring's `Error` wraps them.
    sink.flush().map_err(|e| {
        crate::error::Error::Io(std::io::Error::new(e.kind(), format!("sink flush: {e}")))
    })?;

    Ok(())
}

/// Round-robin readiness poll across the N captures. Returns
/// `Some(Ok(index))` for the next *readable* capture (the caller then drains
/// its borrowed batches in place), `Some(Err(_))` on a readiness error, or
/// `None` only when there are no captures.
///
/// Fair: `anchor` records the index just past the last serviced capture, so the
/// scan resumes there — a chatty interface can't starve the quiet ones. The
/// readiness guard from `poll_read_ready_mut` is dropped without clearing, so
/// the level-triggered fd stays ready and the caller's `readable()` resolves
/// immediately.
async fn ready_capture(caps: &mut [AnyBackend], anchor: &mut usize) -> Option<Result<usize>> {
    std::future::poll_fn(|cx: &mut Context<'_>| -> Poll<Option<Result<usize>>> {
        let n = caps.len();
        if n == 0 {
            return Poll::Ready(None);
        }
        let start = *anchor % n;
        for offset in 0..n {
            let i = (start + offset) % n;
            match caps[i].poll_read_ready(cx) {
                Poll::Ready(Ok(())) => {
                    *anchor = (i + 1) % n;
                    return Poll::Ready(Some(Ok(i)));
                }
                Poll::Ready(Err(e)) => return Poll::Ready(Some(Err(e))),
                Poll::Pending => {}
            }
        }
        Poll::Pending
    })
    .await
}

/// Round-robin poll across N tick intervals. Returns the index of
/// whichever interval ticked first.
///
/// Symmetric to [`next_packet`] — the same fairness story applies,
/// just without an `anchor` because interval ticks are
/// time-driven, not rate-driven (the slowest interval can't
/// starve the fastest one even with naive ordering). We still
/// scan from index 0 every poll; the win from an anchor is
/// negligible for tick handlers.
async fn next_tick(intervals: &mut [tokio::time::Interval]) -> usize {
    std::future::poll_fn(|cx: &mut Context<'_>| -> Poll<usize> {
        for (i, interval) in intervals.iter_mut().enumerate() {
            if interval.poll_tick(cx).is_ready() {
                return Poll::Ready(i);
            }
        }
        Poll::Pending
    })
    .await
}

/// 0.22 §5.1: await the next cross-shard merge probe. When no merge
/// receiver is wired the future never resolves (the `select!` branch is
/// gated `if merge_rx.is_some()`, so this only runs in the `Some` case).
async fn recv_merge(
    rx: &mut Option<tokio::sync::mpsc::UnboundedReceiver<crate::monitor::merge::MergeRequest>>,
) -> Option<crate::monitor::merge::MergeRequest> {
    match rx {
        Some(r) => r.recv().await,
        None => std::future::pending().await,
    }
}

/// 0.24 Phase C: await the next capture-telemetry sample tick. When no
/// `on_capture_stats` handler is registered the interval is `None` and the
/// future never resolves (the `select!` branch is gated `if
/// telemetry_interval.is_some()`, so this only runs in the `Some` case).
async fn next_telemetry_sample(interval: &mut Option<tokio::time::Interval>) {
    match interval {
        Some(int) => {
            int.tick().await;
        }
        None => std::future::pending().await,
    }
}

/// 0.24 Phase C: read each capture source's cumulative kernel counters,
/// fold them into a windowed [`CaptureTelemetry`], and fire the registered
/// `on_capture_stats` handler once per source.
///
/// Reads `cumulative_stats` (non-destructive at the API level — the inner
/// `Capture` accumulates the destructive `u32` kernel reads internally), so
/// it never disturbs the user-visible counters. A per-source stats read
/// that errors is logged and skipped rather than tearing down the monitor:
/// telemetry is best-effort observability.
#[allow(clippy::too_many_arguments)]
fn sample_and_fire_capture_stats(
    caps: &[AnyBackend],
    sampler: &mut crate::monitor::telemetry::TelemetrySampler,
    reg: &mut crate::monitor::telemetry::CaptureStatsRegistration,
    sink: &mut dyn AnomalySink,
    state_map: &mut StateMap,
    counters: &mut CounterRegistry,
    monitor_name: Option<&str>,
    flow_states: &mut crate::ctx::FlowStateRegistry,
    label_table: &flowscope::well_known::LabelTable,
    health: &crate::monitor::health::HealthState,
) -> Result<()> {
    let now = flowscope::Timestamp::from_system_time(SystemTime::now());
    // Accumulate the cumulative totals across sources for the health
    // handle (the per-source telemetry still goes to the user handler).
    let mut total_packets: u64 = 0;
    let mut total_drops: u64 = 0;
    for (i, cap) in caps.iter().enumerate() {
        let cum = match cap.cumulative_stats() {
            Ok(s) => s,
            Err(e) => {
                tracing::warn!(
                    source = i,
                    error = %e,
                    "capture stats read failed; skipping telemetry sample for this source"
                );
                continue;
            }
        };
        let telemetry = sampler.sample(i, cum);
        total_packets += telemetry.packets;
        total_drops += telemetry.drops;
        let mut ctx = Ctx::new(
            None,
            now,
            SourceIdx(i as u8),
            state_map,
            sink,
            counters,
            flow_states,
        );
        ctx.monitor_name = monitor_name;
        ctx.label_table = label_table;
        (reg.handler)(&telemetry, &mut ctx)?;
    }
    health.record_totals(total_packets, total_drops);
    Ok(())
}

/// Dispatch the lifecycle events drained from the central tracker — sync
/// handlers first, then async — and clear the buffer. The events are owned
/// (they don't borrow the capture ring), so this is safe to call **after** a
/// borrowed batch has been dropped, which is what keeps the borrowed run loop's
/// future `Send` (no `!Sync` ring borrow is held across the async `.await`).
///
/// Shared by the live run loop and the pcap replay loop so the dispatch
/// semantics stay identical (and are exercised by the cap-free
/// `monitor_replay` tests).
#[allow(clippy::too_many_arguments)]
async fn dispatch_tracked_events(
    dispatcher: &mut Dispatcher,
    sink: &mut dyn AnomalySink,
    state_map: &mut StateMap,
    counters: &mut CounterRegistry,
    events: &mut Vec<FsEvent<FlowKey>>,
    source: SourceIdx,
    monitor_name: Option<&str>,
    flow_states: &mut crate::ctx::FlowStateRegistry,
    label_table: &flowscope::well_known::LabelTable,
    policy: HandlerErrorPolicy,
    flow_exporters: &mut [Box<dyn crate::export::FlowExporter>],
    health: &crate::monitor::health::HealthState,
) -> Result<()> {
    for evt in events.drain(..) {
        // 0.24 Phase D: a flow just ended → build a FlowRecord and fan it
        // out to every registered exporter. Cheap no-op when none are
        // registered. Done before dispatch so exporters see the flow even
        // if a downstream handler errors under `Propagate`.
        if !flow_exporters.is_empty()
            && let FsEvent::FlowEnded {
                key, stats, reason, ..
            } = &evt
        {
            let record = crate::export::FlowRecord::from_ended(key, stats, *reason);
            for exporter in flow_exporters.iter_mut() {
                exporter.export(&record);
            }
        }
        // Sync handlers first, then async — but on the SAME event, so one error
        // is isolated per-event under `Isolate` (a malformed flow can't tear
        // down the pipeline).
        let res = match dispatch_lifecycle(
            dispatcher,
            sink,
            state_map,
            counters,
            evt.clone(),
            source,
            monitor_name,
            flow_states,
            label_table,
        ) {
            Ok(()) => dispatch_lifecycle_async(dispatcher, evt).await,
            Err(e) => Err(e),
        };
        if let Err(e) = res {
            match policy {
                HandlerErrorPolicy::Propagate => return Err(e),
                HandlerErrorPolicy::Isolate => {
                    health.record_handler_error();
                    tracing::warn!(error = %e, "handler error isolated (per-event)")
                }
            }
        }
    }
    Ok(())
}

/// Drain each protocol slot's queued typed messages (e.g. parsed HTTP/DNS/TLS)
/// and dispatch them. The parsers were already fed by `driver.track_into`
/// (in-borrow); the messages they produced are owned, so this needs only a
/// shared `&driver` for the flow-tracker join — no capture-ring borrow.
#[allow(clippy::too_many_arguments)]
fn drain_protocol_slots(
    dispatcher: &mut Dispatcher,
    protocol_slots: &mut [Box<dyn crate::monitor::ProtocolSlot>],
    driver: &flowscope::driver::Driver<FiveTuple>,
    sink: &mut dyn AnomalySink,
    state_map: &mut StateMap,
    counters: &mut CounterRegistry,
    flow_states: &mut crate::ctx::FlowStateRegistry,
    ts: flowscope::Timestamp,
    source: SourceIdx,
    monitor_name: Option<&str>,
    label_table: &flowscope::well_known::LabelTable,
    policy: HandlerErrorPolicy,
    health: &crate::monitor::health::HealthState,
) -> Result<()> {
    for slot in protocol_slots.iter_mut() {
        let mut ctx = Ctx::new(None, ts, source, state_map, sink, counters, flow_states);
        ctx.monitor_name = monitor_name;
        ctx.label_table = label_table;
        ctx.tracker = Some(driver.tracker());
        if let Err(e) = slot.drain_and_dispatch(dispatcher, &mut ctx) {
            match policy {
                HandlerErrorPolicy::Propagate => return Err(e),
                HandlerErrorPolicy::Isolate => {
                    health.record_handler_error();
                    tracing::warn!(error = %e, "handler error isolated (per-slot)")
                }
            }
        }
    }
    Ok(())
}

/// Fire the tick handler at `tick_idx`.
///
/// Two paths fire on every tick:
///
/// 1. The `.tick(period, handler)` registration's boxed closure —
///    drives the period scheduling and is the ergonomic
///    registration form.
/// 2. The dispatcher's typed `Tick` slot (sync + async) — so
///    users who registered via `.on::<Tick>(...)` also receive
///    the event.
///
/// Both run in the order: closure first, then dispatcher.
#[allow(clippy::too_many_arguments)]
async fn fire_tick(
    tick_idx: usize,
    tick_handlers: &mut [crate::monitor::tick::TickRegistration],
    dispatcher: &mut Dispatcher,
    sink: &mut dyn AnomalySink,
    state_map: &mut StateMap,
    counters: &mut CounterRegistry,
    monitor_name: Option<&str>,
    flow_states: &mut crate::ctx::FlowStateRegistry,
    label_table: &flowscope::well_known::LabelTable,
) -> Result<()> {
    let reg = &mut tick_handlers[tick_idx];
    let tick = Tick {
        now: flowscope::Timestamp::from_system_time(SystemTime::now()),
        period: reg.period,
    };
    {
        let mut ctx = Ctx::new(
            None,
            tick.now,
            SourceIdx(0),
            state_map,
            sink,
            counters,
            flow_states,
        );
        ctx.monitor_name = monitor_name;
        ctx.label_table = label_table;
        (reg.handler)(&tick, &mut ctx)?;
    }
    {
        let mut ctx = Ctx::new(
            None,
            tick.now,
            SourceIdx(0),
            state_map,
            sink,
            counters,
            flow_states,
        );
        ctx.monitor_name = monitor_name;
        ctx.label_table = label_table;
        dispatcher.dispatch::<Tick>(&tick, &mut ctx)?;
    }
    dispatcher.dispatch_async::<Tick>(&tick).await?;
    Ok(())
}

/// Tracks both a packet-batch deadline and an OS shutdown signal.
struct ShutdownSignal {
    stop: StopCondition,
    sig_int: Option<tokio::signal::unix::Signal>,
    sig_term: Option<tokio::signal::unix::Signal>,
}

impl ShutdownSignal {
    fn new(stop: StopCondition) -> Self {
        let (sig_int, sig_term) = match &stop {
            StopCondition::Signal => {
                let sigint =
                    tokio::signal::unix::signal(tokio::signal::unix::SignalKind::interrupt()).ok();
                let sigterm =
                    tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate()).ok();
                (sigint, sigterm)
            }
            StopCondition::Deadline(_) | StopCondition::Idle(_) => (None, None),
        };
        Self {
            stop,
            sig_int,
            sig_term,
        }
    }

    /// 0.21 E.2: `last_event_at` parameterizes the idle-window
    /// deadline. For `Deadline` / `Signal` it's ignored.
    async fn recv(&mut self, last_event_at: Instant) {
        match &mut self.stop {
            StopCondition::Deadline(t) => {
                tokio::time::sleep_until((*t).into()).await;
            }
            StopCondition::Idle(window) => {
                tokio::time::sleep_until((last_event_at + *window).into()).await;
            }
            StopCondition::Signal => match (self.sig_int.as_mut(), self.sig_term.as_mut()) {
                (Some(i), Some(t)) => {
                    tokio::select! {
                        _ = i.recv() => {},
                        _ = t.recv() => {},
                    }
                }
                (Some(i), None) => {
                    let _ = i.recv().await;
                }
                (None, Some(t)) => {
                    let _ = t.recv().await;
                }
                // Couldn't install handlers — fall back to never-firing
                // (the user can still abort with the runtime exiting).
                (None, None) => std::future::pending::<()>().await,
            },
        }
    }
}

/// Async sibling of [`dispatch_lifecycle`]. Translates each
/// flowscope lifecycle event into its typed `FlowStarted<P>` /
/// `FlowEnded<P>` / `FlowEstablished<P>` / `AnyFlowAnomaly`
/// payload and dispatches through the async handler chain.
///
/// Cheap when no async handlers are registered:
/// [`Dispatcher::dispatch_async`] returns immediately if the
/// payload TypeId has no async slot. No allocation in that case.
async fn dispatch_lifecycle_async(
    dispatcher: &mut Dispatcher,
    evt: FsEvent<FlowKey>,
) -> Result<()> {
    match evt {
        FsEvent::FlowStarted { key, ts, l4 } => match l4 {
            Some(L4Proto::Tcp) => {
                dispatcher
                    .dispatch_async(&FlowStarted::<Tcp>::new(key, l4, ts))
                    .await?;
            }
            Some(L4Proto::Udp) => {
                dispatcher
                    .dispatch_async(&FlowStarted::<Udp>::new(key, l4, ts))
                    .await?;
            }
            #[cfg(feature = "icmp")]
            Some(L4Proto::Icmp) | Some(L4Proto::IcmpV6) => {
                dispatcher
                    .dispatch_async(&FlowStarted::<Icmp>::new(key, l4, ts))
                    .await?;
            }
            _ => {}
        },
        FsEvent::FlowEnded {
            key,
            reason,
            stats,
            ts,
            l4,
            ..
        } => match l4 {
            Some(L4Proto::Tcp) => {
                // 0.22 §2.6: async TcpRst synthesis mirrors the sync arm.
                let is_rst = reason == flowscope::EndReason::Rst;
                dispatcher
                    .dispatch_async(&FlowEnded::<Tcp>::new(key, reason, stats.clone(), l4, ts))
                    .await?;
                if is_rst {
                    dispatcher
                        .dispatch_async(&TcpRst::new(key, stats, ts))
                        .await?;
                }
            }
            Some(L4Proto::Udp) => {
                dispatcher
                    .dispatch_async(&FlowEnded::<Udp>::new(key, reason, stats, l4, ts))
                    .await?;
            }
            #[cfg(feature = "icmp")]
            Some(L4Proto::Icmp) | Some(L4Proto::IcmpV6) => {
                dispatcher
                    .dispatch_async(&FlowEnded::<Icmp>::new(key, reason, stats, l4, ts))
                    .await?;
            }
            _ => {}
        },
        FsEvent::FlowEstablished { key, ts, l4 } => {
            if matches!(l4, Some(L4Proto::Tcp)) {
                dispatcher
                    .dispatch_async(&FlowEstablished::<Tcp>::new(key, ts))
                    .await?;
            }
        }
        FsEvent::FlowAnomaly { key, kind, ts } => {
            dispatcher
                .dispatch_async(&AnyFlowAnomaly {
                    key: Some(key),
                    kind,
                    ts,
                })
                .await?;
        }
        FsEvent::TrackerAnomaly { kind, ts } => {
            dispatcher
                .dispatch_async(&AnyFlowAnomaly {
                    key: None,
                    kind,
                    ts,
                })
                .await?;
        }
        // 0.22 R2: one flat FlowPacket carrying `proto`; no per-L4
        // dispatch fan-out.
        FsEvent::FlowPacket {
            key,
            side,
            len,
            ts,
            tcp,
        } => {
            dispatcher
                .dispatch_async(&FlowPacket::new(key.proto, key, side, len, tcp, ts))
                .await?;
        }
        FsEvent::FlowTick { key, stats, ts } => match key.proto {
            L4Proto::Tcp => {
                dispatcher
                    .dispatch_async(&FlowTick::<Tcp>::new(key, stats, ts))
                    .await?;
            }
            L4Proto::Udp => {
                dispatcher
                    .dispatch_async(&FlowTick::<Udp>::new(key, stats, ts))
                    .await?;
            }
            #[cfg(feature = "icmp")]
            L4Proto::Icmp | L4Proto::IcmpV6 => {
                dispatcher
                    .dispatch_async(&FlowTick::<Icmp>::new(key, stats, ts))
                    .await?;
            }
            _ => {}
        },
        FsEvent::ParserClosed {
            key,
            parser_kind,
            reason,
            ts,
        } => match key.proto {
            L4Proto::Tcp => {
                dispatcher
                    .dispatch_async(&ParserClosed::<Tcp>::new(key, parser_kind, reason, ts))
                    .await?;
            }
            L4Proto::Udp => {
                dispatcher
                    .dispatch_async(&ParserClosed::<Udp>::new(key, parser_kind, reason, ts))
                    .await?;
            }
            #[cfg(feature = "icmp")]
            L4Proto::Icmp | L4Proto::IcmpV6 => {
                dispatcher
                    .dispatch_async(&ParserClosed::<Icmp>::new(key, parser_kind, reason, ts))
                    .await?;
            }
            _ => {}
        },
        _ => {}
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn dispatch_lifecycle(
    dispatcher: &mut Dispatcher,
    sink: &mut dyn AnomalySink,
    state_map: &mut StateMap,
    counters: &mut CounterRegistry,
    evt: FsEvent<FlowKey>,
    source: SourceIdx,
    monitor_name: Option<&str>,
    flow_states: &mut crate::ctx::FlowStateRegistry,
    label_table: &flowscope::well_known::LabelTable,
) -> Result<()> {
    // Macro inlines the Ctx construction at each match arm so the
    // borrow checker can shorten each `&mut` borrow to the
    // dispatch call. Hoisting it into a closure trips
    // higher-rank-lifetime inference.
    macro_rules! dispatch_one {
        ($ty:ty, $payload:expr, $flow:expr, $ts:expr) => {{
            let mut ctx = Ctx {
                flow: $flow,
                ts: $ts,
                source,
                monitor_name,
                state_map: &mut *state_map,
                sink: &mut *sink,
                counters: &mut *counters,
                flow_states: &mut *flow_states,
                label_table,
                tracker: None,
            };
            dispatcher.dispatch::<$ty>(&$payload, &mut ctx)?;
        }};
    }

    match evt {
        FsEvent::FlowStarted { key, ts, l4 } => match l4 {
            Some(L4Proto::Tcp) => {
                dispatch_one!(
                    FlowStarted<Tcp>,
                    FlowStarted::<Tcp>::new(key, l4, ts),
                    Some(key),
                    ts
                );
            }
            Some(L4Proto::Udp) => {
                dispatch_one!(
                    FlowStarted<Udp>,
                    FlowStarted::<Udp>::new(key, l4, ts),
                    Some(key),
                    ts
                );
            }
            #[cfg(feature = "icmp")]
            Some(L4Proto::Icmp) | Some(L4Proto::IcmpV6) => {
                dispatch_one!(
                    FlowStarted<Icmp>,
                    FlowStarted::<Icmp>::new(key, l4, ts),
                    Some(key),
                    ts
                );
            }
            _ => {}
        },
        FsEvent::FlowEnded {
            key,
            reason,
            stats,
            ts,
            l4,
            ..
        } => match l4 {
            Some(L4Proto::Tcp) => {
                // 0.22 §2.6: synthesise a TcpRst alongside FlowEnded<Tcp>
                // when the close reason is RST. Cheap — the struct is
                // built only on real RSTs; dispatch is a no-op when no
                // TcpRst handler is registered.
                let is_rst = reason == flowscope::EndReason::Rst;
                dispatch_one!(
                    FlowEnded<Tcp>,
                    FlowEnded::<Tcp>::new(key, reason, stats.clone(), l4, ts),
                    Some(key),
                    ts
                );
                if is_rst {
                    dispatch_one!(TcpRst, TcpRst::new(key, stats, ts), Some(key), ts);
                }
            }
            Some(L4Proto::Udp) => {
                dispatch_one!(
                    FlowEnded<Udp>,
                    FlowEnded::<Udp>::new(key, reason, stats, l4, ts),
                    Some(key),
                    ts
                );
            }
            #[cfg(feature = "icmp")]
            Some(L4Proto::Icmp) | Some(L4Proto::IcmpV6) => {
                dispatch_one!(
                    FlowEnded<Icmp>,
                    FlowEnded::<Icmp>::new(key, reason, stats, l4, ts),
                    Some(key),
                    ts
                );
            }
            _ => {}
        },
        FsEvent::FlowEstablished { key, ts, l4 } => {
            if matches!(l4, Some(L4Proto::Tcp)) {
                dispatch_one!(
                    FlowEstablished<Tcp>,
                    FlowEstablished::<Tcp>::new(key, ts),
                    Some(key),
                    ts
                );
            }
        }
        FsEvent::FlowAnomaly { key, kind, ts } => {
            dispatch_one!(
                AnyFlowAnomaly,
                AnyFlowAnomaly {
                    key: Some(key),
                    kind,
                    ts,
                },
                Some(key),
                ts
            );
        }
        FsEvent::TrackerAnomaly { kind, ts } => {
            dispatch_one!(
                AnyFlowAnomaly,
                AnyFlowAnomaly {
                    key: None,
                    kind,
                    ts,
                },
                None,
                ts
            );
        }
        // 0.22 R2: one flat FlowPacket carrying `proto`.
        FsEvent::FlowPacket {
            key,
            side,
            len,
            ts,
            tcp,
        } => {
            dispatch_one!(
                FlowPacket,
                FlowPacket::new(key.proto, key, side, len, tcp, ts),
                Some(key),
                ts
            );
        }
        FsEvent::FlowTick { key, stats, ts } => match key.proto {
            L4Proto::Tcp => {
                dispatch_one!(
                    FlowTick<Tcp>,
                    FlowTick::<Tcp>::new(key, stats, ts),
                    Some(key),
                    ts
                );
            }
            L4Proto::Udp => {
                dispatch_one!(
                    FlowTick<Udp>,
                    FlowTick::<Udp>::new(key, stats, ts),
                    Some(key),
                    ts
                );
            }
            #[cfg(feature = "icmp")]
            L4Proto::Icmp | L4Proto::IcmpV6 => {
                dispatch_one!(
                    FlowTick<Icmp>,
                    FlowTick::<Icmp>::new(key, stats, ts),
                    Some(key),
                    ts
                );
            }
            _ => {}
        },
        FsEvent::ParserClosed {
            key,
            parser_kind,
            reason,
            ts,
        } => match key.proto {
            L4Proto::Tcp => {
                dispatch_one!(
                    ParserClosed<Tcp>,
                    ParserClosed::<Tcp>::new(key, parser_kind, reason, ts),
                    Some(key),
                    ts
                );
            }
            L4Proto::Udp => {
                dispatch_one!(
                    ParserClosed<Udp>,
                    ParserClosed::<Udp>::new(key, parser_kind, reason, ts),
                    Some(key),
                    ts
                );
            }
            #[cfg(feature = "icmp")]
            L4Proto::Icmp | L4Proto::IcmpV6 => {
                dispatch_one!(
                    ParserClosed<Icmp>,
                    ParserClosed::<Icmp>::new(key, parser_kind, reason, ts),
                    Some(key),
                    ts
                );
            }
            _ => {}
        },
        _ => {}
    }
    Ok(())
}