Since we already have libbpf in tools/lib, there's no need to maintain
a duplicate BPF loading and operations library in samples.
This patch utilises previous introduced utils.[ch], which calls libbpf
to do these work.
The main changing in this relative large patch is very simple:
1. Makefile modification, makes all hostprogs depend on libbpf.a.
Add include and library searching patch and rules for linker.
Remove old bpf_load.o and libbpf.o, replaces them with utils.o
2. Make sure there are correct 'version' section so the BPF source
files can be loaded by libbpf.
3. Uses API provided by utils.h and libbpf.
Signed-off-by: Wang Nan
Cc: Alexei Starovoitov
Cc: Alex Gartrell
Cc: Arnaldo Carvalho de Melo
Cc: Brenden Blanco
Cc: Daniel Borkmann
Cc: Daniel Wagner
Cc: David S. Miller
Cc: Ingo Molnar
Cc: Kaixu Xia
Cc: Michael Holzheu
Cc: Yang Shi
---
samples/bpf/Makefile| 65 ++---
samples/bpf/fds_example.c | 26 +++---
samples/bpf/lathist_user.c | 13 ---
samples/bpf/sock_example.c | 13 +++
samples/bpf/sockex1_kern.c | 2 ++
samples/bpf/sockex1_user.c | 27 --
samples/bpf/sockex2_kern.c | 2 ++
samples/bpf/sockex2_user.c | 26 --
samples/bpf/sockex3_kern.c | 2 ++
samples/bpf/sockex3_user.c | 23 +++-
samples/bpf/test_maps.c | 80 -
samples/bpf/test_verifier.c | 13 ---
samples/bpf/trace_output_user.c | 17 +
samples/bpf/tracex1_user.c | 9 +++--
samples/bpf/tracex2_user.c | 31 +---
samples/bpf/tracex3_user.c | 15
samples/bpf/tracex4_user.c | 15
samples/bpf/tracex5_user.c | 9 +++--
samples/bpf/tracex6_user.c | 16 -
19 files changed, 221 insertions(+), 183 deletions(-)
diff --git a/samples/bpf/Makefile b/samples/bpf/Makefile
index edd638b..edb7c04 100644
--- a/samples/bpf/Makefile
+++ b/samples/bpf/Makefile
@@ -1,6 +1,20 @@
# kbuild trick to avoid linker error. Can be omitted if a module is built.
obj- := dummy.o
+INC_DIR = $(src)/include
+TOOLS_DIR = $(realpath $(srctree))/tools
+TOOLS_LIB_DIR = $(TOOLS_DIR)/lib
+LIBBPF_DIR = $(TOOLS_LIB_DIR)/bpf
+
+$(obj)/libbpf/libbpf.a:
+ $(Q)mkdir -p $(obj)/libbpf
+ $(MAKE) -C $(LIBBPF_DIR) O=$(realpath $(obj))/libbpf CFLAGS= LDFLAGS=
V=1 $(realpath $(obj))/libbpf/libbpf.a
+
+HOSTCFLAGS += -I$(TOOLS_LIB_DIR) -I$(INC_DIR)
+HOSTLDFLAGS += -L$(obj)/libbpf
+HOSTCFLAGS += -I$(INC_DIR) -I$(TOOLS_LIB_DIR)
+HOST_LOADLIBES += -lelf -lbpf
+
# List of programs to build
hostprogs-y := test_verifier test_maps
hostprogs-y += sock_example
@@ -17,21 +31,27 @@ hostprogs-y += tracex6
hostprogs-y += trace_output
hostprogs-y += lathist
-test_verifier-objs := test_verifier.o libbpf.o
-test_maps-objs := test_maps.o libbpf.o
-sock_example-objs := sock_example.o libbpf.o
-fds_example-objs := bpf_load.o libbpf.o fds_example.o
-sockex1-objs := bpf_load.o libbpf.o sockex1_user.o
-sockex2-objs := bpf_load.o libbpf.o sockex2_user.o
-sockex3-objs := bpf_load.o libbpf.o sockex3_user.o
-tracex1-objs := bpf_load.o libbpf.o tracex1_user.o
-tracex2-objs := bpf_load.o libbpf.o tracex2_user.o
-tracex3-objs := bpf_load.o libbpf.o tracex3_user.o
-tracex4-objs := bpf_load.o libbpf.o tracex4_user.o
-tracex5-objs := bpf_load.o libbpf.o tracex5_user.o
-tracex6-objs := bpf_load.o libbpf.o tracex6_user.o
-trace_output-objs := bpf_load.o libbpf.o trace_output_user.o
-lathist-objs := bpf_load.o libbpf.o lathist_user.o
+test_verifier-objs := test_verifier.o
+test_maps-objs := test_maps.o
+sock_example-objs := sock_example.o utils.o
+fds_example-objs := utils.o fds_example.o
+sockex1-objs := utils.o sockex1_user.o
+sockex2-objs := utils.o sockex2_user.o
+sockex3-objs := utils.o sockex3_user.o
+tracex1-objs := utils.o tracex1_user.o
+tracex2-objs := utils.o tracex2_user.o
+tracex3-objs := utils.o tracex3_user.o
+tracex4-objs := utils.o tracex4_user.o
+tracex5-objs := utils.o tracex5_user.o
+tracex6-objs := utils.o tracex6_user.o
+trace_output-objs := utils.o trace_output_user.o
+lathist-objs := utils.o lathist_user.o
+
+define add_depend
+$(foreach m, $(notdir $1), \
+ $(eval $(obj)/$m: $(obj)/libbpf/libbpf.a))
+endef
+$(call add_depend, $(hostprogs-y))
# Tell kbuild to always build the programs
always := $(hostprogs-y)
@@ -50,19 +70,8 @@ always += lathist_kern.o
HOSTCFLAGS += -I$(objtree)/usr/include
-HOSTCFLAGS_bpf_load.o += -I$(objtree)/usr/include -Wno-unused-variable
-HOSTLOADLIBES_fds_example += -lelf
-HOSTLOADLIBES_sockex1 += -lelf
-HOSTLOADLIBES_sockex2 += -lelf
-HOSTLOADLIBES_sockex3 += -lelf
-HOSTLOADLIBES_tracex1 += -lelf
-HOSTLOADLIBES_tracex2 += -lelf
-HOSTLOADLIBES_tracex3 += -lelf
-HOSTLOADLIBES_tracex4 += -lelf -lrt
-HOSTLOADLIBES_tracex5 += -lelf
-HOSTLOADLIBES_tracex6 += -lelf
-HOSTLOADLIBES_trace_output += -lelf -lrt
-HOSTLOADLIBES_lathist += -lelf