Browse Source

Merge branch 'aya-rs:main' into integration-tests-cli-options

abhi 2 years ago
parent
commit
4183c7a7d2

+ 0 - 4
aya-tool/Cargo.toml

@@ -9,8 +9,4 @@ bindgen = "0.60"
 clap = { version = "3", features = ["derive"] }
 anyhow = "1"
 thiserror = "1"
-syn = "1"
-quote = "1"
-proc-macro2 = "1"
-indexmap = "1.6"
 tempfile = "3"

+ 4 - 5
aya/Cargo.toml

@@ -18,21 +18,20 @@ bitflags = "1.2.1"
 bytes = "1"
 lazy_static = "1"
 parking_lot = { version = "0.12.0", features = ["send_guard"] }
-futures = { version = "0.3.12", optional = true, default-features = false, features = ["std"] }
 tokio = { version = "1.2.0", features = ["macros", "rt", "rt-multi-thread", "net"], optional = true }
-async-std = { version = "1.9.0", optional = true }
 async-io = { version = "1.3", optional = true }
 log = "0.4"
 
 [dev-dependencies]
 matches = "0.1.8"
+futures = { version = "0.3.12", default-features = false, features = ["std"] }
 
 [features]
 default = []
-async = ["futures"]
+async = []
 async_tokio = ["tokio", "async"]
-async_std = ["async-std", "async-io", "async"]
+async_std = ["async-io", "async"]
 
 [package.metadata.docs.rs]
 all-features = true
-rustdoc-args = ["--cfg", "docsrs"]
+rustdoc-args = ["--cfg", "docsrs"]

+ 1 - 3
aya/src/maps/array/array.rs

@@ -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;

+ 1 - 3
aya/src/maps/array/per_cpu_array.rs

@@ -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;

+ 1 - 3
aya/src/maps/array/program_array.rs

@@ -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;

+ 1 - 3
aya/src/maps/bloom_filter.rs

@@ -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>();

+ 1 - 3
aya/src/maps/hash_map/hash_map.rs

@@ -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()?;

+ 1 - 3
aya/src/maps/hash_map/per_cpu_hash_map.rs

@@ -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()?;

+ 1 - 3
aya/src/maps/lpm_trie.rs

@@ -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;

+ 1 - 3
aya/src/maps/perf/perf_event_array.rs

@@ -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()?;
 

+ 1 - 3
aya/src/maps/queue.rs

@@ -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;

+ 1 - 3
aya/src/maps/sock/sock_hash.rs

@@ -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()?;

+ 1 - 3
aya/src/maps/sock/sock_map.rs

@@ -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;

+ 1 - 3
aya/src/maps/stack.rs

@@ -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;

+ 1 - 3
aya/src/maps/stack_trace.rs

@@ -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;

+ 4 - 4
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::<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,
@@ -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::<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,
                 })?;

+ 1 - 1
aya/src/obj/btf/types.rs

@@ -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 {

+ 5 - 5
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<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),
             })
         }
     };
@@ -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),
             })
         }
     };
@@ -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),
             })
         }
     };

+ 5 - 8
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<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,
         };
 
@@ -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),
     };
 
@@ -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)
 }

+ 1 - 5
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();
     }

+ 3 - 3
bpf/aya-bpf/src/helpers.rs

@@ -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`.
@@ -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`.
@@ -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`.

+ 2 - 2
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 {

+ 5 - 4
test/README.md

@@ -10,14 +10,15 @@ common usage behaviours work on real Linux distros
 To run locally all you need is:
 
 1. Rust nightly
-1. A checkout of `libbpf`
-1. `cargo install bpf-linker`
-1. `bpftool`
+2. `libelf`
+3. A checkout of [libbpf](https://github.com/libbpf/libbpf)
+4. `cargo install bpf-linker`
+5. `bpftool`
 
 ### Other OSs
 
 1. A POSIX shell
-1. A checkout of `libbpf`
+1. A checkout of [libbpf](https://github.com/libbpf/libbpf)
 1. `rustup target add x86_64-unknown-linux-musl`
 1. `cargo install bpf-linker`
 1. Install `qemu` and `cloud-init-utils` package - or any package that provides `cloud-localds`

+ 1 - 2
xtask/Cargo.toml

@@ -11,7 +11,6 @@ anyhow = "1"
 syn = "1"
 quote = "1"
 proc-macro2 = "1"
-indexmap = "1.6"
 indoc = "1.0"
 lazy_static = "1"
-serde_json = "1"
+serde_json = "1"