Skip to content

Commit

Permalink
Merge pull request linuxkerneltravel#859 from zhangxianyu777/develop
Browse files Browse the repository at this point in the history
netwatcher:增加依赖及根据版本适配代码
  • Loading branch information
chenamy2017 committed Jul 11, 2024
2 parents 2ff8593 + b4d0c69 commit 310a28f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
47 changes: 45 additions & 2 deletions eBPF_Supermarket/Network_Subsystem/net_watcher/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ BPFTOOL_SRC := $(abspath ../../lib/bpftool/src)
LIBBPF_OBJ := $(abspath $(OUTPUT)/libbpf.a)
BPFTOOL_OUTPUT ?= $(abspath $(OUTPUT)/bpftool)
BPFTOOL ?= $(BPFTOOL_OUTPUT)/bootstrap/bpftool

VERSION_INFO := $(shell uname -r | cut -d'-' -f1)
VERSION_MAJOR := $(shell echo $(VERSION_INFO) | cut -d'.' -f1)
VERSION_MINOR := $(shell echo $(VERSION_INFO) | cut -d'.' -f2)
VERSION_PATCH := $(shell echo $(VERSION_INFO) | cut -d'.' -f3)
export VERSION_INFO
export VERSION_MAJOR
export VERSION_MINOR
export VERSION_PATCH
ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' \
| sed 's/arm.*/arm/' \
| sed 's/aarch64/arm64/' \
Expand Down Expand Up @@ -63,10 +70,42 @@ $(call allow-override,LD,$(CROSS_COMPILE)ld)
all: deps $(APPS)
#更新vmlinux.h文件
.PHONY: deps
deps:
deps:check_bpftool check_clang check_multilib
@echo "Kernel version is $(VERSION_MAJOR).$(VERSION_MINOR).$(VERSION_PATCH)"
mkdir -p $(VMLINUXSRC)
bpftool btf dump file /sys/kernel/btf/vmlinux format c > $(VMLINUX)

.PHONY: check_bpftool
check_bpftool:
@if ! command -v bpftool &> /dev/null; then \
echo "bpftool 未安装,正在安装..."; \
sudo apt-get update; \
sudo apt-get install -y linux-tools-$(shell uname -r); \
else \
echo "bpftool 已经安装"; \
fi
.PHONY: check_clang

check_clang:
@if ! command -v clang &> /dev/null; then \
echo "clang 未安装,正在安装..."; \
sudo apt-get update; \
sudo apt-get install -y clang; \
else \
echo "clang 已经安装"; \
fi

# 检查 multilib 是否安装,如果没有则安装
.PHONY: check_multilib
check_multilib:
@if [ ! -f /usr/include/x86_64-linux-gnu/gnu/stubs-32.h ]; then \
echo "multilib 未安装,正在安装..."; \
sudo apt-get update; \
sudo apt-get install -y gcc-multilib g++-multilib; \
else \
echo "multilib 已经安装"; \
fi

.PHONY: clean
clean:
$(call msg,CLEAN)
Expand Down Expand Up @@ -105,6 +144,10 @@ $(BPFTOOL): | $(BPFTOOL_OUTPUT)
$(OUTPUT)/%.bpf.o: %.bpf.c $(LIBBPF_OBJ) $(wildcard *.h) $(VMLINUX) | $(OUTPUT) $(BPFTOOL)
$(call msg,BPF,$@)
$(Q)$(CLANG) -g -O2 -target bpf -D__TARGET_ARCH_$(ARCH) \
-D__TARGET_ARCH_$(ARCH) \
-DVERSION_MAJOR=$(VERSION_MAJOR) \
-DVERSION_MINOR=$(VERSION_MINOR) \
-DVERSION_PATCH=$(VERSION_PATCH) \
$(INCLUDES) $(CLANG_BPF_SYS_INCLUDES) \
-c $(filter %.c,$^) -o $(patsubst %.bpf.o,%.tmp.bpf.o,$@)
$(Q)$(BPFTOOL) gen object $@ $(patsubst %.bpf.o,%.tmp.bpf.o,$@)
Expand Down
6 changes: 5 additions & 1 deletion eBPF_Supermarket/Network_Subsystem/net_watcher/common.bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,11 @@ int getstack(void *ctx) {

return 0;
}

#if KERNEL_VERSION(VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH) >= KERNEL_VERSION(6, 3, 1)
#define GET_USER_DATA(msg) BPF_CORE_READ(msg, msg_iter.__iov, iov_base)
#else
#define GET_USER_DATA(msg) BPF_CORE_READ(msg, msg_iter.iov, iov_base)
#endif
/* help functions end */

#endif
7 changes: 1 addition & 6 deletions eBPF_Supermarket/Network_Subsystem/net_watcher/packet.bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,12 +393,7 @@ int __tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)

// TX HTTP info
if (http_info) {
// #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 3, 1)
// u8 *user_data = BPF_CORE_READ(msg, msg_iter.__iov, iov_base);
// #else
// u8 *user_data = BPF_CORE_READ(msg, msg_iter.iov,iov_base);
// #endif
u8 *user_data = BPF_CORE_READ(msg, msg_iter.__iov, iov_base);
u8 *user_data = GET_USER_DATA(msg);
tinfo = (struct ktime_info *)bpf_map_lookup_or_try_init(
&timestamps, &pkt_tuple, &zero);
if (tinfo == NULL) {
Expand Down

0 comments on commit 310a28f

Please sign in to comment.