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
use libbpf_sys::{
_bpf_helper_func_names, libbpf_probe_bpf_helper, libbpf_probe_bpf_map_type,
libbpf_probe_bpf_prog_type, __BPF_FUNC_MAX_ID,
};
use num_enum::{IntoPrimitive, TryFromPrimitive};
use std::{
ffi::CStr,
fmt::{Debug, Display},
os::raw,
ptr,
time::Duration,
};
use thiserror::Error as ThisError;
#[derive(ThisError, Debug)]
pub enum Error {
#[error("errno: {0}")]
Errno(i32),
#[error("error code: {0}")]
Code(i32),
#[error("unknown: {0}")]
Unknown(i32),
}
#[non_exhaustive]
#[repr(u32)]
#[derive(Debug, TryFromPrimitive, IntoPrimitive, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ProgramType {
Unspec = 0,
SocketFilter,
Kprobe,
SchedCls,
SchedAct,
Tracepoint,
Xdp,
PerfEvent,
CgroupSkb,
CgroupSock,
LwtIn,
LwtOut,
LwtXmit,
SockOps,
SkSkb,
CgroupDevice,
SkMsg,
RawTracepoint,
CgroupSockAddr,
LwtSeg6local,
LircMode2,
SkReuseport,
FlowDissector,
CgroupSysctl,
RawTracepointWritable,
CgroupSockopt,
Tracing,
StructOps,
Ext,
Lsm,
SkLookup,
Syscall,
}
impl ProgramType {
pub fn name(&self) -> &'static str {
match *self {
ProgramType::Unspec => "unspec",
ProgramType::SocketFilter => "socket_filter",
ProgramType::Kprobe => "kprobe",
ProgramType::SchedCls => "sched_cls",
ProgramType::SchedAct => "sched_act",
ProgramType::Tracepoint => "tracepoint",
ProgramType::Xdp => "xdp",
ProgramType::PerfEvent => "perf_event",
ProgramType::CgroupSkb => "cgroup_skb",
ProgramType::CgroupSock => "cgroup_sock",
ProgramType::LwtIn => "lwt_in",
ProgramType::LwtOut => "lwt_out",
ProgramType::LwtXmit => "lwt_xmit",
ProgramType::SockOps => "sock_ops",
ProgramType::SkSkb => "sk_skb",
ProgramType::CgroupDevice => "cgroup_device",
ProgramType::SkMsg => "sk_msg",
ProgramType::RawTracepoint => "raw_tracepoint",
ProgramType::CgroupSockAddr => "cgroup_sock_addr",
ProgramType::LwtSeg6local => "lwt_seg6local",
ProgramType::LircMode2 => "lirc_mode2",
ProgramType::SkReuseport => "sk_reuseport",
ProgramType::FlowDissector => "flow_dissector",
ProgramType::CgroupSysctl => "cgroup_sysctl",
ProgramType::RawTracepointWritable => "raw_tracepoint_writable",
ProgramType::CgroupSockopt => "cgroup_sockopt",
ProgramType::Tracing => "tracing",
ProgramType::StructOps => "struct_ops",
ProgramType::Ext => "ext",
ProgramType::Lsm => "lsm",
ProgramType::SkLookup => "sk_lookup",
ProgramType::Syscall => "syscall",
}
}
pub fn probe(&self) -> Result<bool, Error> {
match unsafe { libbpf_probe_bpf_prog_type((*self).into(), ptr::null()) } {
negative if negative < 0 => Err(Error::Code(negative)),
0 => Ok(false),
1 => Ok(true),
positive if positive > 1 => Err(Error::Unknown(positive)),
_ => unreachable!(),
}
}
pub fn probe_helper(&self, helper: BpfHelper) -> Result<bool, Error> {
match unsafe { libbpf_probe_bpf_helper((*self).into(), helper.0, ptr::null()) } {
negative if negative < 0 => Err(Error::Code(negative)),
0 => Ok(false),
1 => Ok(true),
positive if positive > 1 => Err(Error::Unknown(positive)),
_ => unreachable!(),
}
}
pub fn iter() -> impl Iterator<Item = ProgramType> {
ProgramTypeIter(1)
}
}
impl Display for ProgramType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name())
}
}
struct ProgramTypeIter(u32);
impl Iterator for ProgramTypeIter {
type Item = ProgramType;
fn next(&mut self) -> Option<Self::Item> {
let next = self.0;
if next > ProgramType::Syscall.into() {
None
} else {
self.0 = self.0 + 1;
ProgramType::try_from_primitive(next).ok()
}
}
}
#[derive(Debug)]
#[repr(C)]
pub struct ProgramInfo {
pub name: String,
pub ty: ProgramType,
pub tag: [u8; 8],
pub id: u32,
pub jited_prog_len: u32,
pub xlated_prog_len: u32,
pub jited_prog_insns: u64,
pub xlated_prog_insns: u64,
pub load_time: Duration,
pub created_by_uid: u32,
pub nr_map_ids: u32,
pub map_ids: u64,
pub ifindex: u32,
pub gpl_compatible: bool,
pub netns_dev: u64,
pub netns_ino: u64,
pub nr_jited_ksyms: u32,
pub nr_jited_func_lens: u32,
pub jited_ksyms: u64,
pub jited_func_lens: u64,
pub btf_id: u32,
pub func_info_rec_size: u32,
pub func_info: u64,
pub nr_func_info: u32,
pub nr_line_info: u32,
pub line_info: u64,
pub jited_line_info: u64,
pub nr_jited_line_info: u32,
pub line_info_rec_size: u32,
pub jited_line_info_rec_size: u32,
pub nr_prog_tags: u32,
pub prog_tags: u64,
pub run_time_ns: u64,
pub run_cnt: u64,
}
#[non_exhaustive]
pub enum ProgramLicense {
GPL,
}
impl ProgramLicense {
pub fn as_str_with_nul(&self) -> &'static str {
match *self {
ProgramLicense::GPL => "GPL\0",
}
}
pub fn as_ptr(&self) -> *const raw::c_char {
CStr::from_bytes_with_nul(self.as_str_with_nul().as_bytes())
.unwrap()
.as_ptr()
}
}
#[non_exhaustive]
#[repr(u32)]
#[derive(Debug, TryFromPrimitive, IntoPrimitive, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MapType {
Unspec = 0,
Hash,
Array,
ProgArray,
PerfEventArray,
PerCpuHash,
PerCpuArray,
StackTrace,
CgroupArray,
LruHash,
LruPerCpuHash,
LpmTrie,
ArrayOfMaps,
HashOfMaps,
DevMap,
SockMap,
CpuMap,
XskMap,
SockHash,
CgroupStorage,
ReusePortSockArray,
PerCpuCgroupStorage,
Queue,
Stack,
SkStorage,
DevMapHash,
StructOps,
RingBuf,
InodeStorage,
TaskStorage,
BloomFilter,
}
impl MapType {
pub fn name(&self) -> &'static str {
match *self {
MapType::Unspec => "unspec",
MapType::Hash => "hash",
MapType::Array => "array",
MapType::ProgArray => "prog_array",
MapType::PerfEventArray => "perf_event_array",
MapType::PerCpuHash => "percpu_hash",
MapType::PerCpuArray => "percpu_array",
MapType::StackTrace => "stack_trace",
MapType::CgroupArray => "cgroup_array",
MapType::LruHash => "lru_hash",
MapType::LruPerCpuHash => "lru_percpu_hash",
MapType::LpmTrie => "lpm_trie",
MapType::ArrayOfMaps => "array_of_maps",
MapType::HashOfMaps => "hash_of_maps",
MapType::DevMap => "devmap",
MapType::SockMap => "sockmap",
MapType::CpuMap => "cpumap",
MapType::XskMap => "xskmap",
MapType::SockHash => "sockhash",
MapType::CgroupStorage => "cgroup_storage",
MapType::ReusePortSockArray => "reuseport_sockarray",
MapType::PerCpuCgroupStorage => "percpu_cgroup_storage",
MapType::Queue => "queue",
MapType::Stack => "stack",
MapType::SkStorage => "sk_storage",
MapType::DevMapHash => "devmap_hash",
MapType::StructOps => "struct_ops",
MapType::RingBuf => "ringbuf",
MapType::InodeStorage => "inode_storage",
MapType::TaskStorage => "task_storage",
MapType::BloomFilter => "bloom_filter",
}
}
pub fn probe(&self) -> Result<bool, Error> {
match unsafe { libbpf_probe_bpf_map_type((*self).into(), ptr::null()) } {
negative if negative < 0 => Err(Error::Code(negative)),
0 => Ok(false),
1 => Ok(true),
positive if positive > 1 => Err(Error::Unknown(positive)),
_ => unreachable!(),
}
}
pub fn iter() -> impl Iterator<Item = MapType> {
MapTypeIter(1)
}
}
impl Display for MapType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name())
}
}
struct MapTypeIter(u32);
impl Iterator for MapTypeIter {
type Item = MapType;
fn next(&mut self) -> Option<Self::Item> {
let next = self.0;
if next > MapType::BloomFilter.into() {
None
} else {
self.0 = self.0 + 1;
MapType::try_from_primitive(next).ok()
}
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct BpfHelper(pub u32);
impl BpfHelper {
pub fn name(&self) -> &'static str {
match usize::try_from(self.0) {
Ok(func_idx) => {
if func_idx >= unsafe { _bpf_helper_func_names.len() } {
"<unknown>"
} else {
let fn_name_ptr = unsafe { _bpf_helper_func_names[func_idx] };
let cstr = unsafe { CStr::from_ptr(fn_name_ptr) };
cstr.to_str().unwrap_or("<utf8err>")
}
}
Err(_) => "<unknown>",
}
}
}
impl Display for BpfHelper {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name())
}
}
impl Debug for BpfHelper {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple(format!("{}<{}>", "BpfHelper", &self.name()).as_str())
.field(&self.0)
.finish()
}
}
pub struct BpfHelperIter(u32);
impl BpfHelperIter {
pub fn new() -> Self {
Self(1)
}
}
impl Iterator for BpfHelperIter {
type Item = BpfHelper;
fn next(&mut self) -> Option<Self::Item> {
let next = self.0;
if next >= __BPF_FUNC_MAX_ID {
None
} else {
self.0 = self.0 + 1;
Some(BpfHelper(next))
}
}
}
pub mod bpf_asm {
use libbpf_sys::{
bpf_insn, BPF_JLT, BPF_JNE, BPF_REG_0, BPF_REG_1, BPF_SUB, _BPF_ALU64_IMM, _BPF_EXIT_INSN,
_BPF_JMP32_IMM, _BPF_JMP_IMM, _BPF_MOV64_IMM,
};
use num_enum::{IntoPrimitive, TryFromPrimitive};
#[repr(u8)]
#[derive(Debug, TryFromPrimitive, IntoPrimitive, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BpfRegister {
R0 = BPF_REG_0 as u8,
R1 = BPF_REG_1 as u8,
}
#[repr(u8)]
#[derive(Debug, TryFromPrimitive, IntoPrimitive, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BpfOp {
Sub = BPF_SUB as u8,
}
#[repr(u8)]
#[derive(Debug, TryFromPrimitive, IntoPrimitive, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BpfJmp {
JNE = BPF_JNE as u8,
JLT = BPF_JLT as u8,
}
pub fn mov64_imm(reg: BpfRegister, imm: i32) -> bpf_insn {
unsafe { _BPF_MOV64_IMM(reg.into(), imm) }
}
pub fn alu64_imm(op: BpfOp, reg: BpfRegister, imm: i32) -> bpf_insn {
unsafe { _BPF_ALU64_IMM(op.into(), reg.into(), imm) }
}
pub fn jmp_imm(jmp: BpfJmp, reg: BpfRegister, imm: i32, off: i16) -> bpf_insn {
unsafe { _BPF_JMP_IMM(jmp.into(), reg.into(), imm, off) }
}
pub fn jmp32_imm(jmp: BpfJmp, reg: BpfRegister, imm: i32, off: i16) -> bpf_insn {
unsafe { _BPF_JMP32_IMM(jmp.into(), reg.into(), imm, off) }
}
pub fn exit() -> bpf_insn {
unsafe { _BPF_EXIT_INSN() }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
}
#[test]
fn bpf_helper_iter() {
let count = BpfHelperIter::new()
.map(|helper| {
let name = helper.name();
assert_ne!(name, "<utf8err>");
assert_ne!(name, "<unknown>");
})
.count();
assert_eq!(count, usize::try_from(__BPF_FUNC_MAX_ID - 1).unwrap());
let invalid_helper = BpfHelper(__BPF_FUNC_MAX_ID + 1);
assert_eq!(invalid_helper.name(), "<unknown>");
}
#[test]
fn program_license_ptr() {
assert!(ProgramLicense::GPL.as_ptr().is_null() == false);
}
}