Skip to content

Commit 87685e3

Browse files
rbartlenskydanielocfb
authored andcommitted
Implement From<u32> for ProgramType.
Tests ensure that we didn't mess anything up.
1 parent e46d89e commit 87685e3

File tree

1 file changed

+95
-5
lines changed

1 file changed

+95
-5
lines changed

libbpf-rs/src/program.rs

Lines changed: 95 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,7 @@ impl OpenProgram {
126126

127127
// The `ProgramType` of this `OpenProgram`.
128128
pub fn prog_type(&self) -> ProgramType {
129-
match ProgramType::try_from(unsafe { libbpf_sys::bpf_program__type(self.ptr.as_ptr()) }) {
130-
Ok(ty) => ty,
131-
Err(_) => ProgramType::Unknown,
132-
}
129+
ProgramType::from(unsafe { libbpf_sys::bpf_program__type(self.ptr.as_ptr()) })
133130
}
134131

135132
pub fn set_attach_type(&mut self, attach_type: ProgramAttachType) {
@@ -249,7 +246,7 @@ impl AsRawLibbpf for OpenProgram {
249246
/// Type of a [`Program`]. Maps to `enum bpf_prog_type` in kernel uapi.
250247
#[non_exhaustive]
251248
#[repr(u32)]
252-
#[derive(Copy, Clone, TryFromPrimitive, Display, Debug)]
249+
#[derive(Copy, Clone, Display, Debug)]
253250
// TODO: Document variants.
254251
#[allow(missing_docs)]
255252
pub enum ProgramType {
@@ -319,6 +316,48 @@ impl ProgramType {
319316
}
320317
}
321318

319+
impl From<u32> for ProgramType {
320+
fn from(value: u32) -> Self {
321+
use ProgramType::*;
322+
323+
match value {
324+
x if x == Unspec as u32 => Unspec,
325+
x if x == SocketFilter as u32 => SocketFilter,
326+
x if x == Kprobe as u32 => Kprobe,
327+
x if x == SchedCls as u32 => SchedCls,
328+
x if x == SchedAct as u32 => SchedAct,
329+
x if x == Tracepoint as u32 => Tracepoint,
330+
x if x == Xdp as u32 => Xdp,
331+
x if x == PerfEvent as u32 => PerfEvent,
332+
x if x == CgroupSkb as u32 => CgroupSkb,
333+
x if x == CgroupSock as u32 => CgroupSock,
334+
x if x == LwtIn as u32 => LwtIn,
335+
x if x == LwtOut as u32 => LwtOut,
336+
x if x == LwtXmit as u32 => LwtXmit,
337+
x if x == SockOps as u32 => SockOps,
338+
x if x == SkSkb as u32 => SkSkb,
339+
x if x == CgroupDevice as u32 => CgroupDevice,
340+
x if x == SkMsg as u32 => SkMsg,
341+
x if x == RawTracepoint as u32 => RawTracepoint,
342+
x if x == CgroupSockAddr as u32 => CgroupSockAddr,
343+
x if x == LwtSeg6local as u32 => LwtSeg6local,
344+
x if x == LircMode2 as u32 => LircMode2,
345+
x if x == SkReuseport as u32 => SkReuseport,
346+
x if x == FlowDissector as u32 => FlowDissector,
347+
x if x == CgroupSysctl as u32 => CgroupSysctl,
348+
x if x == RawTracepointWritable as u32 => RawTracepointWritable,
349+
x if x == CgroupSockopt as u32 => CgroupSockopt,
350+
x if x == Tracing as u32 => Tracing,
351+
x if x == StructOps as u32 => StructOps,
352+
x if x == Ext as u32 => Ext,
353+
x if x == Lsm as u32 => Lsm,
354+
x if x == SkLookup as u32 => SkLookup,
355+
x if x == Syscall as u32 => Syscall,
356+
_ => Unknown,
357+
}
358+
}
359+
}
360+
322361
/// Attach type of a [`Program`]. Maps to `enum bpf_attach_type` in kernel uapi.
323362
#[non_exhaustive]
324363
#[repr(u32)]
@@ -1001,3 +1040,54 @@ impl AsRawLibbpf for Program {
10011040
self.ptr
10021041
}
10031042
}
1043+
1044+
#[cfg(test)]
1045+
mod tests {
1046+
use super::*;
1047+
1048+
use std::mem::discriminant;
1049+
1050+
#[test]
1051+
fn program_type() {
1052+
use ProgramType::*;
1053+
1054+
for t in [
1055+
Unspec,
1056+
SocketFilter,
1057+
Kprobe,
1058+
SchedCls,
1059+
SchedAct,
1060+
Tracepoint,
1061+
Xdp,
1062+
PerfEvent,
1063+
CgroupSkb,
1064+
CgroupSock,
1065+
LwtIn,
1066+
LwtOut,
1067+
LwtXmit,
1068+
SockOps,
1069+
SkSkb,
1070+
CgroupDevice,
1071+
SkMsg,
1072+
RawTracepoint,
1073+
CgroupSockAddr,
1074+
LwtSeg6local,
1075+
LircMode2,
1076+
SkReuseport,
1077+
FlowDissector,
1078+
CgroupSysctl,
1079+
RawTracepointWritable,
1080+
CgroupSockopt,
1081+
Tracing,
1082+
StructOps,
1083+
Ext,
1084+
Lsm,
1085+
SkLookup,
1086+
Syscall,
1087+
Unknown,
1088+
] {
1089+
// check if discriminants match after a roundtrip conversion
1090+
assert_eq!(discriminant(&t), discriminant(&ProgramType::from(t as u32)));
1091+
}
1092+
}
1093+
}

0 commit comments

Comments
 (0)