sof 0.18.2

Solana Observer Framework for low-latency shred ingestion and plugin-driven transaction observation
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
#![cfg_attr(
    not(all(target_os = "linux", feature = "kernel-bypass")),
    allow(unused)
)]
//! AF_XDP external-ingress metrics example for SOF kernel-bypass runtime mode.

#[cfg(not(all(target_os = "linux", feature = "kernel-bypass")))]
fn main() {
    eprintln!("This example requires Linux and --features kernel-bypass");
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
use std::{
    ffi::CString,
    io,
    net::{IpAddr, Ipv4Addr, SocketAddr},
    sync::{
        Arc,
        atomic::{AtomicU64, Ordering},
    },
    time::{Duration, Instant},
};

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
use async_trait::async_trait;
#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
use sof::{
    event::TxKind,
    framework::{
        ObserverPlugin, PluginConfig, PluginDispatchMode, PluginHost, RawPacketEvent, ShredEvent,
    },
    ingest::{RawPacketBatch, RawPacketIngress},
    runtime::KernelBypassIngressSender,
};
#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
use xdp::{
    RingConfigBuilder, Umem,
    slab::{HeapSlab, Slab},
    socket::{PollTimeout, XdpSocket, XdpSocketBuilder},
    umem::{FrameSize, UmemCfgBuilder},
};

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
/// Example-local type alias.
pub(crate) type ExampleError = Box<dyn std::error::Error + Send + Sync>;

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
/// Example configuration constant.
pub(crate) const DEFAULT_DURATION_SECS: u64 = 180;
#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
/// Example configuration constant.
pub(crate) const DEFAULT_INTERFACE: &str = "enp17s0";
#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
/// Example configuration constant.
pub(crate) const DEFAULT_QUEUE_ID: u32 = 0;
#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
/// Example configuration constant.
pub(crate) const DEFAULT_BATCH_SIZE: usize = 64;
#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
/// Example configuration constant.
pub(crate) const DEFAULT_UMEM_FRAME_COUNT: u32 = 4_096;
#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
/// Example configuration constant.
pub(crate) const DEFAULT_RING_DEPTH: u32 = 2_048;
#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
/// Example configuration constant.
pub(crate) const DEFAULT_POLL_TIMEOUT_MS: u64 = 100;

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq)]
/// Example-local state or configuration type.
pub(crate) struct RawIngressSnapshot {
    /// Example-local field.
    pub(crate) packets: u64,
    /// Example-local field.
    pub(crate) bytes: u64,
    /// Example-local field.
    pub(crate) shreds: u64,
    /// Example-local field.
    pub(crate) data_shreds: u64,
    /// Example-local field.
    pub(crate) code_shreds: u64,
    /// Example-local field.
    pub(crate) tx_total: u64,
    /// Example-local field.
    pub(crate) tx_vote_only: u64,
    /// Example-local field.
    pub(crate) tx_mixed: u64,
    /// Example-local field.
    pub(crate) tx_non_vote: u64,
    /// Example-local field.
    pub(crate) source_8899_packets: u64,
    /// Example-local field.
    pub(crate) source_8900_packets: u64,
    /// Example-local field.
    pub(crate) source_other_packets: u64,
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
#[derive(Default)]
/// Example-local state or configuration type.
pub(crate) struct RawIngressMetricsPlugin {
    /// Example-local field.
    pub(crate) packets: AtomicU64,
    /// Example-local field.
    pub(crate) bytes: AtomicU64,
    /// Example-local field.
    pub(crate) shreds: AtomicU64,
    /// Example-local field.
    pub(crate) data_shreds: AtomicU64,
    /// Example-local field.
    pub(crate) code_shreds: AtomicU64,
    /// Example-local field.
    pub(crate) tx_total: AtomicU64,
    /// Example-local field.
    pub(crate) tx_vote_only: AtomicU64,
    /// Example-local field.
    pub(crate) tx_mixed: AtomicU64,
    /// Example-local field.
    pub(crate) tx_non_vote: AtomicU64,
    /// Example-local field.
    pub(crate) source_8899_packets: AtomicU64,
    /// Example-local field.
    pub(crate) source_8900_packets: AtomicU64,
    /// Example-local field.
    pub(crate) source_other_packets: AtomicU64,
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
impl RawIngressMetricsPlugin {
    /// Example helper used by this binary.
    pub(crate) fn snapshot(&self) -> RawIngressSnapshot {
        RawIngressSnapshot {
            packets: self.packets.load(Ordering::Relaxed),
            bytes: self.bytes.load(Ordering::Relaxed),
            shreds: self.shreds.load(Ordering::Relaxed),
            data_shreds: self.data_shreds.load(Ordering::Relaxed),
            code_shreds: self.code_shreds.load(Ordering::Relaxed),
            tx_total: self.tx_total.load(Ordering::Relaxed),
            tx_vote_only: self.tx_vote_only.load(Ordering::Relaxed),
            tx_mixed: self.tx_mixed.load(Ordering::Relaxed),
            tx_non_vote: self.tx_non_vote.load(Ordering::Relaxed),
            source_8899_packets: self.source_8899_packets.load(Ordering::Relaxed),
            source_8900_packets: self.source_8900_packets.load(Ordering::Relaxed),
            source_other_packets: self.source_other_packets.load(Ordering::Relaxed),
        }
    }
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
#[async_trait]
impl ObserverPlugin for RawIngressMetricsPlugin {
    fn name(&self) -> &'static str {
        "af-xdp-kernel-bypass-ingress-metrics-plugin"
    }

    fn config(&self) -> PluginConfig {
        PluginConfig::new()
            .with_raw_packet()
            .with_shred()
            .with_transaction()
    }

    async fn on_raw_packet(&self, event: RawPacketEvent) {
        self.packets.fetch_add(1, Ordering::Relaxed);
        self.bytes.fetch_add(
            u64::try_from(event.bytes.len()).unwrap_or(u64::MAX),
            Ordering::Relaxed,
        );
        match event.source.port() {
            8_899 => {
                self.source_8899_packets.fetch_add(1, Ordering::Relaxed);
            }
            8_900 => {
                self.source_8900_packets.fetch_add(1, Ordering::Relaxed);
            }
            _ => {
                self.source_other_packets.fetch_add(1, Ordering::Relaxed);
            }
        }
    }

    async fn on_shred(&self, event: ShredEvent) {
        self.shreds.fetch_add(1, Ordering::Relaxed);
        match event.parsed.as_ref() {
            sof::shred::wire::ParsedShredHeader::Data(_) => {
                self.data_shreds.fetch_add(1, Ordering::Relaxed);
            }
            sof::shred::wire::ParsedShredHeader::Code(_) => {
                self.code_shreds.fetch_add(1, Ordering::Relaxed);
            }
        }
    }

    async fn on_transaction(&self, event: &sof::framework::TransactionEvent) {
        self.tx_total.fetch_add(1, Ordering::Relaxed);
        match event.kind {
            TxKind::VoteOnly => {
                self.tx_vote_only.fetch_add(1, Ordering::Relaxed);
            }
            TxKind::Mixed => {
                self.tx_mixed.fetch_add(1, Ordering::Relaxed);
            }
            TxKind::NonVote => {
                self.tx_non_vote.fetch_add(1, Ordering::Relaxed);
            }
        }
    }
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq)]
/// Example-local state or configuration type.
pub(crate) struct AfXdpProducerStats {
    /// Example-local field.
    pub(crate) frames_seen: u64,
    /// Example-local field.
    pub(crate) udp_frames_forwarded: u64,
    /// Example-local field.
    pub(crate) filtered_frames: u64,
    /// Example-local field.
    pub(crate) parse_error_frames: u64,
    /// Example-local field.
    pub(crate) batches_sent: u64,
    /// Example-local field.
    pub(crate) bytes_forwarded: u64,
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
/// Example-local state or configuration type.
pub(crate) struct PortFilter {
    /// Example-local field.
    pub(crate) range_start: u16,
    /// Example-local field.
    pub(crate) range_end: u16,
    /// Example-local field.
    pub(crate) extra_port: u16,
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
impl PortFilter {
    /// Example configuration constant.
    pub(crate) const fn default_sol() -> Self {
        Self {
            range_start: 12_000,
            range_end: 12_100,
            extra_port: 8_001,
        }
    }

    /// Example configuration constant.
    pub(crate) const fn allows(self, port: u16) -> bool {
        (port >= self.range_start && port <= self.range_end) || port == self.extra_port
    }
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
/// Example-local enum used by this example binary.
pub(crate) enum FrameParseOutcome {
    /// Example-local variant.
    ParseError,
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
#[derive(Debug, Clone, Eq, PartialEq)]
/// Example-local state or configuration type.
pub(crate) struct AfXdpConfig {
    /// Example-local field.
    pub(crate) interface: String,
    /// Example-local field.
    pub(crate) queue_id: u32,
    /// Example-local field.
    pub(crate) batch_size: usize,
    /// Example-local field.
    pub(crate) umem_frame_count: u32,
    /// Example-local field.
    pub(crate) ring_depth: u32,
    /// Example-local field.
    pub(crate) poll_timeout: Duration,
    /// Example-local field.
    pub(crate) filter: PortFilter,
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
/// Example-local state or configuration type.
pub(crate) struct AfXdpSocketState {
    /// Example-local field.
    pub(crate) socket: XdpSocket,
    /// Example-local field.
    pub(crate) rings: xdp::WakableRings,
    /// Example-local field.
    pub(crate) umem: Umem,
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
/// Example helper used by this binary.
pub(crate) fn read_env_u64(name: &str, default: u64) -> u64 {
    std::env::var(name)
        .ok()
        .and_then(|value| value.parse::<u64>().ok())
        .filter(|value| *value > 0)
        .unwrap_or(default)
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
/// Example helper used by this binary.
pub(crate) fn read_env_u32(name: &str, default: u32) -> u32 {
    std::env::var(name)
        .ok()
        .and_then(|value| value.parse::<u32>().ok())
        .filter(|value| *value > 0)
        .unwrap_or(default)
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
/// Example helper used by this binary.
pub(crate) fn read_env_usize(name: &str, default: usize) -> usize {
    std::env::var(name)
        .ok()
        .and_then(|value| value.parse::<usize>().ok())
        .filter(|value| *value > 0)
        .unwrap_or(default)
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
/// Example helper used by this binary.
pub(crate) fn read_af_xdp_config() -> AfXdpConfig {
    AfXdpConfig {
        interface: std::env::var("SOF_AF_XDP_IFACE")
            .unwrap_or_else(|_| DEFAULT_INTERFACE.to_owned()),
        queue_id: read_env_u32("SOF_AF_XDP_QUEUE_ID", DEFAULT_QUEUE_ID),
        batch_size: read_env_usize("SOF_AF_XDP_BATCH_SIZE", DEFAULT_BATCH_SIZE),
        umem_frame_count: read_env_u32("SOF_AF_XDP_UMEM_FRAMES", DEFAULT_UMEM_FRAME_COUNT),
        ring_depth: read_env_u32("SOF_AF_XDP_RING_DEPTH", DEFAULT_RING_DEPTH),
        poll_timeout: Duration::from_millis(read_env_u64(
            "SOF_AF_XDP_POLL_TIMEOUT_MS",
            DEFAULT_POLL_TIMEOUT_MS,
        )),
        filter: PortFilter::default_sol(),
    }
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
/// Example helper used by this binary.
pub(crate) fn build_af_xdp_socket(config: &AfXdpConfig) -> Result<AfXdpSocketState, ExampleError> {
    let ifname = CString::new(config.interface.clone())?;
    let nic = xdp::nic::NicIndex::lookup_by_name(&ifname)?.ok_or_else(|| {
        io::Error::other(format!(
            "network interface `{}` not found",
            config.interface
        ))
    })?;

    let mut builder = XdpSocketBuilder::new()?;
    let umem_cfg = UmemCfgBuilder {
        frame_size: FrameSize::TwoK,
        frame_count: config.umem_frame_count,
        ..Default::default()
    }
    .build()?;
    let mut umem = Umem::map(umem_cfg)?;
    let ring_cfg = RingConfigBuilder {
        rx_count: config.ring_depth,
        tx_count: 0,
        fill_count: config.ring_depth,
        completion_count: config.ring_depth,
    }
    .build()?;
    let (mut rings, bind_flags) = builder.build_wakable_rings(&umem, ring_cfg)?;
    let socket = builder.bind(nic, config.queue_id, bind_flags)?;

    // SAFETY: `umem` and `fill_ring` were created together by `build_wakable_rings`,
    // and the requested descriptor count is bounded by the configured ring depth.
    let fill_queued = unsafe {
        rings.fill_ring.enqueue(
            &mut umem,
            usize::try_from(config.ring_depth).unwrap_or(0),
            true,
        )?
    };
    tracing::info!(
        interface = %config.interface,
        queue_id = config.queue_id,
        ring_depth = config.ring_depth,
        fill_queued,
        "AF_XDP socket initialized"
    );

    Ok(AfXdpSocketState {
        socket,
        rings,
        umem,
    })
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
/// Example helper used by this binary.
pub(crate) fn parse_udp_payload_to_raw_packet(
    frame: &[u8],
    filter: PortFilter,
) -> Result<Option<(SocketAddr, &[u8])>, FrameParseOutcome> {
    const ETH_LEN: usize = 14;
    const ETH_TYPE_OFFSET: usize = 12;
    const ETH_P_IPV4: u16 = 0x0800;
    const IP_PROTO_UDP: u8 = 17;
    if frame.len() < ETH_LEN + 20 {
        return Err(FrameParseOutcome::ParseError);
    }

    if read_u16_be(frame, ETH_TYPE_OFFSET)? != ETH_P_IPV4 {
        return Ok(None);
    }

    let ip_offset = ETH_LEN;
    let version_ihl = read_u8(frame, ip_offset)?;
    if version_ihl >> 4 != 4 {
        return Ok(None);
    }
    let ihl = usize::from(version_ihl & 0x0F).checked_mul(4).unwrap_or(0);
    let udp_header_end = checked_add(checked_add(ip_offset, ihl)?, 8)?;
    if ihl < 20 || frame.len() < udp_header_end {
        return Err(FrameParseOutcome::ParseError);
    }
    if read_u8(frame, checked_add(ip_offset, 9)?)? != IP_PROTO_UDP {
        return Ok(None);
    }
    let frag_field = read_u16_be(frame, checked_add(ip_offset, 6)?)?;
    if (frag_field & 0x1FFF) != 0 {
        return Ok(None);
    }

    let udp_offset = checked_add(ip_offset, ihl)?;
    let src_port = read_u16_be(frame, udp_offset)?;
    let dst_port = read_u16_be(frame, checked_add(udp_offset, 2)?)?;
    if !filter.allows(dst_port) {
        return Ok(None);
    }
    let udp_len = usize::from(read_u16_be(frame, checked_add(udp_offset, 4)?)?);
    if udp_len < 8 {
        return Err(FrameParseOutcome::ParseError);
    }
    let payload_start = checked_add(udp_offset, 8)?;
    let payload_end = checked_add(payload_start, udp_len.saturating_sub(8))?;
    if payload_end > frame.len() {
        return Err(FrameParseOutcome::ParseError);
    }

    let source = SocketAddr::new(
        IpAddr::V4(read_ipv4_addr(frame, checked_add(ip_offset, 12)?)?),
        src_port,
    );
    Ok(Some((
        source,
        frame
            .get(payload_start..payload_end)
            .ok_or(FrameParseOutcome::ParseError)?,
    )))
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
/// Example helper used by this binary.
pub(crate) fn checked_add(lhs: usize, rhs: usize) -> Result<usize, FrameParseOutcome> {
    lhs.checked_add(rhs).ok_or(FrameParseOutcome::ParseError)
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
/// Example helper used by this binary.
pub(crate) fn read_u8(frame: &[u8], offset: usize) -> Result<u8, FrameParseOutcome> {
    frame
        .get(offset)
        .copied()
        .ok_or(FrameParseOutcome::ParseError)
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
/// Example helper used by this binary.
pub(crate) fn read_u16_be(frame: &[u8], offset: usize) -> Result<u16, FrameParseOutcome> {
    let end = checked_add(offset, 2)?;
    let bytes: [u8; 2] = frame
        .get(offset..end)
        .ok_or(FrameParseOutcome::ParseError)?
        .try_into()
        .map_err(|_slice_error| FrameParseOutcome::ParseError)?;
    Ok(u16::from_be_bytes(bytes))
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
/// Example helper used by this binary.
pub(crate) fn read_ipv4_addr(frame: &[u8], offset: usize) -> Result<Ipv4Addr, FrameParseOutcome> {
    let end = checked_add(offset, 4)?;
    let octets: [u8; 4] = frame
        .get(offset..end)
        .ok_or(FrameParseOutcome::ParseError)?
        .try_into()
        .map_err(|_slice_error| FrameParseOutcome::ParseError)?;
    Ok(Ipv4Addr::from(octets))
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
/// Example helper used by this binary.
pub(crate) fn run_af_xdp_producer(
    tx: &KernelBypassIngressSender,
    config: &AfXdpConfig,
    run_for: Duration,
) -> Result<AfXdpProducerStats, ExampleError> {
    let mut state = build_af_xdp_socket(config)?;
    let mut slab = HeapSlab::with_capacity(usize::try_from(config.ring_depth).unwrap_or(0));
    let mut batch = RawPacketBatch::with_capacity(config.batch_size);
    let started_at = Instant::now();
    let mut stats = AfXdpProducerStats::default();

    while started_at.elapsed() < run_for {
        drop(
            state
                .socket
                .poll_read(PollTimeout::new(Some(config.poll_timeout))),
        );

        let Some(rx_ring) = state.rings.rx_ring.as_mut() else {
            return Err(io::Error::other("AF_XDP socket has no RX ring configured").into());
        };
        // SAFETY: `rx_ring` is paired with `state.umem` from the same socket setup,
        // and `slab` is the destination scratch storage expected by the xdp crate.
        let received = unsafe { rx_ring.recv(&state.umem, &mut slab) };
        if received == 0 {
            continue;
        }

        for _ in 0..received {
            let Some(frame) = slab.pop_back() else {
                break;
            };
            stats.frames_seen = stats.frames_seen.saturating_add(1);
            match parse_udp_payload_to_raw_packet(&frame, config.filter) {
                Ok(Some((source, payload))) => {
                    stats.udp_frames_forwarded = stats.udp_frames_forwarded.saturating_add(1);
                    stats.bytes_forwarded = stats
                        .bytes_forwarded
                        .saturating_add(u64::try_from(payload.len()).unwrap_or(u64::MAX));
                    batch.push_packet_bytes(source, RawPacketIngress::Udp, payload)?;
                    if batch.len() >= config.batch_size {
                        if !tx.send_batch(std::mem::take(&mut batch), false) {
                            state.umem.free_packet(frame);
                            return Ok(stats);
                        }
                        batch = RawPacketBatch::with_capacity(config.batch_size);
                        stats.batches_sent = stats.batches_sent.saturating_add(1);
                    }
                }
                Ok(None) => {
                    stats.filtered_frames = stats.filtered_frames.saturating_add(1);
                }
                Err(FrameParseOutcome::ParseError) => {
                    stats.parse_error_frames = stats.parse_error_frames.saturating_add(1);
                }
            }
            state.umem.free_packet(frame);
        }

        // SAFETY: Returned RX buffers are recycled into the matching fill ring for the
        // same `umem`, bounded by the number of descriptors just received.
        unsafe {
            state
                .rings
                .fill_ring
                .enqueue(&mut state.umem, received, true)?
        };
    }

    if !batch.is_empty() && tx.send_batch(batch, false) {
        stats.batches_sent = stats.batches_sent.saturating_add(1);
    }

    Ok(stats)
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
/// Example helper used by this binary.
pub(crate) fn print_summary(
    duration: Duration,
    config: &AfXdpConfig,
    producer_stats: AfXdpProducerStats,
    plugin_snapshot: RawIngressSnapshot,
    dropped_events: u64,
) {
    let elapsed_ms = u64::try_from(duration.as_millis()).unwrap_or(u64::MAX);
    let plugin_mib_per_sec = format_mib_per_sec(plugin_snapshot.bytes, elapsed_ms);

    println!("af_xdp_kernel_bypass summary");
    println!(
        "af_xdp: interface={} queue_id={} ring_depth={} batch_size={} frames_seen={} udp_frames_forwarded={} filtered_frames={} parse_error_frames={} batches_sent={} bytes_forwarded={}",
        config.interface,
        config.queue_id,
        config.ring_depth,
        config.batch_size,
        producer_stats.frames_seen,
        producer_stats.udp_frames_forwarded,
        producer_stats.filtered_frames,
        producer_stats.parse_error_frames,
        producer_stats.batches_sent,
        producer_stats.bytes_forwarded
    );
    println!(
        "plugin: packets={} bytes={} shreds={} data_shreds={} code_shreds={} tx_total={} tx_vote_only={} tx_mixed={} tx_non_vote={} source_8899={} source_8900={} source_other={}",
        plugin_snapshot.packets,
        plugin_snapshot.bytes,
        plugin_snapshot.shreds,
        plugin_snapshot.data_shreds,
        plugin_snapshot.code_shreds,
        plugin_snapshot.tx_total,
        plugin_snapshot.tx_vote_only,
        plugin_snapshot.tx_mixed,
        plugin_snapshot.tx_non_vote,
        plugin_snapshot.source_8899_packets,
        plugin_snapshot.source_8900_packets,
        plugin_snapshot.source_other_packets
    );
    println!("plugin_throughput_mib_per_sec={plugin_mib_per_sec}");
    println!("plugin_dispatch_dropped_events={dropped_events}");
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
/// Example helper used by this binary.
pub(crate) fn format_mib_per_sec(bytes: u64, elapsed_ms: u64) -> String {
    if elapsed_ms == 0 {
        return "0.000".to_owned();
    }

    let numerator = u128::from(bytes).saturating_mul(1_000_000);
    let denominator = u128::from(elapsed_ms).saturating_mul(1_048_576);
    let scaled = numerator.checked_div(denominator).unwrap_or(0);
    let whole = scaled / 1_000;
    let fractional = scaled % 1_000;

    format!("{whole}.{fractional:03}")
}

#[cfg(all(target_os = "linux", feature = "kernel-bypass"))]
#[tokio::main]
async fn main() -> Result<(), ExampleError> {
    let duration = Duration::from_secs(read_env_u64(
        "SOF_AF_XDP_EXAMPLE_DURATION_SECS",
        DEFAULT_DURATION_SECS,
    ));
    let config = read_af_xdp_config();

    tracing::info!(
        duration_secs = duration.as_secs(),
        interface = %config.interface,
        queue_id = config.queue_id,
        ring_depth = config.ring_depth,
        "starting AF_XDP kernel-bypass ingress metrics example"
    );

    let plugin = Arc::new(RawIngressMetricsPlugin::default());
    let plugin_host = PluginHost::builder()
        .with_event_queue_capacity(262_144)
        .with_dispatch_mode(PluginDispatchMode::BoundedConcurrent(16))
        .add_shared_plugin(plugin.clone())
        .build();
    let plugin_host_metrics = plugin_host.clone();

    let (tx, rx) = sof::runtime::create_kernel_bypass_ingress_queue();
    let runtime_task = tokio::spawn(async move {
        sof::runtime::run_async_with_plugin_host_and_kernel_bypass_ingress(plugin_host, rx).await
    });

    let producer_config = config.clone();
    let producer_task =
        tokio::task::spawn_blocking(move || run_af_xdp_producer(&tx, &producer_config, duration));
    let producer_stats = producer_task.await.map_err(|error| {
        io::Error::other(format!("AF_XDP producer task join failed: {error}"))
    })??;

    let runtime_result = tokio::time::timeout(Duration::from_secs(30), runtime_task)
        .await
        .map_err(|timeout_error| {
            io::Error::new(
                io::ErrorKind::TimedOut,
                format!("runtime task timed out: {timeout_error}"),
            )
        })?;
    match runtime_result {
        Ok(Ok(())) => {}
        Ok(Err(error)) => {
            return Err(io::Error::other(format!("runtime returned error: {error}")).into());
        }
        Err(error) => {
            return Err(io::Error::other(format!("runtime task join failed: {error}")).into());
        }
    }

    let plugin_snapshot = plugin.snapshot();
    let dropped_events = plugin_host_metrics.dropped_event_count();
    print_summary(
        duration,
        &config,
        producer_stats,
        plugin_snapshot,
        dropped_events,
    );

    if plugin_snapshot.packets == 0 {
        return Err(io::Error::other(
            "AF_XDP ingress observed zero packets; ensure CAP_NET_ADMIN/CAP_BPF and an XDP redirect program are configured",
        )
        .into());
    }

    Ok(())
}