diff --git a/Cargo.toml b/Cargo.toml index f4d1df95..1eb34af9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -66,7 +66,6 @@ core-error = { version = "0.0.0", default-features = false } dialoguer = { version = "0.10", default-features = false } diff = { version = "0.1.13", default-features = false } env_logger = { version = "0.10", default-features = false } -futures = { version = "0.3.12", default-features = false } hashbrown = { version = "0.14", default-features = false } indoc = { version = "2.0", default-features = false } integration-ebpf = { path = "test/integration-ebpf", default-features = false } @@ -77,7 +76,6 @@ netns-rs = { version = "0.1", default-features = false } nix = { version = "0.26.2", default-features = false } num_enum = { version = "0.7", default-features = false } object = { version = "0.32", default-features = false } -parking_lot = { version = "0.12.0", default-features = false } proc-macro-error = { version = "1.0", default-features = false } proc-macro2 = { version = "1", default-features = false } public-api = { version = "0.31.2", default-features = false } diff --git a/aya/Cargo.toml b/aya/Cargo.toml index 4e5eec16..59025b2d 100644 --- a/aya/Cargo.toml +++ b/aya/Cargo.toml @@ -24,13 +24,11 @@ object = { workspace = true, default-features = false, features = [ "read_core", "std", ] } -parking_lot = { workspace = true } thiserror = { workspace = true } tokio = { workspace = true, features = ["rt"], optional = true } [dev-dependencies] assert_matches = { workspace = true } -futures = { workspace = true } tempfile = { workspace = true } [features] diff --git a/aya/src/bpf.rs b/aya/src/bpf.rs index f5841f3f..f8bf467c 100644 --- a/aya/src/bpf.rs +++ b/aya/src/bpf.rs @@ -71,7 +71,7 @@ unsafe impl Pod for [T; N] {} pub use aya_obj::maps::{bpf_map_def, PinningType}; -lazy_static! { +lazy_static::lazy_static! { pub(crate) static ref FEATURES: Features = detect_features(); } @@ -138,7 +138,7 @@ pub struct BpfLoader<'a> { allow_unsupported_maps: bool, } -bitflags! { +bitflags::bitflags! { /// Used to set the verifier log level flags in [BpfLoader](BpfLoader::verifier_log_level()). #[derive(Clone, Copy, Debug)] pub struct VerifierLogLevel: u32 { @@ -161,8 +161,8 @@ impl Default for VerifierLogLevel { impl<'a> BpfLoader<'a> { /// Creates a new loader instance. - pub fn new() -> BpfLoader<'a> { - BpfLoader { + pub fn new() -> Self { + Self { btf: Btf::from_sys_fs().ok().map(Cow::Owned), map_pin_path: None, globals: HashMap::new(), @@ -738,7 +738,7 @@ fn parse_map(data: (String, MapData)) -> Result<(String, Map), BpfError> { Ok((name, map)) } -impl<'a> Default for BpfLoader<'a> { +impl Default for BpfLoader<'_> { fn default() -> Self { BpfLoader::new() } @@ -768,7 +768,7 @@ impl Bpf { /// let bpf = Bpf::load_file("file.o")?; /// # Ok::<(), aya::BpfError>(()) /// ``` - pub fn load_file>(path: P) -> Result { + pub fn load_file>(path: P) -> Result { BpfLoader::new() .btf(Btf::from_sys_fs().ok().as_ref()) .load_file(path) @@ -793,7 +793,7 @@ impl Bpf { /// let bpf = Bpf::load(&data)?; /// # Ok::<(), aya::BpfError>(()) /// ``` - pub fn load(data: &[u8]) -> Result { + pub fn load(data: &[u8]) -> Result { BpfLoader::new() .btf(Btf::from_sys_fs().ok().as_ref()) .load(data) diff --git a/aya/src/lib.rs b/aya/src/lib.rs index 4ba70c2a..743ea26d 100644 --- a/aya/src/lib.rs +++ b/aya/src/lib.rs @@ -37,13 +37,47 @@ html_favicon_url = "https://aya-rs.dev/assets/images/crabby.svg" )] #![cfg_attr(docsrs, feature(doc_cfg))] -#![deny(clippy::all, missing_docs)] +#![deny( + clippy::all, + clippy::use_self, + absolute_paths_not_starting_with_crate, + deprecated_in_future, + elided_lifetimes_in_paths, + explicit_outlives_requirements, + ffi_unwind_calls, + keyword_idents, + //let_underscore_drop, + macro_use_extern_crate, + meta_variable_misuse, + missing_abi, + //missing_copy_implementations, + missing_docs, + non_ascii_idents, + noop_method_call, + pointer_structural_match, + rust_2021_incompatible_closure_captures, + rust_2021_incompatible_or_patterns, + rust_2021_prefixes_incompatible_syntax, + rust_2021_prelude_collisions, + single_use_lifetimes, + trivial_numeric_casts, + unreachable_pub, + //unsafe_op_in_unsafe_fn, + unstable_features, + unused_crate_dependencies, + unused_extern_crates, + unused_import_braces, + unused_lifetimes, + unused_macro_rules, + unused_qualifications, + //unused_results, + unused_tuple_struct_fields, +)] #![allow(clippy::missing_safety_doc, clippy::len_without_is_empty)] - -#[macro_use] -extern crate lazy_static; -#[macro_use] -extern crate bitflags; +#![cfg_attr( + all(feature = "async_tokio", feature = "async_std"), + allow(unused_crate_dependencies) +)] mod bpf; use aya_obj::generated; diff --git a/aya/src/maps/array/array.rs b/aya/src/maps/array/array.rs index 97bb46d6..64454a3c 100644 --- a/aya/src/maps/array/array.rs +++ b/aya/src/maps/array/array.rs @@ -35,11 +35,11 @@ pub struct Array { } impl, V: Pod> Array { - pub(crate) fn new(map: T) -> Result, MapError> { + pub(crate) fn new(map: T) -> Result { let data = map.borrow(); check_kv_size::(data)?; - Ok(Array { + Ok(Self { inner: map, _v: PhantomData, }) diff --git a/aya/src/maps/array/per_cpu_array.rs b/aya/src/maps/array/per_cpu_array.rs index 6405af4a..db6169bd 100644 --- a/aya/src/maps/array/per_cpu_array.rs +++ b/aya/src/maps/array/per_cpu_array.rs @@ -54,11 +54,11 @@ pub struct PerCpuArray { } impl, V: Pod> PerCpuArray { - pub(crate) fn new(map: T) -> Result, MapError> { + pub(crate) fn new(map: T) -> Result { let data = map.borrow(); check_kv_size::(data)?; - Ok(PerCpuArray { + Ok(Self { inner: map, _v: PhantomData, }) diff --git a/aya/src/maps/array/program_array.rs b/aya/src/maps/array/program_array.rs index e2152b56..a38ce023 100644 --- a/aya/src/maps/array/program_array.rs +++ b/aya/src/maps/array/program_array.rs @@ -52,11 +52,11 @@ pub struct ProgramArray { } impl> ProgramArray { - pub(crate) fn new(map: T) -> Result, MapError> { + pub(crate) fn new(map: T) -> Result { let data = map.borrow(); check_kv_size::(data)?; - Ok(ProgramArray { inner: map }) + Ok(Self { inner: map }) } /// An iterator over the indices of the array that point to a program. The iterator item type diff --git a/aya/src/maps/bloom_filter.rs b/aya/src/maps/bloom_filter.rs index 84bff986..0396d0f6 100644 --- a/aya/src/maps/bloom_filter.rs +++ b/aya/src/maps/bloom_filter.rs @@ -40,11 +40,11 @@ pub struct BloomFilter { } impl, V: Pod> BloomFilter { - pub(crate) fn new(map: T) -> Result, MapError> { + pub(crate) fn new(map: T) -> Result { let data = map.borrow(); check_v_size::(data)?; - Ok(BloomFilter { + Ok(Self { inner: map, _v: PhantomData, }) diff --git a/aya/src/maps/hash_map/hash_map.rs b/aya/src/maps/hash_map/hash_map.rs index b2f9e547..1f8a2f9c 100644 --- a/aya/src/maps/hash_map/hash_map.rs +++ b/aya/src/maps/hash_map/hash_map.rs @@ -39,11 +39,11 @@ pub struct HashMap { } impl, K: Pod, V: Pod> HashMap { - pub(crate) fn new(map: T) -> Result, MapError> { + pub(crate) fn new(map: T) -> Result { let data = map.borrow(); check_kv_size::(data)?; - Ok(HashMap { + Ok(Self { inner: map, _k: PhantomData, _v: PhantomData, @@ -96,7 +96,7 @@ impl, K: Pod, V: Pod> IterableMap for HashMap } fn get(&self, key: &K) -> Result { - HashMap::get(self, key, 0) + Self::get(self, key, 0) } } 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 7fd90ec0..5ae177f2 100644 --- a/aya/src/maps/hash_map/per_cpu_hash_map.rs +++ b/aya/src/maps/hash_map/per_cpu_hash_map.rs @@ -48,11 +48,11 @@ pub struct PerCpuHashMap { } impl, K: Pod, V: Pod> PerCpuHashMap { - pub(crate) fn new(map: T) -> Result, MapError> { + pub(crate) fn new(map: T) -> Result { let data = map.borrow(); check_kv_size::(data)?; - Ok(PerCpuHashMap { + Ok(Self { inner: map, _k: PhantomData, _v: PhantomData, @@ -143,6 +143,6 @@ impl, K: Pod, V: Pod> IterableMap> } fn get(&self, key: &K) -> Result, MapError> { - PerCpuHashMap::get(self, key, 0) + Self::get(self, key, 0) } } diff --git a/aya/src/maps/lpm_trie.rs b/aya/src/maps/lpm_trie.rs index d05ec2cf..dcdc443d 100644 --- a/aya/src/maps/lpm_trie.rs +++ b/aya/src/maps/lpm_trie.rs @@ -113,11 +113,11 @@ impl Clone for Key { unsafe impl Pod for Key {} impl, K: Pod, V: Pod> LpmTrie { - pub(crate) fn new(map: T) -> Result, MapError> { + pub(crate) fn new(map: T) -> Result { let data = map.borrow(); check_kv_size::, V>(data)?; - Ok(LpmTrie { + Ok(Self { inner: map, _k: PhantomData, _v: PhantomData, diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index cf39277d..0cd5386e 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -261,22 +261,22 @@ impl Map { /// Returns the low level map type. fn map_type(&self) -> u32 { match self { - Map::Array(map) => map.obj.map_type(), - Map::PerCpuArray(map) => map.obj.map_type(), - Map::ProgramArray(map) => map.obj.map_type(), - Map::HashMap(map) => map.obj.map_type(), - Map::LruHashMap(map) => map.obj.map_type(), - Map::PerCpuHashMap(map) => map.obj.map_type(), - Map::PerCpuLruHashMap(map) => map.obj.map_type(), - Map::PerfEventArray(map) => map.obj.map_type(), - Map::SockHash(map) => map.obj.map_type(), - Map::SockMap(map) => map.obj.map_type(), - Map::BloomFilter(map) => map.obj.map_type(), - Map::LpmTrie(map) => map.obj.map_type(), - Map::Stack(map) => map.obj.map_type(), - Map::StackTraceMap(map) => map.obj.map_type(), - Map::Queue(map) => map.obj.map_type(), - Map::Unsupported(map) => map.obj.map_type(), + Self::Array(map) => map.obj.map_type(), + Self::PerCpuArray(map) => map.obj.map_type(), + Self::ProgramArray(map) => map.obj.map_type(), + Self::HashMap(map) => map.obj.map_type(), + Self::LruHashMap(map) => map.obj.map_type(), + Self::PerCpuHashMap(map) => map.obj.map_type(), + Self::PerCpuLruHashMap(map) => map.obj.map_type(), + Self::PerfEventArray(map) => map.obj.map_type(), + Self::SockHash(map) => map.obj.map_type(), + Self::SockMap(map) => map.obj.map_type(), + Self::BloomFilter(map) => map.obj.map_type(), + Self::LpmTrie(map) => map.obj.map_type(), + Self::Stack(map) => map.obj.map_type(), + Self::StackTraceMap(map) => map.obj.map_type(), + Self::Queue(map) => map.obj.map_type(), + Self::Unsupported(map) => map.obj.map_type(), } } } @@ -500,9 +500,11 @@ impl MapData { } })?; + #[allow(trivial_numeric_casts)] + let fd = fd as RawFd; Ok(Self { obj, - fd: fd as RawFd, + fd, pinned: false, }) } @@ -537,7 +539,7 @@ impl MapData { } /// Loads a map from a pinned path in bpffs. - pub fn from_pin>(path: P) -> Result { + pub fn from_pin>(path: P) -> Result { let path_string = CString::new(path.as_ref().to_string_lossy().into_owned()).map_err(|e| { MapError::PinError { @@ -555,7 +557,7 @@ impl MapData { let info = bpf_map_get_info_by_fd(fd.as_fd())?; - Ok(MapData { + Ok(Self { obj: parse_map_info(info, PinningType::ByName), fd: fd.into_raw_fd(), pinned: true, @@ -567,10 +569,10 @@ impl MapData { /// If loading from a BPF Filesystem (bpffs) you should use [`Map::from_pin`](crate::maps::MapData::from_pin). /// This API is intended for cases where you have received a valid BPF FD from some other means. /// For example, you received an FD over Unix Domain Socket. - pub fn from_fd(fd: OwnedFd) -> Result { + pub fn from_fd(fd: OwnedFd) -> Result { let info = bpf_map_get_info_by_fd(fd.as_fd())?; - Ok(MapData { + Ok(Self { obj: parse_map_info(info, PinningType::None), fd: fd.into_raw_fd(), pinned: false, @@ -641,8 +643,8 @@ pub struct MapKeys<'coll, K: Pod> { } impl<'coll, K: Pod> MapKeys<'coll, K> { - fn new(map: &'coll MapData) -> MapKeys<'coll, K> { - MapKeys { + fn new(map: &'coll MapData) -> Self { + Self { map, err: false, key: None, @@ -685,8 +687,8 @@ pub struct MapIter<'coll, K: Pod, V, I: IterableMap> { } impl<'coll, K: Pod, V, I: IterableMap> MapIter<'coll, K, V, I> { - fn new(map: &'coll I) -> MapIter<'coll, K, V, I> { - MapIter { + fn new(map: &'coll I) -> Self { + Self { keys: MapKeys::new(map.map()), map, _v: PhantomData, @@ -761,7 +763,7 @@ impl TryFrom> for PerCpuValues { format!("not enough values ({}), nr_cpus: {}", values.len(), nr_cpus), )); } - Ok(PerCpuValues { + Ok(Self { values: values.into_boxed_slice(), }) } @@ -775,7 +777,7 @@ impl PerCpuValues { }) } - pub(crate) unsafe fn from_kernel_mem(mem: PerCpuKernelMem) -> PerCpuValues { + pub(crate) unsafe fn from_kernel_mem(mem: PerCpuKernelMem) -> Self { let mem_ptr = mem.bytes.as_ptr() as usize; let value_size = (mem::size_of::() + 7) & !7; let mut values = Vec::new(); @@ -785,13 +787,13 @@ impl PerCpuValues { offset += value_size; } - PerCpuValues { + Self { values: values.into_boxed_slice(), } } pub(crate) fn build_kernel_mem(&self) -> Result { - let mut mem = PerCpuValues::::alloc_kernel_mem()?; + let mut mem = Self::alloc_kernel_mem()?; let mem_ptr = mem.as_mut_ptr() as usize; let value_size = (mem::size_of::() + 7) & !7; for i in 0..self.values.len() { diff --git a/aya/src/maps/perf/async_perf_event_array.rs b/aya/src/maps/perf/async_perf_event_array.rs index 447df95d..7c942e26 100644 --- a/aya/src/maps/perf/async_perf_event_array.rs +++ b/aya/src/maps/perf/async_perf_event_array.rs @@ -52,7 +52,6 @@ use crate::maps::{ /// # let mut bpf = aya::Bpf::load(&[])?; /// use aya::maps::perf::{AsyncPerfEventArray, PerfBufferError}; /// use aya::util::online_cpus; -/// use futures::future; /// use bytes::BytesMut; /// use tokio::task; // or async_std::task /// @@ -118,8 +117,8 @@ impl + Borrow> AsyncPerfEventArray { } impl> AsyncPerfEventArray { - pub(crate) fn new(map: T) -> Result, MapError> { - Ok(AsyncPerfEventArray { + pub(crate) fn new(map: T) -> Result { + Ok(Self { perf_map: PerfEventArray::new(map)?, }) } diff --git a/aya/src/maps/perf/perf_buffer.rs b/aya/src/maps/perf/perf_buffer.rs index 2a5bfe3d..898dd04b 100644 --- a/aya/src/maps/perf/perf_buffer.rs +++ b/aya/src/maps/perf/perf_buffer.rs @@ -96,7 +96,7 @@ impl PerfBuffer { cpu_id: u32, page_size: usize, page_count: usize, - ) -> Result { + ) -> Result { if !page_count.is_power_of_two() { return Err(PerfBufferError::InvalidPageCount { page_count }); } @@ -120,7 +120,7 @@ impl PerfBuffer { }); } - let perf_buf = PerfBuffer { + let perf_buf = Self { buf: AtomicPtr::new(buf as *mut perf_event_mmap_page), fd, size, @@ -305,15 +305,15 @@ unsafe fn mmap( #[repr(C)] struct Sample { header: perf_event_header, - pub size: u32, + size: u32, } #[repr(C)] #[derive(Debug)] struct LostSamples { header: perf_event_header, - pub id: u64, - pub count: u64, + id: u64, + count: u64, } #[cfg(test)] diff --git a/aya/src/maps/perf/perf_event_array.rs b/aya/src/maps/perf/perf_event_array.rs index 7d647164..bde255ed 100644 --- a/aya/src/maps/perf/perf_event_array.rs +++ b/aya/src/maps/perf/perf_event_array.rs @@ -161,8 +161,8 @@ pub struct PerfEventArray { } impl> PerfEventArray { - pub(crate) fn new(map: T) -> Result, MapError> { - Ok(PerfEventArray { + pub(crate) fn new(map: T) -> Result { + Ok(Self { map: Arc::new(map), page_size: page_size(), }) diff --git a/aya/src/maps/queue.rs b/aya/src/maps/queue.rs index 78b81ec2..6eebbf01 100644 --- a/aya/src/maps/queue.rs +++ b/aya/src/maps/queue.rs @@ -34,11 +34,11 @@ pub struct Queue { } impl, V: Pod> Queue { - pub(crate) fn new(map: T) -> Result, MapError> { + pub(crate) fn new(map: T) -> Result { let data = map.borrow(); check_kv_size::<(), V>(data)?; - Ok(Queue { + Ok(Self { inner: map, _v: PhantomData, }) diff --git a/aya/src/maps/sock/sock_hash.rs b/aya/src/maps/sock/sock_hash.rs index 1904ec1e..503dcaae 100644 --- a/aya/src/maps/sock/sock_hash.rs +++ b/aya/src/maps/sock/sock_hash.rs @@ -69,11 +69,11 @@ pub struct SockHash { } impl, K: Pod> SockHash { - pub(crate) fn new(map: T) -> Result, MapError> { + pub(crate) fn new(map: T) -> Result { let data = map.borrow(); check_kv_size::(data)?; - Ok(SockHash { + Ok(Self { inner: map, _k: PhantomData, }) @@ -138,6 +138,6 @@ impl, K: Pod> IterableMap for SockHash { } fn get(&self, key: &K) -> Result { - SockHash::get(self, key, 0) + Self::get(self, key, 0) } } diff --git a/aya/src/maps/sock/sock_map.rs b/aya/src/maps/sock/sock_map.rs index 52574f23..8194cd71 100644 --- a/aya/src/maps/sock/sock_map.rs +++ b/aya/src/maps/sock/sock_map.rs @@ -45,11 +45,11 @@ pub struct SockMap { } impl> SockMap { - pub(crate) fn new(map: T) -> Result, MapError> { + pub(crate) fn new(map: T) -> Result { let data = map.borrow(); check_kv_size::(data)?; - Ok(SockMap { inner: map }) + Ok(Self { inner: map }) } /// An iterator over the indices of the array that point to a program. The iterator item type diff --git a/aya/src/maps/stack.rs b/aya/src/maps/stack.rs index 0cc9b1d8..c4afeebb 100644 --- a/aya/src/maps/stack.rs +++ b/aya/src/maps/stack.rs @@ -34,11 +34,11 @@ pub struct Stack { } impl, V: Pod> Stack { - pub(crate) fn new(map: T) -> Result, MapError> { + pub(crate) fn new(map: T) -> Result { let data = map.borrow(); check_kv_size::<(), V>(data)?; - Ok(Stack { + Ok(Self { inner: map, _v: PhantomData, }) diff --git a/aya/src/maps/stack_trace.rs b/aya/src/maps/stack_trace.rs index 8f84a09e..07281a2b 100644 --- a/aya/src/maps/stack_trace.rs +++ b/aya/src/maps/stack_trace.rs @@ -72,7 +72,7 @@ pub struct StackTraceMap { } impl> StackTraceMap { - pub(crate) fn new(map: T) -> Result, MapError> { + pub(crate) fn new(map: T) -> Result { let data = map.borrow(); let expected = mem::size_of::(); let size = data.obj.key_size() as usize; @@ -90,7 +90,7 @@ impl> StackTraceMap { return Err(MapError::InvalidValueSize { size, expected }); } - Ok(StackTraceMap { + Ok(Self { inner: map, max_stack_depth, }) diff --git a/aya/src/programs/cgroup_device.rs b/aya/src/programs/cgroup_device.rs index 1244d27c..42ce1e17 100644 --- a/aya/src/programs/cgroup_device.rs +++ b/aya/src/programs/cgroup_device.rs @@ -129,15 +129,15 @@ impl Link for CgroupDeviceLinkInner { fn id(&self) -> Self::Id { match self { - CgroupDeviceLinkInner::Fd(fd) => CgroupDeviceLinkIdInner::Fd(fd.id()), - CgroupDeviceLinkInner::ProgAttach(p) => CgroupDeviceLinkIdInner::ProgAttach(p.id()), + Self::Fd(fd) => CgroupDeviceLinkIdInner::Fd(fd.id()), + Self::ProgAttach(p) => CgroupDeviceLinkIdInner::ProgAttach(p.id()), } } fn detach(self) -> Result<(), ProgramError> { match self { - CgroupDeviceLinkInner::Fd(fd) => fd.detach(), - CgroupDeviceLinkInner::ProgAttach(p) => p.detach(), + Self::Fd(fd) => fd.detach(), + Self::ProgAttach(p) => p.detach(), } } } diff --git a/aya/src/programs/cgroup_skb.rs b/aya/src/programs/cgroup_skb.rs index 684fd18e..4b815c15 100644 --- a/aya/src/programs/cgroup_skb.rs +++ b/aya/src/programs/cgroup_skb.rs @@ -179,15 +179,15 @@ impl Link for CgroupSkbLinkInner { fn id(&self) -> Self::Id { match self { - CgroupSkbLinkInner::Fd(fd) => CgroupSkbLinkIdInner::Fd(fd.id()), - CgroupSkbLinkInner::ProgAttach(p) => CgroupSkbLinkIdInner::ProgAttach(p.id()), + Self::Fd(fd) => CgroupSkbLinkIdInner::Fd(fd.id()), + Self::ProgAttach(p) => CgroupSkbLinkIdInner::ProgAttach(p.id()), } } fn detach(self) -> Result<(), ProgramError> { match self { - CgroupSkbLinkInner::Fd(fd) => fd.detach(), - CgroupSkbLinkInner::ProgAttach(p) => p.detach(), + Self::Fd(fd) => fd.detach(), + Self::ProgAttach(p) => p.detach(), } } } diff --git a/aya/src/programs/cgroup_sock.rs b/aya/src/programs/cgroup_sock.rs index c906ee6c..57dfeaf0 100644 --- a/aya/src/programs/cgroup_sock.rs +++ b/aya/src/programs/cgroup_sock.rs @@ -151,15 +151,15 @@ impl Link for CgroupSockLinkInner { fn id(&self) -> Self::Id { match self { - CgroupSockLinkInner::Fd(fd) => CgroupSockLinkIdInner::Fd(fd.id()), - CgroupSockLinkInner::ProgAttach(p) => CgroupSockLinkIdInner::ProgAttach(p.id()), + Self::Fd(fd) => CgroupSockLinkIdInner::Fd(fd.id()), + Self::ProgAttach(p) => CgroupSockLinkIdInner::ProgAttach(p.id()), } } fn detach(self) -> Result<(), ProgramError> { match self { - CgroupSockLinkInner::Fd(fd) => fd.detach(), - CgroupSockLinkInner::ProgAttach(p) => p.detach(), + Self::Fd(fd) => fd.detach(), + Self::ProgAttach(p) => p.detach(), } } } diff --git a/aya/src/programs/cgroup_sock_addr.rs b/aya/src/programs/cgroup_sock_addr.rs index 98d60eb1..2c2b4ac9 100644 --- a/aya/src/programs/cgroup_sock_addr.rs +++ b/aya/src/programs/cgroup_sock_addr.rs @@ -157,15 +157,15 @@ impl Link for CgroupSockAddrLinkInner { fn id(&self) -> Self::Id { match self { - CgroupSockAddrLinkInner::Fd(fd) => CgroupSockAddrLinkIdInner::Fd(fd.id()), - CgroupSockAddrLinkInner::ProgAttach(p) => CgroupSockAddrLinkIdInner::ProgAttach(p.id()), + Self::Fd(fd) => CgroupSockAddrLinkIdInner::Fd(fd.id()), + Self::ProgAttach(p) => CgroupSockAddrLinkIdInner::ProgAttach(p.id()), } } fn detach(self) -> Result<(), ProgramError> { match self { - CgroupSockAddrLinkInner::Fd(fd) => fd.detach(), - CgroupSockAddrLinkInner::ProgAttach(p) => p.detach(), + Self::Fd(fd) => fd.detach(), + Self::ProgAttach(p) => p.detach(), } } } diff --git a/aya/src/programs/cgroup_sockopt.rs b/aya/src/programs/cgroup_sockopt.rs index c137d769..cbe8302e 100644 --- a/aya/src/programs/cgroup_sockopt.rs +++ b/aya/src/programs/cgroup_sockopt.rs @@ -152,15 +152,15 @@ impl Link for CgroupSockoptLinkInner { fn id(&self) -> Self::Id { match self { - CgroupSockoptLinkInner::Fd(fd) => CgroupSockoptLinkIdInner::Fd(fd.id()), - CgroupSockoptLinkInner::ProgAttach(p) => CgroupSockoptLinkIdInner::ProgAttach(p.id()), + Self::Fd(fd) => CgroupSockoptLinkIdInner::Fd(fd.id()), + Self::ProgAttach(p) => CgroupSockoptLinkIdInner::ProgAttach(p.id()), } } fn detach(self) -> Result<(), ProgramError> { match self { - CgroupSockoptLinkInner::Fd(fd) => fd.detach(), - CgroupSockoptLinkInner::ProgAttach(p) => p.detach(), + Self::Fd(fd) => fd.detach(), + Self::ProgAttach(p) => p.detach(), } } } diff --git a/aya/src/programs/cgroup_sysctl.rs b/aya/src/programs/cgroup_sysctl.rs index d5325089..a1b158d7 100644 --- a/aya/src/programs/cgroup_sysctl.rs +++ b/aya/src/programs/cgroup_sysctl.rs @@ -132,15 +132,15 @@ impl Link for CgroupSysctlLinkInner { fn id(&self) -> Self::Id { match self { - CgroupSysctlLinkInner::Fd(fd) => CgroupSysctlLinkIdInner::Fd(fd.id()), - CgroupSysctlLinkInner::ProgAttach(p) => CgroupSysctlLinkIdInner::ProgAttach(p.id()), + Self::Fd(fd) => CgroupSysctlLinkIdInner::Fd(fd.id()), + Self::ProgAttach(p) => CgroupSysctlLinkIdInner::ProgAttach(p.id()), } } fn detach(self) -> Result<(), ProgramError> { match self { - CgroupSysctlLinkInner::Fd(fd) => fd.detach(), - CgroupSysctlLinkInner::ProgAttach(p) => p.detach(), + Self::Fd(fd) => fd.detach(), + Self::ProgAttach(p) => p.detach(), } } } diff --git a/aya/src/programs/kprobe.rs b/aya/src/programs/kprobe.rs index f9cbb667..a0c8d2b4 100644 --- a/aya/src/programs/kprobe.rs +++ b/aya/src/programs/kprobe.rs @@ -139,7 +139,7 @@ impl TryFrom for KProbeLink { fn try_from(fd_link: FdLink) -> Result { let info = bpf_link_get_info_by_fd(fd_link.fd.as_fd())?; if info.type_ == (bpf_link_type::BPF_LINK_TYPE_KPROBE_MULTI as u32) { - return Ok(KProbeLink::new(PerfLinkInner::FdLink(fd_link))); + return Ok(Self::new(PerfLinkInner::FdLink(fd_link))); } Err(LinkError::InvalidLink) } diff --git a/aya/src/programs/links.rs b/aya/src/programs/links.rs index 52aa60de..77341adc 100644 --- a/aya/src/programs/links.rs +++ b/aya/src/programs/links.rs @@ -35,8 +35,8 @@ pub(crate) struct LinkMap { } impl LinkMap { - pub(crate) fn new() -> LinkMap { - LinkMap { + pub(crate) fn new() -> Self { + Self { links: HashMap::new(), } } @@ -112,8 +112,8 @@ pub struct FdLink { } impl FdLink { - pub(crate) fn new(fd: OwnedFd) -> FdLink { - FdLink { fd } + pub(crate) fn new(fd: OwnedFd) -> Self { + Self { fd } } /// Pins the link to a BPF file system. @@ -198,7 +198,7 @@ pub struct PinnedLink { impl PinnedLink { fn new(path: PathBuf, link: FdLink) -> Self { - PinnedLink { inner: link, path } + Self { inner: link, path } } /// Creates a [`crate::programs::links::PinnedLink`] from a valid path on bpffs. @@ -210,10 +210,7 @@ impl PinnedLink { io_error, }) })?; - Ok(PinnedLink::new( - path.as_ref().to_path_buf(), - FdLink::new(fd), - )) + Ok(Self::new(path.as_ref().to_path_buf(), FdLink::new(fd))) } /// Removes the pinned link from the filesystem and returns an [`FdLink`]. @@ -236,12 +233,8 @@ pub struct ProgAttachLink { } impl ProgAttachLink { - pub(crate) fn new( - prog_fd: RawFd, - target_fd: RawFd, - attach_type: bpf_attach_type, - ) -> ProgAttachLink { - ProgAttachLink { + pub(crate) fn new(prog_fd: RawFd, target_fd: RawFd, attach_type: bpf_attach_type) -> Self { + Self { prog_fd, target_fd: unsafe { dup(target_fd) }, attach_type, @@ -300,7 +293,7 @@ macro_rules! define_link_wrapper { } } - impl crate::programs::Link for $wrapper { + impl $crate::programs::Link for $wrapper { type Id = $wrapper_id; fn id(&self) -> Self::Id { @@ -359,8 +352,8 @@ mod tests { } impl TestLink { - fn new(a: u8, b: u8) -> TestLink { - TestLink { + fn new(a: u8, b: u8) -> Self { + Self { id: (a, b), detached: Rc::new(RefCell::new(0)), } diff --git a/aya/src/programs/lirc_mode2.rs b/aya/src/programs/lirc_mode2.rs index 906eec96..0d34bfce 100644 --- a/aya/src/programs/lirc_mode2.rs +++ b/aya/src/programs/lirc_mode2.rs @@ -121,8 +121,8 @@ pub struct LircLink { } impl LircLink { - pub(crate) fn new(prog_fd: RawFd, target_fd: RawFd) -> LircLink { - LircLink { + pub(crate) fn new(prog_fd: RawFd, target_fd: RawFd) -> Self { + Self { prog_fd, target_fd: unsafe { dup(target_fd) }, } diff --git a/aya/src/programs/mod.rs b/aya/src/programs/mod.rs index b79627a8..dcec8bf3 100644 --- a/aya/src/programs/mod.rs +++ b/aya/src/programs/mod.rs @@ -108,7 +108,7 @@ pub use 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, Function, VerifierLog}, + obj::{self, btf::BtfError, VerifierLog}, pin::PinError, programs::utils::{boot_time, get_fdinfo}, sys::{ @@ -292,90 +292,90 @@ impl Program { pub fn prog_type(&self) -> bpf_prog_type { use crate::generated::bpf_prog_type::*; match self { - Program::KProbe(_) => BPF_PROG_TYPE_KPROBE, - Program::UProbe(_) => BPF_PROG_TYPE_KPROBE, - Program::TracePoint(_) => BPF_PROG_TYPE_TRACEPOINT, - Program::SocketFilter(_) => BPF_PROG_TYPE_SOCKET_FILTER, - Program::Xdp(_) => BPF_PROG_TYPE_XDP, - Program::SkMsg(_) => BPF_PROG_TYPE_SK_MSG, - Program::SkSkb(_) => BPF_PROG_TYPE_SK_SKB, - Program::SockOps(_) => BPF_PROG_TYPE_SOCK_OPS, - Program::SchedClassifier(_) => BPF_PROG_TYPE_SCHED_CLS, - Program::CgroupSkb(_) => BPF_PROG_TYPE_CGROUP_SKB, - Program::CgroupSysctl(_) => BPF_PROG_TYPE_CGROUP_SYSCTL, - Program::CgroupSockopt(_) => BPF_PROG_TYPE_CGROUP_SOCKOPT, - Program::LircMode2(_) => BPF_PROG_TYPE_LIRC_MODE2, - Program::PerfEvent(_) => BPF_PROG_TYPE_PERF_EVENT, - Program::RawTracePoint(_) => BPF_PROG_TYPE_RAW_TRACEPOINT, - Program::Lsm(_) => BPF_PROG_TYPE_LSM, - Program::BtfTracePoint(_) => BPF_PROG_TYPE_TRACING, - Program::FEntry(_) => BPF_PROG_TYPE_TRACING, - Program::FExit(_) => BPF_PROG_TYPE_TRACING, - Program::Extension(_) => BPF_PROG_TYPE_EXT, - Program::CgroupSockAddr(_) => BPF_PROG_TYPE_CGROUP_SOCK_ADDR, - Program::SkLookup(_) => BPF_PROG_TYPE_SK_LOOKUP, - Program::CgroupSock(_) => BPF_PROG_TYPE_CGROUP_SOCK, - Program::CgroupDevice(_) => BPF_PROG_TYPE_CGROUP_DEVICE, + Self::KProbe(_) => BPF_PROG_TYPE_KPROBE, + Self::UProbe(_) => BPF_PROG_TYPE_KPROBE, + Self::TracePoint(_) => BPF_PROG_TYPE_TRACEPOINT, + Self::SocketFilter(_) => BPF_PROG_TYPE_SOCKET_FILTER, + Self::Xdp(_) => BPF_PROG_TYPE_XDP, + Self::SkMsg(_) => BPF_PROG_TYPE_SK_MSG, + Self::SkSkb(_) => BPF_PROG_TYPE_SK_SKB, + Self::SockOps(_) => BPF_PROG_TYPE_SOCK_OPS, + Self::SchedClassifier(_) => BPF_PROG_TYPE_SCHED_CLS, + Self::CgroupSkb(_) => BPF_PROG_TYPE_CGROUP_SKB, + Self::CgroupSysctl(_) => BPF_PROG_TYPE_CGROUP_SYSCTL, + Self::CgroupSockopt(_) => BPF_PROG_TYPE_CGROUP_SOCKOPT, + Self::LircMode2(_) => BPF_PROG_TYPE_LIRC_MODE2, + Self::PerfEvent(_) => BPF_PROG_TYPE_PERF_EVENT, + Self::RawTracePoint(_) => BPF_PROG_TYPE_RAW_TRACEPOINT, + Self::Lsm(_) => BPF_PROG_TYPE_LSM, + Self::BtfTracePoint(_) => BPF_PROG_TYPE_TRACING, + Self::FEntry(_) => BPF_PROG_TYPE_TRACING, + Self::FExit(_) => BPF_PROG_TYPE_TRACING, + Self::Extension(_) => BPF_PROG_TYPE_EXT, + Self::CgroupSockAddr(_) => BPF_PROG_TYPE_CGROUP_SOCK_ADDR, + Self::SkLookup(_) => BPF_PROG_TYPE_SK_LOOKUP, + Self::CgroupSock(_) => BPF_PROG_TYPE_CGROUP_SOCK, + Self::CgroupDevice(_) => BPF_PROG_TYPE_CGROUP_DEVICE, } } /// Pin the program to the provided path pub fn pin>(&mut self, path: P) -> Result<(), PinError> { match self { - Program::KProbe(p) => p.pin(path), - Program::UProbe(p) => p.pin(path), - Program::TracePoint(p) => p.pin(path), - Program::SocketFilter(p) => p.pin(path), - Program::Xdp(p) => p.pin(path), - Program::SkMsg(p) => p.pin(path), - Program::SkSkb(p) => p.pin(path), - Program::SockOps(p) => p.pin(path), - Program::SchedClassifier(p) => p.pin(path), - Program::CgroupSkb(p) => p.pin(path), - Program::CgroupSysctl(p) => p.pin(path), - Program::CgroupSockopt(p) => p.pin(path), - Program::LircMode2(p) => p.pin(path), - Program::PerfEvent(p) => p.pin(path), - Program::RawTracePoint(p) => p.pin(path), - Program::Lsm(p) => p.pin(path), - Program::BtfTracePoint(p) => p.pin(path), - Program::FEntry(p) => p.pin(path), - Program::FExit(p) => p.pin(path), - Program::Extension(p) => p.pin(path), - Program::CgroupSockAddr(p) => p.pin(path), - Program::SkLookup(p) => p.pin(path), - Program::CgroupSock(p) => p.pin(path), - Program::CgroupDevice(p) => p.pin(path), + Self::KProbe(p) => p.pin(path), + Self::UProbe(p) => p.pin(path), + Self::TracePoint(p) => p.pin(path), + Self::SocketFilter(p) => p.pin(path), + Self::Xdp(p) => p.pin(path), + Self::SkMsg(p) => p.pin(path), + Self::SkSkb(p) => p.pin(path), + Self::SockOps(p) => p.pin(path), + Self::SchedClassifier(p) => p.pin(path), + Self::CgroupSkb(p) => p.pin(path), + Self::CgroupSysctl(p) => p.pin(path), + Self::CgroupSockopt(p) => p.pin(path), + Self::LircMode2(p) => p.pin(path), + Self::PerfEvent(p) => p.pin(path), + Self::RawTracePoint(p) => p.pin(path), + Self::Lsm(p) => p.pin(path), + Self::BtfTracePoint(p) => p.pin(path), + Self::FEntry(p) => p.pin(path), + Self::FExit(p) => p.pin(path), + Self::Extension(p) => p.pin(path), + Self::CgroupSockAddr(p) => p.pin(path), + Self::SkLookup(p) => p.pin(path), + Self::CgroupSock(p) => p.pin(path), + Self::CgroupDevice(p) => p.pin(path), } } /// Unloads the program from the kernel. pub fn unload(self) -> Result<(), ProgramError> { match self { - Program::KProbe(mut p) => p.unload(), - Program::UProbe(mut p) => p.unload(), - Program::TracePoint(mut p) => p.unload(), - Program::SocketFilter(mut p) => p.unload(), - Program::Xdp(mut p) => p.unload(), - Program::SkMsg(mut p) => p.unload(), - Program::SkSkb(mut p) => p.unload(), - Program::SockOps(mut p) => p.unload(), - Program::SchedClassifier(mut p) => p.unload(), - Program::CgroupSkb(mut p) => p.unload(), - Program::CgroupSysctl(mut p) => p.unload(), - Program::CgroupSockopt(mut p) => p.unload(), - Program::LircMode2(mut p) => p.unload(), - Program::PerfEvent(mut p) => p.unload(), - Program::RawTracePoint(mut p) => p.unload(), - Program::Lsm(mut p) => p.unload(), - Program::BtfTracePoint(mut p) => p.unload(), - Program::FEntry(mut p) => p.unload(), - Program::FExit(mut p) => p.unload(), - Program::Extension(mut p) => p.unload(), - Program::CgroupSockAddr(mut p) => p.unload(), - Program::SkLookup(mut p) => p.unload(), - Program::CgroupSock(mut p) => p.unload(), - Program::CgroupDevice(mut p) => p.unload(), + Self::KProbe(mut p) => p.unload(), + Self::UProbe(mut p) => p.unload(), + Self::TracePoint(mut p) => p.unload(), + Self::SocketFilter(mut p) => p.unload(), + Self::Xdp(mut p) => p.unload(), + Self::SkMsg(mut p) => p.unload(), + Self::SkSkb(mut p) => p.unload(), + Self::SockOps(mut p) => p.unload(), + Self::SchedClassifier(mut p) => p.unload(), + Self::CgroupSkb(mut p) => p.unload(), + Self::CgroupSysctl(mut p) => p.unload(), + Self::CgroupSockopt(mut p) => p.unload(), + Self::LircMode2(mut p) => p.unload(), + Self::PerfEvent(mut p) => p.unload(), + Self::RawTracePoint(mut p) => p.unload(), + Self::Lsm(mut p) => p.unload(), + Self::BtfTracePoint(mut p) => p.unload(), + Self::FEntry(mut p) => p.unload(), + Self::FExit(mut p) => p.unload(), + Self::Extension(mut p) => p.unload(), + Self::CgroupSockAddr(mut p) => p.unload(), + Self::SkLookup(mut p) => p.unload(), + Self::CgroupSock(mut p) => p.unload(), + Self::CgroupDevice(mut p) => p.unload(), } } @@ -384,30 +384,30 @@ impl Program { /// Can be used to add a program to a [`crate::maps::ProgramArray`] or attach an [`Extension`] program. pub fn fd(&self) -> Result<&ProgramFd, ProgramError> { match self { - Program::KProbe(p) => p.fd(), - Program::UProbe(p) => p.fd(), - Program::TracePoint(p) => p.fd(), - Program::SocketFilter(p) => p.fd(), - Program::Xdp(p) => p.fd(), - Program::SkMsg(p) => p.fd(), - Program::SkSkb(p) => p.fd(), - Program::SockOps(p) => p.fd(), - Program::SchedClassifier(p) => p.fd(), - Program::CgroupSkb(p) => p.fd(), - Program::CgroupSysctl(p) => p.fd(), - Program::CgroupSockopt(p) => p.fd(), - Program::LircMode2(p) => p.fd(), - Program::PerfEvent(p) => p.fd(), - Program::RawTracePoint(p) => p.fd(), - Program::Lsm(p) => p.fd(), - Program::BtfTracePoint(p) => p.fd(), - Program::FEntry(p) => p.fd(), - Program::FExit(p) => p.fd(), - Program::Extension(p) => p.fd(), - Program::CgroupSockAddr(p) => p.fd(), - Program::SkLookup(p) => p.fd(), - Program::CgroupSock(p) => p.fd(), - Program::CgroupDevice(p) => p.fd(), + Self::KProbe(p) => p.fd(), + Self::UProbe(p) => p.fd(), + Self::TracePoint(p) => p.fd(), + Self::SocketFilter(p) => p.fd(), + Self::Xdp(p) => p.fd(), + Self::SkMsg(p) => p.fd(), + Self::SkSkb(p) => p.fd(), + Self::SockOps(p) => p.fd(), + Self::SchedClassifier(p) => p.fd(), + Self::CgroupSkb(p) => p.fd(), + Self::CgroupSysctl(p) => p.fd(), + Self::CgroupSockopt(p) => p.fd(), + Self::LircMode2(p) => p.fd(), + Self::PerfEvent(p) => p.fd(), + Self::RawTracePoint(p) => p.fd(), + Self::Lsm(p) => p.fd(), + Self::BtfTracePoint(p) => p.fd(), + Self::FEntry(p) => p.fd(), + Self::FExit(p) => p.fd(), + Self::Extension(p) => p.fd(), + Self::CgroupSockAddr(p) => p.fd(), + Self::SkLookup(p) => p.fd(), + Self::CgroupSock(p) => p.fd(), + Self::CgroupDevice(p) => p.fd(), } } } @@ -434,8 +434,8 @@ impl ProgramData { obj: (obj::Program, obj::Function), btf_fd: Option>, verifier_log_level: VerifierLogLevel, - ) -> ProgramData { - ProgramData { + ) -> Self { + Self { name, obj: Some(obj), fd: None, @@ -457,7 +457,7 @@ impl ProgramData { path: &Path, info: bpf_prog_info, verifier_log_level: VerifierLogLevel, - ) -> Result, ProgramError> { + ) -> Result { let attach_btf_id = if info.attach_btf_id > 0 { Some(info.attach_btf_id) } else { @@ -467,7 +467,7 @@ impl ProgramData { .then(|| bpf_btf_get_fd_by_id(info.attach_btf_obj_id)) .transpose()?; - Ok(ProgramData { + Ok(Self { name, obj: None, fd: Some(ProgramFd(fd)), @@ -486,7 +486,7 @@ impl ProgramData { pub(crate) fn from_pinned_path>( path: P, verifier_log_level: VerifierLogLevel, - ) -> Result, ProgramError> { + ) -> Result { let path_string = CString::new(path.as_ref().as_os_str().to_string_lossy().as_bytes()).unwrap(); @@ -497,7 +497,7 @@ impl ProgramData { let info = ProgramInfo::new_from_fd(fd.as_fd())?; let name = info.name_as_str().map(|s| s.to_string()); - ProgramData::from_bpf_prog_info(name, fd, path.as_ref(), info.0, verifier_log_level) + Self::from_bpf_prog_info(name, fd, path.as_ref(), info.0, verifier_log_level) } } @@ -571,7 +571,7 @@ fn load_program( kernel_version, .. }, - Function { + obj::Function { instructions, func_info, line_info, @@ -1072,7 +1072,7 @@ impl ProgramInfo { } /// Loads a program from a pinned path in bpffs. - pub fn from_pin>(path: P) -> Result { + pub fn from_pin>(path: P) -> Result { let path_string = CString::new(path.as_ref().to_str().unwrap()).unwrap(); let fd = bpf_get_object(&path_string).map_err(|(_, io_error)| SyscallError { call: "BPF_OBJ_GET", @@ -1080,7 +1080,7 @@ impl ProgramInfo { })?; let info = bpf_prog_get_info_by_fd(fd.as_fd(), &mut [])?; - Ok(ProgramInfo(info)) + Ok(Self(info)) } } diff --git a/aya/src/programs/perf_attach.rs b/aya/src/programs/perf_attach.rs index e9be37d5..dd2f8831 100644 --- a/aya/src/programs/perf_attach.rs +++ b/aya/src/programs/perf_attach.rs @@ -28,15 +28,15 @@ impl Link for PerfLinkInner { fn id(&self) -> Self::Id { match self { - PerfLinkInner::FdLink(link) => PerfLinkIdInner::FdLinkId(link.id()), - PerfLinkInner::PerfLink(link) => PerfLinkIdInner::PerfLinkId(link.id()), + Self::FdLink(link) => PerfLinkIdInner::FdLinkId(link.id()), + Self::PerfLink(link) => PerfLinkIdInner::PerfLinkId(link.id()), } } fn detach(self) -> Result<(), ProgramError> { match self { - PerfLinkInner::FdLink(link) => link.detach(), - PerfLinkInner::PerfLink(link) => link.detach(), + Self::FdLink(link) => link.detach(), + Self::PerfLink(link) => link.detach(), } } } diff --git a/aya/src/programs/perf_event.rs b/aya/src/programs/perf_event.rs index bb60f082..36398967 100644 --- a/aya/src/programs/perf_event.rs +++ b/aya/src/programs/perf_event.rs @@ -213,7 +213,7 @@ impl TryFrom for PerfEventLink { fn try_from(fd_link: FdLink) -> Result { let info = bpf_link_get_info_by_fd(fd_link.fd.as_fd())?; if info.type_ == (bpf_link_type::BPF_LINK_TYPE_PERF_EVENT as u32) { - return Ok(PerfEventLink::new(PerfLinkInner::FdLink(fd_link))); + return Ok(Self::new(PerfLinkInner::FdLink(fd_link))); } Err(LinkError::InvalidLink) } diff --git a/aya/src/programs/probe.rs b/aya/src/programs/probe.rs index 151c87ef..bff48612 100644 --- a/aya/src/programs/probe.rs +++ b/aya/src/programs/probe.rs @@ -36,8 +36,8 @@ pub enum ProbeKind { impl ProbeKind { fn pmu(&self) -> &'static str { match *self { - ProbeKind::KProbe | ProbeKind::KRetProbe => "kprobe", - ProbeKind::UProbe | ProbeKind::URetProbe => "uprobe", + Self::KProbe | Self::KRetProbe => "kprobe", + Self::UProbe | Self::URetProbe => "uprobe", } } } diff --git a/aya/src/programs/tc.rs b/aya/src/programs/tc.rs index e289e7d0..59a32ab2 100644 --- a/aya/src/programs/tc.rs +++ b/aya/src/programs/tc.rs @@ -95,9 +95,9 @@ pub enum TcError { impl TcAttachType { pub(crate) fn parent(&self) -> u32 { match self { - TcAttachType::Custom(parent) => *parent, - TcAttachType::Ingress => tc_handler_make(TC_H_CLSACT, TC_H_MIN_INGRESS), - TcAttachType::Egress => tc_handler_make(TC_H_CLSACT, TC_H_MIN_EGRESS), + Self::Custom(parent) => *parent, + Self::Ingress => tc_handler_make(TC_H_CLSACT, TC_H_MIN_INGRESS), + Self::Egress => tc_handler_make(TC_H_CLSACT, TC_H_MIN_EGRESS), } } } @@ -284,9 +284,9 @@ impl SchedClassifierLink { attach_type: TcAttachType, priority: u16, handle: u32, - ) -> Result { + ) -> Result { let if_index = ifindex_from_ifname(if_name)?; - Ok(SchedClassifierLink(Some(TcLink { + Ok(Self(Some(TcLink { if_index: if_index as i32, attach_type, priority, diff --git a/aya/src/programs/trace_point.rs b/aya/src/programs/trace_point.rs index 4c5d38db..8ed34eb4 100644 --- a/aya/src/programs/trace_point.rs +++ b/aya/src/programs/trace_point.rs @@ -140,7 +140,7 @@ impl TryFrom for TracePointLink { fn try_from(fd_link: FdLink) -> Result { let info = bpf_link_get_info_by_fd(fd_link.fd.as_fd())?; if info.type_ == (bpf_link_type::BPF_LINK_TYPE_TRACING as u32) { - return Ok(TracePointLink::new(PerfLinkInner::FdLink(fd_link))); + return Ok(Self::new(PerfLinkInner::FdLink(fd_link))); } Err(LinkError::InvalidLink) } diff --git a/aya/src/programs/uprobe.rs b/aya/src/programs/uprobe.rs index e40bada2..5e30d174 100644 --- a/aya/src/programs/uprobe.rs +++ b/aya/src/programs/uprobe.rs @@ -28,7 +28,7 @@ use crate::{ const LD_SO_CACHE_FILE: &str = "/etc/ld.so.cache"; -lazy_static! { +lazy_static::lazy_static! { static ref LD_SO_CACHE: Result> = LdSoCache::load(LD_SO_CACHE_FILE).map_err(Arc::new); } @@ -204,7 +204,7 @@ impl TryFrom for UProbeLink { fn try_from(fd_link: FdLink) -> Result { let info = bpf_link_get_info_by_fd(fd_link.fd.as_fd())?; if info.type_ == (bpf_link_type::BPF_LINK_TYPE_TRACING as u32) { - return Ok(UProbeLink::new(PerfLinkInner::FdLink(fd_link))); + return Ok(Self::new(PerfLinkInner::FdLink(fd_link))); } Err(LinkError::InvalidLink) } @@ -297,7 +297,7 @@ pub(crate) struct LdSoCache { } impl LdSoCache { - pub fn load>(path: T) -> Result { + fn load>(path: T) -> Result { let data = fs::read(path)?; Self::parse(&data) } @@ -386,10 +386,10 @@ impl LdSoCache { }) .collect::>()?; - Ok(LdSoCache { entries }) + Ok(Self { entries }) } - pub fn resolve(&self, lib: &str) -> Option<&str> { + fn resolve(&self, lib: &str) -> Option<&str> { let lib = if !lib.contains(".so") { lib.to_string() + ".so" } else { diff --git a/aya/src/programs/utils.rs b/aya/src/programs/utils.rs index beb9b5f5..c4e2e618 100644 --- a/aya/src/programs/utils.rs +++ b/aya/src/programs/utils.rs @@ -79,7 +79,7 @@ pub(crate) fn boot_time() -> SystemTime { } /// Get the specified information from a file descriptor's fdinfo. -pub(crate) fn get_fdinfo(fd: BorrowedFd, key: &str) -> Result { +pub(crate) fn get_fdinfo(fd: BorrowedFd<'_>, key: &str) -> Result { let info = File::open(format!("/proc/self/fdinfo/{}", fd.as_raw_fd()))?; let reader = BufReader::new(info); for line in reader.lines() { diff --git a/aya/src/programs/xdp.rs b/aya/src/programs/xdp.rs index 36eec7d6..14db680c 100644 --- a/aya/src/programs/xdp.rs +++ b/aya/src/programs/xdp.rs @@ -14,11 +14,8 @@ use thiserror::Error; use crate::{ generated::{ - bpf_attach_type::{self, BPF_XDP}, - bpf_link_type, - bpf_prog_type::BPF_PROG_TYPE_XDP, - XDP_FLAGS_DRV_MODE, XDP_FLAGS_HW_MODE, XDP_FLAGS_REPLACE, XDP_FLAGS_SKB_MODE, - XDP_FLAGS_UPDATE_IF_NOEXIST, + bpf_attach_type, 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, }, programs::{ define_link_wrapper, load_program, FdLink, Link, LinkError, ProgramData, ProgramError, @@ -38,7 +35,7 @@ pub enum XdpError { }, } -bitflags! { +bitflags::bitflags! { /// Flags passed to [`Xdp::attach()`]. #[derive(Clone, Copy, Debug, Default)] pub struct XdpFlags: u32 { @@ -86,7 +83,7 @@ impl Xdp { /// Loads the program inside the kernel. pub fn load(&mut self) -> Result<(), ProgramError> { self.data.expected_attach_type = Some(bpf_attach_type::BPF_XDP); - load_program(BPF_PROG_TYPE_XDP, &mut self.data) + load_program(bpf_prog_type::BPF_PROG_TYPE_XDP, &mut self.data) } /// Attaches the program to the given `interface`. @@ -134,12 +131,17 @@ impl Xdp { let if_index = if_index as RawFd; if KernelVersion::current().unwrap() >= KernelVersion::new(5, 9, 0) { - let link_fd = bpf_link_create(prog_fd, if_index, BPF_XDP, None, flags.bits()).map_err( - |(_, io_error)| SyscallError { - call: "bpf_link_create", - io_error, - }, - )?; + let link_fd = bpf_link_create( + prog_fd, + if_index, + bpf_attach_type::BPF_XDP, + None, + flags.bits(), + ) + .map_err(|(_, io_error)| SyscallError { + call: "bpf_link_create", + io_error, + })?; self.data .links .insert(XdpLink::new(XdpLinkInner::FdLink(FdLink::new(link_fd)))) @@ -257,15 +259,15 @@ impl Link for XdpLinkInner { fn id(&self) -> Self::Id { match self { - XdpLinkInner::FdLink(link) => XdpLinkIdInner::FdLinkId(link.id()), - XdpLinkInner::NlLink(link) => XdpLinkIdInner::NlLinkId(link.id()), + Self::FdLink(link) => XdpLinkIdInner::FdLinkId(link.id()), + Self::NlLink(link) => XdpLinkIdInner::NlLinkId(link.id()), } } fn detach(self) -> Result<(), ProgramError> { match self { - XdpLinkInner::FdLink(link) => link.detach(), - XdpLinkInner::NlLink(link) => link.detach(), + Self::FdLink(link) => link.detach(), + Self::NlLink(link) => link.detach(), } } } @@ -289,7 +291,7 @@ impl TryFrom for XdpLink { // unwrap of fd_link.fd will not panic since it's only None when being dropped. let info = bpf_link_get_info_by_fd(fd_link.fd.as_fd())?; if info.type_ == (bpf_link_type::BPF_LINK_TYPE_XDP as u32) { - return Ok(XdpLink::new(XdpLinkInner::FdLink(fd_link))); + return Ok(Self::new(XdpLinkInner::FdLink(fd_link))); } Err(LinkError::InvalidLink) } diff --git a/aya/src/sys/bpf.rs b/aya/src/sys/bpf.rs index 23fcf8e3..e142c6c4 100644 --- a/aya/src/sys/bpf.rs +++ b/aya/src/sys/bpf.rs @@ -1,5 +1,5 @@ use std::{ - cmp::{self, min}, + cmp, ffi::{CStr, CString}, io, iter, mem::{self, MaybeUninit}, @@ -129,7 +129,7 @@ pub(crate) struct BpfLoadProgramAttrs<'a> { } pub(crate) fn bpf_load_program( - aya_attr: &BpfLoadProgramAttrs, + aya_attr: &BpfLoadProgramAttrs<'_>, log_buf: &mut [u8], verifier_log_level: VerifierLogLevel, ) -> SysResult { @@ -140,7 +140,7 @@ pub(crate) fn bpf_load_program( if let Some(prog_name) = &aya_attr.name { let mut name: [c_char; 16] = [0; 16]; let name_bytes = prog_name.to_bytes(); - let len = min(name.len(), name_bytes.len()); + let len = cmp::min(name.len(), name_bytes.len()); name[..len].copy_from_slice(unsafe { slice::from_raw_parts(name_bytes.as_ptr() as *const c_char, len) }); @@ -613,7 +613,7 @@ pub(crate) fn is_prog_name_supported() -> bool { let mut name: [c_char; 16] = [0; 16]; let cstring = CString::new("aya_name_check").unwrap(); let name_bytes = cstring.to_bytes(); - let len = min(name.len(), name_bytes.len()); + let len = cmp::min(name.len(), name_bytes.len()); name[..len].copy_from_slice(unsafe { slice::from_raw_parts(name_bytes.as_ptr() as *const c_char, len) }); @@ -980,7 +980,7 @@ pub(crate) fn retry_with_verifier_logs( f: impl Fn(&mut [u8]) -> SysResult, ) -> (SysResult, VerifierLog) { const MIN_LOG_BUF_SIZE: usize = 1024 * 10; - const MAX_LOG_BUF_SIZE: usize = (std::u32::MAX >> 8) as usize; + const MAX_LOG_BUF_SIZE: usize = (u32::MAX >> 8) as usize; let mut log_buf = Vec::new(); let mut retries = 0; diff --git a/aya/src/sys/fake.rs b/aya/src/sys/fake.rs index 6cac51b6..df2deece 100644 --- a/aya/src/sys/fake.rs +++ b/aya/src/sys/fake.rs @@ -4,7 +4,7 @@ use libc::c_void; use super::{SysResult, Syscall}; -type SyscallFn = unsafe fn(Syscall) -> SysResult; +type SyscallFn = unsafe fn(Syscall<'_>) -> SysResult; #[cfg(test)] thread_local! { @@ -13,11 +13,11 @@ thread_local! { } #[cfg(test)] -unsafe fn test_syscall(_call: Syscall) -> SysResult { +unsafe fn test_syscall(_call: Syscall<'_>) -> SysResult { Err((-1, io::Error::from_raw_os_error(libc::EINVAL))) } #[cfg(test)] -pub(crate) fn override_syscall(call: unsafe fn(Syscall) -> SysResult) { +pub(crate) fn override_syscall(call: unsafe fn(Syscall<'_>) -> SysResult) { TEST_SYSCALL.with(|test_impl| *test_impl.borrow_mut() = call); } diff --git a/aya/src/sys/mod.rs b/aya/src/sys/mod.rs index ef2a0edf..239e68a9 100644 --- a/aya/src/sys/mod.rs +++ b/aya/src/sys/mod.rs @@ -85,7 +85,7 @@ impl std::fmt::Debug for Syscall<'_> { } } -fn syscall(call: Syscall) -> SysResult { +fn syscall(call: Syscall<'_>) -> SysResult { #[cfg(test)] return TEST_SYSCALL.with(|test_impl| unsafe { test_impl.borrow()(call) }); @@ -103,7 +103,10 @@ fn syscall(call: Syscall) -> SysResult { flags, } => libc::syscall(SYS_perf_event_open, &attr, pid, cpu, group, flags), Syscall::PerfEventIoctl { fd, request, arg } => { - libc::ioctl(fd.as_raw_fd(), request.try_into().unwrap(), arg) as libc::c_long + let int = libc::ioctl(fd.as_raw_fd(), request.try_into().unwrap(), arg); + #[allow(trivial_numeric_casts)] + let int = int as c_long; + int } } } { diff --git a/aya/src/sys/netlink.rs b/aya/src/sys/netlink.rs index 1dfae70d..ac302f50 100644 --- a/aya/src/sys/netlink.rs +++ b/aya/src/sys/netlink.rs @@ -286,7 +286,7 @@ struct NetlinkSocket { } impl NetlinkSocket { - fn open() -> Result { + fn open() -> Result { // Safety: libc wrapper let sock = unsafe { socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE) }; if sock < 0 { @@ -315,7 +315,7 @@ impl NetlinkSocket { return Err(io::Error::last_os_error()); } - Ok(NetlinkSocket { + Ok(Self { sock, _nl_pid: addr.nl_pid, }) @@ -375,7 +375,7 @@ struct NetlinkMessage { } impl NetlinkMessage { - fn read(buf: &[u8]) -> Result { + fn read(buf: &[u8]) -> Result { if mem::size_of::() > buf.len() { return Err(io::Error::new( io::ErrorKind::Other, @@ -413,7 +413,7 @@ impl NetlinkMessage { (buf[data_offset..msg_len].to_vec(), None) }; - Ok(NetlinkMessage { + Ok(Self { header, data, error, @@ -443,8 +443,8 @@ struct NestedAttrs<'a> { } impl<'a> NestedAttrs<'a> { - fn new(buf: &mut [u8], top_attr_type: u16) -> NestedAttrs<'_> { - NestedAttrs { + fn new(buf: &'a mut [u8], top_attr_type: u16) -> Self { + Self { buf, top_attr_type, offset: NLA_HDR_LEN, @@ -528,8 +528,8 @@ struct NlAttrsIterator<'a> { } impl<'a> NlAttrsIterator<'a> { - fn new(attrs: &[u8]) -> NlAttrsIterator { - NlAttrsIterator { attrs, offset: 0 } + fn new(attrs: &'a [u8]) -> Self { + Self { attrs, offset: 0 } } } @@ -570,7 +570,7 @@ impl<'a> Iterator for NlAttrsIterator<'a> { } } -fn parse_attrs(buf: &[u8]) -> Result, NlAttrError> { +fn parse_attrs(buf: &[u8]) -> Result>, NlAttrError> { let mut attrs = HashMap::new(); for attr in NlAttrsIterator::new(buf) { let attr = attr?; @@ -595,8 +595,8 @@ enum NlAttrError { } impl From for io::Error { - fn from(e: NlAttrError) -> io::Error { - io::Error::new(io::ErrorKind::Other, e) + fn from(e: NlAttrError) -> Self { + Self::new(io::ErrorKind::Other, e) } } diff --git a/aya/src/util.rs b/aya/src/util.rs index 65bcb415..54c289b0 100644 --- a/aya/src/util.rs +++ b/aya/src/util.rs @@ -133,10 +133,10 @@ impl KernelVersion { } fn parse_kernel_version_string(s: &str) -> Result { - fn parse>(s: Option<&str>) -> Option { + fn parse>(s: Option<&str>) -> Option { match s.map(str::parse).transpose() { Ok(option) => option, - Err(std::num::ParseIntError { .. }) => None, + Err(ParseIntError { .. }) => None, } } let error = || CurrentKernelVersionError::ParseError(s.to_string()); diff --git a/xtask/public-api/aya.txt b/xtask/public-api/aya.txt index ebf7699f..d33436fb 100644 --- a/xtask/public-api/aya.txt +++ b/xtask/public-api/aya.txt @@ -1227,8 +1227,8 @@ pub aya::maps::MapData::pinned: bool impl aya::maps::MapData pub fn aya::maps::MapData::create(obj: aya_obj::maps::Map, name: &str, btf_fd: core::option::Option>) -> core::result::Result pub fn aya::maps::MapData::fd(&self) -> aya::maps::MapFd -pub fn aya::maps::MapData::from_fd(fd: std::os::fd::owned::OwnedFd) -> core::result::Result -pub fn aya::maps::MapData::from_pin>(path: P) -> core::result::Result +pub fn aya::maps::MapData::from_fd(fd: std::os::fd::owned::OwnedFd) -> core::result::Result +pub fn aya::maps::MapData::from_pin>(path: P) -> core::result::Result impl core::clone::Clone for aya::maps::MapData pub fn aya::maps::MapData::clone(&self) -> Self impl core::ops::drop::Drop for aya::maps::MapData @@ -3971,7 +3971,7 @@ impl core::convert::From for aya::programs::tc::SchedClassifier pub fn aya::programs::tc::SchedClassifier::from(t: T) -> T pub struct aya::programs::tc::SchedClassifierLink(_) impl aya::programs::tc::SchedClassifierLink -pub fn aya::programs::tc::SchedClassifierLink::attached(if_name: &str, attach_type: aya::programs::tc::TcAttachType, priority: u16, handle: u32) -> core::result::Result +pub fn aya::programs::tc::SchedClassifierLink::attached(if_name: &str, attach_type: aya::programs::tc::TcAttachType, priority: u16, handle: u32) -> core::result::Result pub fn aya::programs::tc::SchedClassifierLink::handle(&self) -> u32 pub fn aya::programs::tc::SchedClassifierLink::priority(&self) -> u16 impl aya::programs::links::Link for aya::programs::tc::SchedClassifierLink @@ -6219,7 +6219,7 @@ pub struct aya::programs::ProgramInfo(_) impl aya::programs::ProgramInfo pub fn aya::programs::ProgramInfo::btf_id(&self) -> core::option::Option pub fn aya::programs::ProgramInfo::fd(&self) -> core::result::Result -pub fn aya::programs::ProgramInfo::from_pin>(path: P) -> core::result::Result +pub fn aya::programs::ProgramInfo::from_pin>(path: P) -> core::result::Result pub fn aya::programs::ProgramInfo::gpl_compatible(&self) -> bool pub fn aya::programs::ProgramInfo::id(&self) -> u32 pub fn aya::programs::ProgramInfo::loaded_at(&self) -> std::time::SystemTime @@ -7041,8 +7041,8 @@ impl core::convert::From for aya::BpfError pub fn aya::BpfError::from(t: T) -> T pub struct aya::Bpf impl aya::Bpf -pub fn aya::Bpf::load(data: &[u8]) -> core::result::Result -pub fn aya::Bpf::load_file>(path: P) -> core::result::Result +pub fn aya::Bpf::load(data: &[u8]) -> core::result::Result +pub fn aya::Bpf::load_file>(path: P) -> core::result::Result pub fn aya::Bpf::map(&self, name: &str) -> core::option::Option<&aya::maps::Map> pub fn aya::Bpf::map_mut(&mut self, name: &str) -> core::option::Option<&mut aya::maps::Map> pub fn aya::Bpf::maps(&self) -> impl core::iter::traits::iterator::Iterator @@ -7082,12 +7082,12 @@ pub fn aya::BpfLoader<'a>::extension(&mut self, name: &'a str) -> &mut aya::BpfL pub fn aya::BpfLoader<'a>::load(&mut self, data: &[u8]) -> core::result::Result pub fn aya::BpfLoader<'a>::load_file>(&mut self, path: P) -> core::result::Result pub fn aya::BpfLoader<'a>::map_pin_path>(&mut self, path: P) -> &mut aya::BpfLoader<'a> -pub fn aya::BpfLoader<'a>::new() -> aya::BpfLoader<'a> +pub fn aya::BpfLoader<'a>::new() -> Self pub fn aya::BpfLoader<'a>::set_global>>(&mut self, name: &'a str, value: T, must_exist: bool) -> &mut aya::BpfLoader<'a> pub fn aya::BpfLoader<'a>::set_max_entries(&mut self, name: &'a str, size: u32) -> &mut aya::BpfLoader<'a> pub fn aya::BpfLoader<'a>::verifier_log_level(&mut self, level: aya::VerifierLogLevel) -> &mut aya::BpfLoader<'a> -impl<'a> core::default::Default for aya::BpfLoader<'a> -pub fn aya::BpfLoader<'a>::default() -> Self +impl core::default::Default for aya::BpfLoader<'_> +pub fn aya::BpfLoader<'_>::default() -> Self impl<'a> core::fmt::Debug for aya::BpfLoader<'a> pub fn aya::BpfLoader<'a>::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result impl<'a> core::marker::Send for aya::BpfLoader<'a>