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

Commit

Permalink
Merge pull request #278 from kriomant/feature/libbpf
Browse files Browse the repository at this point in the history
Use libbpf-sys
  • Loading branch information
rsdy authored Feb 21, 2022
2 parents f8b64c2 + c2b4dcb commit af990ff
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 201 deletions.
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
2 changes: 2 additions & 0 deletions bpf-sys/bindings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include <btf.h>
#include <stdio.h>
75 changes: 4 additions & 71 deletions bpf-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,91 +4,24 @@
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// 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 bindgen;
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")
.header("bindings.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")
.allowlist_function("btf_dump__new")
.allowlist_function("vdprintf")
.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!");
}
2 changes: 1 addition & 1 deletion bpf-sys/libbpf
Submodule libbpf updated 77 files
+4 −2 .github/actions/setup/action.yml
+40 −0 .github/workflows/cifuzz.yml
+2 −1 .github/workflows/coverity.yml
+0 −20 .github/workflows/pahole.yml
+19 −2 .github/workflows/test.yml
+17 −0 .readthedocs.yaml
+1 −1 BPF-CHECKPOINT-COMMIT
+1 −1 CHECKPOINT-COMMIT
+7 −1 README.md
+2 −0 docs/.gitignore
+51 −0 docs/api.rst
+40 −0 docs/conf.py
+22 −0 docs/index.rst
+37 −0 docs/libbpf_build.rst
+202 −0 docs/libbpf_naming_convention.rst
+9 −0 docs/sphinx/Makefile
+277 −0 docs/sphinx/doxygen/Doxyfile
+1 −0 docs/sphinx/requirements.txt
+8 −0 include/linux/filter.h
+128 −5 include/uapi/linux/bpf.h
+36 −20 include/uapi/linux/btf.h
+2 −0 include/uapi/linux/if_link.h
+9 −5 scripts/sync-kernel.sh
+5 −4 src/Makefile
+226 −155 src/bpf.c
+109 −6 src/bpf.h
+1 −1 src/bpf_core_read.h
+33 −6 src/bpf_gen_internal.h
+126 −4 src/bpf_helper_defs.h
+43 −8 src/bpf_helpers.h
+32 −0 src/bpf_tracing.h
+376 −169 src/btf.c
+195 −13 src/btf.h
+67 −36 src/btf_dump.c
+420 −104 src/gen_loader.c
+2,221 −2,481 src/libbpf.c
+299 −57 src/libbpf.h
+43 −0 src/libbpf.map
+37 −1 src/libbpf_common.h
+94 −123 src/libbpf_internal.h
+19 −0 src/libbpf_legacy.h
+27 −27 src/libbpf_probes.c
+9 −0 src/libbpf_version.h
+36 −25 src/linker.c
+1,295 −0 src/relo_core.c
+100 −0 src/relo_core.h
+15 −4 src/skel_internal.h
+1 −0 src/strset.c
+24 −38 src/xsk.c
+52 −38 src/xsk.h
+7 −3 travis-ci/managers/debian.sh
+14 −0 travis-ci/managers/test_compile.sh
+1 −1 travis-ci/managers/ubuntu.sh
+4 −5 travis-ci/vmtest/build_selftests.sh
+1 −1 travis-ci/vmtest/checkout_latest_kernel.sh
+8 −7 travis-ci/vmtest/configs/INDEX
+10 −0 travis-ci/vmtest/configs/blacklist/BLACKLIST-5.5.0
+4 −1 travis-ci/vmtest/configs/blacklist/BLACKLIST-latest
+52 −0 travis-ci/vmtest/configs/blacklist/BLACKLIST-latest.s390x
+2,703 −0 travis-ci/vmtest/configs/config-latest.s390x
+0 −0 travis-ci/vmtest/configs/config-latest.x86_64
+1 −0 travis-ci/vmtest/configs/whitelist/WHITELIST-4.9.0
+56 −0 travis-ci/vmtest/configs/whitelist/WHITELIST-5.5.0
+23 −1 travis-ci/vmtest/helpers.sh
+1 −53 travis-ci/vmtest/mkrootfs_arch.sh
+40 −0 travis-ci/vmtest/mkrootfs_debian.sh
+61 −0 travis-ci/vmtest/mkrootfs_tweak.sh
+7 −6 travis-ci/vmtest/prepare_selftests.sh
+182 −73 travis-ci/vmtest/run.sh
+25 −15 travis-ci/vmtest/run_selftests.sh
+22 −7 travis-ci/vmtest/run_vmtest.sh
+72 −0 travis-ci/vmtest/s390x-self-hosted-builder/README.md
+48 −0 travis-ci/vmtest/s390x-self-hosted-builder/actions-runner-libbpf.Dockerfile
+24 −0 travis-ci/vmtest/s390x-self-hosted-builder/actions-runner-libbpf.service
+40 −0 travis-ci/vmtest/s390x-self-hosted-builder/fs/usr/bin/actions-runner
+35 −0 travis-ci/vmtest/s390x-self-hosted-builder/fs/usr/bin/entrypoint
+11 −0 travis-ci/vmtest/s390x-self-hosted-builder/qemu-user-static.service
19 changes: 7 additions & 12 deletions bpf-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,14 @@
// 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;

// FIXME: Remove libbpf_bindings in favor of libbpf-sys
mod libbpf_bindings {
#![allow(non_camel_case_types)]
#![allow(dead_code)]
include!(concat!(env!("OUT_DIR"), "/libbpf_bindings.rs"));
}
13 changes: 0 additions & 13 deletions bpf-sys/src/perf_reader.rs

This file was deleted.

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

use super::{
use super::libbpf_bindings;
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,
btf__type_by_id, btf_dump, btf_dump__dump_type, btf_dump__free, libbpf_find_kernel_btf,
};
use libc::{c_char, c_void};
use regex::RegexSet;
use std::env;
use std::ffi::{CStr, CString};
use std::fs::File;
use std::io::{self, Write};
use std::mem::{self, MaybeUninit};
use std::mem;
use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
use std::path::Path;
use std::path::PathBuf;
Expand Down Expand Up @@ -194,21 +194,16 @@ impl VmlinuxBtfDump {
None
};
unsafe {
let dump_opts = {
let mut uninit = MaybeUninit::<btf_dump_opts>::zeroed();
(*uninit.as_mut_ptr()).ctx = &mut rawfd as *mut _ as *mut _;
uninit.assume_init()
};
let dumpptr = btf_dump__new(
self.btfptr,
ptr::null(),
&dump_opts as *const _,
let dumpptr = libbpf_bindings::btf_dump__new(
self.btfptr as _,
Some(vdprintf_wrapper),
&mut rawfd as *mut _ as *mut _,
ptr::null(),
);
if (dumpptr as isize) < 0 {
return Err(TypeGenError::DumpError);
}
let dumpptr = BtfDumpWrapper(dumpptr);
let dumpptr = BtfDumpWrapper(dumpptr as _);
for type_id in 1..=btf__get_nr_types(self.btfptr) {
let btftypeptr = btf__type_by_id(self.btfptr, type_id);
let nameptr = btf__name_by_offset(self.btfptr, (*btftypeptr).name_off);
Expand Down Expand Up @@ -245,27 +240,22 @@ impl Drop for VmlinuxBtfDump {
}
}

// FIXME: remove libbpf_bindings in favor of libbpf-sys
// wrapping vdprintf to get rid of return type
#[cfg(not(any(target_arch = "arm", target_arch = "aarch64")))]
unsafe extern "C" fn vdprintf_wrapper(
ctx: *mut c_void,
format: *const c_char,
va_list: *mut super::__va_list_tag,
) {
let rawfd_wrapper = &*(ctx as *mut RawFdWrapper);
vdprintf(rawfd_wrapper.0, format, va_list);
}

// wrapping vdprintf to get rid of return type
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
unsafe extern "C" fn vdprintf_wrapper(
ctx: *mut c_void,
format: *const c_char,
#[cfg(target_env = "musl")] va_list: super::__isoc_va_list,
#[cfg(not(target_env = "musl"))] va_list: super::__gnuc_va_list,
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
#[cfg(target_env = "musl")]
va_list: libbpf_bindings::__isoc_va_list,
#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
#[cfg(not(target_env = "musl"))]
va_list: libbpf_bindings::__gnuc_va_list,
#[cfg(not(any(target_arch = "arm", target_arch = "aarch64")))]
va_list: *mut libbpf_bindings::__va_list_tag,
) {
let rawfd_wrapper = &*(ctx as *mut RawFdWrapper);
vdprintf(rawfd_wrapper.0, format, va_list);
libbpf_bindings::vdprintf(rawfd_wrapper.0, format, va_list);
}

pub fn get_custom_vmlinux_path() -> Option<PathBuf> {
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 @@ -50,7 +51,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
2 changes: 2 additions & 0 deletions redbpf-probes/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ fn generate_bindings_kernel_headers() -> Result<()> {
"__sk_.*",
"sk_.*",
"inet_sock",
"unix_sock",
"sockaddr",
"sockaddr_in",
"in_addr",
Expand Down Expand Up @@ -181,6 +182,7 @@ fn generate_bindings_vmlinux() -> Result<()> {
"^__sk_.*",
"^sk_.*",
"^inet_sock$",
"^unix_sock$",
"^sockaddr$",
"^sockaddr_in$",
"^in_addr$",
Expand Down
1 change: 1 addition & 0 deletions redbpf-probes/include/redbpf_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <uapi/linux/bpf.h>
#include <net/sock.h>
#include <net/inet_sock.h>
#include <net/af_unix.h>
#include <linux/ipv6.h>
#include "xdp.h"
#include "bpf_iter.h"
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

0 comments on commit af990ff

Please sign in to comment.