summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-03-11 00:43:27 +0800
committerruki <[email protected]>2021-03-11 00:43:27 +0800
commitdc85d553a8d5ec0214f535d1d4ef584218bb50fa (patch)
tree17caa5576ab97d44d38a72c7bd0bba00ae4d4069
parent035f310d1076abdab7f3f43d925431e1b906b08c (diff)
add bpf rule
-rw-r--r--tests/projects/bpf/minimal/src/minimal.bpf.c21
-rw-r--r--tests/projects/bpf/minimal/src/minimal.c73
-rw-r--r--tests/projects/bpf/minimal/xmake.lua12
-rw-r--r--xmake/rules/platform/linux/bpf/xmake.lua43
4 files changed, 149 insertions, 0 deletions
diff --git a/tests/projects/bpf/minimal/src/minimal.bpf.c b/tests/projects/bpf/minimal/src/minimal.bpf.c
new file mode 100644
index 000000000..ea1eeefbb
--- /dev/null
+++ b/tests/projects/bpf/minimal/src/minimal.bpf.c
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
+/* Copyright (c) 2020 Facebook */
+#include <linux/bpf.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/tests/projects/bpf/minimal/src/minimal.c b/tests/projects/bpf/minimal/src/minimal.c
new file mode 100644
index 000000000..4b6ec3181
--- /dev/null
+++ b/tests/projects/bpf/minimal/src/minimal.c
@@ -0,0 +1,73 @@
+// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
+/* Copyright (c) 2020 Facebook */
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/resource.h>
+#include <bpf/libbpf.h>
+#include "minimal.skel.h"
+
+static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
+{
+ return vfprintf(stderr, format, args);
+}
+
+static void bump_memlock_rlimit(void)
+{
+ struct rlimit rlim_new = {
+ .rlim_cur = RLIM_INFINITY,
+ .rlim_max = RLIM_INFINITY,
+ };
+
+ if (setrlimit(RLIMIT_MEMLOCK, &rlim_new)) {
+ fprintf(stderr, "Failed to increase RLIMIT_MEMLOCK limit!\n");
+ exit(1);
+ }
+}
+
+int main(int argc, char **argv)
+{
+ struct minimal_bpf *skel;
+ int err;
+
+ /* Set up libbpf errors and debug info callback */
+ libbpf_set_print(libbpf_print_fn);
+
+ /* Bump RLIMIT_MEMLOCK to allow BPF sub-system to do anything */
+ bump_memlock_rlimit();
+
+ /* Open BPF application */
+ skel = minimal_bpf__open();
+ if (!skel) {
+ fprintf(stderr, "Failed to open BPF skeleton\n");
+ return 1;
+ }
+
+ /* ensure BPF program only handles write() syscalls from our process */
+ skel->bss->my_pid = getpid();
+
+ /* Load & verify BPF programs */
+ err = minimal_bpf__load(skel);
+ if (err) {
+ fprintf(stderr, "Failed to load and verify BPF skeleton\n");
+ goto cleanup;
+ }
+
+ /* Attach tracepoint handler */
+ err = minimal_bpf__attach(skel);
+ if (err) {
+ fprintf(stderr, "Failed to attach BPF skeleton\n");
+ goto cleanup;
+ }
+
+ printf("Successfully started!\n");
+
+ for (;;) {
+ /* trigger our BPF program */
+ fprintf(stderr, ".");
+ sleep(1);
+ }
+
+cleanup:
+ minimal_bpf__destroy(skel);
+ return -err;
+}
diff --git a/tests/projects/bpf/minimal/xmake.lua b/tests/projects/bpf/minimal/xmake.lua
new file mode 100644
index 000000000..2b838a4e8
--- /dev/null
+++ b/tests/projects/bpf/minimal/xmake.lua
@@ -0,0 +1,12 @@
+add_rules("mode.release", "mode.debug")
+add_requires("linux-tools", {alias = "bpf", configs = {bpf = true}})
+add_requires("linux-headers")
+
+target("minimal")
+ set_kind("binary")
+ add_rules("platform.linux.bpf")
+ add_files("src/minimal.c")
+ add_files("src/minimal.bpf.c")
+ add_packages("bpf", "linux-headers")
+ set_toolchains("clang")
+ set_license("GPL-2.0")
diff --git a/xmake/rules/platform/linux/bpf/xmake.lua b/xmake/rules/platform/linux/bpf/xmake.lua
new file mode 100644
index 000000000..d9bec3fc6
--- /dev/null
+++ b/xmake/rules/platform/linux/bpf/xmake.lua
@@ -0,0 +1,43 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file xmake.lua
+--
+
+-- add *.bpf.c for linux bpf program
+-- @see https://github.com/xmake-io/xmake/issues/1274
+rule("platform.linux.bpf")
+ set_extensions(".bpf.c")
+ on_config(function (target)
+ local headerdir = path.join(target:autogendir(), "rules", "bpf")
+ target:add("includedirs", headerdir)
+ end)
+ before_buildcmd_file(function (target, batchcmds, sourcefile, opt)
+ local headerfile = path.join(target:autogendir(), "rules", "bpf", (path.filename(sourcefile):gsub("%.bpf%.c", ".skel.h")))
+ local objectfile = path.join(target:autogendir(), "rules", "bpf", (path.filename(sourcefile):gsub("%.bpf%.c", ".bpf.o")))
+ target:add("includedirs", path.directory(headerfile))
+ batchcmds:show_progress(opt.progress, "${color.build.object}compiling.bpf %s", sourcefile)
+ batchcmds:mkdir(path.directory(objectfile))
+ batchcmds:compile(sourcefile, objectfile, {configs = {force = {cxflags = {"-target bpf", "-g"}, defines = "__TARGET_ARCH_x86"}}})
+ batchcmds:mkdir(path.directory(headerfile))
+ batchcmds:vrunv("llvm-strip", {"-g", objectfile})
+ batchcmds:vrunv("bpftool", {"gen", "skeleton", objectfile}, {stdout = headerfile})
+ batchcmds:add_depfiles(sourcefile)
+ batchcmds:set_depmtime(os.mtime(headerfile))
+ batchcmds:set_depcache(target:dependfile(headerfile))
+ end)
+