sys: push error construction up

reviewable/pr712/r16
Tamir Duberstein 1 year ago
parent a0af7e0b2f
commit b1404e9a73
No known key found for this signature in database

@ -560,10 +560,7 @@ impl MapData {
io_error,
})? as RawFd;
let info = bpf_map_get_info_by_fd(fd).map_err(|io_error| SyscallError {
call: "BPF_MAP_GET_INFO_BY_FD",
io_error,
})?;
let info = bpf_map_get_info_by_fd(fd)?;
Ok(MapData {
obj: parse_map_info(info, PinningType::ByName),
@ -579,10 +576,7 @@ impl MapData {
/// This API is intended for cases where you have received a valid BPF FD from some other means.
/// For example, you received an FD over Unix Domain Socket.
pub fn from_fd(fd: RawFd) -> Result<MapData, MapError> {
let info = bpf_map_get_info_by_fd(fd).map_err(|io_error| SyscallError {
call: "BPF_OBJ_GET",
io_error,
})?;
let info = bpf_map_get_info_by_fd(fd)?;
Ok(MapData {
obj: parse_map_info(info, PinningType::None),

@ -151,10 +151,7 @@ impl Extension {
/// with the name `func_name` within that BTF object.
fn get_btf_info(prog_fd: i32, func_name: &str) -> Result<(RawFd, u32), ProgramError> {
// retrieve program information
let info = sys::bpf_prog_get_info_by_fd(prog_fd).map_err(|io_error| SyscallError {
call: "bpf_prog_get_info_by_fd",
io_error,
})?;
let info = sys::bpf_prog_get_info_by_fd(prog_fd)?;
// btf_id refers to the ID of the program btf that was loaded with bpf(BPF_BTF_LOAD)
if info.btf_id == 0 {

@ -10,7 +10,7 @@ use crate::{
probe::{attach, ProbeKind},
FdLink, LinkError, ProgramData, ProgramError,
},
sys::{bpf_link_get_info_by_fd, SyscallError},
sys::bpf_link_get_info_by_fd,
VerifierLogLevel,
};
@ -137,10 +137,7 @@ impl TryFrom<FdLink> for KProbeLink {
type Error = LinkError;
fn try_from(fd_link: FdLink) -> Result<Self, Self::Error> {
let info = bpf_link_get_info_by_fd(fd_link.fd).map_err(|io_error| SyscallError {
call: "BPF_OBJ_GET_INFO_BY_FD",
io_error,
})?;
let info = bpf_link_get_info_by_fd(fd_link.fd)?;
if info.type_ == (bpf_link_type::BPF_LINK_TYPE_KPROBE_MULTI as u32) {
return Ok(KProbeLink::new(PerfLinkInner::FdLink(fd_link)));
}

@ -99,11 +99,7 @@ impl LircMode2 {
let mut prog_fds = Vec::with_capacity(prog_ids.len());
for id in prog_ids {
let fd = bpf_prog_get_fd_by_id(id).map_err(|io_error| SyscallError {
call: "bpf_prog_get_fd_by_id",
io_error,
})?;
let fd = bpf_prog_get_fd_by_id(id)?;
prog_fds.push(fd);
}
@ -137,13 +133,7 @@ impl LircLink {
pub fn info(&self) -> Result<ProgramInfo, ProgramError> {
bpf_prog_get_info_by_fd(self.prog_fd)
.map(ProgramInfo)
.map_err(|io_error| {
SyscallError {
call: "bpf_prog_get_info_by_fd",
io_error,
}
.into()
})
.map_err(Into::into)
}
}

@ -487,11 +487,7 @@ impl<T: Link> ProgramData<T> {
io_error,
})? as RawFd;
let info = bpf_prog_get_info_by_fd(fd).map_err(|io_error| SyscallError {
call: "bpf_prog_get_info_by_fd",
io_error,
})?;
let info = bpf_prog_get_info_by_fd(fd)?;
let name = ProgramInfo(info).name_as_str().map(|s| s.to_string());
ProgramData::from_bpf_prog_info(name, fd, path.as_ref(), info, verifier_log_level)
}
@ -945,10 +941,8 @@ impl ProgramInfo {
///
/// The returned fd must be closed when no longer needed.
pub fn fd(&self) -> Result<RawFd, ProgramError> {
let fd = bpf_prog_get_fd_by_id(self.0.id).map_err(|io_error| SyscallError {
call: "bpf_prog_get_fd_by_id",
io_error,
})?;
let Self(info) = self;
let fd = bpf_prog_get_fd_by_id(info.id)?;
Ok(fd.into_raw_fd())
}
@ -960,10 +954,7 @@ impl ProgramInfo {
io_error,
})? as RawFd;
let info = bpf_prog_get_info_by_fd(fd).map_err(|io_error| SyscallError {
call: "bpf_prog_get_info_by_fd",
io_error,
})?;
let info = bpf_prog_get_info_by_fd(fd)?;
unsafe {
libc::close(fd);
}
@ -998,17 +989,11 @@ pub fn loaded_programs() -> impl Iterator<Item = Result<ProgramInfo, ProgramErro
iter_prog_ids()
.map(|id| {
let id = id?;
bpf_prog_get_fd_by_id(id).map_err(|io_error| SyscallError {
call: "bpf_prog_get_fd_by_id",
io_error,
})
bpf_prog_get_fd_by_id(id)
})
.map(|fd| {
let fd = fd?;
bpf_prog_get_info_by_fd(fd.as_raw_fd()).map_err(|io_error| SyscallError {
call: "bpf_prog_get_info_by_fd",
io_error,
})
bpf_prog_get_info_by_fd(fd.as_raw_fd())
})
.map(|result| result.map(ProgramInfo).map_err(Into::into))
}

@ -206,10 +206,7 @@ impl TryFrom<FdLink> for PerfEventLink {
type Error = LinkError;
fn try_from(fd_link: FdLink) -> Result<Self, Self::Error> {
let info = bpf_link_get_info_by_fd(fd_link.fd).map_err(|io_error| SyscallError {
call: "BPF_OBJ_GET_INFO_BY_FD",
io_error,
})?;
let info = bpf_link_get_info_by_fd(fd_link.fd)?;
if info.type_ == (bpf_link_type::BPF_LINK_TYPE_PERF_EVENT as u32) {
return Ok(PerfEventLink::new(PerfLinkInner::FdLink(fd_link)));
}

@ -131,10 +131,7 @@ impl TryFrom<FdLink> for TracePointLink {
type Error = LinkError;
fn try_from(fd_link: FdLink) -> Result<Self, Self::Error> {
let info = bpf_link_get_info_by_fd(fd_link.fd).map_err(|io_error| SyscallError {
call: "BPF_OBJ_GET_INFO_BY_FD",
io_error,
})?;
let info = bpf_link_get_info_by_fd(fd_link.fd)?;
if info.type_ == (bpf_link_type::BPF_LINK_TYPE_TRACING as u32) {
return Ok(TracePointLink::new(PerfLinkInner::FdLink(fd_link)));
}

@ -21,7 +21,7 @@ use crate::{
probe::{attach, ProbeKind},
FdLink, LinkError, ProgramData, ProgramError,
},
sys::{bpf_link_get_info_by_fd, SyscallError},
sys::bpf_link_get_info_by_fd,
VerifierLogLevel,
};
@ -177,10 +177,7 @@ impl TryFrom<FdLink> for UProbeLink {
type Error = LinkError;
fn try_from(fd_link: FdLink) -> Result<Self, Self::Error> {
let info = bpf_link_get_info_by_fd(fd_link.fd).map_err(|io_error| SyscallError {
call: "BPF_OBJ_GET_INFO_BY_FD",
io_error,
})?;
let info = bpf_link_get_info_by_fd(fd_link.fd)?;
if info.type_ == (bpf_link_type::BPF_LINK_TYPE_TRACING as u32) {
return Ok(UProbeLink::new(PerfLinkInner::FdLink(fd_link)));
}

@ -279,10 +279,7 @@ impl TryFrom<FdLink> for XdpLink {
fn try_from(fd_link: FdLink) -> Result<Self, Self::Error> {
// unwrap of fd_link.fd will not panic since it's only None when being dropped.
let info = bpf_link_get_info_by_fd(fd_link.fd).map_err(|io_error| SyscallError {
call: "BPF_OBJ_GET_INFO_BY_FD",
io_error,
})?;
let info = bpf_link_get_info_by_fd(fd_link.fd)?;
if info.type_ == (bpf_link_type::BPF_LINK_TYPE_XDP as u32) {
return Ok(XdpLink::new(XdpLinkInner::FdLink(fd_link)));
}

@ -460,57 +460,57 @@ pub(crate) fn bpf_prog_query(
ret
}
pub(crate) fn bpf_prog_get_fd_by_id(prog_id: u32) -> Result<OwnedFd, io::Error> {
pub(crate) fn bpf_prog_get_fd_by_id(prog_id: u32) -> Result<OwnedFd, SyscallError> {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
attr.__bindgen_anon_6.__bindgen_anon_1.prog_id = prog_id;
// SAFETY: BPF_PROG_GET_FD_BY_ID returns a new file descriptor.
unsafe { fd_sys_bpf(bpf_cmd::BPF_PROG_GET_FD_BY_ID, &mut attr).map_err(|(_, e)| e) }
unsafe { fd_sys_bpf(bpf_cmd::BPF_PROG_GET_FD_BY_ID, &mut attr) }.map_err(|(code, io_error)| {
assert_eq!(code, -1);
SyscallError {
call: "bpf_prog_get_fd_by_id",
io_error,
}
})
}
pub(crate) fn bpf_prog_get_info_by_fd(prog_fd: RawFd) -> Result<bpf_prog_info, io::Error> {
fn bpf_obj_get_info_by_fd<T>(fd: BorrowedFd<'_>) -> Result<T, SyscallError> {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
// info gets entirely populated by the kernel
let info = MaybeUninit::zeroed();
attr.info.bpf_fd = prog_fd as u32;
attr.info.bpf_fd = fd.as_raw_fd() as u32;
attr.info.info = &info as *const _ as u64;
attr.info.info_len = mem::size_of::<bpf_prog_info>() as u32;
attr.info.info_len = mem::size_of_val(&info) as u32;
match sys_bpf(bpf_cmd::BPF_OBJ_GET_INFO_BY_FD, &mut attr) {
Ok(_) => Ok(unsafe { info.assume_init() }),
Err((_, err)) => Err(err),
Ok(code) => {
assert_eq!(code, 0);
Ok(unsafe { info.assume_init() })
}
Err((code, io_error)) => {
assert_eq!(code, -1);
Err(SyscallError {
call: "bpf_obj_get_info_by_fd",
io_error,
})
}
}
}
pub(crate) fn bpf_map_get_info_by_fd(prog_fd: RawFd) -> Result<bpf_map_info, io::Error> {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
// info gets entirely populated by the kernel
let info = MaybeUninit::zeroed();
attr.info.bpf_fd = prog_fd as u32;
attr.info.info = info.as_ptr() as *const _ as u64;
attr.info.info_len = mem::size_of::<bpf_map_info>() as u32;
match sys_bpf(bpf_cmd::BPF_OBJ_GET_INFO_BY_FD, &mut attr) {
Ok(_) => Ok(unsafe { info.assume_init() }),
Err((_, err)) => Err(err),
}
pub(crate) fn bpf_prog_get_info_by_fd(fd: RawFd) -> Result<bpf_prog_info, SyscallError> {
let fd = unsafe { BorrowedFd::borrow_raw(fd) };
bpf_obj_get_info_by_fd::<bpf_prog_info>(fd)
}
pub(crate) fn bpf_link_get_info_by_fd(link_fd: RawFd) -> Result<bpf_link_info, io::Error> {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
// info gets entirely populated by the kernel
let info = unsafe { MaybeUninit::zeroed().assume_init() };
attr.info.bpf_fd = link_fd as u32;
attr.info.info = &info as *const _ as u64;
attr.info.info_len = mem::size_of::<bpf_link_info>() as u32;
pub(crate) fn bpf_map_get_info_by_fd(fd: RawFd) -> Result<bpf_map_info, SyscallError> {
let fd = unsafe { BorrowedFd::borrow_raw(fd) };
bpf_obj_get_info_by_fd::<bpf_map_info>(fd)
}
match sys_bpf(bpf_cmd::BPF_OBJ_GET_INFO_BY_FD, &mut attr) {
Ok(_) => Ok(info),
Err((_, err)) => Err(err),
}
pub(crate) fn bpf_link_get_info_by_fd(fd: RawFd) -> Result<bpf_link_info, SyscallError> {
let fd = unsafe { BorrowedFd::borrow_raw(fd) };
bpf_obj_get_info_by_fd::<bpf_link_info>(fd)
}
pub(crate) fn btf_obj_get_info_by_fd(

Loading…
Cancel
Save