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, io_error,
})? as RawFd; })? as RawFd;
let info = bpf_map_get_info_by_fd(fd).map_err(|io_error| SyscallError { let info = bpf_map_get_info_by_fd(fd)?;
call: "BPF_MAP_GET_INFO_BY_FD",
io_error,
})?;
Ok(MapData { Ok(MapData {
obj: parse_map_info(info, PinningType::ByName), 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. /// 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. /// For example, you received an FD over Unix Domain Socket.
pub fn from_fd(fd: RawFd) -> Result<MapData, MapError> { pub fn from_fd(fd: RawFd) -> Result<MapData, MapError> {
let info = bpf_map_get_info_by_fd(fd).map_err(|io_error| SyscallError { let info = bpf_map_get_info_by_fd(fd)?;
call: "BPF_OBJ_GET",
io_error,
})?;
Ok(MapData { Ok(MapData {
obj: parse_map_info(info, PinningType::None), obj: parse_map_info(info, PinningType::None),

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

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

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

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

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

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

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

@ -460,57 +460,57 @@ pub(crate) fn bpf_prog_query(
ret 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>() }; let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
attr.__bindgen_anon_6.__bindgen_anon_1.prog_id = prog_id; attr.__bindgen_anon_6.__bindgen_anon_1.prog_id = prog_id;
// SAFETY: BPF_PROG_GET_FD_BY_ID returns a new file descriptor. // 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>() }; let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
// info gets entirely populated by the kernel // info gets entirely populated by the kernel
let info = MaybeUninit::zeroed(); 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 = &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) { match sys_bpf(bpf_cmd::BPF_OBJ_GET_INFO_BY_FD, &mut attr) {
Ok(_) => Ok(unsafe { info.assume_init() }), Ok(code) => {
Err((_, err)) => Err(err), 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_link_get_info_by_fd(link_fd: RawFd) -> Result<bpf_link_info, io::Error> { pub(crate) fn bpf_prog_get_info_by_fd(fd: RawFd) -> Result<bpf_prog_info, SyscallError> {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() }; let fd = unsafe { BorrowedFd::borrow_raw(fd) };
// info gets entirely populated by the kernel bpf_obj_get_info_by_fd::<bpf_prog_info>(fd)
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;
match sys_bpf(bpf_cmd::BPF_OBJ_GET_INFO_BY_FD, &mut attr) { pub(crate) fn bpf_map_get_info_by_fd(fd: RawFd) -> Result<bpf_map_info, SyscallError> {
Ok(_) => Ok(info), let fd = unsafe { BorrowedFd::borrow_raw(fd) };
Err((_, err)) => Err(err), bpf_obj_get_info_by_fd::<bpf_map_info>(fd)
} }
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( pub(crate) fn btf_obj_get_info_by_fd(

Loading…
Cancel
Save