aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorDaniel Borkmann <dborkman@redhat.com>2014-05-23 12:44:00 -0400
committerDavid S. Miller <davem@davemloft.net>2014-05-23 16:48:06 -0400
commit10f18e0ba1ea7eb004bbaecb4748b1eb28802d24 (patch)
treea536357e238b2f9389f9d9c88100c10773102557 /lib
parent04caa489301c5e01ac4e5d1c13abcecb2041300b (diff)
net: filter: improve test case framework
This patch simplifies and refactors the test case code a bit and also adds a summary of all test that passed or failed in the kernel log, so that it's easier to spot if something has failed. Future work could further extend the test framework to also support different input 'stimuli' i.e. related structures to seccomp. Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Acked-by: Alexei Starovoitov <ast@plumgrid.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'lib')
-rw-r--r--lib/test_bpf.c388
1 files changed, 233 insertions, 155 deletions
diff --git a/lib/test_bpf.c b/lib/test_bpf.c
index e03991ea8cc2..da34e337f45b 100644
--- a/lib/test_bpf.c
+++ b/lib/test_bpf.c
@@ -22,12 +22,14 @@
22#include <linux/netdevice.h> 22#include <linux/netdevice.h>
23#include <linux/if_vlan.h> 23#include <linux/if_vlan.h>
24 24
25/* General test specific settings */
25#define MAX_SUBTESTS 3 26#define MAX_SUBTESTS 3
27#define MAX_TESTRUNS 10000
26#define MAX_DATA 128 28#define MAX_DATA 128
27#define MAX_INSNS 512 29#define MAX_INSNS 512
28#define MAX_K 0xffffFFFF 30#define MAX_K 0xffffFFFF
29 31
30/* define few constants used to init test 'skb' */ 32/* Few constants used to init test 'skb' */
31#define SKB_TYPE 3 33#define SKB_TYPE 3
32#define SKB_MARK 0x1234aaaa 34#define SKB_MARK 0x1234aaaa
33#define SKB_HASH 0x1234aaab 35#define SKB_HASH 0x1234aaab
@@ -36,18 +38,29 @@
36#define SKB_DEV_IFINDEX 577 38#define SKB_DEV_IFINDEX 577
37#define SKB_DEV_TYPE 588 39#define SKB_DEV_TYPE 588
38 40
39/* redefine REGs to make tests less verbose */ 41/* Redefine REGs to make tests less verbose */
40#define R0 BPF_REG_0 42#define R0 BPF_REG_0
41#define R1 BPF_REG_1 43#define R1 BPF_REG_1
42#define R2 BPF_REG_2 44#define R2 BPF_REG_2
43#define R3 BPF_REG_3 45#define R3 BPF_REG_3
44#define R4 BPF_REG_4 46#define R4 BPF_REG_4
45#define R5 BPF_REG_5 47#define R5 BPF_REG_5
46#define R6 BPF_REG_6 48#define R6 BPF_REG_6
47#define R7 BPF_REG_7 49#define R7 BPF_REG_7
48#define R8 BPF_REG_8 50#define R8 BPF_REG_8
49#define R9 BPF_REG_9 51#define R9 BPF_REG_9
50#define R10 BPF_REG_10 52#define R10 BPF_REG_10
53
54/* Flags that can be passed to test cases */
55#define FLAG_NO_DATA BIT(0)
56#define FLAG_EXPECTED_FAIL BIT(1)
57
58enum {
59 CLASSIC = BIT(6), /* Old BPF instructions only. */
60 INTERNAL = BIT(7), /* Extended instruction set. */
61};
62
63#define TEST_TYPE_MASK (CLASSIC | INTERNAL)
51 64
52struct bpf_test { 65struct bpf_test {
53 const char *descr; 66 const char *descr;
@@ -55,12 +68,7 @@ struct bpf_test {
55 struct sock_filter insns[MAX_INSNS]; 68 struct sock_filter insns[MAX_INSNS];
56 struct sock_filter_int insns_int[MAX_INSNS]; 69 struct sock_filter_int insns_int[MAX_INSNS];
57 } u; 70 } u;
58 enum { 71 __u8 aux;
59 NO_DATA,
60 EXPECTED_FAIL,
61 SKB,
62 SKB_INT
63 } data_type;
64 __u8 data[MAX_DATA]; 72 __u8 data[MAX_DATA];
65 struct { 73 struct {
66 int data_size; 74 int data_size;
@@ -84,7 +92,7 @@ static struct bpf_test tests[] = {
84 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1), 92 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
85 BPF_STMT(BPF_RET | BPF_A, 0) 93 BPF_STMT(BPF_RET | BPF_A, 0)
86 }, 94 },
87 SKB, 95 CLASSIC,
88 { 10, 20, 30, 40, 50 }, 96 { 10, 20, 30, 40, 50 },
89 { { 2, 10 }, { 3, 20 }, { 4, 30 } }, 97 { { 2, 10 }, { 3, 20 }, { 4, 30 } },
90 }, 98 },
@@ -96,7 +104,7 @@ static struct bpf_test tests[] = {
96 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0), 104 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
97 BPF_STMT(BPF_RET | BPF_A, 0) /* A == len * 2 */ 105 BPF_STMT(BPF_RET | BPF_A, 0) /* A == len * 2 */
98 }, 106 },
99 SKB, 107 CLASSIC,
100 { 10, 20, 30, 40, 50 }, 108 { 10, 20, 30, 40, 50 },
101 { { 1, 2 }, { 3, 6 }, { 4, 8 } }, 109 { { 1, 2 }, { 3, 6 }, { 4, 8 } },
102 }, 110 },
@@ -111,7 +119,7 @@ static struct bpf_test tests[] = {
111 BPF_STMT(BPF_ALU | BPF_MUL | BPF_K, 3), 119 BPF_STMT(BPF_ALU | BPF_MUL | BPF_K, 3),
112 BPF_STMT(BPF_RET | BPF_A, 0) 120 BPF_STMT(BPF_RET | BPF_A, 0)
113 }, 121 },
114 0, 122 CLASSIC | FLAG_NO_DATA,
115 { }, 123 { },
116 { { 0, 0xfffffffd } } 124 { { 0, 0xfffffffd } }
117 }, 125 },
@@ -129,7 +137,7 @@ static struct bpf_test tests[] = {
129 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0), 137 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
130 BPF_STMT(BPF_RET | BPF_A, 0) 138 BPF_STMT(BPF_RET | BPF_A, 0)
131 }, 139 },
132 0, 140 CLASSIC | FLAG_NO_DATA,
133 { }, 141 { },
134 { { 0, 0x40000001 } } 142 { { 0, 0x40000001 } }
135 }, 143 },
@@ -145,7 +153,7 @@ static struct bpf_test tests[] = {
145 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0), 153 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
146 BPF_STMT(BPF_RET | BPF_A, 0) 154 BPF_STMT(BPF_RET | BPF_A, 0)
147 }, 155 },
148 0, 156 CLASSIC | FLAG_NO_DATA,
149 { }, 157 { },
150 { { 0, 0x800000ff }, { 1, 0x800000ff } }, 158 { { 0, 0x800000ff }, { 1, 0x800000ff } },
151 }, 159 },
@@ -156,7 +164,7 @@ static struct bpf_test tests[] = {
156 BPF_STMT(BPF_LD | BPF_H | BPF_IND, MAX_K), 164 BPF_STMT(BPF_LD | BPF_H | BPF_IND, MAX_K),
157 BPF_STMT(BPF_RET | BPF_K, 1) 165 BPF_STMT(BPF_RET | BPF_K, 1)
158 }, 166 },
159 SKB, 167 CLASSIC,
160 { }, 168 { },
161 { { 1, 0 }, { 10, 0 }, { 60, 0 } }, 169 { { 1, 0 }, { 10, 0 }, { 60, 0 } },
162 }, 170 },
@@ -166,7 +174,7 @@ static struct bpf_test tests[] = {
166 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 1000), 174 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 1000),
167 BPF_STMT(BPF_RET | BPF_K, 1) 175 BPF_STMT(BPF_RET | BPF_K, 1)
168 }, 176 },
169 SKB, 177 CLASSIC,
170 { }, 178 { },
171 { { 1, 0 }, { 10, 0 }, { 60, 0 } }, 179 { { 1, 0 }, { 10, 0 }, { 60, 0 } },
172 }, 180 },
@@ -179,7 +187,7 @@ static struct bpf_test tests[] = {
179 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0), 187 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
180 BPF_STMT(BPF_RET | BPF_A, 0) 188 BPF_STMT(BPF_RET | BPF_A, 0)
181 }, 189 },
182 SKB, 190 CLASSIC,
183 { 1, 2, 3 }, 191 { 1, 2, 3 },
184 { { 1, 0 }, { 2, 3 } }, 192 { { 1, 0 }, { 2, 3 } },
185 }, 193 },
@@ -193,7 +201,7 @@ static struct bpf_test tests[] = {
193 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0), 201 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
194 BPF_STMT(BPF_RET | BPF_A, 0) 202 BPF_STMT(BPF_RET | BPF_A, 0)
195 }, 203 },
196 SKB, 204 CLASSIC,
197 { 1, 2, 3, 0xff }, 205 { 1, 2, 3, 0xff },
198 { { 1, 1 }, { 3, 3 }, { 4, 0xff } }, 206 { { 1, 1 }, { 3, 3 }, { 4, 0xff } },
199 }, 207 },
@@ -206,7 +214,7 @@ static struct bpf_test tests[] = {
206 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0), 214 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
207 BPF_STMT(BPF_RET | BPF_A, 0) 215 BPF_STMT(BPF_RET | BPF_A, 0)
208 }, 216 },
209 SKB, 217 CLASSIC,
210 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 }, 218 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 },
211 { { 15, 0 }, { 16, 3 } }, 219 { { 15, 0 }, { 16, 3 } },
212 }, 220 },
@@ -220,7 +228,7 @@ static struct bpf_test tests[] = {
220 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0), 228 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
221 BPF_STMT(BPF_RET | BPF_A, 0) 229 BPF_STMT(BPF_RET | BPF_A, 0)
222 }, 230 },
223 SKB, 231 CLASSIC,
224 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 }, 232 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 },
225 { { 14, 0 }, { 15, 1 }, { 17, 3 } }, 233 { { 14, 0 }, { 15, 1 }, { 17, 3 } },
226 }, 234 },
@@ -241,7 +249,7 @@ static struct bpf_test tests[] = {