Merge pull request #766 from aya-rs/obj-better-sense

aya-obj: reduce indirection in section parsing
pull/768/head
Tamir Duberstein 1 year ago committed by GitHub
commit e9690df834
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -266,10 +266,15 @@ impl FromStr for ProgramSection {
// parse the common case, eg "xdp/program_name" or // parse the common case, eg "xdp/program_name" or
// "sk_skb/stream_verdict/program_name" // "sk_skb/stream_verdict/program_name"
let (kind, name) = match section.rsplit_once('/') { let mut pieces = section.split('/');
None => (section, section), let mut next = || {
Some((kind, name)) => (kind, name), pieces
.next()
.ok_or_else(|| ParseError::InvalidProgramSection {
section: section.to_owned(),
})
}; };
let kind = next()?;
Ok(match kind { Ok(match kind {
"kprobe" => KProbe, "kprobe" => KProbe,
@ -281,128 +286,105 @@ impl FromStr for ProgramSection {
"xdp" => Xdp { frags: false }, "xdp" => Xdp { frags: false },
"xdp.frags" => Xdp { frags: true }, "xdp.frags" => Xdp { frags: true },
"tp_btf" => BtfTracePoint, "tp_btf" => BtfTracePoint,
kind if kind.starts_with("tracepoint") || kind.starts_with("tp") => TracePoint, "tracepoint" | "tp" => TracePoint,
"socket" => SocketFilter, "socket" => SocketFilter,
"sk_msg" => SkMsg, "sk_msg" => SkMsg,
"sk_skb" => match name { "sk_skb" => {
"stream_parser" => SkSkbStreamParser, let name = next()?;
"stream_verdict" => SkSkbStreamVerdict, match name {
_ => { "stream_parser" => SkSkbStreamParser,
return Err(ParseError::InvalidProgramSection { "stream_verdict" => SkSkbStreamVerdict,
section: section.to_owned(), _ => {
})
}
},
"sk_skb/stream_parser" => SkSkbStreamParser,
"sk_skb/stream_verdict" => SkSkbStreamVerdict,
"sockops" => SockOps,
"classifier" => SchedClassifier,
"cgroup_skb" => match name {
"ingress" => CgroupSkbIngress,
"egress" => CgroupSkbEgress,
_ => {
return Err(ParseError::InvalidProgramSection {
section: section.to_owned(),
})
}
},
"cgroup_skb/ingress" => CgroupSkbIngress,
"cgroup_skb/egress" => CgroupSkbEgress,
"cgroup/skb" => CgroupSkb,
"cgroup/sock" => CgroupSock {
attach_type: CgroupSockAttachType::default(),
},
"cgroup/sysctl" => CgroupSysctl,
"cgroup/dev" => CgroupDevice,
"cgroup/getsockopt" => CgroupSockopt {
attach_type: CgroupSockoptAttachType::Get,
},
"cgroup/setsockopt" => CgroupSockopt {
attach_type: CgroupSockoptAttachType::Set,
},
"cgroup" => match name {
"skb" => CgroupSkb,
"sysctl" => CgroupSysctl,
"dev" => CgroupDevice,
"getsockopt" | "setsockopt" => {
if let Ok(attach_type) = CgroupSockoptAttachType::try_from(name) {
CgroupSockopt { attach_type }
} else {
return Err(ParseError::InvalidProgramSection { return Err(ParseError::InvalidProgramSection {
section: section.to_owned(), section: section.to_owned(),
}); })
} }
} }
"sock" => CgroupSock { }
attach_type: CgroupSockAttachType::default(), "sockops" => SockOps,
}, "classifier" => SchedClassifier,
"post_bind4" | "post_bind6" | "sock_create" | "sock_release" => { "cgroup_skb" => {
if let Ok(attach_type) = CgroupSockAttachType::try_from(name) { let name = next()?;
CgroupSock { attach_type } match name {
} else { "ingress" => CgroupSkbIngress,
"egress" => CgroupSkbEgress,
_ => {
return Err(ParseError::InvalidProgramSection { return Err(ParseError::InvalidProgramSection {
section: section.to_owned(), section: section.to_owned(),
}); })
} }
} }
name => { }
if let Ok(attach_type) = CgroupSockAddrAttachType::try_from(name) { "cgroup" => {
CgroupSockAddr { attach_type } let name = next()?;
} else { match name {
"skb" => CgroupSkb,
"sysctl" => CgroupSysctl,
"dev" => CgroupDevice,
"getsockopt" => CgroupSockopt {
attach_type: CgroupSockoptAttachType::Get,
},
"setsockopt" => CgroupSockopt {
attach_type: CgroupSockoptAttachType::Set,
},
"sock" => CgroupSock {
attach_type: CgroupSockAttachType::default(),
},
"post_bind4" => CgroupSock {
attach_type: CgroupSockAttachType::PostBind4,
},
"post_bind6" => CgroupSock {
attach_type: CgroupSockAttachType::PostBind6,
},
"sock_create" => CgroupSock {
attach_type: CgroupSockAttachType::SockCreate,
},
"sock_release" => CgroupSock {
attach_type: CgroupSockAttachType::SockRelease,
},
"bind4" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::Bind4,
},
"bind6" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::Bind6,
},
"connect4" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::Connect4,
},
"connect6" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::Connect6,
},
"getpeername4" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::GetPeerName4,
},
"getpeername6" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::GetPeerName6,
},
"getsockname4" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::GetSockName4,
},
"getsockname6" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::GetSockName6,
},
"sendmsg4" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::UDPSendMsg4,
},
"sendmsg6" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::UDPSendMsg6,
},
"recvmsg4" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::UDPRecvMsg4,
},
"recvmsg6" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::UDPRecvMsg6,
},
_ => {
return Err(ParseError::InvalidProgramSection { return Err(ParseError::InvalidProgramSection {
section: section.to_owned(), section: section.to_owned(),
}); });
} }
} }
}, }
"cgroup/post_bind4" => CgroupSock {
attach_type: CgroupSockAttachType::PostBind4,
},
"cgroup/post_bind6" => CgroupSock {
attach_type: CgroupSockAttachType::PostBind6,
},
"cgroup/sock_create" => CgroupSock {
attach_type: CgroupSockAttachType::SockCreate,
},
"cgroup/sock_release" => CgroupSock {
attach_type: CgroupSockAttachType::SockRelease,
},
"cgroup/bind4" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::Bind4,
},
"cgroup/bind6" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::Bind6,
},
"cgroup/connect4" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::Connect4,
},
"cgroup/connect6" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::Connect6,
},
"cgroup/getpeername4" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::GetPeerName4,
},
"cgroup/getpeername6" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::GetPeerName6,
},
"cgroup/getsockname4" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::GetSockName4,
},
"cgroup/getsockname6" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::GetSockName6,
},
"cgroup/sendmsg4" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::UDPSendMsg4,
},
"cgroup/sendmsg6" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::UDPSendMsg6,
},
"cgroup/recvmsg4" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::UDPRecvMsg4,
},
"cgroup/recvmsg6" => CgroupSockAddr {
attach_type: CgroupSockAddrAttachType::UDPRecvMsg6,
},
"lirc_mode2" => LircMode2, "lirc_mode2" => LircMode2,
"perf_event" => PerfEvent, "perf_event" => PerfEvent,
"raw_tp" | "raw_tracepoint" => RawTracePoint, "raw_tp" | "raw_tracepoint" => RawTracePoint,

@ -1,11 +1,6 @@
//! Cgroup socket programs. //! Cgroup socket programs.
use alloc::{borrow::ToOwned, string::String};
use crate::generated::bpf_attach_type; use crate::generated::bpf_attach_type;
#[cfg(not(feature = "std"))]
use crate::std;
/// Defines where to attach a `CgroupSock` program. /// Defines where to attach a `CgroupSock` program.
#[derive(Copy, Clone, Debug, Default)] #[derive(Copy, Clone, Debug, Default)]
pub enum CgroupSockAttachType { pub enum CgroupSockAttachType {
@ -30,19 +25,3 @@ impl From<CgroupSockAttachType> for bpf_attach_type {
} }
} }
} }
#[derive(Debug, thiserror::Error)]
#[error("{0} is not a valid attach type for a CGROUP_SOCK program")]
pub(crate) struct InvalidAttachType(String);
impl CgroupSockAttachType {
pub(crate) fn try_from(value: &str) -> Result<CgroupSockAttachType, InvalidAttachType> {
match value {
"post_bind4" => Ok(CgroupSockAttachType::PostBind4),
"post_bind6" => Ok(CgroupSockAttachType::PostBind6),
"sock_create" => Ok(CgroupSockAttachType::SockCreate),
"sock_release" => Ok(CgroupSockAttachType::SockRelease),
_ => Err(InvalidAttachType(value.to_owned())),
}
}
}

@ -1,11 +1,6 @@
//! Cgroup socket address programs. //! Cgroup socket address programs.
use alloc::{borrow::ToOwned, string::String};
use crate::generated::bpf_attach_type; use crate::generated::bpf_attach_type;
#[cfg(not(feature = "std"))]
use crate::std;
/// Defines where to attach a `CgroupSockAddr` program. /// Defines where to attach a `CgroupSockAddr` program.
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub enum CgroupSockAddrAttachType { pub enum CgroupSockAddrAttachType {
@ -53,27 +48,3 @@ impl From<CgroupSockAddrAttachType> for bpf_attach_type {
} }
} }
} }
#[derive(Debug, thiserror::Error)]
#[error("{0} is not a valid attach type for a CGROUP_SOCK_ADDR program")]
pub(crate) struct InvalidAttachType(String);
impl CgroupSockAddrAttachType {
pub(crate) fn try_from(value: &str) -> Result<CgroupSockAddrAttachType, InvalidAttachType> {
match value {
"bind4" => Ok(CgroupSockAddrAttachType::Bind4),
"bind6" => Ok(CgroupSockAddrAttachType::Bind6),
"connect4" => Ok(CgroupSockAddrAttachType::Connect4),
"connect6" => Ok(CgroupSockAddrAttachType::Connect6),
"getpeername4" => Ok(CgroupSockAddrAttachType::GetPeerName4),
"getpeername6" => Ok(CgroupSockAddrAttachType::GetPeerName6),
"getsockname4" => Ok(CgroupSockAddrAttachType::GetSockName4),
"getsockname6" => Ok(CgroupSockAddrAttachType::GetSockName6),
"sendmsg4" => Ok(CgroupSockAddrAttachType::UDPSendMsg4),
"sendmsg6" => Ok(CgroupSockAddrAttachType::UDPSendMsg6),
"recvmsg4" => Ok(CgroupSockAddrAttachType::UDPRecvMsg4),
"recvmsg6" => Ok(CgroupSockAddrAttachType::UDPRecvMsg6),
_ => Err(InvalidAttachType(value.to_owned())),
}
}
}

@ -1,11 +1,6 @@
//! Cgroup socket option programs. //! Cgroup socket option programs.
use alloc::{borrow::ToOwned, string::String};
use crate::generated::bpf_attach_type; use crate::generated::bpf_attach_type;
#[cfg(not(feature = "std"))]
use crate::std;
/// Defines where to attach a `CgroupSockopt` program. /// Defines where to attach a `CgroupSockopt` program.
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub enum CgroupSockoptAttachType { pub enum CgroupSockoptAttachType {
@ -23,17 +18,3 @@ impl From<CgroupSockoptAttachType> for bpf_attach_type {
} }
} }
} }
#[derive(Debug, thiserror::Error)]
#[error("{0} is not a valid attach type for a CGROUP_SOCKOPT program")]
pub(crate) struct InvalidAttachType(String);
impl CgroupSockoptAttachType {
pub(crate) fn try_from(value: &str) -> Result<CgroupSockoptAttachType, InvalidAttachType> {
match value {
"getsockopt" => Ok(CgroupSockoptAttachType::Get),
"setsockopt" => Ok(CgroupSockoptAttachType::Set),
_ => Err(InvalidAttachType(value.to_owned())),
}
}
}

Loading…
Cancel
Save