diff --git a/aya/src/maps/array/array.rs b/aya/src/maps/array/array.rs index b1bef3e9..ac5c6587 100644 --- a/aya/src/maps/array/array.rs +++ b/aya/src/maps/array/array.rs @@ -40,9 +40,7 @@ impl, V: Pod> Array { fn new(map: T) -> Result, MapError> { let map_type = map.obj.map_type(); if map_type != BPF_MAP_TYPE_ARRAY as u32 { - return Err(MapError::InvalidMapType { - map_type: map_type as u32, - }); + return Err(MapError::InvalidMapType { map_type }); } let expected = mem::size_of::(); let size = map.obj.key_size() as usize; diff --git a/aya/src/maps/array/per_cpu_array.rs b/aya/src/maps/array/per_cpu_array.rs index 99a3e7ed..6cbfb37b 100644 --- a/aya/src/maps/array/per_cpu_array.rs +++ b/aya/src/maps/array/per_cpu_array.rs @@ -59,9 +59,7 @@ impl, V: Pod> PerCpuArray { fn new(map: T) -> Result, MapError> { let map_type = map.obj.map_type(); if map_type != BPF_MAP_TYPE_PERCPU_ARRAY as u32 { - return Err(MapError::InvalidMapType { - map_type: map_type as u32, - }); + return Err(MapError::InvalidMapType { map_type }); } let expected = mem::size_of::(); let size = map.obj.key_size() as usize; diff --git a/aya/src/maps/array/program_array.rs b/aya/src/maps/array/program_array.rs index 5d02e026..aa5372cc 100644 --- a/aya/src/maps/array/program_array.rs +++ b/aya/src/maps/array/program_array.rs @@ -57,9 +57,7 @@ impl> ProgramArray { fn new(map: T) -> Result, MapError> { let map_type = map.obj.map_type(); if map_type != BPF_MAP_TYPE_PROG_ARRAY as u32 { - return Err(MapError::InvalidMapType { - map_type: map_type as u32, - }); + return Err(MapError::InvalidMapType { map_type }); } let expected = mem::size_of::(); let size = map.obj.key_size() as usize; diff --git a/aya/src/maps/bloom_filter.rs b/aya/src/maps/bloom_filter.rs index 47ba0207..03902ae6 100644 --- a/aya/src/maps/bloom_filter.rs +++ b/aya/src/maps/bloom_filter.rs @@ -44,9 +44,7 @@ impl, V: Pod> BloomFilter { // validate the map definition if map_type != BPF_MAP_TYPE_BLOOM_FILTER as u32 { - return Err(MapError::InvalidMapType { - map_type: map_type as u32, - }); + return Err(MapError::InvalidMapType { map_type }); } let size = mem::size_of::(); diff --git a/aya/src/maps/hash_map/hash_map.rs b/aya/src/maps/hash_map/hash_map.rs index b0edd734..6e36081c 100644 --- a/aya/src/maps/hash_map/hash_map.rs +++ b/aya/src/maps/hash_map/hash_map.rs @@ -44,9 +44,7 @@ impl, K: Pod, V: Pod> HashMap { // validate the map definition if map_type != BPF_MAP_TYPE_HASH as u32 && map_type != BPF_MAP_TYPE_LRU_HASH as u32 { - return Err(MapError::InvalidMapType { - map_type: map_type as u32, - }); + return Err(MapError::InvalidMapType { map_type }); } hash_map::check_kv_size::(&map)?; let _ = map.fd_or_err()?; 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 dded8477..a539965d 100644 --- a/aya/src/maps/hash_map/per_cpu_hash_map.rs +++ b/aya/src/maps/hash_map/per_cpu_hash_map.rs @@ -56,9 +56,7 @@ impl, K: Pod, V: Pod> PerCpuHashMap { if map_type != BPF_MAP_TYPE_PERCPU_HASH as u32 && map_type != BPF_MAP_TYPE_LRU_PERCPU_HASH as u32 { - return Err(MapError::InvalidMapType { - map_type: map_type as u32, - }); + return Err(MapError::InvalidMapType { map_type }); } hash_map::check_kv_size::(&map)?; let _ = map.fd_or_err()?; diff --git a/aya/src/maps/lpm_trie.rs b/aya/src/maps/lpm_trie.rs index 98c217d7..989ee51f 100644 --- a/aya/src/maps/lpm_trie.rs +++ b/aya/src/maps/lpm_trie.rs @@ -102,9 +102,7 @@ impl, K: Pod, V: Pod> LpmTrie { // validate the map definition if map_type != BPF_MAP_TYPE_LPM_TRIE as u32 { - return Err(MapError::InvalidMapType { - map_type: map_type as u32, - }); + return Err(MapError::InvalidMapType { map_type }); } let size = mem::size_of::>(); let expected = map.obj.key_size() as usize; diff --git a/aya/src/maps/perf/perf_event_array.rs b/aya/src/maps/perf/perf_event_array.rs index c9374cb7..2a74a437 100644 --- a/aya/src/maps/perf/perf_event_array.rs +++ b/aya/src/maps/perf/perf_event_array.rs @@ -164,9 +164,7 @@ impl> PerfEventArray { pub(crate) fn new(map: T) -> Result, MapError> { let map_type = map.obj.map_type(); if map_type != BPF_MAP_TYPE_PERF_EVENT_ARRAY as u32 { - return Err(MapError::InvalidMapType { - map_type: map_type as u32, - }); + return Err(MapError::InvalidMapType { map_type }); } let _fd = map.fd_or_err()?; diff --git a/aya/src/maps/queue.rs b/aya/src/maps/queue.rs index f53abe57..f8fb56b0 100644 --- a/aya/src/maps/queue.rs +++ b/aya/src/maps/queue.rs @@ -39,9 +39,7 @@ impl, V: Pod> Queue { fn new(map: T) -> Result, MapError> { let map_type = map.obj.map_type(); if map_type != BPF_MAP_TYPE_QUEUE as u32 { - return Err(MapError::InvalidMapType { - map_type: map_type as u32, - }); + return Err(MapError::InvalidMapType { map_type }); } let expected = 0; let size = map.obj.key_size() as usize; diff --git a/aya/src/maps/sock/sock_hash.rs b/aya/src/maps/sock/sock_hash.rs index a4016ead..e6350f78 100644 --- a/aya/src/maps/sock/sock_hash.rs +++ b/aya/src/maps/sock/sock_hash.rs @@ -71,9 +71,7 @@ impl, K: Pod> SockHash { // validate the map definition if map_type != BPF_MAP_TYPE_SOCKHASH as u32 { - return Err(MapError::InvalidMapType { - map_type: map_type as u32, - }); + return Err(MapError::InvalidMapType { map_type }); } hash_map::check_kv_size::(&map)?; let _ = map.fd_or_err()?; diff --git a/aya/src/maps/sock/sock_map.rs b/aya/src/maps/sock/sock_map.rs index 1abbf16a..542f6102 100644 --- a/aya/src/maps/sock/sock_map.rs +++ b/aya/src/maps/sock/sock_map.rs @@ -47,9 +47,7 @@ impl> SockMap { fn new(map: T) -> Result, MapError> { let map_type = map.obj.map_type(); if map_type != BPF_MAP_TYPE_SOCKMAP as u32 { - return Err(MapError::InvalidMapType { - map_type: map_type as u32, - }); + return Err(MapError::InvalidMapType { map_type }); } let expected = mem::size_of::(); let size = map.obj.key_size() as usize; diff --git a/aya/src/maps/stack.rs b/aya/src/maps/stack.rs index 60d85758..601c1194 100644 --- a/aya/src/maps/stack.rs +++ b/aya/src/maps/stack.rs @@ -39,9 +39,7 @@ impl, V: Pod> Stack { fn new(map: T) -> Result, MapError> { let map_type = map.obj.map_type(); if map_type != BPF_MAP_TYPE_STACK as u32 { - return Err(MapError::InvalidMapType { - map_type: map_type as u32, - }); + return Err(MapError::InvalidMapType { map_type }); } let expected = 0; let size = map.obj.key_size() as usize; diff --git a/aya/src/maps/stack_trace.rs b/aya/src/maps/stack_trace.rs index 0d73353f..0b7113f1 100644 --- a/aya/src/maps/stack_trace.rs +++ b/aya/src/maps/stack_trace.rs @@ -72,9 +72,7 @@ impl> StackTraceMap { fn new(map: T) -> Result, MapError> { let map_type = map.obj.map_type(); if map_type != BPF_MAP_TYPE_STACK_TRACE as u32 { - return Err(MapError::InvalidMapType { - map_type: map_type as u32, - }); + return Err(MapError::InvalidMapType { map_type }); } let expected = mem::size_of::(); let size = map.obj.key_size() as usize; diff --git a/aya/src/obj/btf/relocation.rs b/aya/src/obj/btf/relocation.rs index d34b69d1..aafc385a 100644 --- a/aya/src/obj/btf/relocation.rs +++ b/aya/src/obj/btf/relocation.rs @@ -196,7 +196,7 @@ fn relocate_btf_program<'target>( ) -> Result<(), ErrorWrapper> { for rel in relos { let instructions = &mut program.function.instructions; - let ins_index = rel.ins_offset as usize / std::mem::size_of::(); + let ins_index = rel.ins_offset / std::mem::size_of::(); if ins_index >= instructions.len() { return Err(RelocationError::InvalidInstructionIndex { index: ins_index, @@ -616,7 +616,7 @@ impl<'a> AccessSpec<'a> { index: parts[0], name: None, }]; - let mut bit_offset = accessors[0].index as usize * btf.type_size(type_id)?; + let mut bit_offset = accessors[0].index * btf.type_size(type_id)?; for index in parts.iter().skip(1).cloned() { type_id = btf.resolve_type(type_id)?; let ty = btf.type_by_id(type_id)?; @@ -770,12 +770,12 @@ impl ComputedRelocation { ) -> Result<(), ErrorWrapper> { let instructions = &mut program.function.instructions; let num_instructions = instructions.len(); - let ins_index = rel.ins_offset as usize / std::mem::size_of::(); + let ins_index = rel.ins_offset / std::mem::size_of::(); let mut ins = instructions .get_mut(ins_index) .ok_or(RelocationError::InvalidInstructionIndex { - index: rel.ins_offset as usize, + index: rel.ins_offset, num_instructions, relocation_number: rel.number, })?; diff --git a/aya/src/obj/btf/types.rs b/aya/src/obj/btf/types.rs index 6d65330a..bb267aff 100644 --- a/aya/src/obj/btf/types.rs +++ b/aya/src/obj/btf/types.rs @@ -907,7 +907,7 @@ impl BtfType { pub(crate) unsafe fn read(data: &[u8], endianness: Endianness) -> Result { let ty = unsafe { read_array::(data, 3)? }; let data = &data[mem::size_of::() * 3..]; - let vlen = type_vlen(ty[1]) as usize; + let vlen = type_vlen(ty[1]); Ok(match type_kind(ty[1])? { BtfKind::Unknown => BtfType::Unknown, BtfKind::Fwd => BtfType::Fwd(Fwd { diff --git a/aya/src/obj/mod.rs b/aya/src/obj/mod.rs index c5abe056..4cf11415 100644 --- a/aya/src/obj/mod.rs +++ b/aya/src/obj/mod.rs @@ -741,7 +741,7 @@ impl Object { let mut line_info = btf_ext.line_info.get(section.name); line_info.line_info.retain(|l| { l.insn_off >= bytes_offset - && l.insn_off < (bytes_offset + section_size_bytes) as u32 + && l.insn_off < (bytes_offset + section_size_bytes) }); ( @@ -1144,7 +1144,7 @@ fn get_map_field(btf: &Btf, type_id: u32) -> Result { BtfType::Ptr(pty) => pty, other => { return Err(BtfError::UnexpectedBtfType { - type_id: other.btf_type().unwrap_or(0) as u32, + type_id: other.btf_type().unwrap_or(0), }) } }; @@ -1153,7 +1153,7 @@ fn get_map_field(btf: &Btf, type_id: u32) -> Result { BtfType::Array(Array { array, .. }) => array, other => { return Err(BtfError::UnexpectedBtfType { - type_id: other.btf_type().unwrap_or(0) as u32, + type_id: other.btf_type().unwrap_or(0), }) } }; @@ -1231,7 +1231,7 @@ fn parse_btf_map_def(btf: &Btf, info: &DataSecEntry) -> Result<(String, BtfMapDe BtfType::Var(var) => var, other => { return Err(BtfError::UnexpectedBtfType { - type_id: other.btf_type().unwrap_or(0) as u32, + type_id: other.btf_type().unwrap_or(0), }) } }; @@ -1244,7 +1244,7 @@ fn parse_btf_map_def(btf: &Btf, info: &DataSecEntry) -> Result<(String, BtfMapDe BtfType::Struct(s) => s, other => { return Err(BtfError::UnexpectedBtfType { - type_id: other.btf_type().unwrap_or(0) as u32, + type_id: other.btf_type().unwrap_or(0), }) } }; diff --git a/aya/src/sys/netlink.rs b/aya/src/sys/netlink.rs index 70301cac..47fce1cf 100644 --- a/aya/src/sys/netlink.rs +++ b/aya/src/sys/netlink.rs @@ -90,7 +90,7 @@ pub(crate) unsafe fn netlink_qdisc_add_clsact(if_index: i32) -> Result<(), io::E // add the TCA_KIND attribute let attrs_buf = request_attributes(&mut req, nlmsg_len); let attr_len = write_attr_bytes(attrs_buf, 0, TCA_KIND as u16, b"clsact\0")?; - req.header.nlmsg_len += align_to(attr_len as usize, NLA_ALIGNTO as usize) as u32; + req.header.nlmsg_len += align_to(attr_len, NLA_ALIGNTO as usize) as u32; sock.send(&bytes_of(&req)[..req.header.nlmsg_len as usize])?; sock.recv()?; @@ -135,7 +135,7 @@ pub(crate) unsafe fn netlink_qdisc_attach( options.write_attr(TCA_BPF_FLAGS as u16, flags)?; let options_len = options.finish()?; - req.header.nlmsg_len += align_to(kind_len + options_len as usize, NLA_ALIGNTO as usize) as u32; + req.header.nlmsg_len += align_to(kind_len + options_len, NLA_ALIGNTO as usize) as u32; sock.send(&bytes_of(&req)[..req.header.nlmsg_len as usize])?; // find the RTM_NEWTFILTER reply and read the tcm_info field which we'll @@ -440,7 +440,7 @@ impl<'a> NestedAttrs<'a> { fn finish(self) -> Result { let nla_len = self.offset; let attr = nlattr { - nla_type: NLA_F_NESTED as u16 | self.top_attr_type as u16, + nla_type: NLA_F_NESTED as u16 | self.top_attr_type, nla_len: nla_len as u16, }; @@ -467,7 +467,7 @@ fn write_attr_bytes( value: &[u8], ) -> Result { let attr = nlattr { - nla_type: attr_type as u16, + nla_type: attr_type, nla_len: ((NLA_HDR_LEN + value.len()) as u16), }; @@ -575,10 +575,7 @@ impl From for io::Error { } unsafe fn request_attributes(req: &mut T, msg_len: usize) -> &mut [u8] { - let attrs_addr = align_to( - req as *const _ as usize + msg_len as usize, - NLMSG_ALIGNTO as usize, - ); + let attrs_addr = align_to(req as *const _ as usize + msg_len, NLMSG_ALIGNTO as usize); let attrs_end = req as *const _ as usize + mem::size_of::(); slice::from_raw_parts_mut(attrs_addr as *mut u8, attrs_end - attrs_addr) } diff --git a/aya/src/util.rs b/aya/src/util.rs index 7cc524f9..5bbb4468 100644 --- a/aya/src/util.rs +++ b/aya/src/util.rs @@ -1,6 +1,5 @@ //! Utility functions. use std::{ - cmp, collections::BTreeMap, ffi::{CStr, CString}, fs::{self, File}, @@ -173,10 +172,7 @@ impl VerifierLog { } pub(crate) fn grow(&mut self) { - let len = cmp::max( - MIN_LOG_BUF_SIZE, - cmp::min(MAX_LOG_BUF_SIZE, self.buf.capacity() * 10), - ); + let len = (self.buf.capacity() * 10).clamp(MIN_LOG_BUF_SIZE, MAX_LOG_BUF_SIZE); self.buf.resize(len, 0); self.reset(); } diff --git a/bpf/aya-bpf/src/helpers.rs b/bpf/aya-bpf/src/helpers.rs index a0d11468..42a71e52 100644 --- a/bpf/aya-bpf/src/helpers.rs +++ b/bpf/aya-bpf/src/helpers.rs @@ -279,7 +279,7 @@ pub unsafe fn bpf_probe_read_str(src: *const u8, dest: &mut [u8]) -> Result Result // bounded len = dest.len(); } - Ok(len as usize) + Ok(len) } /// Returns a byte slice read from _user space_ address `src`. @@ -474,7 +474,7 @@ pub unsafe fn bpf_probe_read_kernel_str(src: *const u8, dest: &mut [u8]) -> Resu // bounded len = dest.len(); } - Ok(len as usize) + Ok(len) } /// Returns a byte slice read from _kernel space_ address `src`. diff --git a/bpf/aya-bpf/src/programs/sk_msg.rs b/bpf/aya-bpf/src/programs/sk_msg.rs index bc6adc78..6cecc7a1 100644 --- a/bpf/aya-bpf/src/programs/sk_msg.rs +++ b/bpf/aya-bpf/src/programs/sk_msg.rs @@ -28,7 +28,7 @@ impl SkMsgContext { } pub fn push_data(&self, start: u32, len: u32, flags: u64) -> Result<(), i64> { - let ret = unsafe { bpf_msg_push_data(self.msg, start, len as u32, flags) }; + let ret = unsafe { bpf_msg_push_data(self.msg, start, len, flags) }; if ret == 0 { Ok(()) } else { @@ -37,7 +37,7 @@ impl SkMsgContext { } pub fn pop_data(&self, start: u32, len: u32, flags: u64) -> Result<(), i64> { - let ret = unsafe { bpf_msg_pop_data(self.msg, start, len as u32, flags) }; + let ret = unsafe { bpf_msg_pop_data(self.msg, start, len, flags) }; if ret == 0 { Ok(()) } else {