@ -59,13 +59,12 @@ use std::{
ptr ,
ptr ,
} ;
} ;
use aya_obj ::{ generated ::bpf_map_type , InvalidTypeBinding} ;
use aya_obj ::{ generated ::bpf_map_type , parse_map_info, EbpfSectionKind , InvalidTypeBinding} ;
use libc ::{ getrlimit , rlim_t , rlimit , RLIMIT_MEMLOCK , RLIM_INFINITY } ;
use libc ::{ getrlimit , rlim_t , rlimit , RLIMIT_MEMLOCK , RLIM_INFINITY } ;
use log ::warn ;
use log ::warn ;
use thiserror ::Error ;
use thiserror ::Error ;
use crate ::{
use crate ::{
obj ::{ self , parse_map_info , EbpfSectionKind } ,
pin ::PinError ,
pin ::PinError ,
sys ::{
sys ::{
bpf_create_map , bpf_get_object , bpf_map_freeze , bpf_map_get_fd_by_id , bpf_map_get_next_key ,
bpf_create_map , bpf_get_object , bpf_map_freeze , bpf_map_get_fd_by_id , bpf_map_get_next_key ,
@ -544,14 +543,14 @@ pub(crate) fn check_v_size<V>(map: &MapData) -> Result<(), MapError> {
/// You should never need to use this unless you're implementing a new map type.
/// You should never need to use this unless you're implementing a new map type.
#[ derive(Debug) ]
#[ derive(Debug) ]
pub struct MapData {
pub struct MapData {
obj : obj::Map ,
obj : aya_ obj::Map ,
fd : MapFd ,
fd : MapFd ,
}
}
impl MapData {
impl MapData {
/// Creates a new map with the provided `name`
/// Creates a new map with the provided `name`
pub fn create (
pub fn create (
mut obj : obj::Map ,
mut obj : aya_ obj::Map ,
name : & str ,
name : & str ,
btf_fd : Option < BorrowedFd < ' _ > > ,
btf_fd : Option < BorrowedFd < ' _ > > ,
) -> Result < Self , MapError > {
) -> Result < Self , MapError > {
@ -599,7 +598,7 @@ impl MapData {
pub ( crate ) fn create_pinned_by_name < P : AsRef < Path > > (
pub ( crate ) fn create_pinned_by_name < P : AsRef < Path > > (
path : P ,
path : P ,
obj : obj::Map ,
obj : aya_ obj::Map ,
name : & str ,
name : & str ,
btf_fd : Option < BorrowedFd < ' _ > > ,
btf_fd : Option < BorrowedFd < ' _ > > ,
) -> Result < Self , MapError > {
) -> Result < Self , MapError > {
@ -750,7 +749,7 @@ impl MapData {
fd
fd
}
}
pub ( crate ) fn obj ( & self ) -> & obj::Map {
pub ( crate ) fn obj ( & self ) -> & aya_ obj::Map {
let Self { obj , fd : _ } = self ;
let Self { obj , fd : _ } = self ;
obj
obj
}
}
@ -951,15 +950,19 @@ impl<T: Pod> Deref for PerCpuValues<T> {
#[ cfg(test) ]
#[ cfg(test) ]
mod test_utils {
mod test_utils {
use aya_obj ::{
generated ::{ bpf_cmd , bpf_map_type } ,
maps ::LegacyMap ,
EbpfSectionKind ,
} ;
use crate ::{
use crate ::{
bpf_map_def ,
bpf_map_def ,
generated ::{ bpf_cmd , bpf_map_type } ,
maps ::MapData ,
maps ::MapData ,
obj ::{ self , maps ::LegacyMap , EbpfSectionKind } ,
sys ::{ override_syscall , Syscall } ,
sys ::{ override_syscall , Syscall } ,
} ;
} ;
pub ( super ) fn new_map ( obj : obj::Map ) -> MapData {
pub ( super ) fn new_map ( obj : aya_ obj::Map ) -> MapData {
override_syscall ( | call | match call {
override_syscall ( | call | match call {
Syscall ::Ebpf {
Syscall ::Ebpf {
cmd : bpf_cmd ::BPF_MAP_CREATE ,
cmd : bpf_cmd ::BPF_MAP_CREATE ,
@ -970,8 +973,8 @@ mod test_utils {
MapData ::create ( obj , "foo" , None ) . unwrap ( )
MapData ::create ( obj , "foo" , None ) . unwrap ( )
}
}
pub ( super ) fn new_obj_map < K > ( map_type : bpf_map_type ) -> obj::Map {
pub ( super ) fn new_obj_map < K > ( map_type : bpf_map_type ) -> aya_ obj::Map {
obj::Map ::Legacy ( LegacyMap {
aya_ obj::Map ::Legacy ( LegacyMap {
def : bpf_map_def {
def : bpf_map_def {
map_type : map_type as u32 ,
map_type : map_type as u32 ,
key_size : std ::mem ::size_of ::< K > ( ) as u32 ,
key_size : std ::mem ::size_of ::< K > ( ) as u32 ,
@ -989,8 +992,8 @@ mod test_utils {
pub ( super ) fn new_obj_map_with_max_entries < K > (
pub ( super ) fn new_obj_map_with_max_entries < K > (
map_type : bpf_map_type ,
map_type : bpf_map_type ,
max_entries : u32 ,
max_entries : u32 ,
) -> obj::Map {
) -> aya_ obj::Map {
obj::Map ::Legacy ( LegacyMap {
aya_ obj::Map ::Legacy ( LegacyMap {
def : bpf_map_def {
def : bpf_map_def {
map_type : map_type as u32 ,
map_type : map_type as u32 ,
key_size : std ::mem ::size_of ::< K > ( ) as u32 ,
key_size : std ::mem ::size_of ::< K > ( ) as u32 ,
@ -1011,17 +1014,15 @@ mod tests {
use std ::os ::fd ::AsRawFd as _ ;
use std ::os ::fd ::AsRawFd as _ ;
use assert_matches ::assert_matches ;
use assert_matches ::assert_matches ;
use aya_obj ::generated ::{ bpf_cmd , bpf_map_info , bpf_map_type } ;
use libc ::{ c_char , EFAULT } ;
use libc ::{ c_char , EFAULT } ;
fn new_obj_map ( ) -> obj ::Map {
test_utils ::new_obj_map ::< u32 > ( crate ::generated ::bpf_map_type ::BPF_MAP_TYPE_HASH )
}
use super ::* ;
use super ::* ;
use crate ::{
use crate ::sys ::{ override_syscall , Syscall } ;
generated ::bpf_cmd ,
sys ::{ override_syscall , Syscall } ,
fn new_obj_map ( ) -> aya_obj ::Map {
} ;
test_utils ::new_obj_map ::< u32 > ( bpf_map_type ::BPF_MAP_TYPE_HASH )
}
#[ test ]
#[ test ]
fn test_from_map_id ( ) {
fn test_from_map_id ( ) {
@ -1093,7 +1094,7 @@ mod tests {
// Create with max_entries > nr_cpus is clamped to nr_cpus
// Create with max_entries > nr_cpus is clamped to nr_cpus
assert_matches ! (
assert_matches ! (
MapData ::create ( test_utils ::new_obj_map_with_max_entries ::< u32 > (
MapData ::create ( test_utils ::new_obj_map_with_max_entries ::< u32 > (
crate ::generated ::bpf_map_type ::BPF_MAP_TYPE_PERF_EVENT_ARRAY ,
bpf_map_type ::BPF_MAP_TYPE_PERF_EVENT_ARRAY ,
65535 ,
65535 ,
) , "foo" , None ) ,
) , "foo" , None ) ,
Ok ( MapData {
Ok ( MapData {
@ -1108,7 +1109,7 @@ mod tests {
// Create with max_entries = 0 is set to nr_cpus
// Create with max_entries = 0 is set to nr_cpus
assert_matches ! (
assert_matches ! (
MapData ::create ( test_utils ::new_obj_map_with_max_entries ::< u32 > (
MapData ::create ( test_utils ::new_obj_map_with_max_entries ::< u32 > (
crate ::generated ::bpf_map_type ::BPF_MAP_TYPE_PERF_EVENT_ARRAY ,
bpf_map_type ::BPF_MAP_TYPE_PERF_EVENT_ARRAY ,
0 ,
0 ,
) , "foo" , None ) ,
) , "foo" , None ) ,
Ok ( MapData {
Ok ( MapData {
@ -1123,7 +1124,7 @@ mod tests {
// Create with max_entries < nr_cpus is unchanged
// Create with max_entries < nr_cpus is unchanged
assert_matches ! (
assert_matches ! (
MapData ::create ( test_utils ::new_obj_map_with_max_entries ::< u32 > (
MapData ::create ( test_utils ::new_obj_map_with_max_entries ::< u32 > (
crate ::generated ::bpf_map_type ::BPF_MAP_TYPE_PERF_EVENT_ARRAY ,
bpf_map_type ::BPF_MAP_TYPE_PERF_EVENT_ARRAY ,
1 ,
1 ,
) , "foo" , None ) ,
) , "foo" , None ) ,
Ok ( MapData {
Ok ( MapData {
@ -1142,8 +1143,6 @@ mod tests {
ignore = "`let map_info = unsafe { &mut *(attr.info.info as *mut bpf_map_info) }` is trying to retag from <wildcard> for Unique permission, but no exposed tags have suitable permission in the borrow stack for this location"
ignore = "`let map_info = unsafe { &mut *(attr.info.info as *mut bpf_map_info) }` is trying to retag from <wildcard> for Unique permission, but no exposed tags have suitable permission in the borrow stack for this location"
) ]
) ]
fn test_name ( ) {
fn test_name ( ) {
use crate ::generated ::bpf_map_info ;
const TEST_NAME : & str = "foo" ;
const TEST_NAME : & str = "foo" ;
override_syscall ( | call | match call {
override_syscall ( | call | match call {
@ -1178,8 +1177,6 @@ mod tests {
ignore = "`let map_info = unsafe { &mut *(attr.info.info as *mut bpf_map_info) }` is trying to retag from <wildcard> for Unique permission, but no exposed tags have suitable permission in the borrow stack for this location"
ignore = "`let map_info = unsafe { &mut *(attr.info.info as *mut bpf_map_info) }` is trying to retag from <wildcard> for Unique permission, but no exposed tags have suitable permission in the borrow stack for this location"
) ]
) ]
fn test_loaded_maps ( ) {
fn test_loaded_maps ( ) {
use crate ::generated ::bpf_map_info ;
override_syscall ( | call | match call {
override_syscall ( | call | match call {
Syscall ::Ebpf {
Syscall ::Ebpf {
cmd : bpf_cmd ::BPF_MAP_GET_NEXT_ID ,
cmd : bpf_cmd ::BPF_MAP_GET_NEXT_ID ,