]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
epf: improving the ebpf makefile
authorJesper Dangaard Brouer <netoptimizer@brouer.com>
Wed, 7 Feb 2018 22:21:24 +0000 (23:21 +0100)
committerVictor Julien <victor@inliniac.net>
Thu, 1 Mar 2018 13:36:50 +0000 (14:36 +0100)
The current ebpf/Makefile.am have the problem that clang compile
errors still result in an ELF .bpf output file.  This is obviously
problematic as the problem is first seen runtime when loading
the bpf-prog.  This is caused by the uses of a pipe from
clang to llc.

To address this problem, split up the clang and llc invocations
up into two separate commands, to get proper reaction based on
the compiler exit code. The clang compiler is used as a
frontend (+ optimizer) and instructed (via -S -emit-llvm) to
generate LLVM IR (Intermediate Representation) with suffix .ll.
The LLVM llc command is used as a compiler backend taking IR and
producing BPF machine bytecode, and storing this into a ELF
object.  In the last step the IR .ll suffix code it removed.

The official documentation of the IR language:
 http://llvm.org/docs/LangRef.html

Also fix the previous make portability warning:
 '%-style pattern rules are a GNU make extension'
I instead use some static pattern rules:
 https://www.gnu.org/software/make/manual/html_node/Static-Usage.html

Signed-off-by: Jesper Dangaard Brouer <netoptimizer@brouer.com>
ebpf/Makefile.am

index d0dc0b214f08f7fc3c11332c1eba5a2146303526..bfa99dc2df3cdb159b14ebb3f3739938cadca855 100644 (file)
@@ -3,11 +3,26 @@ if BUILD_EBPF
 # Maintaining a local copy of UAPI linux/bpf.h
 BPF_CFLAGS = -Iinclude
 
-all: lb.bpf filter.bpf bypass_filter.bpf xdp_filter.bpf vlan_filter.bpf
+CLANG = ${CC}
 
-%.bpf: %.c
-       ${CC} -Wall $(BPF_CFLAGS) -O2  -I/usr/include/$(build_cpu)-$(build_os)/ -D__KERNEL__ -D__ASM_SYSREG_H -target bpf -emit-llvm -c $< -o - | ${LLC} -march=bpf -filetype=obj -o $@
+BPF_TARGETS  = lb.bpf
+BPF_TARGETS += filter.bpf
+BPF_TARGETS += bypass_filter.bpf
+BPF_TARGETS += xdp_filter.bpf
+BPF_TARGETS += vlan_filter.bpf
 
-CLEANFILES = *.bpf
+all: $(BPF_TARGETS)
+
+$(BPF_TARGETS): %.bpf: %.c
+#      From C-code to LLVM-IR format suffix .ll (clang -S -emit-llvm)
+       ${CLANG} -Wall $(BPF_CFLAGS) -O2 \
+               -I/usr/include/$(build_cpu)-$(build_os)/ \
+               -D__KERNEL__ -D__ASM_SYSREG_H \
+               -target bpf -S -emit-llvm $< -o ${@:.bpf=.ll}
+#      From LLVM-IR to BPF-bytecode in ELF-obj file
+       ${LLC} -march=bpf -filetype=obj ${@:.bpf=.ll} -o $@
+       ${RM} ${@:.bpf=.ll}
+
+CLEANFILES = *.bpf *.ll
 
 endif