[PATCH v11 03/39] bpf tools: Introduce 'bpf' library and add bpf feature check

2015-07-08 Thread Wang Nan
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 
Acked-by: Alexei Starovoitov 
Cc: Brendan Gregg 
Cc: Daniel Borkmann 
Cc: David Ahern 
Cc: He Kuang 
Cc: Jiri Olsa 
Cc: Kaixu Xia 
Cc: Masami Hiramatsu 
Cc: Namhyung Kim 
Cc: Paul Mackerras 
Cc: Peter Zijlstra 
Cc: Zefan Li 
Bcc: pi3or...@163.com
Link: 
http://lkml.kernel.org/r/1435716878-189507-4-git-send-email-wangn...@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo 
---
 tools/build/feature/Makefile   |   6 +-
 tools/build/feature/test-bpf.c |  18 
 tools/lib/bpf/.gitignore   |   2 +
 tools/lib/bpf/Build|   1 +
 tools/lib/bpf/Makefile | 195 +
 tools/lib/bpf/libbpf.c |  14 +++
 tools/lib/bpf/libbpf.h |  11 +++
 7 files changed, 246 insertions(+), 1 deletion(-)
 create mode 100644 tools/build/feature/test-bpf.c
 create mode 100644 tools/lib/bpf/.gitignore
 create mode 100644 tools/lib/bpf/Build
 create mode 100644 tools/lib/bpf/Makefile
 create mode 100644 tools/lib/bpf/libbpf.c
 create mode 100644 tools/lib/bpf/libbpf.h

diff --git a/tools/build/feature/Makefile b/tools/build/feature/Makefile
index 463ed8f..1c0d69f 100644
--- a/tools/build/feature/Makefile
+++ b/tools/build/feature/Makefile
@@ -33,7 +33,8 @@ FILES=\
test-compile-32.bin \
test-compile-x32.bin\
test-zlib.bin   \
-   test-lzma.bin
+   test-lzma.bin   \
+   test-bpf.bin
 
 CC := $(CROSS_COMPILE)gcc -MD
 PKG_CONFIG := $(CROSS_COMPILE)pkg-config
@@ -156,6 +157,9 @@ test-zlib.bin:
 test-lzma.bin:
$(BUILD) -llzma
 
+test-bpf.bin:
+   $(BUILD)
+
 -include *.d
 
 ###
diff --git a/tools/build/feature/test-bpf.c b/tools/build/feature/test-bpf.c
new file mode 100644
index 000..062bac8
--- /dev/null
+++ b/tools/build/feature/test-bpf.c
@@ -0,0 +1,18 @@
+#include 
+
+int main(void)
+{
+   union bpf_attr attr;
+
+   attr.prog_type = BPF_PROG_TYPE_KPROBE;
+   attr.insn_cnt = 0;
+   attr.insns = 0;
+   attr.license = 0;
+   attr.log_buf = 0;
+   attr.log_size = 0;
+   attr.log_level = 0;
+   attr.kern_version = 0;
+
+   attr = attr;
+   return 0;
+}
diff --git a/tools/lib/bpf/.gitignore b/tools/lib/bpf/.gitignore
new file mode 100644
index 000..812aeed
--- /dev/null
+++ b/tools/lib/bpf/.gitignore
@@ -0,0 +1,2 @@
+libbpf_version.h
+FEATURE-DUMP
diff --git a/tools/lib/bpf/Build b/tools/lib/bpf/Build
new file mode 100644
index 000..a316484
--- /dev/null
+++ b/tools/lib/bpf/Build
@@ -0,0 +1 @@
+libbpf-y := libbpf.o
diff --git a/tools/lib/bpf/Makefile b/tools/lib/bpf/Makefile
new file mode 100644
index 000..f68d23a
--- /dev/null
+++ b/tools/lib/bpf/Makefile
@@ -0,0 +1,195 @@
+# Most of this file is copied from tools/lib/traceevent/Makefile
+
+BPF_VERSION = 0
+BPF_PATCHLEVEL = 0
+BPF_EXTRAVERSION = 1
+
+MAKEFLAGS += --no-print-directory
+
+
+# Makefiles suck: This macro sets a default value of $(2) for the
+# variable named by $(1), unless the variable has been set by
+# environment or command line. This is necessary for CC and AR
+# because make sets default values, so the simpler ?= approach
+# won't work as expected.
+define allow-override
+  $(if $(or $(findstring environment,$(origin $(1))),\
+$(findstring command line,$(origin $(1,,\
+$(eval $(1) = $(2)))
+endef
+
+# Allow setting CC and AR, or setting CROSS_COMPILE as a prefix.
+$(call allow-override,CC,$(CROSS_COMPILE)gcc)
+$(call allow-override,AR,$(CROSS_COMPILE)ar)
+
+INSTALL = install
+
+# Use DESTDIR 

[PATCH v11 03/39] bpf tools: Introduce 'bpf' library and add bpf feature check

2015-07-08 Thread Wang Nan
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 wangn...@huawei.com
Acked-by: Alexei Starovoitov a...@plumgrid.com
Cc: Brendan Gregg brendan.d.gr...@gmail.com
Cc: Daniel Borkmann dan...@iogearbox.net
Cc: David Ahern dsah...@gmail.com
Cc: He Kuang heku...@huawei.com
Cc: Jiri Olsa jo...@kernel.org
Cc: Kaixu Xia xiaka...@huawei.com
Cc: Masami Hiramatsu masami.hiramatsu...@hitachi.com
Cc: Namhyung Kim namhy...@kernel.org
Cc: Paul Mackerras pau...@samba.org
Cc: Peter Zijlstra a.p.zijls...@chello.nl
Cc: Zefan Li lize...@huawei.com
Bcc: pi3or...@163.com
Link: 
http://lkml.kernel.org/r/1435716878-189507-4-git-send-email-wangn...@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo a...@redhat.com
---
 tools/build/feature/Makefile   |   6 +-
 tools/build/feature/test-bpf.c |  18 
 tools/lib/bpf/.gitignore   |   2 +
 tools/lib/bpf/Build|   1 +
 tools/lib/bpf/Makefile | 195 +
 tools/lib/bpf/libbpf.c |  14 +++
 tools/lib/bpf/libbpf.h |  11 +++
 7 files changed, 246 insertions(+), 1 deletion(-)
 create mode 100644 tools/build/feature/test-bpf.c
 create mode 100644 tools/lib/bpf/.gitignore
 create mode 100644 tools/lib/bpf/Build
 create mode 100644 tools/lib/bpf/Makefile
 create mode 100644 tools/lib/bpf/libbpf.c
 create mode 100644 tools/lib/bpf/libbpf.h

diff --git a/tools/build/feature/Makefile b/tools/build/feature/Makefile
index 463ed8f..1c0d69f 100644
--- a/tools/build/feature/Makefile
+++ b/tools/build/feature/Makefile
@@ -33,7 +33,8 @@ FILES=\
test-compile-32.bin \
test-compile-x32.bin\
test-zlib.bin   \
-   test-lzma.bin
+   test-lzma.bin   \
+   test-bpf.bin
 
 CC := $(CROSS_COMPILE)gcc -MD
 PKG_CONFIG := $(CROSS_COMPILE)pkg-config
@@ -156,6 +157,9 @@ test-zlib.bin:
 test-lzma.bin:
$(BUILD) -llzma
 
+test-bpf.bin:
+   $(BUILD)
+
 -include *.d
 
 ###
diff --git a/tools/build/feature/test-bpf.c b/tools/build/feature/test-bpf.c
new file mode 100644
index 000..062bac8
--- /dev/null
+++ b/tools/build/feature/test-bpf.c
@@ -0,0 +1,18 @@
+#include linux/bpf.h
+
+int main(void)
+{
+   union bpf_attr attr;
+
+   attr.prog_type = BPF_PROG_TYPE_KPROBE;
+   attr.insn_cnt = 0;
+   attr.insns = 0;
+   attr.license = 0;
+   attr.log_buf = 0;
+   attr.log_size = 0;
+   attr.log_level = 0;
+   attr.kern_version = 0;
+
+   attr = attr;
+   return 0;
+}
diff --git a/tools/lib/bpf/.gitignore b/tools/lib/bpf/.gitignore
new file mode 100644
index 000..812aeed
--- /dev/null
+++ b/tools/lib/bpf/.gitignore
@@ -0,0 +1,2 @@
+libbpf_version.h
+FEATURE-DUMP
diff --git a/tools/lib/bpf/Build b/tools/lib/bpf/Build
new file mode 100644
index 000..a316484
--- /dev/null
+++ b/tools/lib/bpf/Build
@@ -0,0 +1 @@
+libbpf-y := libbpf.o
diff --git a/tools/lib/bpf/Makefile b/tools/lib/bpf/Makefile
new file mode 100644
index 000..f68d23a
--- /dev/null
+++ b/tools/lib/bpf/Makefile
@@ -0,0 +1,195 @@
+# Most of this file is copied from tools/lib/traceevent/Makefile
+
+BPF_VERSION = 0
+BPF_PATCHLEVEL = 0
+BPF_EXTRAVERSION = 1
+
+MAKEFLAGS += --no-print-directory
+
+
+# Makefiles suck: This macro sets a default value of $(2) for the
+# variable named by $(1), unless the variable has been set by
+# environment or command line. This is necessary for CC and AR
+# because make sets default values, so the simpler ?= approach
+# won't work as expected.
+define allow-override
+  $(if $(or $(findstring environment,$(origin $(1))),\
+