sys: refactor btf_obj_get_info_by_fd to share code

pull/747/head
Tamir Duberstein 1 year ago
parent c7a19bcefb
commit 5ac186299b
No known key found for this signature in database

@ -159,36 +159,23 @@ fn get_btf_info(prog_fd: i32, func_name: &str) -> Result<(RawFd, u32), ProgramEr
} }
// the bpf fd of the BTF object // the bpf fd of the BTF object
let btf_fd = sys::bpf_btf_get_fd_by_id(info.btf_id).map_err(|io_error| SyscallError { let btf_fd = sys::bpf_btf_get_fd_by_id(info.btf_id)?;
call: "bpf_btf_get_fd_by_id",
io_error,
})?;
// we need to read the btf bytes into a buffer but we don't know the size ahead of time. // we need to read the btf bytes into a buffer but we don't know the size ahead of time.
// assume 4kb. if this is too small we can resize based on the size obtained in the response. // assume 4kb. if this is too small we can resize based on the size obtained in the response.
let mut buf = vec![0u8; 4096]; let mut buf = vec![0u8; 4096];
let btf_info = match sys::btf_obj_get_info_by_fd(btf_fd, &buf) { loop {
Ok(info) => { let info = sys::btf_obj_get_info_by_fd(btf_fd, &mut buf)?;
if info.btf_size > buf.len() as u32 { let btf_size = info.btf_size as usize;
buf.resize(info.btf_size as usize, 0u8); if btf_size > buf.len() {
let btf_info = buf.resize(btf_size, 0u8);
sys::btf_obj_get_info_by_fd(btf_fd, &buf).map_err(|io_error| SyscallError { continue;
call: "bpf_prog_get_info_by_fd",
io_error,
})?;
Ok(btf_info)
} else {
Ok(info)
}
} }
Err(io_error) => Err(SyscallError { buf.truncate(btf_size);
call: "bpf_prog_get_info_by_fd", break;
io_error, }
}),
}?;
let btf = Btf::parse(&buf[0..btf_info.btf_size as usize], Endianness::default()) let btf = Btf::parse(&buf, Endianness::default()).map_err(ProgramError::Btf)?;
.map_err(ProgramError::Btf)?;
let btf_id = btf let btf_id = btf
.id_by_type_name_kind(func_name, BtfKind::Func) .id_by_type_name_kind(func_name, BtfKind::Func)

@ -539,22 +539,14 @@ pub(crate) fn bpf_link_get_info_by_fd(fd: BorrowedFd<'_>) -> Result<bpf_link_inf
} }
pub(crate) fn btf_obj_get_info_by_fd( pub(crate) fn btf_obj_get_info_by_fd(
prog_fd: RawFd, fd: RawFd,
buf: &[u8], buf: &mut [u8],
) -> Result<bpf_btf_info, io::Error> { ) -> Result<bpf_btf_info, SyscallError> {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() }; let fd = unsafe { BorrowedFd::borrow_raw(fd) };
let mut info = unsafe { mem::zeroed::<bpf_btf_info>() }; bpf_obj_get_info_by_fd(fd, |info: &mut bpf_btf_info| {
let buf_size = buf.len() as u32; info.btf = buf.as_mut_ptr() as _;
info.btf = buf.as_ptr() as u64; info.btf_size = buf.len() as _;
info.btf_size = buf_size; })
attr.info.bpf_fd = prog_fd as u32;
attr.info.info = &info as *const bpf_btf_info as u64;
attr.info.info_len = mem::size_of::<bpf_btf_info>() as u32;
match sys_bpf(bpf_cmd::BPF_OBJ_GET_INFO_BY_FD, &mut attr) {
Ok(_) => Ok(info),
Err((_, err)) => Err(err),
}
} }
pub(crate) fn bpf_raw_tracepoint_open(name: Option<&CStr>, prog_fd: RawFd) -> SysResult<OwnedFd> { pub(crate) fn bpf_raw_tracepoint_open(name: Option<&CStr>, prog_fd: RawFd) -> SysResult<OwnedFd> {

Loading…
Cancel
Save