diff --git a/aya/src/bpf.rs b/aya/src/bpf.rs index bbe064b1..c50efb9a 100644 --- a/aya/src/bpf.rs +++ b/aya/src/bpf.rs @@ -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, @@ -790,7 +786,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; diff --git a/aya/src/lib.rs b/aya/src/lib.rs index 3278d3dc..808f3ac9 100644 --- a/aya/src/lib.rs +++ b/aya/src/lib.rs @@ -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; diff --git a/aya/src/maps/bloom_filter.rs b/aya/src/maps/bloom_filter.rs index 8e6e521e..03508160 100644 --- a/aya/src/maps/bloom_filter.rs +++ b/aya/src/maps/bloom_filter.rs @@ -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::(BPF_MAP_TYPE_BLOOM_FILTER) } diff --git a/aya/src/maps/hash_map/hash_map.rs b/aya/src/maps/hash_map/hash_map.rs index becac6c4..ace6e1b4 100644 --- a/aya/src/maps/hash_map/hash_map.rs +++ b/aya/src/maps/hash_map/hash_map.rs @@ -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::(BPF_MAP_TYPE_HASH) } diff --git a/aya/src/maps/hash_map/per_cpu_hash_map.rs b/aya/src/maps/hash_map/per_cpu_hash_map.rs index 54cbe118..d9194668 100644 --- a/aya/src/maps/hash_map/per_cpu_hash_map.rs +++ b/aya/src/maps/hash_map/per_cpu_hash_map.rs @@ -151,12 +151,13 @@ impl, K: Pod, V: Pod> IterableMap> #[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::( diff --git a/aya/src/maps/lpm_trie.rs b/aya/src/maps/lpm_trie.rs index f7efd87f..af674eb9 100644 --- a/aya/src/maps/lpm_trie.rs +++ b/aya/src/maps/lpm_trie.rs @@ -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::>(BPF_MAP_TYPE_LPM_TRIE) } diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index 6f36b6b3..eaa03cde 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -59,13 +59,12 @@ use std::{ 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 log::warn; use thiserror::Error; use crate::{ - obj::{self, parse_map_info, EbpfSectionKind}, pin::PinError, sys::{ 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(map: &MapData) -> Result<(), MapError> { /// You should never need to use this unless you're implementing a new map type. #[derive(Debug)] pub struct MapData { - obj: obj::Map, + obj: aya_obj::Map, fd: MapFd, } impl MapData { /// Creates a new map with the provided `name` pub fn create( - mut obj: obj::Map, + mut obj: aya_obj::Map, name: &str, btf_fd: Option>, ) -> Result { @@ -599,7 +598,7 @@ impl MapData { pub(crate) fn create_pinned_by_name>( path: P, - obj: obj::Map, + obj: aya_obj::Map, name: &str, btf_fd: Option>, ) -> Result { @@ -750,7 +749,7 @@ impl MapData { fd } - pub(crate) fn obj(&self) -> &obj::Map { + pub(crate) fn obj(&self) -> &aya_obj::Map { let Self { obj, fd: _ } = self; obj } @@ -951,15 +950,19 @@ impl Deref for PerCpuValues { #[cfg(test)] mod test_utils { + use aya_obj::{ + generated::{bpf_cmd, bpf_map_type}, + maps::LegacyMap, + EbpfSectionKind, + }; + use crate::{ bpf_map_def, - generated::{bpf_cmd, bpf_map_type}, maps::MapData, - obj::{self, maps::LegacyMap, EbpfSectionKind}, 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 { Syscall::Ebpf { cmd: bpf_cmd::BPF_MAP_CREATE, @@ -970,8 +973,8 @@ mod test_utils { MapData::create(obj, "foo", None).unwrap() } - pub(super) fn new_obj_map(map_type: bpf_map_type) -> obj::Map { - obj::Map::Legacy(LegacyMap { + pub(super) fn new_obj_map(map_type: bpf_map_type) -> aya_obj::Map { + aya_obj::Map::Legacy(LegacyMap { def: bpf_map_def { map_type: map_type as u32, key_size: std::mem::size_of::() as u32, @@ -989,8 +992,8 @@ mod test_utils { pub(super) fn new_obj_map_with_max_entries( map_type: bpf_map_type, max_entries: u32, - ) -> obj::Map { - obj::Map::Legacy(LegacyMap { + ) -> aya_obj::Map { + aya_obj::Map::Legacy(LegacyMap { def: bpf_map_def { map_type: map_type as u32, key_size: std::mem::size_of::() as u32, @@ -1011,17 +1014,15 @@ mod tests { use std::os::fd::AsRawFd as _; use assert_matches::assert_matches; + use aya_obj::generated::{bpf_cmd, bpf_map_info, bpf_map_type}; use libc::{c_char, EFAULT}; - fn new_obj_map() -> obj::Map { - test_utils::new_obj_map::(crate::generated::bpf_map_type::BPF_MAP_TYPE_HASH) - } - use super::*; - use crate::{ - generated::bpf_cmd, - sys::{override_syscall, Syscall}, - }; + use crate::sys::{override_syscall, Syscall}; + + fn new_obj_map() -> aya_obj::Map { + test_utils::new_obj_map::(bpf_map_type::BPF_MAP_TYPE_HASH) + } #[test] fn test_from_map_id() { @@ -1093,7 +1094,7 @@ mod tests { // Create with max_entries > nr_cpus is clamped to nr_cpus assert_matches!( MapData::create(test_utils::new_obj_map_with_max_entries::( - crate::generated::bpf_map_type::BPF_MAP_TYPE_PERF_EVENT_ARRAY, + bpf_map_type::BPF_MAP_TYPE_PERF_EVENT_ARRAY, 65535, ), "foo", None), Ok(MapData { @@ -1108,7 +1109,7 @@ mod tests { // Create with max_entries = 0 is set to nr_cpus assert_matches!( MapData::create(test_utils::new_obj_map_with_max_entries::( - crate::generated::bpf_map_type::BPF_MAP_TYPE_PERF_EVENT_ARRAY, + bpf_map_type::BPF_MAP_TYPE_PERF_EVENT_ARRAY, 0, ), "foo", None), Ok(MapData { @@ -1123,7 +1124,7 @@ mod tests { // Create with max_entries < nr_cpus is unchanged assert_matches!( MapData::create(test_utils::new_obj_map_with_max_entries::( - crate::generated::bpf_map_type::BPF_MAP_TYPE_PERF_EVENT_ARRAY, + bpf_map_type::BPF_MAP_TYPE_PERF_EVENT_ARRAY, 1, ), "foo", None), 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 for Unique permission, but no exposed tags have suitable permission in the borrow stack for this location" )] fn test_name() { - use crate::generated::bpf_map_info; - const TEST_NAME: &str = "foo"; 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 for Unique permission, but no exposed tags have suitable permission in the borrow stack for this location" )] fn test_loaded_maps() { - use crate::generated::bpf_map_info; - override_syscall(|call| match call { Syscall::Ebpf { cmd: bpf_cmd::BPF_MAP_GET_NEXT_ID, diff --git a/aya/src/maps/perf/perf_buffer.rs b/aya/src/maps/perf/perf_buffer.rs index 79b799ee..06b3d333 100644 --- a/aya/src/maps/perf/perf_buffer.rs +++ b/aya/src/maps/perf/perf_buffer.rs @@ -6,15 +6,15 @@ use std::{ sync::atomic::{self, AtomicPtr, Ordering}, }; +use aya_obj::generated::{ + perf_event_header, perf_event_mmap_page, + perf_event_type::{PERF_RECORD_LOST, PERF_RECORD_SAMPLE}, +}; use bytes::BytesMut; use libc::{munmap, MAP_FAILED, MAP_SHARED, PROT_READ, PROT_WRITE}; use thiserror::Error; use crate::{ - generated::{ - perf_event_header, perf_event_mmap_page, - perf_event_type::{PERF_RECORD_LOST, PERF_RECORD_SAMPLE}, - }, sys::{mmap, perf_event_ioctl, perf_event_open_bpf, SysResult}, PERF_EVENT_IOC_DISABLE, PERF_EVENT_IOC_ENABLE, }; diff --git a/aya/src/maps/ring_buf.rs b/aya/src/maps/ring_buf.rs index f6fc9377..27b20914 100644 --- a/aya/src/maps/ring_buf.rs +++ b/aya/src/maps/ring_buf.rs @@ -17,10 +17,10 @@ use std::{ sync::atomic::{AtomicU32, AtomicUsize, Ordering}, }; +use aya_obj::generated::{BPF_RINGBUF_BUSY_BIT, BPF_RINGBUF_DISCARD_BIT, BPF_RINGBUF_HDR_SZ}; use libc::{munmap, off_t, MAP_FAILED, MAP_SHARED, PROT_READ, PROT_WRITE}; use crate::{ - generated::{BPF_RINGBUF_BUSY_BIT, BPF_RINGBUF_DISCARD_BIT, BPF_RINGBUF_HDR_SZ}, maps::{MapData, MapError}, sys::{mmap, SyscallError}, util::page_size, diff --git a/aya/src/programs/cgroup_device.rs b/aya/src/programs/cgroup_device.rs index 87c5d4a2..cecadf1b 100644 --- a/aya/src/programs/cgroup_device.rs +++ b/aya/src/programs/cgroup_device.rs @@ -2,8 +2,11 @@ use std::os::fd::AsFd; +use aya_obj::generated::{ + bpf_attach_type::BPF_CGROUP_DEVICE, bpf_prog_type::BPF_PROG_TYPE_CGROUP_DEVICE, +}; + use crate::{ - generated::{bpf_attach_type::BPF_CGROUP_DEVICE, bpf_prog_type::BPF_PROG_TYPE_CGROUP_DEVICE}, programs::{ bpf_prog_get_fd_by_id, define_link_wrapper, load_program, query, CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError, ProgramFd, diff --git a/aya/src/programs/cgroup_skb.rs b/aya/src/programs/cgroup_skb.rs index 637e897a..a1450766 100644 --- a/aya/src/programs/cgroup_skb.rs +++ b/aya/src/programs/cgroup_skb.rs @@ -2,11 +2,12 @@ use std::{hash::Hash, os::fd::AsFd, path::Path}; +use aya_obj::generated::{ + bpf_attach_type::{BPF_CGROUP_INET_EGRESS, BPF_CGROUP_INET_INGRESS}, + bpf_prog_type::BPF_PROG_TYPE_CGROUP_SKB, +}; + use crate::{ - generated::{ - bpf_attach_type::{BPF_CGROUP_INET_EGRESS, BPF_CGROUP_INET_INGRESS}, - bpf_prog_type::BPF_PROG_TYPE_CGROUP_SKB, - }, programs::{ define_link_wrapper, load_program, CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError, diff --git a/aya/src/programs/cgroup_sock.rs b/aya/src/programs/cgroup_sock.rs index cc5db22f..eb1244dd 100644 --- a/aya/src/programs/cgroup_sock.rs +++ b/aya/src/programs/cgroup_sock.rs @@ -2,10 +2,10 @@ use std::{hash::Hash, os::fd::AsFd, path::Path}; +use aya_obj::generated::bpf_prog_type::BPF_PROG_TYPE_CGROUP_SOCK; pub use aya_obj::programs::CgroupSockAttachType; use crate::{ - generated::bpf_prog_type::BPF_PROG_TYPE_CGROUP_SOCK, programs::{ define_link_wrapper, load_program, CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError, diff --git a/aya/src/programs/cgroup_sock_addr.rs b/aya/src/programs/cgroup_sock_addr.rs index a31d8cfd..c08db24a 100644 --- a/aya/src/programs/cgroup_sock_addr.rs +++ b/aya/src/programs/cgroup_sock_addr.rs @@ -2,10 +2,10 @@ use std::{hash::Hash, os::fd::AsFd, path::Path}; +use aya_obj::generated::bpf_prog_type::BPF_PROG_TYPE_CGROUP_SOCK_ADDR; pub use aya_obj::programs::CgroupSockAddrAttachType; use crate::{ - generated::bpf_prog_type::BPF_PROG_TYPE_CGROUP_SOCK_ADDR, programs::{ define_link_wrapper, load_program, CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError, diff --git a/aya/src/programs/cgroup_sockopt.rs b/aya/src/programs/cgroup_sockopt.rs index ee9d40b2..d064c6e1 100644 --- a/aya/src/programs/cgroup_sockopt.rs +++ b/aya/src/programs/cgroup_sockopt.rs @@ -2,10 +2,10 @@ use std::{hash::Hash, os::fd::AsFd, path::Path}; +use aya_obj::generated::bpf_prog_type::BPF_PROG_TYPE_CGROUP_SOCKOPT; pub use aya_obj::programs::CgroupSockoptAttachType; use crate::{ - generated::bpf_prog_type::BPF_PROG_TYPE_CGROUP_SOCKOPT, programs::{ define_link_wrapper, load_program, CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError, diff --git a/aya/src/programs/cgroup_sysctl.rs b/aya/src/programs/cgroup_sysctl.rs index fd997aa1..9d88fa9f 100644 --- a/aya/src/programs/cgroup_sysctl.rs +++ b/aya/src/programs/cgroup_sysctl.rs @@ -2,8 +2,11 @@ use std::{hash::Hash, os::fd::AsFd}; +use aya_obj::generated::{ + bpf_attach_type::BPF_CGROUP_SYSCTL, bpf_prog_type::BPF_PROG_TYPE_CGROUP_SYSCTL, +}; + use crate::{ - generated::{bpf_attach_type::BPF_CGROUP_SYSCTL, bpf_prog_type::BPF_PROG_TYPE_CGROUP_SYSCTL}, programs::{ define_link_wrapper, load_program, CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError, diff --git a/aya/src/programs/extension.rs b/aya/src/programs/extension.rs index 68d77f38..787e1923 100644 --- a/aya/src/programs/extension.rs +++ b/aya/src/programs/extension.rs @@ -2,12 +2,14 @@ use std::os::fd::{AsFd as _, BorrowedFd}; +use aya_obj::{ + btf::BtfKind, + generated::{bpf_attach_type::BPF_CGROUP_INET_INGRESS, bpf_prog_type::BPF_PROG_TYPE_EXT}, +}; use object::Endianness; use thiserror::Error; use crate::{ - generated::{bpf_attach_type::BPF_CGROUP_INET_INGRESS, bpf_prog_type::BPF_PROG_TYPE_EXT}, - obj::btf::BtfKind, programs::{ define_link_wrapper, load_program, FdLink, FdLinkId, ProgramData, ProgramError, ProgramFd, }, diff --git a/aya/src/programs/fentry.rs b/aya/src/programs/fentry.rs index 0a1d082f..6de41b56 100644 --- a/aya/src/programs/fentry.rs +++ b/aya/src/programs/fentry.rs @@ -1,12 +1,13 @@ //! Fentry programs. -use crate::{ +use aya_obj::{ + btf::{Btf, BtfKind}, generated::{bpf_attach_type::BPF_TRACE_FENTRY, bpf_prog_type::BPF_PROG_TYPE_TRACING}, - obj::btf::{Btf, BtfKind}, - programs::{ - define_link_wrapper, load_program, utils::attach_raw_tracepoint, FdLink, FdLinkId, - ProgramData, ProgramError, - }, +}; + +use crate::programs::{ + define_link_wrapper, load_program, utils::attach_raw_tracepoint, FdLink, FdLinkId, ProgramData, + ProgramError, }; /// A program that can be attached to the entry point of (almost) any kernel diff --git a/aya/src/programs/fexit.rs b/aya/src/programs/fexit.rs index 5efca285..22483b94 100644 --- a/aya/src/programs/fexit.rs +++ b/aya/src/programs/fexit.rs @@ -1,12 +1,13 @@ //! Fexit programs. -use crate::{ +use aya_obj::{ + btf::{Btf, BtfKind}, generated::{bpf_attach_type::BPF_TRACE_FEXIT, bpf_prog_type::BPF_PROG_TYPE_TRACING}, - obj::btf::{Btf, BtfKind}, - programs::{ - define_link_wrapper, load_program, utils::attach_raw_tracepoint, FdLink, FdLinkId, - ProgramData, ProgramError, - }, +}; + +use crate::programs::{ + define_link_wrapper, load_program, utils::attach_raw_tracepoint, FdLink, FdLinkId, ProgramData, + ProgramError, }; /// A program that can be attached to the exit point of (almost) anny kernel diff --git a/aya/src/programs/kprobe.rs b/aya/src/programs/kprobe.rs index 81e830e9..e06b0c99 100644 --- a/aya/src/programs/kprobe.rs +++ b/aya/src/programs/kprobe.rs @@ -6,10 +6,10 @@ use std::{ path::{Path, PathBuf}, }; +use aya_obj::generated::{bpf_link_type, bpf_prog_type::BPF_PROG_TYPE_KPROBE}; use thiserror::Error; use crate::{ - generated::{bpf_link_type, bpf_prog_type::BPF_PROG_TYPE_KPROBE}, programs::{ define_link_wrapper, load_program, perf_attach::{PerfLinkIdInner, PerfLinkInner}, diff --git a/aya/src/programs/links.rs b/aya/src/programs/links.rs index 9e8ac1c0..19564de3 100644 --- a/aya/src/programs/links.rs +++ b/aya/src/programs/links.rs @@ -7,13 +7,13 @@ use std::{ path::{Path, PathBuf}, }; +use aya_obj::generated::{ + bpf_attach_type, BPF_F_AFTER, BPF_F_ALLOW_MULTI, BPF_F_ALLOW_OVERRIDE, BPF_F_BEFORE, BPF_F_ID, + BPF_F_LINK, BPF_F_REPLACE, +}; use thiserror::Error; use crate::{ - generated::{ - bpf_attach_type, BPF_F_AFTER, BPF_F_ALLOW_MULTI, BPF_F_ALLOW_OVERRIDE, BPF_F_BEFORE, - BPF_F_ID, BPF_F_LINK, BPF_F_REPLACE, - }, pin::PinError, programs::{MultiProgLink, MultiProgram, ProgramError, ProgramFd, ProgramId}, sys::{bpf_get_object, bpf_pin_object, bpf_prog_attach, bpf_prog_detach, SyscallError}, @@ -521,11 +521,11 @@ mod tests { use std::{cell::RefCell, fs::File, rc::Rc}; use assert_matches::assert_matches; + use aya_obj::generated::{BPF_F_ALLOW_MULTI, BPF_F_ALLOW_OVERRIDE}; use tempfile::tempdir; use super::{FdLink, Link, LinkMap}; use crate::{ - generated::{BPF_F_ALLOW_MULTI, BPF_F_ALLOW_OVERRIDE}, programs::{CgroupAttachMode, ProgramError}, sys::override_syscall, }; diff --git a/aya/src/programs/lirc_mode2.rs b/aya/src/programs/lirc_mode2.rs index fecd261d..e0975de4 100644 --- a/aya/src/programs/lirc_mode2.rs +++ b/aya/src/programs/lirc_mode2.rs @@ -1,8 +1,11 @@ //! Lirc programs. use std::os::fd::{AsFd, AsRawFd as _, RawFd}; +use aya_obj::generated::{ + bpf_attach_type::BPF_LIRC_MODE2, bpf_prog_type::BPF_PROG_TYPE_LIRC_MODE2, +}; + use crate::{ - generated::{bpf_attach_type::BPF_LIRC_MODE2, bpf_prog_type::BPF_PROG_TYPE_LIRC_MODE2}, programs::{ load_program, query, CgroupAttachMode, Link, ProgramData, ProgramError, ProgramFd, ProgramInfo, diff --git a/aya/src/programs/lsm.rs b/aya/src/programs/lsm.rs index 7c1d9ddf..6fb88a50 100644 --- a/aya/src/programs/lsm.rs +++ b/aya/src/programs/lsm.rs @@ -1,12 +1,13 @@ //! LSM probes. -use crate::{ +use aya_obj::{ + btf::{Btf, BtfKind}, generated::{bpf_attach_type::BPF_LSM_MAC, bpf_prog_type::BPF_PROG_TYPE_LSM}, - obj::btf::{Btf, BtfKind}, - programs::{ - define_link_wrapper, load_program, utils::attach_raw_tracepoint, FdLink, FdLinkId, - ProgramData, ProgramError, - }, +}; + +use crate::programs::{ + define_link_wrapper, load_program, utils::attach_raw_tracepoint, FdLink, FdLinkId, ProgramData, + ProgramError, }; /// A program that attaches to Linux LSM hooks. Used to implement security policy and diff --git a/aya/src/programs/mod.rs b/aya/src/programs/mod.rs index 00d61b01..64cc6849 100644 --- a/aya/src/programs/mod.rs +++ b/aya/src/programs/mod.rs @@ -77,6 +77,11 @@ use std::{ sync::Arc, }; +use aya_obj::{ + btf::BtfError, + generated::{bpf_attach_type, bpf_link_info, bpf_prog_info, bpf_prog_type}, + VerifierLog, +}; use info::impl_info; pub use info::{loaded_programs, ProgramInfo, ProgramType}; use libc::ENOSPC; @@ -113,9 +118,7 @@ pub use crate::programs::{ xdp::{Xdp, XdpError, XdpFlags}, }; use crate::{ - generated::{bpf_attach_type, bpf_link_info, bpf_prog_info, bpf_prog_type}, maps::MapError, - obj::{self, btf::BtfError, VerifierLog}, pin::PinError, programs::{links::*, perf_attach::*}, sys::{ @@ -462,7 +465,7 @@ impl Program { #[derive(Debug)] pub(crate) struct ProgramData { pub(crate) name: Option, - pub(crate) obj: Option<(obj::Program, obj::Function)>, + pub(crate) obj: Option<(aya_obj::Program, aya_obj::Function)>, pub(crate) fd: Option, pub(crate) links: LinkMap, pub(crate) expected_attach_type: Option, @@ -478,7 +481,7 @@ pub(crate) struct ProgramData { impl ProgramData { pub(crate) fn new( name: Option, - obj: (obj::Program, obj::Function), + obj: (aya_obj::Program, aya_obj::Function), btf_fd: Option>, verifier_log_level: VerifierLogLevel, ) -> Self { @@ -617,12 +620,12 @@ fn load_program( } let obj = obj.as_ref().unwrap(); let ( - obj::Program { + aya_obj::Program { license, kernel_version, .. }, - obj::Function { + aya_obj::Function { instructions, func_info, line_info, diff --git a/aya/src/programs/perf_attach.rs b/aya/src/programs/perf_attach.rs index 89f68ace..f7e57690 100644 --- a/aya/src/programs/perf_attach.rs +++ b/aya/src/programs/perf_attach.rs @@ -1,8 +1,9 @@ //! Perf attach links. use std::os::fd::{AsFd as _, AsRawFd as _, BorrowedFd, RawFd}; +use aya_obj::generated::bpf_attach_type::BPF_PERF_EVENT; + use crate::{ - generated::bpf_attach_type::BPF_PERF_EVENT, programs::{ probe::{detach_debug_fs, ProbeEvent}, FdLink, Link, ProgramError, diff --git a/aya/src/programs/perf_event.rs b/aya/src/programs/perf_event.rs index 587d6499..ea114941 100644 --- a/aya/src/programs/perf_event.rs +++ b/aya/src/programs/perf_event.rs @@ -2,18 +2,19 @@ use std::os::fd::AsFd as _; -pub use crate::generated::{ +use aya_obj::generated::{ + bpf_link_type, + bpf_prog_type::BPF_PROG_TYPE_PERF_EVENT, + perf_type_id::{ + PERF_TYPE_BREAKPOINT, PERF_TYPE_HARDWARE, PERF_TYPE_HW_CACHE, PERF_TYPE_RAW, + PERF_TYPE_SOFTWARE, PERF_TYPE_TRACEPOINT, + }, +}; +pub use aya_obj::generated::{ perf_hw_cache_id, perf_hw_cache_op_id, perf_hw_cache_op_result_id, perf_hw_id, perf_sw_ids, }; + use crate::{ - generated::{ - bpf_link_type, - bpf_prog_type::BPF_PROG_TYPE_PERF_EVENT, - perf_type_id::{ - PERF_TYPE_BREAKPOINT, PERF_TYPE_HARDWARE, PERF_TYPE_HW_CACHE, PERF_TYPE_RAW, - PERF_TYPE_SOFTWARE, PERF_TYPE_TRACEPOINT, - }, - }, programs::{ links::define_link_wrapper, load_program, perf_attach, diff --git a/aya/src/programs/raw_trace_point.rs b/aya/src/programs/raw_trace_point.rs index 617ebc81..8f7211d2 100644 --- a/aya/src/programs/raw_trace_point.rs +++ b/aya/src/programs/raw_trace_point.rs @@ -1,12 +1,11 @@ //! Raw tracepoints. use std::ffi::CString; -use crate::{ - generated::bpf_prog_type::BPF_PROG_TYPE_RAW_TRACEPOINT, - programs::{ - define_link_wrapper, load_program, utils::attach_raw_tracepoint, FdLink, FdLinkId, - ProgramData, ProgramError, - }, +use aya_obj::generated::bpf_prog_type::BPF_PROG_TYPE_RAW_TRACEPOINT; + +use crate::programs::{ + define_link_wrapper, load_program, utils::attach_raw_tracepoint, FdLink, FdLinkId, ProgramData, + ProgramError, }; /// A program that can be attached at a pre-defined kernel trace point. diff --git a/aya/src/programs/sk_lookup.rs b/aya/src/programs/sk_lookup.rs index 065001ce..4c9babf2 100644 --- a/aya/src/programs/sk_lookup.rs +++ b/aya/src/programs/sk_lookup.rs @@ -1,9 +1,10 @@ //! Programmable socket lookup. use std::os::fd::AsFd; +use aya_obj::generated::{bpf_attach_type::BPF_SK_LOOKUP, bpf_prog_type::BPF_PROG_TYPE_SK_LOOKUP}; + use super::links::FdLink; use crate::{ - generated::{bpf_attach_type::BPF_SK_LOOKUP, bpf_prog_type::BPF_PROG_TYPE_SK_LOOKUP}, programs::{define_link_wrapper, load_program, FdLinkId, ProgramData, ProgramError}, sys::{bpf_link_create, LinkTarget, SyscallError}, }; diff --git a/aya/src/programs/sk_msg.rs b/aya/src/programs/sk_msg.rs index 95510cf2..4dc0bf81 100644 --- a/aya/src/programs/sk_msg.rs +++ b/aya/src/programs/sk_msg.rs @@ -2,8 +2,11 @@ use std::os::fd::AsFd as _; +use aya_obj::generated::{ + bpf_attach_type::BPF_SK_MSG_VERDICT, bpf_prog_type::BPF_PROG_TYPE_SK_MSG, +}; + use crate::{ - generated::{bpf_attach_type::BPF_SK_MSG_VERDICT, bpf_prog_type::BPF_PROG_TYPE_SK_MSG}, maps::sock::SockMapFd, programs::{ define_link_wrapper, load_program, CgroupAttachMode, ProgAttachLink, ProgAttachLinkId, diff --git a/aya/src/programs/sk_skb.rs b/aya/src/programs/sk_skb.rs index eabce593..041cd814 100644 --- a/aya/src/programs/sk_skb.rs +++ b/aya/src/programs/sk_skb.rs @@ -2,11 +2,12 @@ use std::{os::fd::AsFd as _, path::Path}; +use aya_obj::generated::{ + bpf_attach_type::{BPF_SK_SKB_STREAM_PARSER, BPF_SK_SKB_STREAM_VERDICT}, + bpf_prog_type::BPF_PROG_TYPE_SK_SKB, +}; + use crate::{ - generated::{ - bpf_attach_type::{BPF_SK_SKB_STREAM_PARSER, BPF_SK_SKB_STREAM_VERDICT}, - bpf_prog_type::BPF_PROG_TYPE_SK_SKB, - }, maps::sock::SockMapFd, programs::{ define_link_wrapper, load_program, CgroupAttachMode, ProgAttachLink, ProgAttachLinkId, diff --git a/aya/src/programs/sock_ops.rs b/aya/src/programs/sock_ops.rs index 62573215..7153274f 100644 --- a/aya/src/programs/sock_ops.rs +++ b/aya/src/programs/sock_ops.rs @@ -1,8 +1,11 @@ //! Socket option programs. use std::os::fd::AsFd; +use aya_obj::generated::{ + bpf_attach_type::BPF_CGROUP_SOCK_OPS, bpf_prog_type::BPF_PROG_TYPE_SOCK_OPS, +}; + use crate::{ - generated::{bpf_attach_type::BPF_CGROUP_SOCK_OPS, bpf_prog_type::BPF_PROG_TYPE_SOCK_OPS}, programs::{ define_link_wrapper, load_program, CgroupAttachMode, FdLink, Link, ProgAttachLink, ProgramData, ProgramError, diff --git a/aya/src/programs/socket_filter.rs b/aya/src/programs/socket_filter.rs index 02be9e05..3f8e9c32 100644 --- a/aya/src/programs/socket_filter.rs +++ b/aya/src/programs/socket_filter.rs @@ -4,13 +4,13 @@ use std::{ os::fd::{AsFd, AsRawFd, RawFd}, }; +use aya_obj::generated::{ + bpf_prog_type::BPF_PROG_TYPE_SOCKET_FILTER, SO_ATTACH_BPF, SO_DETACH_BPF, +}; use libc::{setsockopt, SOL_SOCKET}; use thiserror::Error; -use crate::{ - generated::{bpf_prog_type::BPF_PROG_TYPE_SOCKET_FILTER, SO_ATTACH_BPF, SO_DETACH_BPF}, - programs::{load_program, Link, ProgramData, ProgramError}, -}; +use crate::programs::{load_program, Link, ProgramData, ProgramError}; /// The type returned when attaching a [`SocketFilter`] fails. #[derive(Debug, Error)] diff --git a/aya/src/programs/tc.rs b/aya/src/programs/tc.rs index 9f55fe92..eae7878f 100644 --- a/aya/src/programs/tc.rs +++ b/aya/src/programs/tc.rs @@ -6,16 +6,16 @@ use std::{ path::Path, }; +use aya_obj::generated::{ + bpf_attach_type::{self, BPF_TCX_EGRESS, BPF_TCX_INGRESS}, + bpf_link_type, + bpf_prog_type::BPF_PROG_TYPE_SCHED_CLS, + TC_H_CLSACT, TC_H_MIN_EGRESS, TC_H_MIN_INGRESS, +}; use thiserror::Error; use super::{FdLink, ProgramInfo}; use crate::{ - generated::{ - bpf_attach_type::{self, BPF_TCX_EGRESS, BPF_TCX_INGRESS}, - bpf_link_type, - bpf_prog_type::BPF_PROG_TYPE_SCHED_CLS, - TC_H_CLSACT, TC_H_MIN_EGRESS, TC_H_MIN_INGRESS, - }, programs::{ define_link_wrapper, load_program, query, Link, LinkError, LinkOrder, ProgramData, ProgramError, diff --git a/aya/src/programs/tp_btf.rs b/aya/src/programs/tp_btf.rs index 8c235d43..7bd351d7 100644 --- a/aya/src/programs/tp_btf.rs +++ b/aya/src/programs/tp_btf.rs @@ -1,12 +1,13 @@ //! BTF-enabled raw tracepoints. -use crate::{ +use aya_obj::{ + btf::{Btf, BtfKind}, generated::{bpf_attach_type::BPF_TRACE_RAW_TP, bpf_prog_type::BPF_PROG_TYPE_TRACING}, - obj::btf::{Btf, BtfKind}, - programs::{ - define_link_wrapper, load_program, utils::attach_raw_tracepoint, FdLink, FdLinkId, - ProgramData, ProgramError, - }, +}; + +use crate::programs::{ + define_link_wrapper, load_program, utils::attach_raw_tracepoint, FdLink, FdLinkId, ProgramData, + ProgramError, }; /// Marks a function as a [BTF-enabled raw tracepoint][1] eBPF program that can be attached at diff --git a/aya/src/programs/trace_point.rs b/aya/src/programs/trace_point.rs index 4ce0be9a..7e19366d 100644 --- a/aya/src/programs/trace_point.rs +++ b/aya/src/programs/trace_point.rs @@ -1,10 +1,10 @@ //! Tracepoint programs. use std::{fs, io, os::fd::AsFd as _, path::Path}; +use aya_obj::generated::{bpf_link_type, bpf_prog_type::BPF_PROG_TYPE_TRACEPOINT}; use thiserror::Error; use crate::{ - generated::{bpf_link_type, bpf_prog_type::BPF_PROG_TYPE_TRACEPOINT}, programs::{ define_link_wrapper, load_program, perf_attach::{perf_attach, PerfLinkIdInner, PerfLinkInner}, diff --git a/aya/src/programs/uprobe.rs b/aya/src/programs/uprobe.rs index 737166ba..507f566b 100644 --- a/aya/src/programs/uprobe.rs +++ b/aya/src/programs/uprobe.rs @@ -11,12 +11,12 @@ use std::{ sync::LazyLock, }; +use aya_obj::generated::{bpf_link_type, bpf_prog_type::BPF_PROG_TYPE_KPROBE}; use libc::pid_t; use object::{Object, ObjectSection, ObjectSymbol, Symbol}; use thiserror::Error; use crate::{ - generated::{bpf_link_type, bpf_prog_type::BPF_PROG_TYPE_KPROBE}, programs::{ define_link_wrapper, load_program, perf_attach::{PerfLinkIdInner, PerfLinkInner}, diff --git a/aya/src/programs/xdp.rs b/aya/src/programs/xdp.rs index 66b32b32..cc3ae8b8 100644 --- a/aya/src/programs/xdp.rs +++ b/aya/src/programs/xdp.rs @@ -8,15 +8,17 @@ use std::{ path::Path, }; -use libc::if_nametoindex; -use thiserror::Error; - -use crate::{ +use aya_obj::{ generated::{ bpf_link_type, bpf_prog_type, XDP_FLAGS_DRV_MODE, XDP_FLAGS_HW_MODE, XDP_FLAGS_REPLACE, XDP_FLAGS_SKB_MODE, XDP_FLAGS_UPDATE_IF_NOEXIST, }, - obj::programs::XdpAttachType, + programs::XdpAttachType, +}; +use libc::if_nametoindex; +use thiserror::Error; + +use crate::{ programs::{ define_link_wrapper, load_program, FdLink, Link, LinkError, ProgramData, ProgramError, }, diff --git a/aya/src/sys/bpf.rs b/aya/src/sys/bpf.rs index a405b3be..2d7e35ba 100644 --- a/aya/src/sys/bpf.rs +++ b/aya/src/sys/bpf.rs @@ -8,28 +8,24 @@ use std::{ }; use assert_matches::assert_matches; -use libc::{ENOENT, ENOSPC}; -use obj::{ - btf::{BtfEnum64, Enum64}, - generated::bpf_stats_type, +use aya_obj::{ + btf::{ + BtfEnum64, BtfParam, BtfType, DataSec, DataSecEntry, DeclTag, Enum64, Float, Func, + FuncLinkage, FuncProto, FuncSecInfo, Int, IntEncoding, LineSecInfo, Ptr, TypeTag, Var, + VarLinkage, + }, + copy_instructions, + generated::{ + bpf_attach_type, bpf_attr, bpf_btf_info, bpf_cmd, bpf_insn, bpf_link_info, bpf_map_info, + bpf_map_type, bpf_prog_info, bpf_prog_type, bpf_stats_type, BPF_F_REPLACE, + }, maps::{bpf_map_def, LegacyMap}, EbpfSectionKind, VerifierLog, }; +use libc::{ENOENT, ENOSPC}; use crate::{ - generated::{ - bpf_attach_type, bpf_attr, bpf_btf_info, bpf_cmd, bpf_insn, bpf_link_info, bpf_map_info, - bpf_map_type, bpf_prog_info, bpf_prog_type, BPF_F_REPLACE, - }, maps::{MapData, PerCpuValues}, - obj::{ - self, - btf::{ - BtfParam, BtfType, DataSec, DataSecEntry, DeclTag, Float, Func, FuncLinkage, FuncProto, - FuncSecInfo, Int, IntEncoding, LineSecInfo, Ptr, TypeTag, Var, VarLinkage, - }, - copy_instructions, - }, programs::links::LinkRef, sys::{syscall, SysResult, Syscall, SyscallError}, util::KernelVersion, @@ -38,7 +34,7 @@ use crate::{ pub(crate) fn bpf_create_map( name: &CStr, - def: &obj::Map, + def: &aya_obj::Map, btf_fd: Option>, kernel_version: KernelVersion, ) -> SysResult { @@ -51,7 +47,7 @@ pub(crate) fn bpf_create_map( u.max_entries = def.max_entries(); u.map_flags = def.map_flags(); - if let obj::Map::Btf(m) = def { + if let aya_obj::Map::Btf(m) = def { use bpf_map_type::*; // Mimic https://github.com/libbpf/libbpf/issues/355 @@ -901,7 +897,7 @@ pub(crate) fn is_bpf_global_data_supported() -> bool { let mut insns = copy_instructions(prog).unwrap(); let map = MapData::create( - obj::Map::Legacy(LegacyMap { + aya_obj::Map::Legacy(LegacyMap { def: bpf_map_def { map_type: bpf_map_type::BPF_MAP_TYPE_ARRAY as u32, key_size: 4, diff --git a/aya/src/sys/mod.rs b/aya/src/sys/mod.rs index 05f8b4dd..5bc72d95 100644 --- a/aya/src/sys/mod.rs +++ b/aya/src/sys/mod.rs @@ -13,6 +13,7 @@ use std::{ os::fd::{AsRawFd as _, BorrowedFd, OwnedFd}, }; +use aya_obj::generated::{bpf_attr, bpf_cmd, perf_event_attr}; pub(crate) use bpf::*; #[cfg(test)] pub(crate) use fake::*; @@ -23,8 +24,6 @@ pub(crate) use netlink::*; pub(crate) use perf_event::*; use thiserror::Error; -use crate::generated::{bpf_attr, bpf_cmd, perf_event_attr}; - pub(crate) type SysResult = Result; pub(crate) enum Syscall<'a> { @@ -152,9 +151,9 @@ pub enum Stats { RunTime, } -impl From for crate::generated::bpf_stats_type { +impl From for aya_obj::generated::bpf_stats_type { fn from(value: Stats) -> Self { - use crate::generated::bpf_stats_type::*; + use aya_obj::generated::bpf_stats_type::*; match value { Stats::RunTime => BPF_STATS_RUN_TIME, diff --git a/aya/src/sys/netlink.rs b/aya/src/sys/netlink.rs index 11b99471..f138808b 100644 --- a/aya/src/sys/netlink.rs +++ b/aya/src/sys/netlink.rs @@ -6,6 +6,11 @@ use std::{ ptr, slice, }; +use aya_obj::generated::{ + ifinfomsg, tcmsg, IFLA_XDP_EXPECTED_FD, IFLA_XDP_FD, IFLA_XDP_FLAGS, NLMSG_ALIGNTO, TCA_BPF_FD, + TCA_BPF_FLAGS, TCA_BPF_FLAG_ACT_DIRECT, TCA_BPF_NAME, TCA_KIND, TCA_OPTIONS, TC_H_CLSACT, + TC_H_INGRESS, TC_H_MAJ_MASK, TC_H_UNSPEC, XDP_FLAGS_REPLACE, +}; use libc::{ getsockname, nlattr, nlmsgerr, nlmsghdr, recv, send, setsockopt, sockaddr_nl, socket, AF_NETLINK, AF_UNSPEC, ETH_P_ALL, IFF_UP, IFLA_XDP, NETLINK_EXT_ACK, NETLINK_ROUTE, @@ -15,15 +20,7 @@ use libc::{ }; use thiserror::Error; -use crate::{ - generated::{ - ifinfomsg, tcmsg, IFLA_XDP_EXPECTED_FD, IFLA_XDP_FD, IFLA_XDP_FLAGS, NLMSG_ALIGNTO, - TCA_BPF_FD, TCA_BPF_FLAGS, TCA_BPF_FLAG_ACT_DIRECT, TCA_BPF_NAME, TCA_KIND, TCA_OPTIONS, - TC_H_CLSACT, TC_H_INGRESS, TC_H_MAJ_MASK, TC_H_UNSPEC, XDP_FLAGS_REPLACE, - }, - programs::TcAttachType, - util::tc_handler_make, -}; +use crate::{programs::TcAttachType, util::tc_handler_make}; const NLA_HDR_LEN: usize = align_to(mem::size_of::(), NLA_ALIGNTO as usize); diff --git a/aya/src/sys/perf_event.rs b/aya/src/sys/perf_event.rs index ec8b1420..e5967b9b 100644 --- a/aya/src/sys/perf_event.rs +++ b/aya/src/sys/perf_event.rs @@ -4,16 +4,16 @@ use std::{ os::fd::{BorrowedFd, FromRawFd as _}, }; -use libc::pid_t; - -use super::{syscall, SysResult, Syscall}; -use crate::generated::{ +use aya_obj::generated::{ perf_event_attr, perf_event_sample_format::PERF_SAMPLE_RAW, perf_sw_ids::PERF_COUNT_SW_BPF_OUTPUT, perf_type_id::{PERF_TYPE_SOFTWARE, PERF_TYPE_TRACEPOINT}, PERF_FLAG_FD_CLOEXEC, }; +use libc::pid_t; + +use super::{syscall, SysResult, Syscall}; #[allow(clippy::too_many_arguments)] pub(crate) fn perf_event_open( diff --git a/aya/src/util.rs b/aya/src/util.rs index 50f96ff4..19abc8aa 100644 --- a/aya/src/util.rs +++ b/aya/src/util.rs @@ -12,12 +12,10 @@ use std::{ str::{FromStr, Utf8Error}, }; +use aya_obj::generated::{TC_H_MAJ_MASK, TC_H_MIN_MASK}; use libc::{if_nametoindex, sysconf, uname, utsname, _SC_PAGESIZE}; -use crate::{ - generated::{TC_H_MAJ_MASK, TC_H_MIN_MASK}, - Pod, -}; +use crate::Pod; /// Represents a kernel version, in major.minor.release version. // Adapted from https://docs.rs/procfs/latest/procfs/sys/kernel/struct.Version.html.