Browse Source

aya-obj: migrate bindgen destination

Aya::obj depends on bindgen generated files, and we start
by migrating bindgen generated files.

This commit adds the new aya-obj crate to the workplace
and migrates generated files into the crate. We use core
instead of std in an effort to make the final crate no_std.

Bindgen was run against libbpf v1.0.1.

Refs: #473
Shenghui Ye 2 years ago
parent
commit
81bc307dce

+ 2 - 2
Cargo.toml

@@ -1,12 +1,12 @@
 [workspace]
 members = [
-    "aya", "aya-tool", "aya-log", "aya-log-common", "aya-log-parser", "test/integration-test", "test/integration-test-macros", "xtask",
+    "aya", "aya-obj", "aya-tool", "aya-log", "aya-log-common", "aya-log-parser", "test/integration-test", "test/integration-test-macros", "xtask",
     # macros
     "aya-bpf-macros", "aya-log-ebpf-macros",
     # ebpf crates
     "bpf/aya-bpf", "bpf/aya-bpf-bindings", "bpf/aya-log-ebpf", "test/integration-ebpf"
 ]
-default-members = ["aya", "aya-tool", "aya-log", "aya-bpf-macros", "aya-log-ebpf-macros"]
+default-members = ["aya", "aya-obj", "aya-tool", "aya-log", "aya-bpf-macros", "aya-log-ebpf-macros"]
 
 [profile.dev]
 panic = "abort"

+ 13 - 0
aya-obj/Cargo.toml

@@ -0,0 +1,13 @@
+[package]
+name = "aya-obj"
+version = "0.11.0"
+description = "A library for loading and relocating eBPF object files"
+keywords = ["ebpf", "bpf", "linux", "kernel"]
+license = "MIT OR Apache-2.0"
+authors = ["The Aya Contributors"]
+repository = "https://github.com/aya-rs/aya"
+readme = "README.md"
+documentation = "https://docs.rs/aya-obj"
+edition = "2021"
+
+[dependencies]

+ 1 - 0
aya-obj/README.md

@@ -0,0 +1 @@
+# aya-obj

+ 0 - 0
aya/include/linux_wrapper.h → aya-obj/include/linux_wrapper.h


+ 5 - 5
aya/src/generated/btf_internal_bindings.rs → aya-obj/src/generated/btf_internal_bindings.rs

@@ -1,10 +1,10 @@
-/* automatically generated by rust-bindgen 0.60.1 */
+/* automatically generated by rust-bindgen 0.63.0 */
 
-pub type __u8 = ::std::os::raw::c_uchar;
-pub type __u16 = ::std::os::raw::c_ushort;
-pub type __u32 = ::std::os::raw::c_uint;
+pub type __u8 = ::core::ffi::c_uchar;
+pub type __u16 = ::core::ffi::c_ushort;
+pub type __u32 = ::core::ffi::c_uint;
 pub mod bpf_core_relo_kind {
-    pub type Type = ::std::os::raw::c_uint;
+    pub type Type = ::core::ffi::c_uint;
     pub const BPF_CORE_FIELD_BYTE_OFFSET: Type = 0;
     pub const BPF_CORE_FIELD_BYTE_SIZE: Type = 1;
     pub const BPF_CORE_FIELD_EXISTS: Type = 2;

File diff suppressed because it is too large
+ 174 - 174
aya-obj/src/generated/linux_bindings_aarch64.rs


File diff suppressed because it is too large
+ 174 - 174
aya-obj/src/generated/linux_bindings_armv7.rs


File diff suppressed because it is too large
+ 174 - 174
aya-obj/src/generated/linux_bindings_riscv64.rs


File diff suppressed because it is too large
+ 174 - 174
aya-obj/src/generated/linux_bindings_x86_64.rs


+ 86 - 0
aya-obj/src/generated/mod.rs

@@ -0,0 +1,86 @@
+//! eBPF bindings generated by rust-bindgen
+
+#![allow(
+    dead_code,
+    non_camel_case_types,
+    non_snake_case,
+    clippy::all,
+    missing_docs
+)]
+
+mod btf_internal_bindings;
+#[cfg(target_arch = "aarch64")]
+mod linux_bindings_aarch64;
+#[cfg(target_arch = "arm")]
+mod linux_bindings_armv7;
+#[cfg(target_arch = "riscv64")]
+mod linux_bindings_riscv64;
+#[cfg(target_arch = "x86_64")]
+mod linux_bindings_x86_64;
+
+pub use btf_internal_bindings::*;
+
+#[cfg(target_arch = "x86_64")]
+pub use linux_bindings_x86_64::*;
+
+#[cfg(target_arch = "arm")]
+pub use linux_bindings_armv7::*;
+
+#[cfg(target_arch = "aarch64")]
+pub use linux_bindings_aarch64::*;
+
+#[cfg(target_arch = "riscv64")]
+pub use linux_bindings_riscv64::*;
+
+pub mod maps {
+    /// Invalid map type encontered
+    pub struct InvalidMapTypeError {
+        /// The map type
+        pub map_type: u32,
+    }
+
+    impl TryFrom<u32> for super::bpf_map_type {
+        type Error = InvalidMapTypeError;
+
+        fn try_from(map_type: u32) -> Result<Self, Self::Error> {
+            use super::bpf_map_type::*;
+            Ok(match map_type {
+                x if x == BPF_MAP_TYPE_UNSPEC as u32 => BPF_MAP_TYPE_UNSPEC,
+                x if x == BPF_MAP_TYPE_HASH as u32 => BPF_MAP_TYPE_HASH,
+                x if x == BPF_MAP_TYPE_ARRAY as u32 => BPF_MAP_TYPE_ARRAY,
+                x if x == BPF_MAP_TYPE_PROG_ARRAY as u32 => BPF_MAP_TYPE_PROG_ARRAY,
+                x if x == BPF_MAP_TYPE_PERF_EVENT_ARRAY as u32 => BPF_MAP_TYPE_PERF_EVENT_ARRAY,
+                x if x == BPF_MAP_TYPE_PERCPU_HASH as u32 => BPF_MAP_TYPE_PERCPU_HASH,
+                x if x == BPF_MAP_TYPE_PERCPU_ARRAY as u32 => BPF_MAP_TYPE_PERCPU_ARRAY,
+                x if x == BPF_MAP_TYPE_STACK_TRACE as u32 => BPF_MAP_TYPE_STACK_TRACE,
+                x if x == BPF_MAP_TYPE_CGROUP_ARRAY as u32 => BPF_MAP_TYPE_CGROUP_ARRAY,
+                x if x == BPF_MAP_TYPE_LRU_HASH as u32 => BPF_MAP_TYPE_LRU_HASH,
+                x if x == BPF_MAP_TYPE_LRU_PERCPU_HASH as u32 => BPF_MAP_TYPE_LRU_PERCPU_HASH,
+                x if x == BPF_MAP_TYPE_LPM_TRIE as u32 => BPF_MAP_TYPE_LPM_TRIE,
+                x if x == BPF_MAP_TYPE_BLOOM_FILTER as u32 => BPF_MAP_TYPE_BLOOM_FILTER,
+                x if x == BPF_MAP_TYPE_ARRAY_OF_MAPS as u32 => BPF_MAP_TYPE_ARRAY_OF_MAPS,
+                x if x == BPF_MAP_TYPE_HASH_OF_MAPS as u32 => BPF_MAP_TYPE_HASH_OF_MAPS,
+                x if x == BPF_MAP_TYPE_DEVMAP as u32 => BPF_MAP_TYPE_DEVMAP,
+                x if x == BPF_MAP_TYPE_SOCKMAP as u32 => BPF_MAP_TYPE_SOCKMAP,
+                x if x == BPF_MAP_TYPE_CPUMAP as u32 => BPF_MAP_TYPE_CPUMAP,
+                x if x == BPF_MAP_TYPE_XSKMAP as u32 => BPF_MAP_TYPE_XSKMAP,
+                x if x == BPF_MAP_TYPE_SOCKHASH as u32 => BPF_MAP_TYPE_SOCKHASH,
+                x if x == BPF_MAP_TYPE_CGROUP_STORAGE as u32 => BPF_MAP_TYPE_CGROUP_STORAGE,
+                x if x == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY as u32 => BPF_MAP_TYPE_REUSEPORT_SOCKARRAY,
+                x if x == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE as u32 => {
+                    BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
+                }
+                x if x == BPF_MAP_TYPE_QUEUE as u32 => BPF_MAP_TYPE_QUEUE,
+                x if x == BPF_MAP_TYPE_STACK as u32 => BPF_MAP_TYPE_STACK,
+                x if x == BPF_MAP_TYPE_SK_STORAGE as u32 => BPF_MAP_TYPE_SK_STORAGE,
+                x if x == BPF_MAP_TYPE_DEVMAP_HASH as u32 => BPF_MAP_TYPE_DEVMAP_HASH,
+                x if x == BPF_MAP_TYPE_STRUCT_OPS as u32 => BPF_MAP_TYPE_STRUCT_OPS,
+                x if x == BPF_MAP_TYPE_RINGBUF as u32 => BPF_MAP_TYPE_RINGBUF,
+                x if x == BPF_MAP_TYPE_INODE_STORAGE as u32 => BPF_MAP_TYPE_INODE_STORAGE,
+                x if x == BPF_MAP_TYPE_TASK_STORAGE as u32 => BPF_MAP_TYPE_TASK_STORAGE,
+                _ => return Err(InvalidMapTypeError { map_type }),
+            })
+        }
+    }
+}
+

+ 12 - 0
aya-obj/src/lib.rs

@@ -0,0 +1,12 @@
+//! A library for loading and relocating eBPF object files.
+
+#![no_std]
+#![doc(
+    html_logo_url = "https://aya-rs.dev/assets/images/crabby.svg",
+    html_favicon_url = "https://aya-rs.dev/assets/images/crabby.svg"
+)]
+#![cfg_attr(docsrs, feature(doc_cfg))]
+#![deny(clippy::all, missing_docs)]
+#![allow(clippy::missing_safety_doc, clippy::len_without_is_empty)]
+
+pub mod generated;

+ 1 - 0
aya-tool/src/bindgen.rs

@@ -2,6 +2,7 @@ use bindgen::{self, Builder, EnumVariation};
 
 pub fn user_builder() -> Builder {
     bindgen::builder()
+        .use_core()
         .layout_tests(false)
         .generate_comments(false)
         .prepend_enum_name(false)

+ 1 - 0
aya/Cargo.toml

@@ -12,6 +12,7 @@ edition = "2021"
 
 [dependencies]
 libc = { version = "0.2.105" }
+aya-obj = { path = "../aya-obj", version = "0.11.0" }
 thiserror = "1"
 object = { version = "0.30", default-features = false, features = ["std", "read_core", "elf"] }
 bitflags = "1.2.1"

+ 4 - 1
aya/src/bpf.rs

@@ -655,7 +655,10 @@ impl<'a> BpfLoader<'a> {
 fn parse_map(data: (String, MapData)) -> Result<(String, Map), BpfError> {
     let name = data.0;
     let map = data.1;
-    let map_type = bpf_map_type::try_from(map.obj.map_type())?;
+    let map_type =
+        bpf_map_type::try_from(map.obj.map_type()).map_err(|e| MapError::InvalidMapType {
+            map_type: e.map_type,
+        })?;
     let map = match map_type {
         BPF_MAP_TYPE_ARRAY => Ok(Map::Array(map)),
         BPF_MAP_TYPE_PERCPU_ARRAY => Ok(Map::PerCpuArray(map)),

+ 0 - 31
aya/src/generated/mod.rs

@@ -1,31 +0,0 @@
-#![allow(
-    dead_code,
-    non_camel_case_types,
-    non_snake_case,
-    clippy::all,
-    missing_docs
-)]
-
-mod btf_internal_bindings;
-#[cfg(target_arch = "aarch64")]
-mod linux_bindings_aarch64;
-#[cfg(target_arch = "arm")]
-mod linux_bindings_armv7;
-#[cfg(target_arch = "riscv64")]
-mod linux_bindings_riscv64;
-#[cfg(target_arch = "x86_64")]
-mod linux_bindings_x86_64;
-
-pub use btf_internal_bindings::*;
-
-#[cfg(target_arch = "x86_64")]
-pub use linux_bindings_x86_64::*;
-
-#[cfg(target_arch = "arm")]
-pub use linux_bindings_armv7::*;
-
-#[cfg(target_arch = "aarch64")]
-pub use linux_bindings_aarch64::*;
-
-#[cfg(target_arch = "riscv64")]
-pub use linux_bindings_riscv64::*;

+ 1 - 1
aya/src/lib.rs

@@ -46,7 +46,7 @@ extern crate lazy_static;
 extern crate bitflags;
 
 mod bpf;
-mod generated;
+use aya_obj::generated as generated;
 pub mod maps;
 mod obj;
 pub mod pin;

+ 0 - 45
aya/src/maps/mod.rs

@@ -53,7 +53,6 @@ use log::warn;
 use thiserror::Error;
 
 use crate::{
-    generated::bpf_map_type,
     obj::{self, parse_map_info},
     pin::PinError,
     sys::{
@@ -739,50 +738,6 @@ impl<K: Pod, V, I: IterableMap<K, V>> Iterator for MapIter<'_, K, V, I> {
     }
 }
 
-impl TryFrom<u32> for bpf_map_type {
-    type Error = MapError;
-
-    fn try_from(map_type: u32) -> Result<Self, Self::Error> {
-        use bpf_map_type::*;
-        Ok(match map_type {
-            x if x == BPF_MAP_TYPE_UNSPEC as u32 => BPF_MAP_TYPE_UNSPEC,
-            x if x == BPF_MAP_TYPE_HASH as u32 => BPF_MAP_TYPE_HASH,
-            x if x == BPF_MAP_TYPE_ARRAY as u32 => BPF_MAP_TYPE_ARRAY,
-            x if x == BPF_MAP_TYPE_PROG_ARRAY as u32 => BPF_MAP_TYPE_PROG_ARRAY,
-            x if x == BPF_MAP_TYPE_PERF_EVENT_ARRAY as u32 => BPF_MAP_TYPE_PERF_EVENT_ARRAY,
-            x if x == BPF_MAP_TYPE_PERCPU_HASH as u32 => BPF_MAP_TYPE_PERCPU_HASH,
-            x if x == BPF_MAP_TYPE_PERCPU_ARRAY as u32 => BPF_MAP_TYPE_PERCPU_ARRAY,
-            x if x == BPF_MAP_TYPE_STACK_TRACE as u32 => BPF_MAP_TYPE_STACK_TRACE,
-            x if x == BPF_MAP_TYPE_CGROUP_ARRAY as u32 => BPF_MAP_TYPE_CGROUP_ARRAY,
-            x if x == BPF_MAP_TYPE_LRU_HASH as u32 => BPF_MAP_TYPE_LRU_HASH,
-            x if x == BPF_MAP_TYPE_LRU_PERCPU_HASH as u32 => BPF_MAP_TYPE_LRU_PERCPU_HASH,
-            x if x == BPF_MAP_TYPE_LPM_TRIE as u32 => BPF_MAP_TYPE_LPM_TRIE,
-            x if x == BPF_MAP_TYPE_BLOOM_FILTER as u32 => BPF_MAP_TYPE_BLOOM_FILTER,
-            x if x == BPF_MAP_TYPE_ARRAY_OF_MAPS as u32 => BPF_MAP_TYPE_ARRAY_OF_MAPS,
-            x if x == BPF_MAP_TYPE_HASH_OF_MAPS as u32 => BPF_MAP_TYPE_HASH_OF_MAPS,
-            x if x == BPF_MAP_TYPE_DEVMAP as u32 => BPF_MAP_TYPE_DEVMAP,
-            x if x == BPF_MAP_TYPE_SOCKMAP as u32 => BPF_MAP_TYPE_SOCKMAP,
-            x if x == BPF_MAP_TYPE_CPUMAP as u32 => BPF_MAP_TYPE_CPUMAP,
-            x if x == BPF_MAP_TYPE_XSKMAP as u32 => BPF_MAP_TYPE_XSKMAP,
-            x if x == BPF_MAP_TYPE_SOCKHASH as u32 => BPF_MAP_TYPE_SOCKHASH,
-            x if x == BPF_MAP_TYPE_CGROUP_STORAGE as u32 => BPF_MAP_TYPE_CGROUP_STORAGE,
-            x if x == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY as u32 => BPF_MAP_TYPE_REUSEPORT_SOCKARRAY,
-            x if x == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE as u32 => {
-                BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
-            }
-            x if x == BPF_MAP_TYPE_QUEUE as u32 => BPF_MAP_TYPE_QUEUE,
-            x if x == BPF_MAP_TYPE_STACK as u32 => BPF_MAP_TYPE_STACK,
-            x if x == BPF_MAP_TYPE_SK_STORAGE as u32 => BPF_MAP_TYPE_SK_STORAGE,
-            x if x == BPF_MAP_TYPE_DEVMAP_HASH as u32 => BPF_MAP_TYPE_DEVMAP_HASH,
-            x if x == BPF_MAP_TYPE_STRUCT_OPS as u32 => BPF_MAP_TYPE_STRUCT_OPS,
-            x if x == BPF_MAP_TYPE_RINGBUF as u32 => BPF_MAP_TYPE_RINGBUF,
-            x if x == BPF_MAP_TYPE_INODE_STORAGE as u32 => BPF_MAP_TYPE_INODE_STORAGE,
-            x if x == BPF_MAP_TYPE_TASK_STORAGE as u32 => BPF_MAP_TYPE_TASK_STORAGE,
-            _ => return Err(MapError::InvalidMapType { map_type }),
-        })
-    }
-}
-
 pub(crate) struct PerCpuKernelMem {
     bytes: Vec<u8>,
 }

+ 2 - 2
xtask/src/codegen/aya.rs

@@ -11,7 +11,7 @@ pub fn codegen(opts: &Options) -> Result<(), anyhow::Error> {
 }
 
 fn codegen_internal_btf_bindings(opts: &Options) -> Result<(), anyhow::Error> {
-    let dir = PathBuf::from("aya");
+    let dir = PathBuf::from("aya-obj");
     let generated = dir.join("src/generated");
     let mut bindgen = bindgen::user_builder()
         .clang_arg(format!(
@@ -154,7 +154,7 @@ fn codegen_bindings(opts: &Options) -> Result<(), anyhow::Error> {
         "BPF_RINGBUF_.*",
     ];
 
-    let dir = PathBuf::from("aya");
+    let dir = PathBuf::from("aya-obj");
     let generated = dir.join("src/generated");
 
     let builder = || {

Some files were not shown because too many files changed in this diff