Skip to content

Commit

Permalink
Merge pull request #28 from hengyoush/compatible
Browse files Browse the repository at this point in the history
[Improvement] Add btf option to specify btf file path
  • Loading branch information
hengyoush authored Sep 10, 2024
2 parents 586d026 + da49a46 commit 192549b
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 139 deletions.
60 changes: 51 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,50 @@ ARCH ?= $(shell uname -m | sed 's/x86_64/x86/' \
CLANG_BPF_SYS_INCLUDES ?= $(shell $(CLANG) -v -E - </dev/null 2>&1 \
| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')
APPS = kyanos
CFLAGS := -O2 -Wall
CFLAGS := -O2
ALL_LDFLAGS := $(LDFLAGS) $(EXTRA_LDFLAGS)
ROOT := $(abspath .)

include $(ROOT)/makefiles/arch.mk

HEADERS := $(if $(KERNEL),$(KERNEL),/lib/modules/$(shell uname -r)/build/)
NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)
USERINCLUDE := \
-I$(HEADERS)/arch/$(SRCARCH)/include/uapi \
-I$(HEADERS)/arch/$(SRCARCH)/include/generated/uapi \
-I$(HEADERS)/include/uapi \
-I$(HEADERS)/include/generated/uapi \
-include $(HEADERS)/include/linux/kconfig.h \
-I/usr/include/

LINUXINCLUDE := \
-I$(HEADERS)/arch/$(SRCARCH)/include \
-I$(HEADERS)/arch/$(SRCARCH)/include/generated \
-I$(HEADERS)/include \
$(USERINCLUDE)

KERNEL_CFLAGS += $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \
-D__KERNEL__ -DKBUILD_MODNAME='\"bpftrace\"' -Wno-unused-value -Wno-pointer-sign \
-Wno-compare-distinct-pointer-types \
-Wno-gnu-variable-sized-type-not-at-end \
-Wno-address-of-packed-member -Wno-tautological-compare \
-Wno-unknown-warning-option -Wno-frame-address

ifdef COMPAT
ifeq ($(wildcard $(HEADERS)),)
$(error kernel headers not exist in COMPAT mode, please install it)
endif
kheaders_cmd := rm -f bpf/kheaders.h && cd bpf && ln -s ../vmlinux_header.h kheaders.h && cd ..
CFLAGS += -DCOMPAT
CFLAGS += $(KERNEL_CFLAGS)
CFLAGS += -I./
VMLINUX := -I./
VMLINUX += $(LINUXINCLUDE)

else
kheaders_cmd := rm -f bpf/kheaders.h && cd bpf && ln -s ../vmlinux/x86/vmlinux.h kheaders.h && cd ..
VMLINUX := ""
endif

ifeq ($(V),1)
Q =
Expand All @@ -43,7 +85,7 @@ all: $(APPS)

clean:
$(call msg,CLEAN)
$(Q)rm -rf $(OUTPUT) $(APPS) kyanos kyanos.log
$(Q)rm -rf $(OUTPUT) $(APPS) kyanos kyanos.log bpf/kheaders.h

$(OUTPUT) $(OUTPUT)/libbpf $(BPFTOOL_OUTPUT):
$(call msg,MKDIR,$@)
Expand All @@ -57,18 +99,18 @@ $(LIBBPF_OBJ): $(wildcard $(LIBBPF_SRC)/*.[ch] $(LIBBPF_SRC)/Makefile) | $(OUTPU
INCLUDEDIR= LIBDIR= UAPIDIR= \
install

# Build bpftool
$(BPFTOOL): | $(BPFTOOL_OUTPUT)
$(call msg,BPFTOOL,$@)
$(Q)$(MAKE) ARCH= CROSS_COMPILE= OUTPUT=$(BPFTOOL_OUTPUT)/ -C $(BPFTOOL_SRC) bootstrap

GO_FILES := $(shell find $(SRC_DIR) -type f -name '*.go' | sort)
bpf/kheaders.h: FORCE
$(call kheaders_cmd)

FORCE:

kyanos: $(LIBBPF_OBJ) $(GO_FILES) $(wildcard bpf/*.[ch]) | $(OUTPUT)
$(call msg,BINARY,$@)
./build.sh
./build.sh "$(CFLAGS)" "$(VMLINUX)"
rm -f bpf/kheaders.h
# delete failed targets
.DELETE_ON_ERROR:
.DELETE_ON_ERROR: bpf/kheaders.h

# keep intermediate (.skel.h, .bpf.o, etc) targets
.SECONDARY:
20 changes: 18 additions & 2 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"unsafe"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/perf"
"github.com/cilium/ebpf/ringbuf"
Expand Down Expand Up @@ -54,6 +55,7 @@ type AgentOptions struct {
LatencyFilter protocol.LatencyFilter
TraceSide common.SideEnum
IfName string
BTFFilePath string
protocol.SizeFilter
AnalysisEnable bool
analysis.AnalysisOptions
Expand Down Expand Up @@ -116,20 +118,34 @@ func SetupAgent(options AgentOptions) {
var objs any
var spec *ebpf.CollectionSpec
var err error
var collectionOptions *ebpf.CollectionOptions
if options.BTFFilePath != "" {
btfPath, err := btf.LoadSpec(options.BTFFilePath)
if err != nil {
log.Fatalf("can't load btf spec: %v", err)
}
collectionOptions = &ebpf.CollectionOptions{
Programs: ebpf.ProgramOptions{
KernelTypes: btfPath,
},
}
} else {
collectionOptions = nil
}
if common.NeedsRunningInCompatibleMode() {
objs = &bpf.AgentOldObjects{}
spec, err = bpf.LoadAgentOld()
if err != nil {
log.Fatal("load Agent error:", err)
}
err = spec.LoadAndAssign(objs, nil)
err = spec.LoadAndAssign(objs, collectionOptions)
} else {
objs = &bpf.AgentObjects{}
spec, err = bpf.LoadAgent()
if err != nil {
log.Fatal("load Agent error:", err)
}
err = spec.LoadAndAssign(objs, nil)
err = spec.LoadAndAssign(objs, collectionOptions)
}
bpf.Objs = objs

Expand Down
4 changes: 2 additions & 2 deletions bpf/gen.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bpf

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -type sock_key -type control_value_index_t -type kern_evt -type kern_evt_data -type conn_evt_t -type conn_type_t -type conn_info_t -type endpoint_role_t -type traffic_direction_t -type traffic_protocol_t -type step_t -target amd64 Agent ./pktlatency.bpf.c -- -I./ -I$OUTPUT -I../libbpf/include/uapi -I../vmlinux/x86/
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -type sock_key -type control_value_index_t -type kern_evt -type kern_evt_data -type conn_evt_t -type conn_type_t -type conn_info_t -type endpoint_role_t -type traffic_direction_t -type traffic_protocol_t -type step_t -cflags "-D OLD_KERNEL" -target amd64 AgentOld ./pktlatency.bpf.c -- -I./ -I$OUTPUT -I../libbpf/include/uapi -I../vmlinux/x86/
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -type sock_key -type control_value_index_t -type kern_evt -type kern_evt_data -type conn_evt_t -type conn_type_t -type conn_info_t -type endpoint_role_t -type traffic_direction_t -type traffic_protocol_t -type step_t -cflags "$CFLAGS" -target amd64 Agent ./pktlatency.bpf.c
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -type sock_key -type control_value_index_t -type kern_evt -type kern_evt_data -type conn_evt_t -type conn_type_t -type conn_info_t -type endpoint_role_t -type traffic_direction_t -type traffic_protocol_t -type step_t -cflags "$CFLAGS -D KERNEL_VERSION_BELOW_58" -target amd64 AgentOld ./pktlatency.bpf.c -- -I$OUTPUT $VMLINUX
Loading

0 comments on commit 192549b

Please sign in to comment.