Skip to content

Commit 54a2eda

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

File tree

1 file changed

+112
-7
lines changed

1 file changed

+112
-7
lines changed

libbpf-rs/src/program.rs

Lines changed: 112 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use std::ptr::NonNull;
1414
use std::slice;
1515

1616
use libbpf_sys::bpf_func_id;
17-
use num_enum::TryFromPrimitive;
1817
use strum_macros::Display;
1918

2019
use crate::util;
@@ -361,7 +360,7 @@ impl From<u32> for ProgramType {
361360
/// Attach type of a [`Program`]. Maps to `enum bpf_attach_type` in kernel uapi.
362361
#[non_exhaustive]
363362
#[repr(u32)]
364-
#[derive(Clone, TryFromPrimitive, Display, Debug)]
363+
#[derive(Clone, Display, Debug)]
365364
// TODO: Document variants.
366365
#[allow(missing_docs)]
367366
pub enum ProgramAttachType {
@@ -411,6 +410,58 @@ pub enum ProgramAttachType {
411410
Unknown = u32::MAX,
412411
}
413412

413+
impl From<u32> for ProgramAttachType {
414+
fn from(value: u32) -> Self {
415+
use ProgramAttachType::*;
416+
417+
match value {
418+
x if x == CgroupInetIngress as u32 => CgroupInetIngress,
419+
x if x == CgroupInetEgress as u32 => CgroupInetEgress,
420+
x if x == CgroupInetSockCreate as u32 => CgroupInetSockCreate,
421+
x if x == CgroupSockOps as u32 => CgroupSockOps,
422+
x if x == SkSkbStreamParser as u32 => SkSkbStreamParser,
423+
x if x == SkSkbStreamVerdict as u32 => SkSkbStreamVerdict,
424+
x if x == CgroupDevice as u32 => CgroupDevice,
425+
x if x == SkMsgVerdict as u32 => SkMsgVerdict,
426+
x if x == CgroupInet4Bind as u32 => CgroupInet4Bind,
427+
x if x == CgroupInet6Bind as u32 => CgroupInet6Bind,
428+
x if x == CgroupInet4Connect as u32 => CgroupInet4Connect,
429+
x if x == CgroupInet6Connect as u32 => CgroupInet6Connect,
430+
x if x == CgroupInet4PostBind as u32 => CgroupInet4PostBind,
431+
x if x == CgroupInet6PostBind as u32 => CgroupInet6PostBind,
432+
x if x == CgroupUdp4Sendmsg as u32 => CgroupUdp4Sendmsg,
433+
x if x == CgroupUdp6Sendmsg as u32 => CgroupUdp6Sendmsg,
434+
x if x == LircMode2 as u32 => LircMode2,
435+
x if x == FlowDissector as u32 => FlowDissector,
436+
x if x == CgroupSysctl as u32 => CgroupSysctl,
437+
x if x == CgroupUdp4Recvmsg as u32 => CgroupUdp4Recvmsg,
438+
x if x == CgroupUdp6Recvmsg as u32 => CgroupUdp6Recvmsg,
439+
x if x == CgroupGetsockopt as u32 => CgroupGetsockopt,
440+
x if x == CgroupSetsockopt as u32 => CgroupSetsockopt,
441+
x if x == TraceRawTp as u32 => TraceRawTp,
442+
x if x == TraceFentry as u32 => TraceFentry,
443+
x if x == TraceFexit as u32 => TraceFexit,
444+
x if x == ModifyReturn as u32 => ModifyReturn,
445+
x if x == LsmMac as u32 => LsmMac,
446+
x if x == TraceIter as u32 => TraceIter,
447+
x if x == CgroupInet4Getpeername as u32 => CgroupInet4Getpeername,
448+
x if x == CgroupInet6Getpeername as u32 => CgroupInet6Getpeername,
449+
x if x == CgroupInet4Getsockname as u32 => CgroupInet4Getsockname,
450+
x if x == CgroupInet6Getsockname as u32 => CgroupInet6Getsockname,
451+
x if x == XdpDevmap as u32 => XdpDevmap,
452+
x if x == CgroupInetSockRelease as u32 => CgroupInetSockRelease,
453+
x if x == XdpCpumap as u32 => XdpCpumap,
454+
x if x == SkLookup as u32 => SkLookup,
455+
x if x == Xdp as u32 => Xdp,
456+
x if x == SkSkbVerdict as u32 => SkSkbVerdict,
457+
x if x == SkReuseportSelect as u32 => SkReuseportSelect,
458+
x if x == SkReuseportSelectOrMigrate as u32 => SkReuseportSelectOrMigrate,
459+
x if x == PerfEvent as u32 => PerfEvent,
460+
_ => Unknown,
461+
}
462+
}
463+
}
464+
414465
/// The input a program accepts.
415466
///
416467
/// This type is mostly used in conjunction with the [`Program::test_run`]
@@ -542,12 +593,9 @@ impl Program {
542593

543594
/// Retrieve the attach type of the program.
544595
pub fn attach_type(&self) -> ProgramAttachType {
545-
match ProgramAttachType::try_from(unsafe {
596+
ProgramAttachType::from(unsafe {
546597
libbpf_sys::bpf_program__expected_attach_type(self.ptr.as_ptr())
547-
}) {
548-
Ok(ty) => ty,
549-
Err(_) => ProgramAttachType::Unknown,
550-
}
598+
})
551599
}
552600

553601
/// Return `true` if the bpf program is set to autoload, `false` otherwise.
@@ -1090,4 +1138,61 @@ mod tests {
10901138
assert_eq!(discriminant(&t), discriminant(&ProgramType::from(t as u32)));
10911139
}
10921140
}
1141+
1142+
#[test]
1143+
fn program_attach_type() {
1144+
use ProgramAttachType::*;
1145+
1146+
for t in [
1147+
CgroupInetIngress,
1148+
CgroupInetEgress,
1149+
CgroupInetSockCreate,
1150+
CgroupSockOps,
1151+
SkSkbStreamParser,
1152+
SkSkbStreamVerdict,
1153+
CgroupDevice,
1154+
SkMsgVerdict,
1155+
CgroupInet4Bind,
1156+
CgroupInet6Bind,
1157+
CgroupInet4Connect,
1158+
CgroupInet6Connect,
1159+
CgroupInet4PostBind,
1160+
CgroupInet6PostBind,
1161+
CgroupUdp4Sendmsg,
1162+
CgroupUdp6Sendmsg,
1163+
LircMode2,
1164+
FlowDissector,
1165+
CgroupSysctl,
1166+
CgroupUdp4Recvmsg,
1167+
CgroupUdp6Recvmsg,
1168+
CgroupGetsockopt,
1169+
CgroupSetsockopt,
1170+
TraceRawTp,
1171+
TraceFentry,
1172+
TraceFexit,
1173+
ModifyReturn,
1174+
LsmMac,
1175+
TraceIter,
1176+
CgroupInet4Getpeername,
1177+
CgroupInet6Getpeername,
1178+
CgroupInet4Getsockname,
1179+
CgroupInet6Getsockname,
1180+
XdpDevmap,
1181+
CgroupInetSockRelease,
1182+
XdpCpumap,
1183+
SkLookup,
1184+
Xdp,
1185+
SkSkbVerdict,
1186+
SkReuseportSelect,
1187+
SkReuseportSelectOrMigrate,
1188+
PerfEvent,
1189+
Unknown,
1190+
] {
1191+
// check if discriminants match after a roundtrip conversion
1192+
assert_eq!(
1193+
discriminant(&t),
1194+
discriminant(&ProgramAttachType::from(t as u32))
1195+
);
1196+
}
1197+
}
10931198
}

0 commit comments

Comments
 (0)