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
//! Core traits for packet capture and injection.
//!
//! - [`PacketSource`] — RX path (batch-oriented, zero-copy)
//! - [`PacketSink`] — TX path (frame-based)
//! - `AsyncPacketSource` — async RX (feature: `tokio`)
use AsFd;
use Duration;
use crateTxSlot;
use crateError;
use cratePacketBatch;
use crateCaptureStats;
/// A source of packet batches (RX path).
///
/// The core abstraction for receiving packets. Implement this trait to add
/// new backends (AF_XDP, mock sources, pcap replay).
///
/// # Ownership Model
///
/// Only **one [`PacketBatch`] can be live at a time** — enforced by `&mut self`
/// on [`next_batch()`](PacketSource::next_batch). When the batch is dropped,
/// the underlying block is returned to the kernel (RAII).
///
/// # AsFd
///
/// Requires [`AsFd`] so the fd can be used with `epoll`, tokio's `AsyncFd`,
/// or for attaching eBPF programs via `aya`.
///
/// # Examples
///
/// ```no_run
/// use netring::{Capture, PacketSource};
/// use std::time::Duration;
///
/// let mut rx = Capture::open("lo").unwrap();
/// while let Some(batch) = rx.next_batch_blocking(Duration::from_millis(100)).unwrap() {
/// for pkt in &batch {
/// println!("{} bytes", pkt.len());
/// }
/// // batch dropped → block returned to kernel
/// }
/// ```
/// A sink for outgoing packets (TX path).
///
/// Provides a two-step send model: [`allocate()`](PacketSink::allocate) a
/// frame, write data into it, then [`send()`](TxSlot::send) it. Call
/// [`flush()`](PacketSink::flush) to trigger kernel transmission.
///
/// # Frame Lifecycle
///
/// ```text
/// allocate() → TxSlot → write data → send() → flush() → kernel transmits
/// → drop (no send) → frame discarded
/// ```
/// Async packet source (feature: `tokio`).
///
/// Uses native `async fn` in traits (stable since Rust 1.75 — no
/// `#[async_trait]` proc macro needed).
/// Packet sources that support atomic BPF filter replacement on a
/// running socket (AF_PACKET semantics: `setsockopt(SO_ATTACH_FILTER)`).
///
/// Implemented for [`crate::Capture`]. **Not** implemented for AF_XDP
/// sockets — XDP filtering happens in the XDP program, not via
/// `SO_ATTACH_FILTER`.
///
/// Use via [`crate::Capture::set_filter`] directly, or via
/// [`crate::AsyncCapture::set_filter`] (where the trait bound gates
/// the method to AF_PACKET-backed captures only).