Skip to content

Commit

Permalink
Merge pull request #1007 from tyrone-wu/aya/info-api
Browse files Browse the repository at this point in the history
aya,aya-obj,integration-test: add better support in `ProgramInfo` & `MapInfo` for old kernels
  • Loading branch information
alessandrod committed Sep 3, 2024
2 parents ab000ad + fbb0930 commit 15eb935
Show file tree
Hide file tree
Showing 26 changed files with 1,979 additions and 549 deletions.
11 changes: 6 additions & 5 deletions aya-log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,11 @@ use std::{
const MAP_NAME: &str = "AYA_LOGS";

use aya::{
loaded_programs,
maps::{
perf::{AsyncPerfEventArray, Events, PerfBufferError},
Map, MapData, MapError, MapInfo,
},
programs::ProgramError,
programs::{loaded_programs, ProgramError},
util::online_cpus,
Ebpf, Pod,
};
Expand Down Expand Up @@ -137,19 +136,21 @@ impl EbpfLogger {
) -> Result<EbpfLogger, Error> {
let program_info = loaded_programs()
.filter_map(|info| info.ok())
.find(|info| info.id() == program_id)
.find(|info| info.id().is_some_and(|id| id.get() == program_id))
.ok_or(Error::ProgramNotFound)?;

let map = program_info
.map_ids()
.map_err(Error::ProgramError)?
.expect("`map_ids` field in `bpf_prog_info` not available")
.iter()
.filter_map(|id| MapInfo::from_id(*id).ok())
.filter_map(|id| MapInfo::from_id(id.get()).ok())
.find(|map_info| match map_info.name_as_str() {
Some(name) => name == MAP_NAME,
None => false,
})
.ok_or(Error::MapNotFound)?;
let map = MapData::from_id(map.id()).map_err(Error::MapError)?;
let map = MapData::from_id(map.id().unwrap().get()).map_err(Error::MapError)?;

Self::read_logs_async(Map::PerfEventArray(map), logger)?;

Expand Down
1 change: 1 addition & 0 deletions aya-obj/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ mod std {

pub mod btf;
pub mod generated;
pub mod links;
pub mod maps;
pub mod obj;
pub mod programs;
Expand Down
100 changes: 100 additions & 0 deletions aya-obj/src/links.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//! Link type bindings.

use crate::{
generated::{bpf_attach_type, bpf_link_type},
InvalidTypeBinding,
};

impl TryFrom<u32> for bpf_link_type {
type Error = InvalidTypeBinding<u32>;

fn try_from(link_type: u32) -> Result<Self, Self::Error> {
use bpf_link_type::*;
Ok(match link_type {
x if x == BPF_LINK_TYPE_UNSPEC as u32 => BPF_LINK_TYPE_UNSPEC,
x if x == BPF_LINK_TYPE_RAW_TRACEPOINT as u32 => BPF_LINK_TYPE_RAW_TRACEPOINT,
x if x == BPF_LINK_TYPE_TRACING as u32 => BPF_LINK_TYPE_TRACING,
x if x == BPF_LINK_TYPE_CGROUP as u32 => BPF_LINK_TYPE_CGROUP,
x if x == BPF_LINK_TYPE_ITER as u32 => BPF_LINK_TYPE_ITER,
x if x == BPF_LINK_TYPE_NETNS as u32 => BPF_LINK_TYPE_NETNS,
x if x == BPF_LINK_TYPE_XDP as u32 => BPF_LINK_TYPE_XDP,
x if x == BPF_LINK_TYPE_PERF_EVENT as u32 => BPF_LINK_TYPE_PERF_EVENT,
x if x == BPF_LINK_TYPE_KPROBE_MULTI as u32 => BPF_LINK_TYPE_KPROBE_MULTI,
x if x == BPF_LINK_TYPE_STRUCT_OPS as u32 => BPF_LINK_TYPE_STRUCT_OPS,
x if x == BPF_LINK_TYPE_NETFILTER as u32 => BPF_LINK_TYPE_NETFILTER,
x if x == BPF_LINK_TYPE_TCX as u32 => BPF_LINK_TYPE_TCX,
x if x == BPF_LINK_TYPE_UPROBE_MULTI as u32 => BPF_LINK_TYPE_UPROBE_MULTI,
x if x == BPF_LINK_TYPE_NETKIT as u32 => BPF_LINK_TYPE_NETKIT,
_ => return Err(InvalidTypeBinding { value: link_type }),
})
}
}

impl TryFrom<u32> for bpf_attach_type {
type Error = InvalidTypeBinding<u32>;

fn try_from(attach_type: u32) -> Result<Self, Self::Error> {
use bpf_attach_type::*;
Ok(match attach_type {
x if x == BPF_CGROUP_INET_INGRESS as u32 => BPF_CGROUP_INET_INGRESS,
x if x == BPF_CGROUP_INET_EGRESS as u32 => BPF_CGROUP_INET_EGRESS,
x if x == BPF_CGROUP_INET_SOCK_CREATE as u32 => BPF_CGROUP_INET_SOCK_CREATE,
x if x == BPF_CGROUP_SOCK_OPS as u32 => BPF_CGROUP_SOCK_OPS,
x if x == BPF_SK_SKB_STREAM_PARSER as u32 => BPF_SK_SKB_STREAM_PARSER,
x if x == BPF_SK_SKB_STREAM_VERDICT as u32 => BPF_SK_SKB_STREAM_VERDICT,
x if x == BPF_CGROUP_DEVICE as u32 => BPF_CGROUP_DEVICE,
x if x == BPF_SK_MSG_VERDICT as u32 => BPF_SK_MSG_VERDICT,
x if x == BPF_CGROUP_INET4_BIND as u32 => BPF_CGROUP_INET4_BIND,
x if x == BPF_CGROUP_INET6_BIND as u32 => BPF_CGROUP_INET6_BIND,
x if x == BPF_CGROUP_INET4_CONNECT as u32 => BPF_CGROUP_INET4_CONNECT,
x if x == BPF_CGROUP_INET6_CONNECT as u32 => BPF_CGROUP_INET6_CONNECT,
x if x == BPF_CGROUP_INET4_POST_BIND as u32 => BPF_CGROUP_INET4_POST_BIND,
x if x == BPF_CGROUP_INET6_POST_BIND as u32 => BPF_CGROUP_INET6_POST_BIND,
x if x == BPF_CGROUP_UDP4_SENDMSG as u32 => BPF_CGROUP_UDP4_SENDMSG,
x if x == BPF_CGROUP_UDP6_SENDMSG as u32 => BPF_CGROUP_UDP6_SENDMSG,
x if x == BPF_LIRC_MODE2 as u32 => BPF_LIRC_MODE2,
x if x == BPF_FLOW_DISSECTOR as u32 => BPF_FLOW_DISSECTOR,
x if x == BPF_CGROUP_SYSCTL as u32 => BPF_CGROUP_SYSCTL,
x if x == BPF_CGROUP_UDP4_RECVMSG as u32 => BPF_CGROUP_UDP4_RECVMSG,
x if x == BPF_CGROUP_UDP6_RECVMSG as u32 => BPF_CGROUP_UDP6_RECVMSG,
x if x == BPF_CGROUP_GETSOCKOPT as u32 => BPF_CGROUP_GETSOCKOPT,
x if x == BPF_CGROUP_SETSOCKOPT as u32 => BPF_CGROUP_SETSOCKOPT,
x if x == BPF_TRACE_RAW_TP as u32 => BPF_TRACE_RAW_TP,
x if x == BPF_TRACE_FENTRY as u32 => BPF_TRACE_FENTRY,
x if x == BPF_TRACE_FEXIT as u32 => BPF_TRACE_FEXIT,
x if x == BPF_MODIFY_RETURN as u32 => BPF_MODIFY_RETURN,
x if x == BPF_LSM_MAC as u32 => BPF_LSM_MAC,
x if x == BPF_TRACE_ITER as u32 => BPF_TRACE_ITER,
x if x == BPF_CGROUP_INET4_GETPEERNAME as u32 => BPF_CGROUP_INET4_GETPEERNAME,
x if x == BPF_CGROUP_INET6_GETPEERNAME as u32 => BPF_CGROUP_INET6_GETPEERNAME,
x if x == BPF_CGROUP_INET4_GETSOCKNAME as u32 => BPF_CGROUP_INET4_GETSOCKNAME,
x if x == BPF_CGROUP_INET6_GETSOCKNAME as u32 => BPF_CGROUP_INET6_GETSOCKNAME,
x if x == BPF_XDP_DEVMAP as u32 => BPF_XDP_DEVMAP,
x if x == BPF_CGROUP_INET_SOCK_RELEASE as u32 => BPF_CGROUP_INET_SOCK_RELEASE,
x if x == BPF_XDP_CPUMAP as u32 => BPF_XDP_CPUMAP,
x if x == BPF_SK_LOOKUP as u32 => BPF_SK_LOOKUP,
x if x == BPF_XDP as u32 => BPF_XDP,
x if x == BPF_SK_SKB_VERDICT as u32 => BPF_SK_SKB_VERDICT,
x if x == BPF_SK_REUSEPORT_SELECT as u32 => BPF_SK_REUSEPORT_SELECT,
x if x == BPF_SK_REUSEPORT_SELECT_OR_MIGRATE as u32 => {
BPF_SK_REUSEPORT_SELECT_OR_MIGRATE
}
x if x == BPF_PERF_EVENT as u32 => BPF_PERF_EVENT,
x if x == BPF_TRACE_KPROBE_MULTI as u32 => BPF_TRACE_KPROBE_MULTI,
x if x == BPF_LSM_CGROUP as u32 => BPF_LSM_CGROUP,
x if x == BPF_STRUCT_OPS as u32 => BPF_STRUCT_OPS,
x if x == BPF_NETFILTER as u32 => BPF_NETFILTER,
x if x == BPF_TCX_INGRESS as u32 => BPF_TCX_INGRESS,
x if x == BPF_TCX_EGRESS as u32 => BPF_TCX_EGRESS,
x if x == BPF_TRACE_UPROBE_MULTI as u32 => BPF_TRACE_UPROBE_MULTI,
x if x == BPF_CGROUP_UNIX_CONNECT as u32 => BPF_CGROUP_UNIX_CONNECT,
x if x == BPF_CGROUP_UNIX_SENDMSG as u32 => BPF_CGROUP_UNIX_SENDMSG,
x if x == BPF_CGROUP_UNIX_RECVMSG as u32 => BPF_CGROUP_UNIX_RECVMSG,
x if x == BPF_CGROUP_UNIX_GETPEERNAME as u32 => BPF_CGROUP_UNIX_GETPEERNAME,
x if x == BPF_CGROUP_UNIX_GETSOCKNAME as u32 => BPF_CGROUP_UNIX_GETSOCKNAME,
x if x == BPF_NETKIT_PRIMARY as u32 => BPF_NETKIT_PRIMARY,
x if x == BPF_NETKIT_PEER as u32 => BPF_NETKIT_PEER,
_ => return Err(InvalidTypeBinding { value: attach_type }),
})
}
}
15 changes: 4 additions & 11 deletions aya-obj/src/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,10 @@ use core::mem;

#[cfg(not(feature = "std"))]
use crate::std;
use crate::EbpfSectionKind;

/// Invalid map type encontered
pub struct InvalidMapTypeError {
/// The map type
pub map_type: u32,
}
use crate::{EbpfSectionKind, InvalidTypeBinding};

impl TryFrom<u32> for crate::generated::bpf_map_type {
type Error = InvalidMapTypeError;
type Error = InvalidTypeBinding<u32>;

fn try_from(map_type: u32) -> Result<Self, Self::Error> {
use crate::generated::bpf_map_type::*;
Expand All @@ -31,7 +25,6 @@ impl TryFrom<u32> for crate::generated::bpf_map_type {
x if x == BPF_MAP_TYPE_LRU_HASH as u32 => BPF_MAP_TYPE_LRU_HASH,
x if x == BPF_MAP_TYPE_LRU_PERCPU_HASH as u32 => BPF_MAP_TYPE_LRU_PERCPU_HASH,
x if x == BPF_MAP_TYPE_LPM_TRIE as u32 => BPF_MAP_TYPE_LPM_TRIE,
x if x == BPF_MAP_TYPE_BLOOM_FILTER as u32 => BPF_MAP_TYPE_BLOOM_FILTER,
x if x == BPF_MAP_TYPE_ARRAY_OF_MAPS as u32 => BPF_MAP_TYPE_ARRAY_OF_MAPS,
x if x == BPF_MAP_TYPE_HASH_OF_MAPS as u32 => BPF_MAP_TYPE_HASH_OF_MAPS,
x if x == BPF_MAP_TYPE_DEVMAP as u32 => BPF_MAP_TYPE_DEVMAP,
Expand All @@ -42,7 +35,6 @@ impl TryFrom<u32> for crate::generated::bpf_map_type {
x if x == BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED as u32 => {
BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED
}
x if x == BPF_MAP_TYPE_CGRP_STORAGE as u32 => BPF_MAP_TYPE_CGRP_STORAGE,
x if x == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY as u32 => BPF_MAP_TYPE_REUSEPORT_SOCKARRAY,
x if x == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE_DEPRECATED as u32 => {
BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE_DEPRECATED
Expand All @@ -58,7 +50,8 @@ impl TryFrom<u32> for crate::generated::bpf_map_type {
x if x == BPF_MAP_TYPE_BLOOM_FILTER as u32 => BPF_MAP_TYPE_BLOOM_FILTER,
x if x == BPF_MAP_TYPE_USER_RINGBUF as u32 => BPF_MAP_TYPE_USER_RINGBUF,
x if x == BPF_MAP_TYPE_CGRP_STORAGE as u32 => BPF_MAP_TYPE_CGRP_STORAGE,
_ => return Err(InvalidMapTypeError { map_type }),
x if x == BPF_MAP_TYPE_ARENA as u32 => BPF_MAP_TYPE_ARENA,
_ => return Err(InvalidTypeBinding { value: map_type }),
})
}
}
Expand Down
27 changes: 26 additions & 1 deletion aya-obj/src/obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub struct Features {
bpf_cookie: bool,
cpumap_prog_id: bool,
devmap_prog_id: bool,
prog_info_map_ids: bool,
prog_info_gpl_compatible: bool,
btf: Option<BtfFeatures>,
}

Expand All @@ -61,6 +63,8 @@ impl Features {
bpf_cookie: bool,
cpumap_prog_id: bool,
devmap_prog_id: bool,
prog_info_map_ids: bool,
prog_info_gpl_compatible: bool,
btf: Option<BtfFeatures>,
) -> Self {
Self {
Expand All @@ -71,11 +75,16 @@ impl Features {
bpf_cookie,
cpumap_prog_id,
devmap_prog_id,
prog_info_map_ids,
prog_info_gpl_compatible,
btf,
}
}

/// Returns whether BPF program names are supported.
/// Returns whether BPF program names and map names are supported.
///
/// Although the feature probe performs the check for program name, we can use this to also
/// detect if map name is supported since they were both introduced in the same commit.
pub fn bpf_name(&self) -> bool {
self.bpf_name
}
Expand Down Expand Up @@ -110,6 +119,16 @@ impl Features {
self.devmap_prog_id
}

/// Returns whether `bpf_prog_info` supports `nr_map_ids` & `map_ids` fields.
pub fn prog_info_map_ids(&self) -> bool {
self.prog_info_map_ids
}

/// Returns whether `bpf_prog_info` supports `gpl_compatible` field.
pub fn prog_info_gpl_compatible(&self) -> bool {
self.prog_info_gpl_compatible
}

/// If BTF is supported, returns which BTF features are supported.
pub fn btf(&self) -> Option<&BtfFeatures> {
self.btf.as_ref()
Expand Down Expand Up @@ -987,6 +1006,12 @@ pub enum ParseError {
NoBTF,
}

/// Invalid bindings to the bpf type from the parsed/received value.
pub struct InvalidTypeBinding<T> {
/// The value parsed/received.
pub value: T,
}

/// The kind of an ELF section.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum EbpfSectionKind {
Expand Down
1 change: 1 addition & 0 deletions aya-obj/src/programs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pub mod cgroup_sock;
pub mod cgroup_sock_addr;
pub mod cgroup_sockopt;
mod types;
pub mod xdp;

pub use cgroup_sock::CgroupSockAttachType;
Expand Down
51 changes: 51 additions & 0 deletions aya-obj/src/programs/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//! Program type bindings.

use crate::{
generated::bpf_prog_type::{self, *},
InvalidTypeBinding,
};

impl TryFrom<u32> for bpf_prog_type {
type Error = InvalidTypeBinding<u32>;

fn try_from(prog_type: u32) -> Result<Self, Self::Error> {
Ok(match prog_type {
x if x == BPF_PROG_TYPE_UNSPEC as u32 => BPF_PROG_TYPE_UNSPEC,
x if x == BPF_PROG_TYPE_SOCKET_FILTER as u32 => BPF_PROG_TYPE_SOCKET_FILTER,
x if x == BPF_PROG_TYPE_KPROBE as u32 => BPF_PROG_TYPE_KPROBE,
x if x == BPF_PROG_TYPE_SCHED_CLS as u32 => BPF_PROG_TYPE_SCHED_CLS,
x if x == BPF_PROG_TYPE_SCHED_ACT as u32 => BPF_PROG_TYPE_SCHED_ACT,
x if x == BPF_PROG_TYPE_TRACEPOINT as u32 => BPF_PROG_TYPE_TRACEPOINT,
x if x == BPF_PROG_TYPE_XDP as u32 => BPF_PROG_TYPE_XDP,
x if x == BPF_PROG_TYPE_PERF_EVENT as u32 => BPF_PROG_TYPE_PERF_EVENT,
x if x == BPF_PROG_TYPE_CGROUP_SKB as u32 => BPF_PROG_TYPE_CGROUP_SKB,
x if x == BPF_PROG_TYPE_CGROUP_SOCK as u32 => BPF_PROG_TYPE_CGROUP_SOCK,
x if x == BPF_PROG_TYPE_LWT_IN as u32 => BPF_PROG_TYPE_LWT_IN,
x if x == BPF_PROG_TYPE_LWT_OUT as u32 => BPF_PROG_TYPE_LWT_OUT,
x if x == BPF_PROG_TYPE_LWT_XMIT as u32 => BPF_PROG_TYPE_LWT_XMIT,
x if x == BPF_PROG_TYPE_SOCK_OPS as u32 => BPF_PROG_TYPE_SOCK_OPS,
x if x == BPF_PROG_TYPE_SK_SKB as u32 => BPF_PROG_TYPE_SK_SKB,
x if x == BPF_PROG_TYPE_CGROUP_DEVICE as u32 => BPF_PROG_TYPE_CGROUP_DEVICE,
x if x == BPF_PROG_TYPE_SK_MSG as u32 => BPF_PROG_TYPE_SK_MSG,
x if x == BPF_PROG_TYPE_RAW_TRACEPOINT as u32 => BPF_PROG_TYPE_RAW_TRACEPOINT,
x if x == BPF_PROG_TYPE_CGROUP_SOCK_ADDR as u32 => BPF_PROG_TYPE_CGROUP_SOCK_ADDR,
x if x == BPF_PROG_TYPE_LWT_SEG6LOCAL as u32 => BPF_PROG_TYPE_LWT_SEG6LOCAL,
x if x == BPF_PROG_TYPE_LIRC_MODE2 as u32 => BPF_PROG_TYPE_LIRC_MODE2,
x if x == BPF_PROG_TYPE_SK_REUSEPORT as u32 => BPF_PROG_TYPE_SK_REUSEPORT,
x if x == BPF_PROG_TYPE_FLOW_DISSECTOR as u32 => BPF_PROG_TYPE_FLOW_DISSECTOR,
x if x == BPF_PROG_TYPE_CGROUP_SYSCTL as u32 => BPF_PROG_TYPE_CGROUP_SYSCTL,
x if x == BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE as u32 => {
BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE
}
x if x == BPF_PROG_TYPE_CGROUP_SOCKOPT as u32 => BPF_PROG_TYPE_CGROUP_SOCKOPT,
x if x == BPF_PROG_TYPE_TRACING as u32 => BPF_PROG_TYPE_TRACING,
x if x == BPF_PROG_TYPE_STRUCT_OPS as u32 => BPF_PROG_TYPE_STRUCT_OPS,
x if x == BPF_PROG_TYPE_EXT as u32 => BPF_PROG_TYPE_EXT,
x if x == BPF_PROG_TYPE_LSM as u32 => BPF_PROG_TYPE_LSM,
x if x == BPF_PROG_TYPE_SK_LOOKUP as u32 => BPF_PROG_TYPE_SK_LOOKUP,
x if x == BPF_PROG_TYPE_SYSCALL as u32 => BPF_PROG_TYPE_SYSCALL,
x if x == BPF_PROG_TYPE_NETFILTER as u32 => BPF_PROG_TYPE_NETFILTER,
_ => return Err(InvalidTypeBinding { value: prog_type }),
})
}
}
12 changes: 7 additions & 5 deletions aya/src/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use thiserror::Error;

use crate::{
generated::{
bpf_map_type, bpf_map_type::*, AYA_PERF_EVENT_IOC_DISABLE, AYA_PERF_EVENT_IOC_ENABLE,
AYA_PERF_EVENT_IOC_SET_BPF,
bpf_map_type::{self, *},
AYA_PERF_EVENT_IOC_DISABLE, AYA_PERF_EVENT_IOC_ENABLE, AYA_PERF_EVENT_IOC_SET_BPF,
},
maps::{Map, MapData, MapError},
obj::{
Expand All @@ -39,9 +39,9 @@ use crate::{
bpf_load_btf, is_bpf_cookie_supported, is_bpf_global_data_supported,
is_btf_datasec_supported, is_btf_decl_tag_supported, is_btf_enum64_supported,
is_btf_float_supported, is_btf_func_global_supported, is_btf_func_supported,
is_btf_supported, is_btf_type_tag_supported, is_perf_link_supported,
is_probe_read_kernel_supported, is_prog_id_supported, is_prog_name_supported,
retry_with_verifier_logs,
is_btf_supported, is_btf_type_tag_supported, is_info_gpl_compatible_supported,
is_info_map_ids_supported, is_perf_link_supported, is_probe_read_kernel_supported,
is_prog_id_supported, is_prog_name_supported, retry_with_verifier_logs,
},
util::{bytes_of, bytes_of_slice, page_size, possible_cpus, POSSIBLE_CPUS},
};
Expand Down Expand Up @@ -96,6 +96,8 @@ fn detect_features() -> Features {
is_bpf_cookie_supported(),
is_prog_id_supported(BPF_MAP_TYPE_CPUMAP),
is_prog_id_supported(BPF_MAP_TYPE_DEVMAP),
is_info_map_ids_supported(),
is_info_gpl_compatible_supported(),
btf,
);
debug!("BPF Feature Detection: {:#?}", f);
Expand Down
1 change: 0 additions & 1 deletion aya/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ use aya_obj::generated;
pub use bpf::*;
pub use obj::btf::{Btf, BtfError};
pub use object::Endianness;
pub use programs::loaded_programs;
#[doc(hidden)]
pub use sys::netlink_set_link_up;

Expand Down
Loading

0 comments on commit 15eb935

Please sign in to comment.