diff options
| author | ruki <[email protected]> | 2021-03-11 00:43:27 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-03-11 00:43:27 +0800 |
| commit | dc85d553a8d5ec0214f535d1d4ef584218bb50fa (patch) | |
| tree | 17caa5576ab97d44d38a72c7bd0bba00ae4d4069 /tests/projects | |
| parent | 035f310d1076abdab7f3f43d925431e1b906b08c (diff) | |
add bpf rule
Diffstat (limited to 'tests/projects')
| -rw-r--r-- | tests/projects/bpf/minimal/src/minimal.bpf.c | 21 | ||||
| -rw-r--r-- | tests/projects/bpf/minimal/src/minimal.c | 73 | ||||
| -rw-r--r-- | tests/projects/bpf/minimal/xmake.lua | 12 |
3 files changed, 106 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") |
