diff options
| author | ruki <[email protected]> | 2021-12-07 23:23:40 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-12-07 23:23:40 +0800 |
| commit | b3ae4feec5afe623183d92f8f3f9f0347f6abaea (patch) | |
| tree | 7c011f929a2902aeec3c56a31949c3755b367b5c /tests/projects/linux | |
| parent | 9d19f1a747c0ebf692fbeca25a78f41b7fcd55e3 (diff) | |
add linux driver example
Diffstat (limited to 'tests/projects/linux')
| -rw-r--r-- | tests/projects/linux/bpf/minimal/src/minimal.bpf.c | 21 | ||||
| -rw-r--r-- | tests/projects/linux/bpf/minimal/src/minimal.c | 73 | ||||
| -rw-r--r-- | tests/projects/linux/bpf/minimal/test.lua | 6 | ||||
| -rw-r--r-- | tests/projects/linux/bpf/minimal/xmake.lua | 19 | ||||
| -rw-r--r-- | tests/projects/linux/driver/hello/Makefile | 13 | ||||
| -rw-r--r-- | tests/projects/linux/driver/hello/hello.c | 21 |
6 files changed, 153 insertions, 0 deletions
diff --git a/tests/projects/linux/bpf/minimal/src/minimal.bpf.c b/tests/projects/linux/bpf/minimal/src/minimal.bpf.c new file mode 100644 index 000000000..ea1eeefbb --- /dev/null +++ b/tests/projects/linux/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/linux/bpf/minimal/src/minimal.c b/tests/projects/linux/bpf/minimal/src/minimal.c new file mode 100644 index 000000000..4b6ec3181 --- /dev/null +++ b/tests/projects/linux/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/linux/bpf/minimal/test.lua b/tests/projects/linux/bpf/minimal/test.lua new file mode 100644 index 000000000..2856a3831 --- /dev/null +++ b/tests/projects/linux/bpf/minimal/test.lua @@ -0,0 +1,6 @@ +function main(t) + if is_host("linux") and os.arch() == "x86_64" then + os.vrun("xmake f -y -p android -vD") + os.vrun("xmake -y -vD") + end +end diff --git a/tests/projects/linux/bpf/minimal/xmake.lua b/tests/projects/linux/bpf/minimal/xmake.lua new file mode 100644 index 000000000..8507b8aa1 --- /dev/null +++ b/tests/projects/linux/bpf/minimal/xmake.lua @@ -0,0 +1,19 @@ +add_rules("mode.release", "mode.debug") +add_rules("platform.linux.bpf") + +add_requires("linux-tools", {configs = {bpftool = true}}) +add_requires("libbpf") +if is_plat("android") then + add_requires("ndk >=22.x") + set_toolchains("@ndk", {sdkver = "23"}) +else + add_requires("llvm >=10.x") + set_toolchains("@llvm") + add_requires("linux-headers") +end + +target("minimal") + set_kind("binary") + add_files("src/*.c") + add_packages("linux-tools", "linux-headers", "libbpf") + set_license("GPL-2.0") diff --git a/tests/projects/linux/driver/hello/Makefile b/tests/projects/linux/driver/hello/Makefile new file mode 100644 index 000000000..d69924339 --- /dev/null +++ b/tests/projects/linux/driver/hello/Makefile @@ -0,0 +1,13 @@ +ifneq ($(KERNELRELEASE),) + obj-m := hello.o +else + KERN_DIR ?= /usr/src/linux-headers-5.11.0-41-generic/ + PWD := $(shell pwd) + +default: + $(MAKE) -C $(KERN_DIR) V=1 M=$(PWD) modules +endif + +clean: + rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions + diff --git a/tests/projects/linux/driver/hello/hello.c b/tests/projects/linux/driver/hello/hello.c new file mode 100644 index 000000000..2614fb2cc --- /dev/null +++ b/tests/projects/linux/driver/hello/hello.c @@ -0,0 +1,21 @@ +#include <linux/init.h> +#include <linux/module.h> + +MODULE_LICENSE("Dual BSD/GPL"); +MODULE_AUTHOR("Ruki"); +MODULE_DESCRIPTION("A simple Hello World Module"); +MODULE_ALIAS("a simplest module"); + +int hello_init(void) +{ + printk(KERN_INFO "Hello World\n"); + return 0; +} + +void hello_exit(void) +{ + printk(KERN_INFO "Goodbye World\n"); +} + +module_init(hello_init); +module_exit(hello_exit); |
