Skip to content

Commit

Permalink
tc-testing: fix build of eBPF programs
Browse files Browse the repository at this point in the history
rely on uAPI headers in the current kernel tree, rather than requiring the
correct version installed on the test system. While at it, group all
sections in a single binary and test the 'section' parameter.

Reported-by: Lucas Bates <lucasb@mojatatu.com>
Signed-off-by: Davide Caratti <dcaratti@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Davide Caratti authored and David S. Miller committed Oct 5, 2018
1 parent 4a84810 commit cf5eafb
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
29 changes: 29 additions & 0 deletions tools/testing/selftests/tc-testing/bpf/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# SPDX-License-Identifier: GPL-2.0

APIDIR := ../../../../include/uapi
TEST_GEN_FILES = action.o

top_srcdir = ../../../../..
include ../../lib.mk

CLANG ?= clang
LLC ?= llc
PROBE := $(shell $(LLC) -march=bpf -mcpu=probe -filetype=null /dev/null 2>&1)

ifeq ($(PROBE),)
CPU ?= probe
else
CPU ?= generic
endif

CLANG_SYS_INCLUDES := $(shell $(CLANG) -v -E - </dev/null 2>&1 \
| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')

CLANG_FLAGS = -I. -I$(APIDIR) \
$(CLANG_SYS_INCLUDES) \
-Wno-compare-distinct-pointer-types

$(OUTPUT)/%.o: %.c
$(CLANG) $(CLANG_FLAGS) \
-O2 -target bpf -emit-llvm -c $< -o - | \
$(LLC) -march=bpf -mcpu=$(CPU) $(LLC_FLAGS) -filetype=obj -o $@
23 changes: 23 additions & 0 deletions tools/testing/selftests/tc-testing/bpf/action.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* SPDX-License-Identifier: GPL-2.0
* Copyright (c) 2018 Davide Caratti, Red Hat inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*/

#include <linux/bpf.h>
#include <linux/pkt_cls.h>

__attribute__((section("action-ok"),used)) int action_ok(struct __sk_buff *s)
{
return TC_ACT_OK;
}

__attribute__((section("action-ko"),used)) int action_ko(struct __sk_buff *s)
{
s->data = 0x0;
return TC_ACT_OK;
}

char _license[] __attribute__((section("license"),used)) = "GPL";
16 changes: 8 additions & 8 deletions tools/testing/selftests/tc-testing/tc-tests/actions/bpf.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,22 @@
"bpf"
],
"setup": [
"printf '#include <linux/bpf.h>\nchar l[] __attribute__((section(\"license\"),used))=\"GPL\"; __attribute__((section(\"action\"),used)) int m(struct __sk_buff *s) { return 2; }' | clang -O2 -x c -c - -target bpf -o _b.o",
"make -C bpf",
[
"$TC action flush action bpf",
0,
1,
255
]
],
"cmdUnderTest": "$TC action add action bpf object-file _b.o index 667",
"cmdUnderTest": "$TC action add action bpf object-file $EBPFDIR/action.o section action-ok index 667",
"expExitCode": "0",
"verifyCmd": "$TC action get action bpf index 667",
"matchPattern": "action order [0-9]*: bpf _b.o:\\[action\\] id [0-9]* tag 3b185187f1855c4c( jited)? default-action pipe.*index 667 ref",
"matchPattern": "action order [0-9]*: bpf action.o:\\[action-ok\\] id [0-9]* tag [0-9a-f]{16}( jited)? default-action pipe.*index 667 ref",
"matchCount": "1",
"teardown": [
"$TC action flush action bpf",
"rm -f _b.o"
"make -C bpf clean"
]
},
{
Expand All @@ -81,18 +81,18 @@
"bpf"
],
"setup": [
"printf '#include <linux/bpf.h>\nchar l[] __attribute__((section(\"license\"),used))=\"GPL\"; __attribute__((section(\"action\"),used)) int m(struct __sk_buff *s) { s->data = 0x0; return 2; }' | clang -O2 -x c -c - -target bpf -o _c.o",
"make -C bpf",
[
"$TC action flush action bpf",
0,
1,
255
]
],
"cmdUnderTest": "$TC action add action bpf object-file _c.o index 667",
"cmdUnderTest": "$TC action add action bpf object-file $EBPFDIR/action.o section action-ko index 667",
"expExitCode": "255",
"verifyCmd": "$TC action get action bpf index 667",
"matchPattern": "action order [0-9]*: bpf _c.o:\\[action\\] id [0-9].*index 667 ref",
"matchPattern": "action order [0-9]*: bpf action.o:\\[action-ko\\] id [0-9].*index 667 ref",
"matchCount": "0",
"teardown": [
[
Expand All @@ -101,7 +101,7 @@
1,
255
],
"rm -f _c.o"
"make -C bpf clean"
]
},
{
Expand Down
4 changes: 3 additions & 1 deletion tools/testing/selftests/tc-testing/tdc_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
'DEV2': '',
'BATCH_FILE': './batch.txt',
# Name of the namespace to use
'NS': 'tcut'
'NS': 'tcut',
# Directory containing eBPF test programs
'EBPFDIR': './bpf'
}


Expand Down

0 comments on commit cf5eafb

Please sign in to comment.