Skip to content

Commit

Permalink
agent: Generate vmlinux.h on the fly in build.rs
Browse files Browse the repository at this point in the history
This adds a logic to generate "vmlinux.h" in build.rs, needed for
compiling BPF programs.  If vmlinux.h is supplied by the makefile, it
will be copied to the build directory; otherwise bpftool will be used
to extract it from the running kernel.

Signed-off-by: Daiki Ueno <[email protected]>
  • Loading branch information
ueno committed Nov 22, 2023
1 parent 0e5d9d7 commit 417ab35
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 20 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ jobs:
- name: Install dependencies
run: dnf install -y cargo clang clippy kernel-devel libbpf-devel llvm-devel rustfmt
- name: Copy vmlinux.h
run: cp $(rpm -ql kernel-devel | grep '/vmlinux.h$' | tail -1) agent/src/bpf
run: |
cp $(rpm -ql kernel-devel | grep '/vmlinux.h$' | tail -1) agent/src/bpf
cp $(rpm -ql kernel-devel | grep '/vmlinux.h$' | tail -1) agent/tests/agenttest/src/bpf
- name: Build
run: cargo build --verbose
- name: Run tests
Expand Down
4 changes: 2 additions & 2 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ conffiles = \
.PHONY: all
all: $(programs)

agent/src/bpf/vmlinux.h:
agent/src/bpf/vmlinux.h agent/tests/agenttest/src/bpf/vmlinux.h:
bpftool btf dump file /sys/kernel/btf/vmlinux format c > $@-t && mv $@-t $@

$(programs): agent/src/bpf/vmlinux.h
$(programs): agent/src/bpf/vmlinux.h agent/tests/agenttest/src/bpf/vmlinux.h
cargo build --target-dir="${TARGETDIR}" ${CARGO_ARGS}

.PHONY: install-programs
Expand Down
40 changes: 32 additions & 8 deletions agent/build.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
// SPDX-License-Identifier: GPL-2.0

use libbpf_cargo::SkeletonBuilder;
use std::{env, path::PathBuf};

const SRC: &str = "src/bpf/audit.bpf.c";
use std::{
env,
fs::{self, File},
path::PathBuf,
process::Command,
};

fn main() {
let mut out =
let builddir =
PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR must be set in build script"));
out.push("audit.skel.rs");
let srcdir = PathBuf::from(
env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR must be set in build script"),
);

let vmlinux_h = srcdir.join("src").join("bpf").join("vmlinux.h");
if vmlinux_h.exists() {
fs::copy(&vmlinux_h, &builddir.join("vmlinux.h")).expect("unable to copy vmlinux.h");
} else {
let file = File::create(&builddir.join("vmlinux.h")).expect("unable to create vmlinux.h");
Command::new("bpftool")
.arg("btf")
.arg("dump")
.arg("file")
.arg("/sys/kernel/btf/vmlinux")
.arg("format")
.arg("c")
.stdout(file)
.status()
.expect("unable to run bpftool");
}
let src = srcdir.join("src").join("bpf").join("audit.bpf.c");
SkeletonBuilder::new()
.source(SRC)
.build_and_generate(&out)
.source(&src)
.clang_args(&format!("-I{}", builddir.display()))
.build_and_generate(&builddir.join("audit.skel.rs"))
.unwrap();
println!("cargo:rerun-if-changed={}", SRC);
println!("cargo:rerun-if-changed={}", src.display());
}
40 changes: 32 additions & 8 deletions agent/tests/agenttest/build.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
// SPDX-License-Identifier: GPL-2.0

use libbpf_cargo::SkeletonBuilder;
use std::{env, path::PathBuf};

const SRC: &str = "src/bpf/agent.bpf.c";
use std::{
env,
fs::{self, File},
path::PathBuf,
process::Command,
};

fn main() {
let mut out =
let builddir =
PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR must be set in build script"));
out.push("agent.skel.rs");
let srcdir = PathBuf::from(
env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR must be set in build script"),
);

let vmlinux_h = srcdir.join("src").join("bpf").join("vmlinux.h");
if vmlinux_h.exists() {
fs::copy(&vmlinux_h, &builddir.join("vmlinux.h")).expect("unable to copy vmlinux.h");
} else {
let file = File::create(&builddir.join("vmlinux.h")).expect("unable to create vmlinux.h");
Command::new("bpftool")
.arg("btf")
.arg("dump")
.arg("file")
.arg("/sys/kernel/btf/vmlinux")
.arg("format")
.arg("c")
.stdout(file)
.status()
.expect("unable to run bpftool");
}
let src = srcdir.join("src").join("bpf").join("agent.bpf.c");
SkeletonBuilder::new()
.source(SRC)
.build_and_generate(&out)
.source(&src)
.clang_args(&format!("-I{}", builddir.display()))
.build_and_generate(&builddir.join("agent.skel.rs"))
.unwrap();
println!("cargo:rerun-if-changed={}", SRC);
println!("cargo:rerun-if-changed={}", src.display());
}
1 change: 0 additions & 1 deletion agent/tests/agenttest/src/bpf/vmlinux.h

This file was deleted.

0 comments on commit 417ab35

Please sign in to comment.