Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf tools: Introduce 'bpf' library and add bpf feature check

This is the first patch of libbpf. The goal of libbpf is to create a
standard way for accessing eBPF object files. This patch creates
'Makefile' and 'Build' for it, allows 'make' to build libbpf.a and
libbpf.so, 'make install' to put them into proper directories.
Most part of Makefile is borrowed from traceevent.

Before building, it checks the existence of libelf in Makefile, and deny
to build if not found. Instead of throwing an error if libelf not found,
the error raises in a phony target "elfdep". This design is to ensure
'make clean' still workable even if libelf is not found.

Because libbpf requires 'kern_version' field set for 'union bpf_attr'
(bpfdep" is used for that dependency), Kernel BPF API is also checked
by intruducing a new feature check 'bpf' into tools/build/feature,
which checks the existence and version of linux/bpf.h. When building
libbpf, it searches that file from include/uapi/linux in kernel source
tree (controlled by FEATURE_CHECK_CFLAGS-bpf). Since it searches kernel
source tree it reside, installing of newest kernel headers is not
required, except we are trying to port these files to an old kernel.

To avoid checking that file when perf building, the newly introduced
'bpf' feature check doesn't added into FEATURE_TESTS and
FEATURE_DISPLAY by default in tools/build/Makefile.feature, but added
into libbpf's specific.

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Acked-by: Alexei Starovoitov <ast@plumgrid.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: David Ahern <dsahern@gmail.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kaixu Xia <xiakaixu@huawei.com>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Zefan Li <lizefan@huawei.com>
Bcc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1435716878-189507-4-git-send-email-wangnan0@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Wang Nan and committed by
Arnaldo Carvalho de Melo
1b76c13e 1354ac6a

+246 -1
+5 -1
tools/build/feature/Makefile
··· 33 33 test-compile-32.bin \ 34 34 test-compile-x32.bin \ 35 35 test-zlib.bin \ 36 - test-lzma.bin 36 + test-lzma.bin \ 37 + test-bpf.bin 37 38 38 39 CC := $(CROSS_COMPILE)gcc -MD 39 40 PKG_CONFIG := $(CROSS_COMPILE)pkg-config ··· 156 155 157 156 test-lzma.bin: 158 157 $(BUILD) -llzma 158 + 159 + test-bpf.bin: 160 + $(BUILD) 159 161 160 162 -include *.d 161 163
+18
tools/build/feature/test-bpf.c
··· 1 + #include <linux/bpf.h> 2 + 3 + int main(void) 4 + { 5 + union bpf_attr attr; 6 + 7 + attr.prog_type = BPF_PROG_TYPE_KPROBE; 8 + attr.insn_cnt = 0; 9 + attr.insns = 0; 10 + attr.license = 0; 11 + attr.log_buf = 0; 12 + attr.log_size = 0; 13 + attr.log_level = 0; 14 + attr.kern_version = 0; 15 + 16 + attr = attr; 17 + return 0; 18 + }
+2
tools/lib/bpf/.gitignore
··· 1 + libbpf_version.h 2 + FEATURE-DUMP
+1
tools/lib/bpf/Build
··· 1 + libbpf-y := libbpf.o
+195
tools/lib/bpf/Makefile
··· 1 + # Most of this file is copied from tools/lib/traceevent/Makefile 2 + 3 + BPF_VERSION = 0 4 + BPF_PATCHLEVEL = 0 5 + BPF_EXTRAVERSION = 1 6 + 7 + MAKEFLAGS += --no-print-directory 8 + 9 + 10 + # Makefiles suck: This macro sets a default value of $(2) for the 11 + # variable named by $(1), unless the variable has been set by 12 + # environment or command line. This is necessary for CC and AR 13 + # because make sets default values, so the simpler ?= approach 14 + # won't work as expected. 15 + define allow-override 16 + $(if $(or $(findstring environment,$(origin $(1))),\ 17 + $(findstring command line,$(origin $(1)))),,\ 18 + $(eval $(1) = $(2))) 19 + endef 20 + 21 + # Allow setting CC and AR, or setting CROSS_COMPILE as a prefix. 22 + $(call allow-override,CC,$(CROSS_COMPILE)gcc) 23 + $(call allow-override,AR,$(CROSS_COMPILE)ar) 24 + 25 + INSTALL = install 26 + 27 + # Use DESTDIR for installing into a different root directory. 28 + # This is useful for building a package. The program will be 29 + # installed in this directory as if it was the root directory. 30 + # Then the build tool can move it later. 31 + DESTDIR ?= 32 + DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))' 33 + 34 + LP64 := $(shell echo __LP64__ | ${CC} ${CFLAGS} -E -x c - | tail -n 1) 35 + ifeq ($(LP64), 1) 36 + libdir_relative = lib64 37 + else 38 + libdir_relative = lib 39 + endif 40 + 41 + prefix ?= /usr/local 42 + libdir = $(prefix)/$(libdir_relative) 43 + man_dir = $(prefix)/share/man 44 + man_dir_SQ = '$(subst ','\'',$(man_dir))' 45 + 46 + export man_dir man_dir_SQ INSTALL 47 + export DESTDIR DESTDIR_SQ 48 + 49 + include ../../scripts/Makefile.include 50 + 51 + # copy a bit from Linux kbuild 52 + 53 + ifeq ("$(origin V)", "command line") 54 + VERBOSE = $(V) 55 + endif 56 + ifndef VERBOSE 57 + VERBOSE = 0 58 + endif 59 + 60 + ifeq ($(srctree),) 61 + srctree := $(patsubst %/,%,$(dir $(shell pwd))) 62 + srctree := $(patsubst %/,%,$(dir $(srctree))) 63 + srctree := $(patsubst %/,%,$(dir $(srctree))) 64 + #$(info Determined 'srctree' to be $(srctree)) 65 + endif 66 + 67 + FEATURE_DISPLAY = libelf libelf-getphdrnum libelf-mmap bpf 68 + FEATURE_TESTS = libelf bpf 69 + 70 + INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/arch/$(ARCH)/include/uapi -I$(srctree)/include/uapi 71 + FEATURE_CHECK_CFLAGS-bpf = $(INCLUDES) 72 + 73 + include $(srctree)/tools/build/Makefile.feature 74 + 75 + export prefix libdir src obj 76 + 77 + # Shell quotes 78 + libdir_SQ = $(subst ','\'',$(libdir)) 79 + libdir_relative_SQ = $(subst ','\'',$(libdir_relative)) 80 + plugin_dir_SQ = $(subst ','\'',$(plugin_dir)) 81 + 82 + LIB_FILE = libbpf.a libbpf.so 83 + 84 + VERSION = $(BPF_VERSION) 85 + PATCHLEVEL = $(BPF_PATCHLEVEL) 86 + EXTRAVERSION = $(BPF_EXTRAVERSION) 87 + 88 + OBJ = $@ 89 + N = 90 + 91 + LIBBPF_VERSION = $(BPF_VERSION).$(BPF_PATCHLEVEL).$(BPF_EXTRAVERSION) 92 + 93 + # Set compile option CFLAGS 94 + ifdef EXTRA_CFLAGS 95 + CFLAGS := $(EXTRA_CFLAGS) 96 + else 97 + CFLAGS := -g -Wall 98 + endif 99 + 100 + ifeq ($(feature-libelf-mmap), 1) 101 + override CFLAGS += -DHAVE_LIBELF_MMAP_SUPPORT 102 + endif 103 + 104 + ifeq ($(feature-libelf-getphdrnum), 1) 105 + override CFLAGS += -DHAVE_ELF_GETPHDRNUM_SUPPORT 106 + endif 107 + 108 + # Append required CFLAGS 109 + override CFLAGS += $(EXTRA_WARNINGS) 110 + override CFLAGS += -Werror -Wall 111 + override CFLAGS += -fPIC 112 + override CFLAGS += $(INCLUDES) 113 + 114 + ifeq ($(VERBOSE),1) 115 + Q = 116 + else 117 + Q = @ 118 + endif 119 + 120 + # Disable command line variables (CFLAGS) overide from top 121 + # level Makefile (perf), otherwise build Makefile will get 122 + # the same command line setup. 123 + MAKEOVERRIDES= 124 + 125 + export srctree OUTPUT CC LD CFLAGS V 126 + build := -f $(srctree)/tools/build/Makefile.build dir=. obj 127 + 128 + BPF_IN := $(OUTPUT)libbpf-in.o 129 + LIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE)) 130 + 131 + CMD_TARGETS = $(LIB_FILE) 132 + 133 + TARGETS = $(CMD_TARGETS) 134 + 135 + all: $(VERSION_FILES) all_cmd 136 + 137 + all_cmd: $(CMD_TARGETS) 138 + 139 + $(BPF_IN): force elfdep bpfdep 140 + $(Q)$(MAKE) $(build)=libbpf 141 + 142 + $(OUTPUT)libbpf.so: $(BPF_IN) 143 + $(QUIET_LINK)$(CC) --shared $^ -o $@ 144 + 145 + $(OUTPUT)libbpf.a: $(BPF_IN) 146 + $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^ 147 + 148 + define update_dir 149 + (echo $1 > $@.tmp; \ 150 + if [ -r $@ ] && cmp -s $@ $@.tmp; then \ 151 + rm -f $@.tmp; \ 152 + else \ 153 + echo ' UPDATE $@'; \ 154 + mv -f $@.tmp $@; \ 155 + fi); 156 + endef 157 + 158 + define do_install 159 + if [ ! -d '$(DESTDIR_SQ)$2' ]; then \ 160 + $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \ 161 + fi; \ 162 + $(INSTALL) $1 '$(DESTDIR_SQ)$2' 163 + endef 164 + 165 + install_lib: all_cmd 166 + $(call QUIET_INSTALL, $(LIB_FILE)) \ 167 + $(call do_install,$(LIB_FILE),$(libdir_SQ)) 168 + 169 + install: install_lib 170 + 171 + ### Cleaning rules 172 + 173 + config-clean: 174 + $(call QUIET_CLEAN, config) 175 + $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null 176 + 177 + clean: 178 + $(call QUIET_CLEAN, libbpf) $(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d \ 179 + $(RM) LIBBPF-CFLAGS 180 + $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP 181 + 182 + 183 + 184 + PHONY += force elfdep bpfdep 185 + force: 186 + 187 + elfdep: 188 + @if [ "$(feature-libelf)" != "1" ]; then echo "No libelf found"; exit -1 ; fi 189 + 190 + bpfdep: 191 + @if [ "$(feature-bpf)" != "1" ]; then echo "BPF API too old"; exit -1 ; fi 192 + 193 + # Declare the contents of the .PHONY variable as phony. We keep that 194 + # information in a variable so we can use it in if_changed and friends. 195 + .PHONY: $(PHONY)
+14
tools/lib/bpf/libbpf.c
··· 1 + /* 2 + * Common eBPF ELF object loading operations. 3 + * 4 + * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org> 5 + * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> 6 + * Copyright (C) 2015 Huawei Inc. 7 + */ 8 + 9 + #include <stdlib.h> 10 + #include <unistd.h> 11 + #include <asm/unistd.h> 12 + #include <linux/bpf.h> 13 + 14 + #include "libbpf.h"
+11
tools/lib/bpf/libbpf.h
··· 1 + /* 2 + * Common eBPF ELF object loading operations. 3 + * 4 + * Copyright (C) 2013-2015 Alexei Starovoitov <ast@kernel.org> 5 + * Copyright (C) 2015 Wang Nan <wangnan0@huawei.com> 6 + * Copyright (C) 2015 Huawei Inc. 7 + */ 8 + #ifndef __BPF_LIBBPF_H 9 + #define __BPF_LIBBPF_H 10 + 11 + #endif