From 8bad7241de46d68bb32c725f1b9064c177cfd2f9 Mon Sep 17 00:00:00 2001 From: xhuacmer Date: Thu, 9 Mar 2023 15:18:14 +0800 Subject: [PATCH] add CTinspector ebpf example migrate,mmap,monitor_address,function_call source file --- ebpf_example/Makefile | 22 ++++++++++++++++++++ ebpf_example/function_call.c | 20 ++++++++++++++++++ ebpf_example/migrate.c | 37 ++++++++++++++++++++++++++++++++++ ebpf_example/mmap.c | 19 +++++++++++++++++ ebpf_example/monitor_address.c | 20 ++++++++++++++++++ 5 files changed, 118 insertions(+) create mode 100644 ebpf_example/Makefile create mode 100644 ebpf_example/function_call.c create mode 100644 ebpf_example/migrate.c create mode 100644 ebpf_example/mmap.c create mode 100644 ebpf_example/monitor_address.c diff --git a/ebpf_example/Makefile b/ebpf_example/Makefile new file mode 100644 index 0000000..aaf7742 --- /dev/null +++ b/ebpf_example/Makefile @@ -0,0 +1,22 @@ +CFLAGS=-O2 -fno-inline -emit-llvm -I../ebpf_vm_executor +LINKFLAGS=-march=bpf -filetype=obj + +all: vm_mmap.o vm_monitor_address.o vm_function_call.o vm_migrate.o vm_clone.o + +vm_mmap.o: + clang $(CFLAGS) -c mmap.c -o - | llc $(LINKFLAGS) -o vm_mmap.o + +vm_monitor_address.o: + clang $(CFLAGS) -c monitor_address.c -o - | llc $(LINKFLAGS) -o vm_monitor_address.o + +vm_function_call.o: + clang $(CFLAGS) -c function_call.c -o - | llc $(LINKFLAGS) -o vm_function_call.o + +vm_migrate.o: + clang $(CFLAGS) -c migrate.c -o - | llc $(LINKFLAGS) -o vm_migrate.o + +vm_clone.o: + clang $(CFLAGS) -c clone.c -o - | llc $(LINKFLAGS) -o vm_clone.o + +clean: + rm -f vm_mmap.o vm_monitor_address.o vm_function_call.o vm_migrate.o vm_clone.o diff --git a/ebpf_example/function_call.c b/ebpf_example/function_call.c new file mode 100644 index 0000000..0d24ac9 --- /dev/null +++ b/ebpf_example/function_call.c @@ -0,0 +1,20 @@ +#include +#include +#include + +uint64_t test(uint64_t a, uint64_t b, uint64_t c, uint64_t d, uint64_t *e) +{ + uint64_t sum = a + b + c + d; + uint64_t *p1 = e + 1; + uint64_t *p2 = e + 2; + return sum + *p1 + *p2; +} + +uint64_t vm_main(void) +{ + uint64_t tmp[3] = {0}; + uint64_t sum = test(1, 2, 3, 4, tmp); + debug_print(sum); + + return 0; +} \ No newline at end of file diff --git a/ebpf_example/migrate.c b/ebpf_example/migrate.c new file mode 100644 index 0000000..c081549 --- /dev/null +++ b/ebpf_example/migrate.c @@ -0,0 +1,37 @@ +#include +#include +#include + +void test_migrate(struct ub_address *a, struct ub_address *b, int cnt) +{ + uint64_t msg = 1000; + int idx; + + for (idx = 0; idx < cnt; idx++) { + debug_print(msg); + msg += 1000; + migrate_to(a); + + debug_print(msg); + msg += 1000; + migrate_to(b); + } +} + +uint64_t vm_main(void) +{ + struct ub_address a = { + .access_key = 0, + .url = {192, 168, 100, 10, 7, 89} + }; + + struct ub_address b = { + .access_key = 0, + .url = {192, 168, 100, 20, 7, 89} + }; + + /* Migrate to 1.1.8.78:1881 */ + test_migrate(&a, &b, 20); + + return 0; +} \ No newline at end of file diff --git a/ebpf_example/mmap.c b/ebpf_example/mmap.c new file mode 100644 index 0000000..79aba8c --- /dev/null +++ b/ebpf_example/mmap.c @@ -0,0 +1,19 @@ +#include +#include +#include + +uint64_t vm_main(uint64_t host_va, uint64_t size, uint64_t test_value) +{ + uint64_t *vm_va; + + vm_va = (uint64_t *)mmap(host_va, size); + if (vm_va == INVALID_MMAP_ADDR) { + return -1; + } + + debug_print(*vm_va); + *vm_va = test_value; + debug_print(*vm_va); + + return 0; +} \ No newline at end of file diff --git a/ebpf_example/monitor_address.c b/ebpf_example/monitor_address.c new file mode 100644 index 0000000..2a296de --- /dev/null +++ b/ebpf_example/monitor_address.c @@ -0,0 +1,20 @@ +#include +#include +#include + +uint64_t vm_main(uint64_t host_va, uint64_t size, uint64_t test_value) +{ + uint64_t *vm_va; + + vm_va = (uint64_t *)mmap(host_va, size); + if (vm_va == INVALID_MMAP_ADDR) { + return -1; + } + + *vm_va = 0; + monitor_address(MONITOR_T_EQUAL_VALUE, (uint64_t)vm_va, test_value, 0); + wait_for_address_event(); + debug_print(*vm_va); + + return 0; +} \ No newline at end of file -- Gitee