Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Use libbpf-sys #278

Merged
merged 6 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,6 @@ It describes build tests of RedBPF that run inside docker containers.

# License

This repository contains code from other software in the following
directories, licensed under their own particular licenses:

* `bpf-sys/libbpf`: LGPL2 + BSD-2

Where '+' means they are dual licensed.

RedBPF and its components, unless otherwise stated, are licensed under either of

* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
Expand Down
1 change: 1 addition & 0 deletions bpf-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ zero = "0.1"
libc = "0.2"
regex = { version = "1.5" }
glob = "0.3.0"
libbpf-sys = "0.6.1-2"

[build-dependencies]
cc = "1.0"
Expand Down
85 changes: 0 additions & 85 deletions bpf-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,90 +5,5 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

#![deny(clippy::all)]
use std::env;
use std::path::PathBuf;
use std::process::Command;

pub mod uname {
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/uname.rs"));
}

pub mod headers {
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/headers.rs"));
}

fn rerun_if_changed_dir(dir: &str) {
println!("cargo:rerun-if-changed={}/", dir);
for ext in &["c", "h", "bash", "map", "md", "rst", "sh", "template"] {
glob::glob(&format!("./{}/**/*.{}", dir, ext))
.expect("Failed to glob for source files from build.rs")
.filter_map(|e| e.ok())
.for_each(|path| println!("cargo:rerun-if-changed={}", path.to_string_lossy()));
}
}

fn main() {
println!(
"cargo:rustc-link-search=native={}",
env::var("OUT_DIR").unwrap()
);
println!("cargo:rustc-link-lib=static=bpf");
println!("cargo:rustc-link-lib=elf");
println!("cargo:rustc-link-lib=z");

rerun_if_changed_dir("libbpf");
println!("cargo:rerun-if-changed=bpfsys-musl.h");
println!("cargo:rerun-if-changed=libbpf_xdp.h");

let out_dir = env::var("OUT_DIR").unwrap();
let out_path = PathBuf::from(out_dir);

// -fPIE is passed because Fedora 35 requires it. Other distros like Ubuntu
// 21.04, Alpine 3.14 also works fine with it
if !Command::new("make")
.args(format!("-C libbpf/src BUILD_STATIC_ONLY=1 OBJDIR={out_dir}/libbpf DESTDIR={out_dir} INCLUDEDIR= LIBDIR= UAPIDIR=", out_dir=env::var("OUT_DIR").unwrap()).split(" "))
.arg("CFLAGS=-g -O2 -Werror -Wall -fPIC")
.arg("install")
.status()
.expect("error on executing `make` command for building `libbpf` static library")
.success() {
panic!("failed to build `libbpf` static library");
}
let bindings = bindgen::Builder::default()
.header("libbpf_xdp.h")
.header("libbpf/src/bpf.h")
.header("libbpf/src/libbpf.h")
.header("libbpf/include/uapi/linux/btf.h")
.header("libbpf/src/btf.h")
.clang_arg("-Ilibbpf/src")
.clang_arg("-Ilibbpf/include/uapi")
.clang_arg("-Ilibbpf/include")
// blacklist `bpf_map_def` to avoid conflict with libbpf_map_def.rs
.blocklist_type("bpf_map_def")
.generate()
.expect("Unable to generate bindings");
bindings
.write_to_file(out_path.join("libbpf_bindings.rs"))
.expect("Couldn't write bindings!");
let bindings = bindgen::Builder::default()
.header("libbpf/src/libbpf.h")
.clang_arg("-Ilibbpf/include/uapi")
.clang_arg("-Ilibbpf/include")
.allowlist_type("bpf_map_def")
.generate()
.expect("Unable to generate bindings");
bindings
.write_to_file(out_path.join("libbpf_map_def.rs"))
.expect("Couldn't write bindings!");
let bindings = bindgen::Builder::default()
.header("libbpf/src/bpf.h")
.clang_arg("-Ilibbpf/src")
.clang_arg("-Ilibbpf/include/uapi")
.clang_arg("-Ilibbpf/include")
.generate()
.expect("Unable to generate bindings");
bindings
.write_to_file(out_path.join("perf_reader_bindings.rs"))
.expect("Couldn't write bindings!");
}
12 changes: 0 additions & 12 deletions bpf-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,7 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

#![allow(non_camel_case_types)]
#![allow(non_upper_case_globals)]
#![allow(non_snake_case)]
#![allow(clippy::all)]

extern crate zero;
pub mod headers;
pub mod perf_reader;
pub mod uname;

include!(concat!(env!("OUT_DIR"), "/libbpf_bindings.rs"));
include!(concat!(env!("OUT_DIR"), "/libbpf_map_def.rs"));
unsafe impl ::zero::Pod for bpf_map_def {}
unsafe impl ::zero::Pod for bpf_insn {}

pub mod type_gen;
13 changes: 0 additions & 13 deletions bpf-sys/src/perf_reader.rs

This file was deleted.

24 changes: 16 additions & 8 deletions bpf-sys/src/type_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ syntax. So macro constants can not be generated from vmlinux image. But
system.
*/

use super::{
use libbpf_sys::{
btf, btf__free, btf__get_nr_types, btf__name_by_offset, btf__parse_elf, btf__parse_raw,
btf__type_by_id, btf_dump, btf_dump__dump_type, btf_dump__free, btf_dump__new, btf_dump_opts,
libbpf_find_kernel_btf, vdprintf,
libbpf_find_kernel_btf, __va_list_tag,
};
use libc::{c_char, c_void};
use regex::RegexSet;
Expand All @@ -36,6 +36,14 @@ use std::path::PathBuf;
use std::ptr;
pub const ENV_VMLINUX_PATH: &'static str = "REDBPF_VMLINUX";

extern "C" {
fn vdprintf(
__fd: libc::c_int,
__fmt: *const c_char,
__arg: *mut __va_list_tag
) -> libc::c_int;
}

// only used for RAII
struct RawFdWrapper(RawFd);
impl Drop for RawFdWrapper {
Expand Down Expand Up @@ -194,16 +202,16 @@ impl VmlinuxBtfDump {
None
};
unsafe {
let dump_opts = {
let mut dump_opts = {
let mut uninit = MaybeUninit::<btf_dump_opts>::zeroed();
(*uninit.as_mut_ptr()).ctx = &mut rawfd as *mut _ as *mut _;
(*uninit.as_mut_ptr()).__bindgen_anon_1.ctx = &mut rawfd as *mut _ as *mut _;
uninit.assume_init()
};
let dumpptr = btf_dump__new(
self.btfptr,
ptr::null(),
&dump_opts as *const _,
Some(vdprintf_wrapper),
None,
&mut dump_opts as *const _ as *mut _,
vdprintf_wrapper as *const _,
);
if (dumpptr as isize) < 0 {
return Err(TypeGenError::DumpError);
Expand Down Expand Up @@ -250,7 +258,7 @@ impl Drop for VmlinuxBtfDump {
unsafe extern "C" fn vdprintf_wrapper(
ctx: *mut c_void,
format: *const c_char,
va_list: *mut super::__va_list_tag,
va_list: *mut __va_list_tag,
) {
let rawfd_wrapper = &*(ctx as *mut RawFdWrapper);
vdprintf(rawfd_wrapper.0, format, va_list);
Expand Down
3 changes: 2 additions & 1 deletion cargo-bpf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ required-features = ["command-line"]
clap = { version = "2.33", optional = true }
bindgen = {version = "0.59.2", default-features = false, features = ["runtime"], optional = true}
toml_edit = { version = "0.2", optional = true }
libbpf-sys = { version = "0.6.1-2", optional = true }
bpf-sys = { version = "2.3.0", path = "../bpf-sys", optional = true }
redbpf = { version = "2.3.0", path = "../redbpf", default-features = false, optional = true }
futures = { version = "0.3", optional = true }
Expand Down Expand Up @@ -49,7 +50,7 @@ cfg-if = "1.0.0"

[features]
default = ["command-line", "llvm-sys"]
bindings = ["bpf-sys", "bindgen", "syn", "quote", "proc-macro2", "tempfile"]
bindings = ["libbpf-sys", "bpf-sys", "bindgen", "syn", "quote", "proc-macro2", "tempfile"]
build = ["bindings", "libc", "toml_edit", "redbpf"]
docsrs-llvm = ["llvm-sys-130/no-llvm-linking", "llvm-sys-130/disable-alltargets-init"]
build-c = []
Expand Down
1 change: 1 addition & 0 deletions redbpf-probes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ufmt = { version = "0.1.0", default-features = false }
[build-dependencies]
cargo-bpf = { version = "2.3.0", path = "../cargo-bpf", default-features = false, features = ["bindings"] }
bpf-sys = { version = "2.3.0", path = "../bpf-sys" }
libbpf-sys = "0.6.1-2"
syn = {version = "1.0", default-features = false, features = ["parsing", "visit"] }
quote = "1.0"
glob = "0.3.0"
Expand Down
1 change: 1 addition & 0 deletions redbpf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ maintenance = { status = "actively-developed" }

[dependencies]
bpf-sys = { path = "../bpf-sys", version = "2.3.0" }
libbpf-sys = "0.6.1-2"
goblin = "0.4"
zero = "0.1"
libc = "0.2"
Expand Down
4 changes: 2 additions & 2 deletions redbpf/src/btf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::ptr;
use std::slice;
use tracing::{debug, error, warn};

use bpf_sys::{
use libbpf_sys::{
btf_array, btf_enum, btf_header, btf_member, btf_param, btf_type, btf_var, btf_var_secinfo,
BTF_INT_BOOL, BTF_INT_CHAR, BTF_INT_SIGNED, BTF_KIND_ARRAY, BTF_KIND_CONST, BTF_KIND_DATASEC,
BTF_KIND_ENUM, BTF_KIND_FLOAT, BTF_KIND_FUNC, BTF_KIND_FUNC_PROTO, BTF_KIND_FWD, BTF_KIND_INT,
Expand Down Expand Up @@ -129,7 +129,7 @@ impl BTF {
let log_buf_size = v.capacity() * mem::size_of_val(&v[0]);
let fd;
unsafe {
fd = bpf_sys::bpf_load_btf(
fd = libbpf_sys::bpf_load_btf(
raw_bytes.as_ptr() as *const _,
raw_bytes.len() as u32,
log_buf as _,
Expand Down
Loading