[PATCH v1 bpf-next 2/9] bpftool: add libbpf-rs dependency and minimal bpf program

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Setting up the basic requirements to build a bpf program using
libbpf-rs.

    $ cargo test
       Compiling bpftool_tests v0.1.0
    (/data/users/chantra/bpf-next/tools/bpf/bpftool/testing)
        Finished test [unoptimized + debuginfo] target(s) in 0.64s
         Running unittests src/main.rs
    (target/debug/deps/bpftool_tests-b5112057d979eb52)

    running 1 test
    test bpftool_tests::run_bpftool ... ok

    test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered
    out; finished in 0.00s

Signed-off-by: Manu Bretelle <chantr4@xxxxxxxxx>
---
 .../selftests/bpf/bpftool_tests/Cargo.toml    |  7 +++++++
 .../selftests/bpf/bpftool_tests/build.rs      | 17 ++++++++++++++++
 .../bpftool_tests/src/bpf/bpftool_tests.bpf.c | 20 +++++++++++++++++++
 .../bpf/bpftool_tests/src/bpf/vmlinux.h       |  1 +
 .../bpf/bpftool_tests/src/bpftool_tests.rs    |  4 ++++
 5 files changed, 49 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/bpftool_tests/build.rs
 create mode 100644 tools/testing/selftests/bpf/bpftool_tests/src/bpf/bpftool_tests.bpf.c
 create mode 120000 tools/testing/selftests/bpf/bpftool_tests/src/bpf/vmlinux.h

diff --git a/tools/testing/selftests/bpf/bpftool_tests/Cargo.toml b/tools/testing/selftests/bpf/bpftool_tests/Cargo.toml
index 34df3002003f..35c834082351 100644
--- a/tools/testing/selftests/bpf/bpftool_tests/Cargo.toml
+++ b/tools/testing/selftests/bpf/bpftool_tests/Cargo.toml
@@ -2,3 +2,10 @@
 name = "bpftool_tests"
 version = "0.1.0"
 edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+[dependencies]
+libbpf-rs = "0.21"
+
+[build-dependencies]
+libbpf-cargo = "0.21"
diff --git a/tools/testing/selftests/bpf/bpftool_tests/build.rs b/tools/testing/selftests/bpf/bpftool_tests/build.rs
new file mode 100644
index 000000000000..ce01824fcd1d
--- /dev/null
+++ b/tools/testing/selftests/bpf/bpftool_tests/build.rs
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
+use libbpf_cargo::SkeletonBuilder;
+use std::env;
+use std::path::PathBuf;
+
+const SRC: &str = "src/bpf/bpftool_tests.bpf.c";
+
+fn main() {
+    let mut out =
+        PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR must be set in build script"));
+    out.push("bpftool_tests.skel.rs");
+    SkeletonBuilder::new()
+        .source(SRC)
+        .build_and_generate(&out)
+        .unwrap();
+    println!("cargo:rerun-if-changed={SRC}");
+}
diff --git a/tools/testing/selftests/bpf/bpftool_tests/src/bpf/bpftool_tests.bpf.c b/tools/testing/selftests/bpf/bpftool_tests/src/bpf/bpftool_tests.bpf.c
new file mode 100644
index 000000000000..8b92171145de
--- /dev/null
+++ b/tools/testing/selftests/bpf/bpftool_tests/src/bpf/bpftool_tests.bpf.c
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+
+char LICENSE[] SEC("license") = "Dual BSD/GPL";
+
+int my_pid = 0;
+
+SEC("tp/syscalls/sys_enter_write")
+int handle_tp(void *ctx)
+{
+	int pid = bpf_get_current_pid_tgid() >> 32;
+
+	if (pid != my_pid)
+		return 0;
+
+	bpf_printk("BPF triggered from PID %d.\n", pid);
+
+	return 0;
+}
diff --git a/tools/testing/selftests/bpf/bpftool_tests/src/bpf/vmlinux.h b/tools/testing/selftests/bpf/bpftool_tests/src/bpf/vmlinux.h
new file mode 120000
index 000000000000..f9515b260426
--- /dev/null
+++ b/tools/testing/selftests/bpf/bpftool_tests/src/bpf/vmlinux.h
@@ -0,0 +1 @@
+../../../tools/include/vmlinux.h
\ No newline at end of file
diff --git a/tools/testing/selftests/bpf/bpftool_tests/src/bpftool_tests.rs b/tools/testing/selftests/bpf/bpftool_tests/src/bpftool_tests.rs
index 251dbf3861fe..35eb35831dce 100644
--- a/tools/testing/selftests/bpf/bpftool_tests/src/bpftool_tests.rs
+++ b/tools/testing/selftests/bpf/bpftool_tests/src/bpftool_tests.rs
@@ -1,4 +1,8 @@
 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
+mod bpftool_tests_skel {
+    include!(concat!(env!("OUT_DIR"), "/bpftool_tests.skel.rs"));
+}
+
 use std::process::Command;
 
 const BPFTOOL_PATH_ENV: &str = "BPFTOOL_PATH";
-- 
2.39.3





[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux