From dcc7fd11d8daf39dfa0ba922089b35716ca1c6b6 Mon Sep 17 00:00:00 2001 From: Shuyi Cheng Date: Thu, 22 Dec 2022 13:15:56 +0800 Subject: [PATCH 1/6] coolbpf: update coolbpf version Signed-off-by: Shuyi Cheng --- source/lib/internal/ebpf/coolbpf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/lib/internal/ebpf/coolbpf b/source/lib/internal/ebpf/coolbpf index 56f2b053..0eeb1fb2 160000 --- a/source/lib/internal/ebpf/coolbpf +++ b/source/lib/internal/ebpf/coolbpf @@ -1 +1 @@ -Subproject commit 56f2b053e576716f7d4d80bb70d5e180ef278924 +Subproject commit 0eeb1fb2d7f6b91f871dbb6ec74f2e1fcd1c03f9 -- Gitee From df866ca236897faebcb01a8e81a067a19c3477cb Mon Sep 17 00:00:00 2001 From: Shuyi Cheng Date: Thu, 22 Dec 2022 13:16:26 +0800 Subject: [PATCH 2/6] coolbpf: compile coolbpf instead of libbpf Signed-off-by: Shuyi Cheng --- source/lib/Makefile | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/source/lib/Makefile b/source/lib/Makefile index c46897f8..08bafc20 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: -- Gitee From 2a31781e1ed4640061ec12e9a1d61108a27aa8ec Mon Sep 17 00:00:00 2001 From: Shuyi Cheng Date: Thu, 22 Dec 2022 13:17:24 +0800 Subject: [PATCH 3/6] bpf.mk: change libbpf to coolbpf Signed-off-by: Shuyi Cheng --- source/mk/bpf.mk | 4 ++-- source/tools/test/bpf_test/bpftest.c | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/source/mk/bpf.mk b/source/mk/bpf.mk index 5d2b6f54..0b6e17e0 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/tools/test/bpf_test/bpftest.c b/source/tools/test/bpf_test/bpftest.c index 55275468..21c01b14 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; -- Gitee From 32727a6a9fcd053f74a635badde7d97b15bc48d1 Mon Sep 17 00:00:00 2001 From: Shuyi Cheng Date: Thu, 22 Dec 2022 13:45:08 +0800 Subject: [PATCH 4/6] mk: test: add bpfso.mk to generate shared object and add releated test Signed-off-by: Shuyi Cheng --- source/lib/internal/ebpf/coolbpf | 2 +- source/mk/bpfso.mk | 78 +++++++++++++++++++ source/tools/test/bpf_so_test/Makefile | 8 ++ .../tools/test/bpf_so_test/bpf/bpftest1.bpf.c | 16 ++++ .../tools/test/bpf_so_test/bpf/bpftest2.bpf.c | 19 +++++ source/tools/test/bpf_so_test/bpftest.c | 69 ++++++++++++++++ source/tools/test/bpf_so_test/bpftest.h | 4 + 7 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 source/mk/bpfso.mk create mode 100644 source/tools/test/bpf_so_test/Makefile create mode 100644 source/tools/test/bpf_so_test/bpf/bpftest1.bpf.c create mode 100644 source/tools/test/bpf_so_test/bpf/bpftest2.bpf.c create mode 100644 source/tools/test/bpf_so_test/bpftest.c create mode 100644 source/tools/test/bpf_so_test/bpftest.h diff --git a/source/lib/internal/ebpf/coolbpf b/source/lib/internal/ebpf/coolbpf index 0eeb1fb2..0047bf73 160000 --- a/source/lib/internal/ebpf/coolbpf +++ b/source/lib/internal/ebpf/coolbpf @@ -1 +1 @@ -Subproject commit 0eeb1fb2d7f6b91f871dbb6ec74f2e1fcd1c03f9 +Subproject commit 0047bf732795584bf573fcd83cbfeff67bdc2ef5 diff --git a/source/mk/bpfso.mk b/source/mk/bpfso.mk new file mode 100644 index 00000000..b60b3d98 --- /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 00000000..39af3434 --- /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 00000000..bd4eee7d --- /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 00000000..7f2298ce --- /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 00000000..adaf2f38 --- /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 00000000..7042b35e --- /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 -- Gitee From 122baab8bb3dd0e755eed900c05bc9e904dfe9ac Mon Sep 17 00:00:00 2001 From: Shuyi Cheng Date: Thu, 22 Dec 2022 13:53:33 +0800 Subject: [PATCH 5/6] remove libbpf_latest submodule Signed-off-by: Shuyi Cheng --- .gitmodules | 3 --- source/lib/internal/ebpf/libbpf_latest | 1 - 2 files changed, 4 deletions(-) delete mode 160000 source/lib/internal/ebpf/libbpf_latest diff --git a/.gitmodules b/.gitmodules index ba4d0428..cef0b394 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/source/lib/internal/ebpf/libbpf_latest b/source/lib/internal/ebpf/libbpf_latest deleted file mode 160000 index d9ed1691..00000000 --- a/source/lib/internal/ebpf/libbpf_latest +++ /dev/null @@ -1 +0,0 @@ -Subproject commit d9ed1691f0e0d7168cde3e91413f872075ea47d7 -- Gitee From b02d7d70efe678b150e3c2746e12388dbffa92df Mon Sep 17 00:00:00 2001 From: Shuyi Cheng Date: Thu, 22 Dec 2022 13:57:06 +0800 Subject: [PATCH 6/6] configure: stop downloading libbpf and support to clone coolbpf Signed-off-by: Shuyi Cheng --- configure | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/configure b/configure index 65e12d34..5a790f1f 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 -- Gitee