Skip to content

Commit

Permalink
fix all clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
abhijeetbhagat committed Oct 11, 2022
1 parent 14ba644 commit 6c813b8
Show file tree
Hide file tree
Showing 20 changed files with 34 additions and 67 deletions.
4 changes: 1 addition & 3 deletions aya/src/maps/array/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ impl<T: Deref<Target = Map>, V: Pod> Array<T, V> {
fn new(map: T) -> Result<Array<T, V>, 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::<u32>();
let size = map.obj.key_size() as usize;
Expand Down
4 changes: 1 addition & 3 deletions aya/src/maps/array/per_cpu_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ impl<T: Deref<Target = Map>, V: Pod> PerCpuArray<T, V> {
fn new(map: T) -> Result<PerCpuArray<T, V>, 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::<u32>();
let size = map.obj.key_size() as usize;
Expand Down
4 changes: 1 addition & 3 deletions aya/src/maps/array/program_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ impl<T: Deref<Target = Map>> ProgramArray<T> {
fn new(map: T) -> Result<ProgramArray<T>, 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::<u32>();
let size = map.obj.key_size() as usize;
Expand Down
4 changes: 1 addition & 3 deletions aya/src/maps/bloom_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ impl<T: Deref<Target = Map>, V: Pod> BloomFilter<T, V> {

// 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::<V>();
Expand Down
4 changes: 1 addition & 3 deletions aya/src/maps/hash_map/hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ impl<T: Deref<Target = Map>, K: Pod, V: Pod> HashMap<T, K, V> {

// 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::<K, V>(&map)?;
let _ = map.fd_or_err()?;
Expand Down
4 changes: 1 addition & 3 deletions aya/src/maps/hash_map/per_cpu_hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ impl<T: Deref<Target = Map>, K: Pod, V: Pod> PerCpuHashMap<T, K, V> {
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::<K, V>(&map)?;
let _ = map.fd_or_err()?;
Expand Down
4 changes: 1 addition & 3 deletions aya/src/maps/lpm_trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ impl<T: Deref<Target = Map>, K: Pod, V: Pod> LpmTrie<T, K, V> {

// 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::<Key<K>>();
let expected = map.obj.key_size() as usize;
Expand Down
4 changes: 1 addition & 3 deletions aya/src/maps/perf/perf_event_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,7 @@ impl<T: DerefMut<Target = Map>> PerfEventArray<T> {
pub(crate) fn new(map: T) -> Result<PerfEventArray<T>, 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()?;

Expand Down
4 changes: 1 addition & 3 deletions aya/src/maps/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ impl<T: Deref<Target = Map>, V: Pod> Queue<T, V> {
fn new(map: T) -> Result<Queue<T, V>, 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;
Expand Down
4 changes: 1 addition & 3 deletions aya/src/maps/sock/sock_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ impl<T: Deref<Target = Map>, K: Pod> SockHash<T, K> {

// 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::<K, u32>(&map)?;
let _ = map.fd_or_err()?;
Expand Down
4 changes: 1 addition & 3 deletions aya/src/maps/sock/sock_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ impl<T: Deref<Target = Map>> SockMap<T> {
fn new(map: T) -> Result<SockMap<T>, 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::<u32>();
let size = map.obj.key_size() as usize;
Expand Down
4 changes: 1 addition & 3 deletions aya/src/maps/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ impl<T: Deref<Target = Map>, V: Pod> Stack<T, V> {
fn new(map: T) -> Result<Stack<T, V>, 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;
Expand Down
4 changes: 1 addition & 3 deletions aya/src/maps/stack_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ impl<T: Deref<Target = Map>> StackTraceMap<T> {
fn new(map: T) -> Result<StackTraceMap<T>, 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::<u32>();
let size = map.obj.key_size() as usize;
Expand Down
8 changes: 4 additions & 4 deletions aya/src/obj/btf/relocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<bpf_insn>();
let ins_index = rel.ins_offset / std::mem::size_of::<bpf_insn>();
if ins_index >= instructions.len() {
return Err(RelocationError::InvalidInstructionIndex {
index: ins_index,
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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::<bpf_insn>();
let ins_index = rel.ins_offset / std::mem::size_of::<bpf_insn>();
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,
})?;
Expand Down
2 changes: 1 addition & 1 deletion aya/src/obj/btf/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ impl BtfType {
pub(crate) unsafe fn read(data: &[u8], endianness: Endianness) -> Result<BtfType, BtfError> {
let ty = unsafe { read_array::<u32>(data, 3)? };
let data = &data[mem::size_of::<u32>() * 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 {
Expand Down
10 changes: 5 additions & 5 deletions aya/src/obj/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});

(
Expand Down Expand Up @@ -1144,7 +1144,7 @@ fn get_map_field(btf: &Btf, type_id: u32) -> Result<u32, BtfError> {
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),
})
}
};
Expand All @@ -1153,7 +1153,7 @@ fn get_map_field(btf: &Btf, type_id: u32) -> Result<u32, BtfError> {
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),
})
}
};
Expand Down Expand Up @@ -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),
})
}
};
Expand All @@ -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),
})
}
};
Expand Down
13 changes: 5 additions & 8 deletions aya/src/sys/netlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -440,7 +440,7 @@ impl<'a> NestedAttrs<'a> {
fn finish(self) -> Result<usize, io::Error> {
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,
};

Expand All @@ -467,7 +467,7 @@ fn write_attr_bytes(
value: &[u8],
) -> Result<usize, io::Error> {
let attr = nlattr {
nla_type: attr_type as u16,
nla_type: attr_type,
nla_len: ((NLA_HDR_LEN + value.len()) as u16),
};

Expand Down Expand Up @@ -575,10 +575,7 @@ impl From<NlAttrError> for io::Error {
}

unsafe fn request_attributes<T>(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::<T>();
slice::from_raw_parts_mut(attrs_addr as *mut u8, attrs_end - attrs_addr)
}
Expand Down
6 changes: 1 addition & 5 deletions aya/src/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Utility functions.
use std::{
cmp,
collections::BTreeMap,
ffi::{CStr, CString},
fs::{self, File},
Expand Down Expand Up @@ -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();
}
Expand Down
6 changes: 3 additions & 3 deletions bpf/aya-bpf/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ pub unsafe fn bpf_probe_read_str(src: *const u8, dest: &mut [u8]) -> Result<usiz
// bounded
len = dest.len();
}
Ok(len as usize)
Ok(len)
}

/// Read a null-terminated string from _user space_ stored at `src` into `dest`.
Expand Down Expand Up @@ -323,7 +323,7 @@ pub unsafe fn bpf_probe_read_user_str(src: *const u8, dest: &mut [u8]) -> Result
// bounded
len = dest.len();
}
Ok(len as usize)
Ok(len)
}

/// Returns a byte slice read from _user space_ address `src`.
Expand Down Expand Up @@ -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`.
Expand Down
4 changes: 2 additions & 2 deletions bpf/aya-bpf/src/programs/sk_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 6c813b8

Please sign in to comment.