|
| 1 | +LIBBPF ?= /usr/include/bpf |
| 2 | +COMPONENT := $(ROOT)/component |
| 3 | +COMMON_SHARED := $(ROOT)/shared/pkt_utils.c $(COMPONENT)/net_utils.c \ |
| 4 | + $(COMPONENT)/arg_parse.c $(COMPONENT)/sys_utils.c \ |
| 5 | + $(ROOT)/shared/bpf_utils.c |
| 6 | + |
| 7 | +CFLAGS += -I./ -I$(ROOT)/shared/bpf/ |
| 8 | +BPF_CFLAGS = $(CFLAGS) -I$(LIBBPF) -Wno-unused-function |
| 9 | +HOST_CFLAGS = \ |
| 10 | + -lbpf -lelf -lz -g -O2 -static $(CFLAGS) \ |
| 11 | + -Wno-deprecated-declarations \ |
| 12 | + -I$(ROOT)/shared/ -I$(ROOT)/component |
| 13 | + |
| 14 | +REMOTE_ROOT := https://raw.githubusercontent.com/xmmgithub/nettrace-eBPF/master/ |
| 15 | +export REMOTE_ROOT |
| 16 | + |
| 17 | +include $(ROOT)/script/arch.mk |
| 18 | + |
| 19 | +HEADERS := $(if $(KERNEL),$(KERNEL),/lib/modules/$(shell uname -r)/build/) |
| 20 | +NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include) |
| 21 | +BTF := $(if $(VMLINUX),$(VMLINUX),/sys/kernel/btf/vmlinux) |
| 22 | +export HEADERS |
| 23 | + |
| 24 | +USERINCLUDE := \ |
| 25 | + -I$(HEADERS)/arch/$(SRCARCH)/include/uapi \ |
| 26 | + -I$(HEADERS)/arch/$(SRCARCH)/include/generated/uapi \ |
| 27 | + -I$(HEADERS)/include/uapi \ |
| 28 | + -I$(HEADERS)/include/generated/uapi \ |
| 29 | + -include $(HEADERS)/include/linux/kconfig.h |
| 30 | + |
| 31 | +LINUXINCLUDE := \ |
| 32 | + -I$(HEADERS)/arch/$(SRCARCH)/include \ |
| 33 | + -I$(HEADERS)/arch/$(SRCARCH)/include/generated \ |
| 34 | + -I$(HEADERS)/include \ |
| 35 | + $(USERINCLUDE) |
| 36 | + |
| 37 | +KERNEL_CFLAGS += $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \ |
| 38 | + -D__KERNEL__ -D__ASM_SYSREG_H -Wno-unused-value -Wno-pointer-sign \ |
| 39 | + -Wno-compare-distinct-pointer-types \ |
| 40 | + -Wno-gnu-variable-sized-type-not-at-end \ |
| 41 | + -Wno-address-of-packed-member -Wno-tautological-compare \ |
| 42 | + -Wno-unknown-warning-option -Wno-frame-address |
| 43 | + |
| 44 | +cmd_download = @if [ ! -f $(1) ]; then wget -O $(1) $(REMOTE_ROOT)/$(2); fi |
| 45 | +cmd_exist = $(if $(wildcard $(1)),$(2),$(3)) |
| 46 | +cmd_or_exist = $(call cmd_exist,$(1),$(1),$(2)) |
| 47 | +ifeq ("$(wildcard $(HEADERS))$(wildcard $(BTF))",) |
| 48 | +$(error BTF is not found in your system, please install kernel headers) |
| 49 | +endif |
| 50 | + |
| 51 | +ifeq ($(if $(KERNEL),$(wildcard $(KERNEL)),"pass"),) |
| 52 | +$(error kernel path not exist) |
| 53 | +endif |
| 54 | + |
| 55 | +ifeq ($(if $(VMLINUX),$(wildcard $(VMLINUX)),"pass"),) |
| 56 | +$(error vmlinux path not exist) |
| 57 | +endif |
| 58 | + |
| 59 | +# preferred to generate drop reason from |
| 60 | +ifeq ("$(KERNEL)$(VMLINUX)","") |
| 61 | + DROP_REASON := $(call cmd_or_exist,$(HEADERS)/include/net/dropreason.h,\ |
| 62 | + $(call cmd_or_exist,$(HEADERS)/include/linux/skbuff.h,\ |
| 63 | + $(call cmd_exist,$(BTF),vmlinux.h,))) |
| 64 | +endif |
| 65 | + |
| 66 | +# preferred to compile from kernel headers, then BTF |
| 67 | +mode := $(if $(VMLINUX),'btf',$(call cmd_exist,$(HEADERS),'kernel','btf')) |
| 68 | +ifeq ($(mode),'btf') |
| 69 | + DROP_REASON ?= vmlinux.h |
| 70 | + kheaders_cmd := ln -s vmlinux.h kheaders.h |
| 71 | + kheaders_dep := vmlinux.h |
| 72 | +else |
| 73 | +ifndef DROP_REASON |
| 74 | + DROP_REASON := $(call cmd_or_exist,$(HEADERS)/include/net/dropreason.h,\ |
| 75 | + $(call cmd_or_exist,$(HEADERS)/include/linux/skbuff.h,)) |
| 76 | +endif |
| 77 | + kheaders_cmd := ln -s vmlinux_header.h kheaders.h |
| 78 | + BPF_CFLAGS += $(KERNEL_CFLAGS) |
| 79 | +endif |
| 80 | + |
| 81 | +ifndef BPFTOOL |
| 82 | +ifneq ("$(shell bpftool gen help 2>&1 | grep skeleton)","") |
| 83 | + BPFTOOL := bpftool |
| 84 | +else |
| 85 | + BPFTOOL := $(ROOT)/script/bpftool |
| 86 | +endif |
| 87 | +endif |
| 88 | + |
| 89 | +vmlinux.h: |
| 90 | + $(BPFTOOL) btf dump file $(BTF) format c > vmlinux.h |
| 91 | + |
| 92 | +kheaders.h: $(kheaders_dep) |
| 93 | + $(call kheaders_cmd) |
| 94 | + |
| 95 | +drop_reason.h: $(DROP_REASON) |
| 96 | + rm -rf $@ |
| 97 | +ifneq ($(strip $(DROP_REASON)),) |
| 98 | + @awk 'BEGIN{ print "#ifndef _H_SKB_DROP_REASON"; \ |
| 99 | + print "#define _H_SKB_DROP_REASON\n";\ |
| 100 | + system("sed -e \"/enum skb_drop_reason {/,/}/!d\" $< >> $@");\ |
| 101 | + print "\n#define __DEFINE_SKB_REASON(FN) \\";\ |
| 102 | + }\ |
| 103 | + /^enum skb_drop/ { dr=1; }\ |
| 104 | + /^\};/ { dr=0; }\ |
| 105 | + /^\tSKB_DROP_REASON_/ {\ |
| 106 | + if (dr) {\ |
| 107 | + sub(/SKB_DROP_REASON_/, "", $$1);\ |
| 108 | + sub(/,/, "", $$1);\ |
| 109 | + printf "\tFN(%s)\t\\\n", $$1;\ |
| 110 | + }\ |
| 111 | + }\ |
| 112 | + END{ print "\n#endif" }' $< >> $@ |
| 113 | + @echo generated drop_reason.h |
| 114 | +else |
| 115 | + touch $@ |
| 116 | + @echo drop reason not supported, skips |
| 117 | +endif |
| 118 | + |
| 119 | +progs/%.o: progs/%.c kheaders.h |
| 120 | + clang -O2 -c -g -S -Wall -Wno-pointer-sign -Wno-unused-value \ |
| 121 | + -Wno-incompatible-pointer-types-discards-qualifiers \ |
| 122 | + -fno-asynchronous-unwind-tables \ |
| 123 | + $< -emit-llvm -Wno-unknown-attributes $(BPF_CFLAGS) -o - | \ |
| 124 | + llc -march=bpf -filetype=obj -o $@ |
| 125 | + @file $@ | grep debug_info > /dev/null || (rm $@ && exit 1) |
| 126 | + |
| 127 | +%.skel.h: %.o |
| 128 | + $(BPFTOOL) gen skeleton $< > $@ |
| 129 | + |
| 130 | +$(bpf_progs): %: %.skel.h |
| 131 | + @echo "bpf compile success" |
| 132 | + |
| 133 | +bpf: $(bpf_progs) |
| 134 | + |
| 135 | +$(progs): %: %.c bpf |
| 136 | + @if [ -n "$(prog-$@)" ]; then \ |
| 137 | + echo gcc $(prog-$@) -o $@ $(HOST_CFLAGS); \ |
| 138 | + gcc $(prog-$@) -o $@ $(HOST_CFLAGS); \ |
| 139 | + else \ |
| 140 | + echo gcc $< -o $@ $(HOST_CFLAGS); \ |
| 141 | + gcc $< -o $@ $(HOST_CFLAGS); \ |
| 142 | + fi |
0 commit comments