diff --git a/.gitmodules b/.gitmodules index ba4d04289b596be3259aef635c1156202f947c7d..cef0b3949b91f95a067df04cce181fb50ebc9e27 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,3 @@ -[submodule "source/lib/internal/ebpf/libbpf_latest"] - path = source/lib/internal/ebpf/libbpf_latest - url = https://gitee.com/anolis/libbpf [submodule "source/lib/internal/ebpf/coolbpf"] path = source/lib/internal/ebpf/coolbpf url = https://gitee.com/anolis/coolbpf.git diff --git a/configure b/configure index 65e12d342ace245c6b3375a6028b0aece0956181..5a790f1f24b4f3523edac299caec0e594667c0b2 100755 --- a/configure +++ b/configure @@ -55,12 +55,9 @@ check_bpf_kernel() { } check_bpf_depend() { - if [ ! -d "source/lib/internal/ebpf/libbpf" ];then - echo "download libbpf ... " - wget https://gitee.com/anolis/libbpf/repository/archive/master.zip -O libbpf.zip - echo "extract libbpf" - unzip libbpf.zip -d source/lib/internal/ebpf/ - mv source/lib/internal/ebpf/libbpf-master source/lib/internal/ebpf/libbpf + if [ ! -d "source/lib/internal/ebpf/coolbpf/build.sh" ];then + echo "Cloning coolbpf" + git submodule update --init fi check_bpf_kernel diff --git a/source/lib/Makefile b/source/lib/Makefile index c46897f8849515d40ad0a7bc3692b71e55db5e94..08bafc202d295550883e4aeb5d5c24d4ac1875c8 100644 --- a/source/lib/Makefile +++ b/source/lib/Makefile @@ -10,10 +10,7 @@ else endif make -C uapi ifeq ($(BUILD_LIBBPF), YES) - make -C internal/ebpf/libbpf/src BUILD_STATIC_ONLY=1 \ - OBJDIR=$(OBJ_LIB_PATH)/libbpf DESTDIR=$(OBJ_LIB_PATH) \ - INCLUDEDIR= LIBDIR= UAPIDIR= \ - install + OBJDIR=$(OBJ_LIB_PATH)/coolbpf DESTDIR=$(OBJ_LIB_PATH) sh internal/ebpf/coolbpf/build.sh endif clean: diff --git a/source/lib/internal/ebpf/coolbpf b/source/lib/internal/ebpf/coolbpf index 56f2b053e576716f7d4d80bb70d5e180ef278924..0047bf732795584bf573fcd83cbfeff67bdc2ef5 160000 --- a/source/lib/internal/ebpf/coolbpf +++ b/source/lib/internal/ebpf/coolbpf @@ -1 +1 @@ -Subproject commit 56f2b053e576716f7d4d80bb70d5e180ef278924 +Subproject commit 0047bf732795584bf573fcd83cbfeff67bdc2ef5 diff --git a/source/lib/internal/ebpf/libbpf_latest b/source/lib/internal/ebpf/libbpf_latest deleted file mode 160000 index d9ed1691f0e0d7168cde3e91413f872075ea47d7..0000000000000000000000000000000000000000 --- a/source/lib/internal/ebpf/libbpf_latest +++ /dev/null @@ -1 +0,0 @@ -Subproject commit d9ed1691f0e0d7168cde3e91413f872075ea47d7 diff --git a/source/mk/bpf.mk b/source/mk/bpf.mk index 5d2b6f54cf9cfa4e3e2f0c99c24ebe8d467eadc6..0b6e17e0fe31f9ffb74debde1756a8ebbc6e1ffc 100644 --- a/source/mk/bpf.mk +++ b/source/mk/bpf.mk @@ -4,7 +4,7 @@ BPFTOOL ?= $(SRC)/lib/internal/ebpf/tools/bpftool APPS_DIR := $(abspath .) prefix ?= /usr/local ARCH := $(shell uname -m | sed 's/x86_64/x86/') -LIBBPF_OBJ += $(OBJ_LIB_PATH)/libbpf.a +COOLBPF_OBJ += $(OBJ_LIB_PATH)/libbpf.a $(OBJ_LIB_PATH)/coolbpf.a ifeq ($(KERNEL_DEPEND), Y) TARGET_PATH := $(OBJ_TOOLS_PATH) @@ -42,7 +42,7 @@ bpfskel := $(patsubst %.bpf.o, %.skel.h, $(target_bpfobjs)) all: $(target) target_rule -$(target): $(target_cobjs) $(bpfskel) $(LIBBPF_OBJ) +$(target): $(target_cobjs) $(bpfskel) $(COOLBPF_OBJ) $(call msg,BINARY,$@) $(Q)$(CC) $(CFLAGS) $(INCLUDES) $^ -lelf -lz -o $(TARGET_PATH)/$@ -L$(OBJ_LIB_PATH) $(LDFLAGS) $(target_cobjs): $(cobjs) diff --git a/source/mk/bpfso.mk b/source/mk/bpfso.mk new file mode 100644 index 0000000000000000000000000000000000000000..b60b3d9821e44b23490082ff1aa4a6c469818fc2 --- /dev/null +++ b/source/mk/bpfso.mk @@ -0,0 +1,78 @@ +CLANG ?= clang +LLVM_STRIP ?= llvm-strip +BPFTOOL ?= $(SRC)/lib/internal/ebpf/tools/bpftool +APPS_DIR := $(abspath .) +prefix ?= /usr/local +ARCH := $(shell uname -m | sed 's/x86_64/x86/') +COOLBPF_OBJ += $(OBJ_LIB_PATH)/libbpf.so $(OBJ_LIB_PATH)/libcoolbpf.so + +ifeq ($(KERNEL_DEPEND), Y) +TARGET_PATH := $(OBJ_TOOLS_PATH) +else +TARGET_PATH := $(OBJ_TOOLS_ROOT) +endif + +DEPEND := "prev{btf}" + +SHARED_CFLAGS += -fPIC -fvisibility=hidden -DSHARED +CFLAGS += $(EXTRA_CLFAGS) -g -O2 -Wall +LDFLAGS += $(EXTRA_LDFLAGS) -L$(OBJ_LIB_PATH) -lbpf -lcoolbpf -lelf -lz +INCLUDES += $(EXTRA_INCLUDES) -I$(OBJPATH) -I$(SRC)/lib/internal/ebpf -I$(TARGET_PATH) -I$(OBJ_LIB_PATH) -I$(SRC)/lib/internal/ebpf/libbpf/include/uapi -I$(SRC)/lib/uapi/include + +ifeq ($(V),1) + Q = + msg = +else + Q = @ + msg = @printf ' %-8s %s%s\n' \ + "$(1)" \ + "$(patsubst $(abspath $(TARGET_PATH))/%,%,$(2))" \ + "$(if $(3), $(3))"; + MAKEFLAGS += --no-print-directory +endif + +newdirs := $(addprefix $(OBJPATH)/, $(newdirs)) + +cobjs := $(patsubst %.c, %.o, $(csrcs)) +target_cobjs := $(foreach n, $(cobjs), $(OBJPATH)/$(n)) + +bpfobjs := $(patsubst %.c, %.o, $(bpfsrcs)) +target_bpfobjs := $(foreach n, $(bpfobjs), $(OBJPATH)/$(n)) + +bpfskel := $(patsubst %.bpf.o, %.skel.h, $(target_bpfobjs)) + +all: $(target) target_rule + +$(target): $(target_cobjs) $(bpfskel) $(COOLBPF_OBJ) + $(call msg,BINARY,$@) + $(Q)$(CC) $(CFLAGS) -shared -Wl,-soname,$(target) $(INCLUDES) $^ -o $(TARGET_PATH)/$@ $(LDFLAGS) +$(target_cobjs): $(cobjs) + +$(cobjs): %.o : %.c $(bpfskel) + $(call msg,CC,$@) + $(Q)$(CC) $(CFLAGS) $(SHARED_CFLAGS) $(INCLUDES) -c $< -o $(OBJPATH)/$@ + +$(bpfskel): %.skel.h : %.bpf.o $(target_bpfobjs) + $(call msg,GEN-SKEL,$@) + $(Q)$(BPFTOOL) gen skeleton $< > $@ + +$(target_bpfobjs): $(bpfobjs) + +NEWVER := $(shell echo $(KERNEL_VERSION)|awk -F. '{if($$1>3) print 1; else print 0}') +ifeq ($(NEWVER),1) + KERN_VER := -DVER310_LATER +endif +$(bpfobjs) : %.o : %.c dirs + $(call msg,BPF,$@) + $(CLANG) -g -O2 $(KERN_VER) -target bpf -D__TARGET_ARCH_$(ARCH) $(INCLUDES) -c $< -o $(OBJPATH)/$@ + $(Q)$(LLVM_STRIP) -g $(OBJPATH)/$@ # strip useless DWARF info + +dirs: + mkdir -p $(newdirs) + +# delete failed targets +.DELETE_ON_ERROR: + +# keep intermediate (.skel.h, .bpf.o, etc) targets +# .SECONDARY: +include $(SRC)/mk/target.inc diff --git a/source/tools/test/bpf_so_test/Makefile b/source/tools/test/bpf_so_test/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..39af343429182f6f2a9260211d54dd55f5dcb8fa --- /dev/null +++ b/source/tools/test/bpf_so_test/Makefile @@ -0,0 +1,8 @@ + +newdirs := $(shell find ./ -type d) + +bpfsrcs := $(wildcard bpf/*.bpf.c) +csrcs := $(wildcard *.c) +target := bpfsotest.so + +include $(SRC)/mk/bpfso.mk \ No newline at end of file diff --git a/source/tools/test/bpf_so_test/bpf/bpftest1.bpf.c b/source/tools/test/bpf_so_test/bpf/bpftest1.bpf.c new file mode 100644 index 0000000000000000000000000000000000000000..bd4eee7d6557821f8da4cb310afe4a2489426733 --- /dev/null +++ b/source/tools/test/bpf_so_test/bpf/bpftest1.bpf.c @@ -0,0 +1,16 @@ +#include +#include +#include +#include +#include "../bpftest.h" + +SEC("kprobe/tcp_v4_connect") +int BPF_KPROBE(kprobe_tcp_v4_connect, struct sock *sk, struct sockaddr *uaddr, int addr_len) +{ + u64 tgid = bpf_get_current_pid_tgid(); + int tmp; + BPF_CORE_READ_INTO(&tmp, sk, sk_rcvbuf); + return 0; +} + +char LICENSE[] SEC("license") = "GPL"; diff --git a/source/tools/test/bpf_so_test/bpf/bpftest2.bpf.c b/source/tools/test/bpf_so_test/bpf/bpftest2.bpf.c new file mode 100644 index 0000000000000000000000000000000000000000..7f2298cea7bf6db8526e80fdadd2072245817e19 --- /dev/null +++ b/source/tools/test/bpf_so_test/bpf/bpftest2.bpf.c @@ -0,0 +1,19 @@ +#include +#include +#include +#include +#include "../bpftest.h" + + + +SEC("kprobe/tcp_v4_connect") +int BPF_KPROBE(kprobe_tcp_v4_connect,struct sock *sk, struct sockaddr *uaddr, int addr_len) +{ + u64 tgid = bpf_get_current_pid_tgid(); + int tmp; + BPF_CORE_READ_INTO(&tmp, sk, sk_rcvbuf); + return 0; +} + + +char LICENSE[] SEC("license") = "GPL"; diff --git a/source/tools/test/bpf_so_test/bpftest.c b/source/tools/test/bpf_so_test/bpftest.c new file mode 100644 index 0000000000000000000000000000000000000000..adaf2f38f8acd093499c60032849cd92642973c4 --- /dev/null +++ b/source/tools/test/bpf_so_test/bpftest.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include "bpftest.h" +#include "bpf/bpftest1.skel.h" +#include "bpf/bpftest2.skel.h" + +static int libbpf_print_fn(enum libbpf_print_level level, + const char *format, va_list args) +{ + // if (level == LIBBPF_DEBUG && !env.verbose) + // return 0; + return vfprintf(stderr, format, args); +} + +#define LOAD_BPF_SKEL(name) \ + ( \ + { \ + __label__ load_bpf_skel_out; \ + int __ret = 0; \ + name = name##_bpf__open(); \ + if (!name) \ + { \ + printf("failed to open BPF object\n"); \ + __ret = -1; \ + goto load_bpf_skel_out; \ + } \ + __ret = name##_bpf__load(name); \ + if (__ret) \ + { \ + printf("failed to load BPF object: %d\n", err); \ + goto load_bpf_skel_out; \ + } \ + __ret = name##_bpf__attach(name); \ + if (__ret) \ + { \ + printf("failed to attach BPF programs: %s\n", strerror(-err)); \ + goto load_bpf_skel_out; \ + } \ + load_bpf_skel_out: \ + __ret; \ + }) + +int test_main(int argc, char **argv) +{ + struct bpftest1_bpf *bpftest1 = NULL; + struct bpftest2_bpf *bpftest2 = NULL; + int err = 0; + libbpf_set_print(libbpf_print_fn); + + printf("coolbpf version is: %s\n", coolbpf_version_string()); + + err = LOAD_BPF_SKEL(bpftest1); + if (err) + goto cleanup; + + printf("bpftest1 program load done.\n"); + err = LOAD_BPF_SKEL(bpftest2); + if (err) + goto cleanup; + + printf("bpftest2 program load done, test finished. exit.\n"); + // while(1){} +cleanup: + // destory the bpf program + bpftest1_bpf__destroy(bpftest1); + bpftest2_bpf__destroy(bpftest2); + return 0; +} diff --git a/source/tools/test/bpf_so_test/bpftest.h b/source/tools/test/bpf_so_test/bpftest.h new file mode 100644 index 0000000000000000000000000000000000000000..7042b35e6437836000e603907709918e59182e13 --- /dev/null +++ b/source/tools/test/bpf_so_test/bpftest.h @@ -0,0 +1,4 @@ +#ifndef __BPFTEST_H +#define __BPFTEST_H + +#endif \ No newline at end of file diff --git a/source/tools/test/bpf_test/bpftest.c b/source/tools/test/bpf_test/bpftest.c index 552754688d9108fc0b661c4426185518d576679c..21c01b143d604e99e8154e15c1722a42a78d1f01 100644 --- a/source/tools/test/bpf_test/bpftest.c +++ b/source/tools/test/bpf_test/bpftest.c @@ -1,5 +1,6 @@ #include #include +#include #include "bpftest.h" #include "bpf/bpftest1.skel.h" #include "bpf/bpftest2.skel.h" @@ -47,6 +48,8 @@ int main(int argc, char **argv) int err = 0; libbpf_set_print(libbpf_print_fn); + printf("coolbpf version is: %s\n", coolbpf_version_string()); + err = LOAD_BPF_SKEL(bpftest1); if (err) goto cleanup;