Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aya: add feature probing for program and map type #1063

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 38 additions & 18 deletions aya-obj/src/obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use crate::{
Array, Btf, BtfError, BtfExt, BtfFeatures, BtfType, DataSecEntry, FuncSecInfo, LineSecInfo,
},
generated::{
bpf_insn, bpf_map_info, bpf_map_type::BPF_MAP_TYPE_ARRAY, BPF_CALL, BPF_F_RDONLY_PROG,
BPF_JMP, BPF_K,
__BindgenBitfieldUnit, bpf_insn, bpf_map_info, bpf_map_type::BPF_MAP_TYPE_ARRAY, BPF_CALL,
BPF_F_RDONLY_PROG, BPF_JMP, BPF_K,
},
maps::{bpf_map_def, BtfMap, BtfMapDef, LegacyMap, Map, PinningType, MINIMUM_MAP_SIZE},
programs::{
Expand All @@ -47,8 +47,6 @@ 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 @@ -63,8 +61,6 @@ 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 @@ -75,8 +71,6 @@ impl Features {
bpf_cookie,
cpumap_prog_id,
devmap_prog_id,
prog_info_map_ids,
prog_info_gpl_compatible,
btf,
}
}
Expand Down Expand Up @@ -119,22 +113,48 @@ 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()
}
}

impl bpf_insn {
/// Creates a [BPF instruction](bpf_insn).
///
/// The arguments will be converted to the host's endianness.
pub const fn new(code: u8, dst_reg: u8, src_reg: u8, off: i16, imm: i32) -> Self {
if dst_reg > 10 || src_reg > 10 {
panic!("invalid register number");
}

let registers;
let offset;
let immediate;

#[cfg(target_endian = "little")]
{
registers = (src_reg << 4) | dst_reg;
offset = off.swap_bytes();
immediate = imm.swap_bytes();
}
#[cfg(target_endian = "big")]
{
registers = (dst_reg << 4) | src_reg;
offset = off;
immediate = imm;
}

bpf_insn {
code,
_bitfield_align_1: [],
_bitfield_1: __BindgenBitfieldUnit::new([registers]),
off: offset,
imm: immediate,
}
}
}

/// The loaded object file representation
#[derive(Clone, Debug)]
pub struct Object {
Expand Down
2 changes: 1 addition & 1 deletion aya/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ bytes = { workspace = true }
libc = { workspace = true }
log = { workspace = true }
object = { workspace = true, features = ["elf", "read_core", "std", "write"] }
once_cell = { workspace = true }
once_cell = { workspace = true, features = ["race"]}
thiserror = { workspace = true }
tokio = { workspace = true, features = ["rt"], optional = true }

Expand Down
28 changes: 11 additions & 17 deletions aya/src/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,20 @@ use std::{
};

use aya_obj::{
btf::{BtfFeatures, BtfRelocationError},
generated::{BPF_F_SLEEPABLE, BPF_F_XDP_HAS_FRAGS},
btf::{Btf, BtfError, BtfFeatures, BtfRelocationError},
generated::{
bpf_map_type::{self, *},
AYA_PERF_EVENT_IOC_DISABLE, AYA_PERF_EVENT_IOC_ENABLE, AYA_PERF_EVENT_IOC_SET_BPF,
BPF_F_SLEEPABLE, BPF_F_XDP_HAS_FRAGS,
},
relocation::EbpfRelocationError,
EbpfSectionKind, Features,
EbpfSectionKind, Features, Object, ParseError, ProgramSection,
};
use log::{debug, warn};
use thiserror::Error;

use crate::{
generated::{
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::{
btf::{Btf, BtfError},
Object, ParseError, ProgramSection,
},
programs::{
BtfTracePoint, CgroupDevice, CgroupSkb, CgroupSkbAttachType, CgroupSock, CgroupSockAddr,
CgroupSockopt, CgroupSysctl, Extension, FEntry, FExit, KProbe, LircMode2, Lsm, PerfEvent,
Expand All @@ -39,9 +35,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_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,
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,
},
util::{bytes_of, bytes_of_slice, nr_cpus, page_size},
};
Expand Down Expand Up @@ -94,8 +90,6 @@ 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 Expand Up @@ -790,7 +784,7 @@ fn adjust_to_page_size(byte_size: u32, page_size: u32) -> u32 {

#[cfg(test)]
mod tests {
use crate::generated::bpf_map_type::*;
use aya_obj::generated::bpf_map_type::*;

const PAGE_SIZE: u32 = 4096;
const NUM_CPUS: u32 = 4;
Expand Down
4 changes: 1 addition & 3 deletions aya/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,8 @@ pub mod util;

use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, OwnedFd, RawFd};

use aya_obj as obj;
use aya_obj::generated;
pub use aya_obj::btf::{Btf, BtfError};
pub use bpf::*;
pub use obj::btf::{Btf, BtfError};
pub use object::Endianness;
#[doc(hidden)]
pub use sys::netlink_set_link_up;
Expand Down
11 changes: 5 additions & 6 deletions aya/src/maps/bloom_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,22 @@ mod tests {
use std::io;

use assert_matches::assert_matches;
use aya_obj::generated::{
bpf_cmd,
bpf_map_type::{BPF_MAP_TYPE_ARRAY, BPF_MAP_TYPE_BLOOM_FILTER},
};
use libc::{EFAULT, ENOENT};

use super::*;
use crate::{
generated::{
bpf_cmd,
bpf_map_type::{BPF_MAP_TYPE_ARRAY, BPF_MAP_TYPE_BLOOM_FILTER},
},
maps::{
test_utils::{self, new_map},
Map,
},
obj,
sys::{override_syscall, SysResult, Syscall},
};

fn new_obj_map() -> obj::Map {
fn new_obj_map() -> aya_obj::Map {
test_utils::new_obj_map::<u32>(BPF_MAP_TYPE_BLOOM_FILTER)
}

Expand Down
11 changes: 5 additions & 6 deletions aya/src/maps/hash_map/hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,22 @@ mod tests {
use std::io;

use assert_matches::assert_matches;
use aya_obj::generated::{
bpf_attr, bpf_cmd,
bpf_map_type::{BPF_MAP_TYPE_HASH, BPF_MAP_TYPE_LRU_HASH},
};
use libc::{EFAULT, ENOENT};

use super::*;
use crate::{
generated::{
bpf_attr, bpf_cmd,
bpf_map_type::{BPF_MAP_TYPE_HASH, BPF_MAP_TYPE_LRU_HASH},
},
maps::{
test_utils::{self, new_map},
Map,
},
obj,
sys::{override_syscall, SysResult, Syscall},
};

fn new_obj_map() -> obj::Map {
fn new_obj_map() -> aya_obj::Map {
test_utils::new_obj_map::<u32>(BPF_MAP_TYPE_HASH)
}

Expand Down
9 changes: 5 additions & 4 deletions aya/src/maps/hash_map/per_cpu_hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,13 @@ impl<T: Borrow<MapData>, K: Pod, V: Pod> IterableMap<K, PerCpuValues<V>>

#[cfg(test)]
mod tests {
use super::*;
use crate::{
generated::bpf_map_type::{BPF_MAP_TYPE_LRU_PERCPU_HASH, BPF_MAP_TYPE_PERCPU_HASH},
maps::{test_utils, Map},
use aya_obj::generated::bpf_map_type::{
BPF_MAP_TYPE_LRU_PERCPU_HASH, BPF_MAP_TYPE_PERCPU_HASH,
};

use super::*;
use crate::maps::{test_utils, Map};

#[test]
fn test_try_from_ok() {
let map = Map::PerCpuHashMap(test_utils::new_map(test_utils::new_obj_map::<u32>(
Expand Down
11 changes: 5 additions & 6 deletions aya/src/maps/lpm_trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,23 +198,22 @@ mod tests {
use std::{io, net::Ipv4Addr};

use assert_matches::assert_matches;
use aya_obj::generated::{
bpf_cmd,
bpf_map_type::{BPF_MAP_TYPE_ARRAY, BPF_MAP_TYPE_LPM_TRIE},
};
use libc::{EFAULT, ENOENT};

use super::*;
use crate::{
generated::{
bpf_cmd,
bpf_map_type::{BPF_MAP_TYPE_ARRAY, BPF_MAP_TYPE_LPM_TRIE},
},
maps::{
test_utils::{self, new_map},
Map,
},
obj,
sys::{override_syscall, SysResult, Syscall},
};

fn new_obj_map() -> obj::Map {
fn new_obj_map() -> aya_obj::Map {
test_utils::new_obj_map::<Key<u32>>(BPF_MAP_TYPE_LPM_TRIE)
}

Expand Down
Loading
Loading