Skip to content

Commit

Permalink
change map_ids type
Browse files Browse the repository at this point in the history
Make the optional field (map_ids) that's part of the
bpf_prog_get_info_by_fd syscall a raw slice reference rather than an
Option.  The user can pass a pre-initialized slice of length
info.nr_map_ids to ensure the map_ids buffer is populated, OR pass an
empty slice if they don't need the map_ids.

Signed-off-by: Andrew Stoycos <astoycos@redhat.com>
  • Loading branch information
astoycos committed Jul 25, 2023
1 parent a034e9a commit 3061cc4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 20 deletions.
2 changes: 1 addition & 1 deletion aya/src/programs/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +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, None).map_err(|io_error| {
let info = sys::bpf_prog_get_info_by_fd(prog_fd, &[]).map_err(|io_error| {
ProgramError::SyscallError {
call: "bpf_prog_get_info_by_fd",
io_error,
Expand Down
2 changes: 1 addition & 1 deletion aya/src/programs/lirc_mode2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl LircLink {

/// Get ProgramInfo from this link
pub fn info(&self) -> Result<ProgramInfo, ProgramError> {
match bpf_prog_get_info_by_fd(self.prog_fd, None) {
match bpf_prog_get_info_by_fd(self.prog_fd, &[]) {
Ok(info) => Ok(ProgramInfo(info)),
Err(io_error) => Err(ProgramError::SyscallError {
call: "bpf_prog_get_info_by_fd",
Expand Down
20 changes: 8 additions & 12 deletions aya/src/programs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ impl<T: Link> ProgramData<T> {
})? as RawFd;

let info =
bpf_prog_get_info_by_fd(fd, None).map_err(|io_error| ProgramError::SyscallError {
bpf_prog_get_info_by_fd(fd, &[]).map_err(|io_error| ProgramError::SyscallError {
call: "bpf_prog_get_info_by_fd",
io_error,
})?;
Expand Down Expand Up @@ -932,7 +932,7 @@ macro_rules! impl_program_info {
pub fn program_info(&self) -> Result<ProgramInfo, ProgramError> {
let fd = self.fd().ok_or(ProgramError::NotLoaded)?;

bpf_prog_get_info_by_fd(fd.as_raw_fd(), None)
bpf_prog_get_info_by_fd(fd.as_raw_fd(), &[])
.map_err(|io_error| ProgramError::SyscallError {
call: "bpf_prog_get_info_by_fd",
io_error,
Expand Down Expand Up @@ -1022,15 +1022,11 @@ impl ProgramInfo {
/// Returns the ids of the maps maps used by the program.
pub fn map_ids(&self) -> Result<Vec<u32>, ProgramError> {
let fd = self.fd()?;
let map_ids = vec![0u32; self.0.nr_map_ids as usize];

let len = self.0.nr_map_ids;
let mut map_ids = vec![0u32; len as usize];

bpf_prog_get_info_by_fd(fd, Some(&mut map_ids)).map_err(|io_error| {
ProgramError::SyscallError {
call: "bpf_prog_get_info_by_fd",
io_error,
}
bpf_prog_get_info_by_fd(fd, &map_ids).map_err(|io_error| ProgramError::SyscallError {
call: "bpf_prog_get_info_by_fd",
io_error,
})?;

unsafe { libc::close(fd) };
Expand Down Expand Up @@ -1099,7 +1095,7 @@ impl ProgramInfo {
})? as RawFd;

let info =
bpf_prog_get_info_by_fd(fd, None).map_err(|io_error| ProgramError::SyscallError {
bpf_prog_get_info_by_fd(fd, &[]).map_err(|io_error| ProgramError::SyscallError {
call: "bpf_prog_get_info_by_fd",
io_error,
})?;
Expand Down Expand Up @@ -1135,7 +1131,7 @@ impl Iterator for ProgramsIter {
io_error,
})
.and_then(|fd| {
let info = bpf_prog_get_info_by_fd(fd, None)
let info = bpf_prog_get_info_by_fd(fd, &[])
.map_err(|io_error| ProgramError::SyscallError {
call: "bpf_prog_get_info_by_fd",
io_error,
Expand Down
9 changes: 3 additions & 6 deletions aya/src/sys/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,15 +473,12 @@ pub(crate) fn bpf_prog_get_fd_by_id(prog_id: u32) -> Result<RawFd, io::Error> {

pub(crate) fn bpf_prog_get_info_by_fd(
prog_fd: RawFd,
map_ids: Option<&mut [u32]>,
map_ids: &[u32],
) -> Result<bpf_prog_info, io::Error> {
let mut attr = unsafe { mem::zeroed::<bpf_attr>() };
let mut info = unsafe { mem::zeroed::<bpf_prog_info>() };

if let Some(i) = map_ids {
info.map_ids = i.as_mut_ptr() as u64;
info.nr_map_ids = i.len() as u32;
};
info.map_ids = map_ids.as_ptr() as u64;
info.nr_map_ids = map_ids.len() as u32;

attr.info.bpf_fd = prog_fd as u32;
attr.info.info = &info as *const _ as u64;
Expand Down

0 comments on commit 3061cc4

Please sign in to comment.