blob: c579e0f58818f8c22709dcce269e33a48265d722 [file] [log] [blame]
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001/*
2 * Testsuite for BPF interpreter and BPF JIT compiler
3 *
4 * Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 */
15
16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
18#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/filter.h>
21#include <linux/skbuff.h>
22#include <linux/netdevice.h>
23#include <linux/if_vlan.h>
24
Daniel Borkmann10f18e02014-05-23 18:44:00 +020025/* General test specific settings */
Alexei Starovoitov64a89462014-05-08 14:10:52 -070026#define MAX_SUBTESTS 3
Daniel Borkmann10f18e02014-05-23 18:44:00 +020027#define MAX_TESTRUNS 10000
Alexei Starovoitov64a89462014-05-08 14:10:52 -070028#define MAX_DATA 128
29#define MAX_INSNS 512
30#define MAX_K 0xffffFFFF
31
Daniel Borkmann10f18e02014-05-23 18:44:00 +020032/* Few constants used to init test 'skb' */
Alexei Starovoitov64a89462014-05-08 14:10:52 -070033#define SKB_TYPE 3
34#define SKB_MARK 0x1234aaaa
35#define SKB_HASH 0x1234aaab
36#define SKB_QUEUE_MAP 123
37#define SKB_VLAN_TCI 0xffff
38#define SKB_DEV_IFINDEX 577
39#define SKB_DEV_TYPE 588
40
Daniel Borkmann10f18e02014-05-23 18:44:00 +020041/* Redefine REGs to make tests less verbose */
42#define R0 BPF_REG_0
43#define R1 BPF_REG_1
44#define R2 BPF_REG_2
45#define R3 BPF_REG_3
46#define R4 BPF_REG_4
47#define R5 BPF_REG_5
48#define R6 BPF_REG_6
49#define R7 BPF_REG_7
50#define R8 BPF_REG_8
51#define R9 BPF_REG_9
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)
Alexei Starovoitov64a89462014-05-08 14:10:52 -070064
65struct bpf_test {
66 const char *descr;
67 union {
68 struct sock_filter insns[MAX_INSNS];
69 struct sock_filter_int insns_int[MAX_INSNS];
Andrew Mortonece80492014-05-22 10:16:46 -070070 } u;
Daniel Borkmann10f18e02014-05-23 18:44:00 +020071 __u8 aux;
Alexei Starovoitov64a89462014-05-08 14:10:52 -070072 __u8 data[MAX_DATA];
73 struct {
74 int data_size;
75 __u32 result;
76 } test[MAX_SUBTESTS];
77};
78
79static struct bpf_test tests[] = {
80 {
81 "TAX",
Andrew Mortonece80492014-05-22 10:16:46 -070082 .u.insns = {
Alexei Starovoitov64a89462014-05-08 14:10:52 -070083 BPF_STMT(BPF_LD | BPF_IMM, 1),
84 BPF_STMT(BPF_MISC | BPF_TAX, 0),
85 BPF_STMT(BPF_LD | BPF_IMM, 2),
86 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
87 BPF_STMT(BPF_ALU | BPF_NEG, 0), /* A == -3 */
88 BPF_STMT(BPF_MISC | BPF_TAX, 0),
89 BPF_STMT(BPF_LD | BPF_LEN, 0),
90 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
91 BPF_STMT(BPF_MISC | BPF_TAX, 0), /* X == len - 3 */
92 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
93 BPF_STMT(BPF_RET | BPF_A, 0)
94 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +020095 CLASSIC,
Alexei Starovoitov64a89462014-05-08 14:10:52 -070096 { 10, 20, 30, 40, 50 },
97 { { 2, 10 }, { 3, 20 }, { 4, 30 } },
98 },
99 {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700100 "TXA",
Andrew Mortonece80492014-05-22 10:16:46 -0700101 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700102 BPF_STMT(BPF_LDX | BPF_LEN, 0),
103 BPF_STMT(BPF_MISC | BPF_TXA, 0),
104 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
105 BPF_STMT(BPF_RET | BPF_A, 0) /* A == len * 2 */
106 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200107 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700108 { 10, 20, 30, 40, 50 },
109 { { 1, 2 }, { 3, 6 }, { 4, 8 } },
110 },
111 {
112 "ADD_SUB_MUL_K",
Andrew Mortonece80492014-05-22 10:16:46 -0700113 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700114 BPF_STMT(BPF_LD | BPF_IMM, 1),
115 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 2),
116 BPF_STMT(BPF_LDX | BPF_IMM, 3),
117 BPF_STMT(BPF_ALU | BPF_SUB | BPF_X, 0),
118 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 0xffffffff),
119 BPF_STMT(BPF_ALU | BPF_MUL | BPF_K, 3),
120 BPF_STMT(BPF_RET | BPF_A, 0)
121 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200122 CLASSIC | FLAG_NO_DATA,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700123 { },
124 { { 0, 0xfffffffd } }
125 },
126 {
127 "DIV_KX",
Andrew Mortonece80492014-05-22 10:16:46 -0700128 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700129 BPF_STMT(BPF_LD | BPF_IMM, 8),
130 BPF_STMT(BPF_ALU | BPF_DIV | BPF_K, 2),
131 BPF_STMT(BPF_MISC | BPF_TAX, 0),
132 BPF_STMT(BPF_LD | BPF_IMM, 0xffffffff),
133 BPF_STMT(BPF_ALU | BPF_DIV | BPF_X, 0),
134 BPF_STMT(BPF_MISC | BPF_TAX, 0),
135 BPF_STMT(BPF_LD | BPF_IMM, 0xffffffff),
136 BPF_STMT(BPF_ALU | BPF_DIV | BPF_K, 0x70000000),
137 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
138 BPF_STMT(BPF_RET | BPF_A, 0)
139 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200140 CLASSIC | FLAG_NO_DATA,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700141 { },
142 { { 0, 0x40000001 } }
143 },
144 {
145 "AND_OR_LSH_K",
Andrew Mortonece80492014-05-22 10:16:46 -0700146 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700147 BPF_STMT(BPF_LD | BPF_IMM, 0xff),
148 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xf0),
149 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 27),
150 BPF_STMT(BPF_MISC | BPF_TAX, 0),
151 BPF_STMT(BPF_LD | BPF_IMM, 0xf),
152 BPF_STMT(BPF_ALU | BPF_OR | BPF_K, 0xf0),
153 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
154 BPF_STMT(BPF_RET | BPF_A, 0)
155 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200156 CLASSIC | FLAG_NO_DATA,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700157 { },
158 { { 0, 0x800000ff }, { 1, 0x800000ff } },
159 },
160 {
Chema Gonzaleze9d94502014-05-30 10:15:12 -0700161 "LD_IMM_0",
162 .u.insns = {
163 BPF_STMT(BPF_LD | BPF_IMM, 0), /* ld #0 */
164 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, 1, 0),
165 BPF_STMT(BPF_RET | BPF_K, 0),
166 BPF_STMT(BPF_RET | BPF_K, 1),
167 },
168 CLASSIC,
169 { },
170 { { 1, 1 } },
171 },
172 {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700173 "LD_IND",
Andrew Mortonece80492014-05-22 10:16:46 -0700174 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700175 BPF_STMT(BPF_LDX | BPF_LEN, 0),
176 BPF_STMT(BPF_LD | BPF_H | BPF_IND, MAX_K),
177 BPF_STMT(BPF_RET | BPF_K, 1)
178 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200179 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700180 { },
181 { { 1, 0 }, { 10, 0 }, { 60, 0 } },
182 },
183 {
184 "LD_ABS",
Andrew Mortonece80492014-05-22 10:16:46 -0700185 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700186 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 1000),
187 BPF_STMT(BPF_RET | BPF_K, 1)
188 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200189 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700190 { },
191 { { 1, 0 }, { 10, 0 }, { 60, 0 } },
192 },
193 {
194 "LD_ABS_LL",
Andrew Mortonece80492014-05-22 10:16:46 -0700195 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700196 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, SKF_LL_OFF),
197 BPF_STMT(BPF_MISC | BPF_TAX, 0),
198 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, SKF_LL_OFF + 1),
199 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
200 BPF_STMT(BPF_RET | BPF_A, 0)
201 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200202 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700203 { 1, 2, 3 },
204 { { 1, 0 }, { 2, 3 } },
205 },
206 {
207 "LD_IND_LL",
Andrew Mortonece80492014-05-22 10:16:46 -0700208 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700209 BPF_STMT(BPF_LD | BPF_IMM, SKF_LL_OFF - 1),
210 BPF_STMT(BPF_LDX | BPF_LEN, 0),
211 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
212 BPF_STMT(BPF_MISC | BPF_TAX, 0),
213 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
214 BPF_STMT(BPF_RET | BPF_A, 0)
215 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200216 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700217 { 1, 2, 3, 0xff },
218 { { 1, 1 }, { 3, 3 }, { 4, 0xff } },
219 },
220 {
221 "LD_ABS_NET",
Andrew Mortonece80492014-05-22 10:16:46 -0700222 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700223 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, SKF_NET_OFF),
224 BPF_STMT(BPF_MISC | BPF_TAX, 0),
225 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, SKF_NET_OFF + 1),
226 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
227 BPF_STMT(BPF_RET | BPF_A, 0)
228 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200229 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700230 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 },
231 { { 15, 0 }, { 16, 3 } },
232 },
233 {
234 "LD_IND_NET",
Andrew Mortonece80492014-05-22 10:16:46 -0700235 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700236 BPF_STMT(BPF_LD | BPF_IMM, SKF_NET_OFF - 15),
237 BPF_STMT(BPF_LDX | BPF_LEN, 0),
238 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
239 BPF_STMT(BPF_MISC | BPF_TAX, 0),
240 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
241 BPF_STMT(BPF_RET | BPF_A, 0)
242 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200243 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700244 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3 },
245 { { 14, 0 }, { 15, 1 }, { 17, 3 } },
246 },
247 {
248 "LD_PKTTYPE",
Andrew Mortonece80492014-05-22 10:16:46 -0700249 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700250 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
251 SKF_AD_OFF + SKF_AD_PKTTYPE),
252 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SKB_TYPE, 1, 0),
253 BPF_STMT(BPF_RET | BPF_K, 1),
254 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
255 SKF_AD_OFF + SKF_AD_PKTTYPE),
256 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SKB_TYPE, 1, 0),
257 BPF_STMT(BPF_RET | BPF_K, 1),
258 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
259 SKF_AD_OFF + SKF_AD_PKTTYPE),
260 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SKB_TYPE, 1, 0),
261 BPF_STMT(BPF_RET | BPF_K, 1),
262 BPF_STMT(BPF_RET | BPF_A, 0)
263 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200264 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700265 { },
266 { { 1, 3 }, { 10, 3 } },
267 },
268 {
269 "LD_MARK",
Andrew Mortonece80492014-05-22 10:16:46 -0700270 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700271 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
272 SKF_AD_OFF + SKF_AD_MARK),
273 BPF_STMT(BPF_RET | BPF_A, 0)
274 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200275 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700276 { },
277 { { 1, SKB_MARK}, { 10, SKB_MARK} },
278 },
279 {
280 "LD_RXHASH",
Andrew Mortonece80492014-05-22 10:16:46 -0700281 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700282 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
283 SKF_AD_OFF + SKF_AD_RXHASH),
284 BPF_STMT(BPF_RET | BPF_A, 0)
285 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200286 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700287 { },
288 { { 1, SKB_HASH}, { 10, SKB_HASH} },
289 },
290 {
291 "LD_QUEUE",
Andrew Mortonece80492014-05-22 10:16:46 -0700292 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700293 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
294 SKF_AD_OFF + SKF_AD_QUEUE),
295 BPF_STMT(BPF_RET | BPF_A, 0)
296 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200297 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700298 { },
299 { { 1, SKB_QUEUE_MAP }, { 10, SKB_QUEUE_MAP } },
300 },
301 {
302 "LD_PROTOCOL",
Andrew Mortonece80492014-05-22 10:16:46 -0700303 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700304 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 1),
305 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 20, 1, 0),
306 BPF_STMT(BPF_RET | BPF_K, 0),
307 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
308 SKF_AD_OFF + SKF_AD_PROTOCOL),
309 BPF_STMT(BPF_MISC | BPF_TAX, 0),
310 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
311 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 30, 1, 0),
312 BPF_STMT(BPF_RET | BPF_K, 0),
313 BPF_STMT(BPF_MISC | BPF_TXA, 0),
314 BPF_STMT(BPF_RET | BPF_A, 0)
315 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200316 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700317 { 10, 20, 30 },
318 { { 10, ETH_P_IP }, { 100, ETH_P_IP } },
319 },
320 {
321 "LD_VLAN_TAG",
Andrew Mortonece80492014-05-22 10:16:46 -0700322 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700323 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
324 SKF_AD_OFF + SKF_AD_VLAN_TAG),
325 BPF_STMT(BPF_RET | BPF_A, 0)
326 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200327 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700328 { },
329 {
330 { 1, SKB_VLAN_TCI & ~VLAN_TAG_PRESENT },
331 { 10, SKB_VLAN_TCI & ~VLAN_TAG_PRESENT }
332 },
333 },
334 {
335 "LD_VLAN_TAG_PRESENT",
Andrew Mortonece80492014-05-22 10:16:46 -0700336 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700337 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
338 SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT),
339 BPF_STMT(BPF_RET | BPF_A, 0)
340 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200341 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700342 { },
343 {
344 { 1, !!(SKB_VLAN_TCI & VLAN_TAG_PRESENT) },
345 { 10, !!(SKB_VLAN_TCI & VLAN_TAG_PRESENT) }
346 },
347 },
348 {
349 "LD_IFINDEX",
Andrew Mortonece80492014-05-22 10:16:46 -0700350 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700351 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
352 SKF_AD_OFF + SKF_AD_IFINDEX),
353 BPF_STMT(BPF_RET | BPF_A, 0)
354 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200355 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700356 { },
357 { { 1, SKB_DEV_IFINDEX }, { 10, SKB_DEV_IFINDEX } },
358 },
359 {
360 "LD_HATYPE",
Andrew Mortonece80492014-05-22 10:16:46 -0700361 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700362 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
363 SKF_AD_OFF + SKF_AD_HATYPE),
364 BPF_STMT(BPF_RET | BPF_A, 0)
365 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200366 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700367 { },
368 { { 1, SKB_DEV_TYPE }, { 10, SKB_DEV_TYPE } },
369 },
370 {
371 "LD_CPU",
Andrew Mortonece80492014-05-22 10:16:46 -0700372 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700373 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
374 SKF_AD_OFF + SKF_AD_CPU),
375 BPF_STMT(BPF_MISC | BPF_TAX, 0),
376 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
377 SKF_AD_OFF + SKF_AD_CPU),
378 BPF_STMT(BPF_ALU | BPF_SUB | BPF_X, 0),
379 BPF_STMT(BPF_RET | BPF_A, 0)
380 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200381 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700382 { },
383 { { 1, 0 }, { 10, 0 } },
384 },
385 {
386 "LD_NLATTR",
Andrew Mortonece80492014-05-22 10:16:46 -0700387 .u.insns = {
Alexei Starovoitovdf6d0f9832014-06-06 17:48:20 -0700388 BPF_STMT(BPF_LDX | BPF_IMM, 2),
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700389 BPF_STMT(BPF_MISC | BPF_TXA, 0),
390 BPF_STMT(BPF_LDX | BPF_IMM, 3),
391 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
392 SKF_AD_OFF + SKF_AD_NLATTR),
393 BPF_STMT(BPF_RET | BPF_A, 0)
394 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200395 CLASSIC,
Alexei Starovoitovdf6d0f9832014-06-06 17:48:20 -0700396#ifdef __BIG_ENDIAN
397 { 0xff, 0xff, 0, 4, 0, 2, 0, 4, 0, 3 },
398#else
399 { 0xff, 0xff, 4, 0, 2, 0, 4, 0, 3, 0 },
400#endif
401 { { 4, 0 }, { 20, 6 } },
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700402 },
403 {
404 "LD_NLATTR_NEST",
Andrew Mortonece80492014-05-22 10:16:46 -0700405 .u.insns = {
Alexei Starovoitovdf6d0f9832014-06-06 17:48:20 -0700406 BPF_STMT(BPF_LD | BPF_IMM, 2),
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700407 BPF_STMT(BPF_LDX | BPF_IMM, 3),
408 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
409 SKF_AD_OFF + SKF_AD_NLATTR_NEST),
Alexei Starovoitovdf6d0f9832014-06-06 17:48:20 -0700410 BPF_STMT(BPF_LD | BPF_IMM, 2),
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700411 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
412 SKF_AD_OFF + SKF_AD_NLATTR_NEST),
Alexei Starovoitovdf6d0f9832014-06-06 17:48:20 -0700413 BPF_STMT(BPF_LD | BPF_IMM, 2),
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700414 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
415 SKF_AD_OFF + SKF_AD_NLATTR_NEST),
Alexei Starovoitovdf6d0f9832014-06-06 17:48:20 -0700416 BPF_STMT(BPF_LD | BPF_IMM, 2),
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700417 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
418 SKF_AD_OFF + SKF_AD_NLATTR_NEST),
Alexei Starovoitovdf6d0f9832014-06-06 17:48:20 -0700419 BPF_STMT(BPF_LD | BPF_IMM, 2),
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700420 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
421 SKF_AD_OFF + SKF_AD_NLATTR_NEST),
Alexei Starovoitovdf6d0f9832014-06-06 17:48:20 -0700422 BPF_STMT(BPF_LD | BPF_IMM, 2),
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700423 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
424 SKF_AD_OFF + SKF_AD_NLATTR_NEST),
Alexei Starovoitovdf6d0f9832014-06-06 17:48:20 -0700425 BPF_STMT(BPF_LD | BPF_IMM, 2),
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700426 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
427 SKF_AD_OFF + SKF_AD_NLATTR_NEST),
Alexei Starovoitovdf6d0f9832014-06-06 17:48:20 -0700428 BPF_STMT(BPF_LD | BPF_IMM, 2),
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700429 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
430 SKF_AD_OFF + SKF_AD_NLATTR_NEST),
431 BPF_STMT(BPF_RET | BPF_A, 0)
432 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200433 CLASSIC,
Alexei Starovoitovdf6d0f9832014-06-06 17:48:20 -0700434#ifdef __BIG_ENDIAN
435 { 0xff, 0xff, 0, 12, 0, 1, 0, 4, 0, 2, 0, 4, 0, 3 },
436#else
437 { 0xff, 0xff, 12, 0, 1, 0, 4, 0, 2, 0, 4, 0, 3, 0 },
438#endif
439 { { 4, 0 }, { 20, 10 } },
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700440 },
441 {
442 "LD_PAYLOAD_OFF",
Andrew Mortonece80492014-05-22 10:16:46 -0700443 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700444 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
445 SKF_AD_OFF + SKF_AD_PAY_OFFSET),
446 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
447 SKF_AD_OFF + SKF_AD_PAY_OFFSET),
448 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
449 SKF_AD_OFF + SKF_AD_PAY_OFFSET),
450 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
451 SKF_AD_OFF + SKF_AD_PAY_OFFSET),
452 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
453 SKF_AD_OFF + SKF_AD_PAY_OFFSET),
454 BPF_STMT(BPF_RET | BPF_A, 0)
455 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200456 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700457 /* 00:00:00:00:00:00 > 00:00:00:00:00:00, ethtype IPv4 (0x0800),
458 * length 98: 127.0.0.1 > 127.0.0.1: ICMP echo request,
459 * id 9737, seq 1, length 64
460 */
461 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
462 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
463 0x08, 0x00,
464 0x45, 0x00, 0x00, 0x54, 0xac, 0x8b, 0x40, 0x00, 0x40,
465 0x01, 0x90, 0x1b, 0x7f, 0x00, 0x00, 0x01 },
466 { { 30, 0 }, { 100, 42 } },
467 },
468 {
469 "LD_ANC_XOR",
Andrew Mortonece80492014-05-22 10:16:46 -0700470 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700471 BPF_STMT(BPF_LD | BPF_IMM, 10),
472 BPF_STMT(BPF_LDX | BPF_IMM, 300),
473 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
474 SKF_AD_OFF + SKF_AD_ALU_XOR_X),
475 BPF_STMT(BPF_RET | BPF_A, 0)
476 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200477 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700478 { },
479 { { 4, 10 ^ 300 }, { 20, 10 ^ 300 } },
480 },
481 {
482 "SPILL_FILL",
Andrew Mortonece80492014-05-22 10:16:46 -0700483 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700484 BPF_STMT(BPF_LDX | BPF_LEN, 0),
485 BPF_STMT(BPF_LD | BPF_IMM, 2),
486 BPF_STMT(BPF_ALU | BPF_RSH, 1),
487 BPF_STMT(BPF_ALU | BPF_XOR | BPF_X, 0),
488 BPF_STMT(BPF_ST, 1), /* M1 = 1 ^ len */
489 BPF_STMT(BPF_ALU | BPF_XOR | BPF_K, 0x80000000),
490 BPF_STMT(BPF_ST, 2), /* M2 = 1 ^ len ^ 0x80000000 */
491 BPF_STMT(BPF_STX, 15), /* M3 = len */
492 BPF_STMT(BPF_LDX | BPF_MEM, 1),
493 BPF_STMT(BPF_LD | BPF_MEM, 2),
494 BPF_STMT(BPF_ALU | BPF_XOR | BPF_X, 0),
495 BPF_STMT(BPF_LDX | BPF_MEM, 15),
496 BPF_STMT(BPF_ALU | BPF_XOR | BPF_X, 0),
497 BPF_STMT(BPF_RET | BPF_A, 0)
498 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200499 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700500 { },
501 { { 1, 0x80000001 }, { 2, 0x80000002 }, { 60, 0x80000000 ^ 60 } }
502 },
503 {
504 "JEQ",
Andrew Mortonece80492014-05-22 10:16:46 -0700505 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700506 BPF_STMT(BPF_LDX | BPF_LEN, 0),
507 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
508 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, 0, 1),
509 BPF_STMT(BPF_RET | BPF_K, 1),
510 BPF_STMT(BPF_RET | BPF_K, MAX_K)
511 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200512 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700513 { 3, 3, 3, 3, 3 },
514 { { 1, 0 }, { 3, 1 }, { 4, MAX_K } },
515 },
516 {
517 "JGT",
Andrew Mortonece80492014-05-22 10:16:46 -0700518 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700519 BPF_STMT(BPF_LDX | BPF_LEN, 0),
520 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
521 BPF_JUMP(BPF_JMP | BPF_JGT | BPF_X, 0, 0, 1),
522 BPF_STMT(BPF_RET | BPF_K, 1),
523 BPF_STMT(BPF_RET | BPF_K, MAX_K)
524 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200525 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700526 { 4, 4, 4, 3, 3 },
527 { { 2, 0 }, { 3, 1 }, { 4, MAX_K } },
528 },
529 {
530 "JGE",
Andrew Mortonece80492014-05-22 10:16:46 -0700531 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700532 BPF_STMT(BPF_LDX | BPF_LEN, 0),
533 BPF_STMT(BPF_LD | BPF_B | BPF_IND, MAX_K),
534 BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, 1, 1, 0),
535 BPF_STMT(BPF_RET | BPF_K, 10),
536 BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, 2, 1, 0),
537 BPF_STMT(BPF_RET | BPF_K, 20),
538 BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, 3, 1, 0),
539 BPF_STMT(BPF_RET | BPF_K, 30),
540 BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, 4, 1, 0),
541 BPF_STMT(BPF_RET | BPF_K, 40),
542 BPF_STMT(BPF_RET | BPF_K, MAX_K)
543 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200544 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700545 { 1, 2, 3, 4, 5 },
546 { { 1, 20 }, { 3, 40 }, { 5, MAX_K } },
547 },
548 {
549 "JSET",
Andrew Mortonece80492014-05-22 10:16:46 -0700550 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700551 BPF_JUMP(BPF_JMP | BPF_JA, 0, 0, 0),
552 BPF_JUMP(BPF_JMP | BPF_JA, 1, 1, 1),
553 BPF_JUMP(BPF_JMP | BPF_JA, 0, 0, 0),
554 BPF_JUMP(BPF_JMP | BPF_JA, 0, 0, 0),
555 BPF_STMT(BPF_LDX | BPF_LEN, 0),
556 BPF_STMT(BPF_MISC | BPF_TXA, 0),
557 BPF_STMT(BPF_ALU | BPF_SUB | BPF_K, 4),
558 BPF_STMT(BPF_MISC | BPF_TAX, 0),
559 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
560 BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, 1, 0, 1),
561 BPF_STMT(BPF_RET | BPF_K, 10),
562 BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, 0x80000000, 0, 1),
563 BPF_STMT(BPF_RET | BPF_K, 20),
564 BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, 0xffffff, 1, 0),
565 BPF_STMT(BPF_RET | BPF_K, 30),
566 BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, 0xffffff, 1, 0),
567 BPF_STMT(BPF_RET | BPF_K, 30),
568 BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, 0xffffff, 1, 0),
569 BPF_STMT(BPF_RET | BPF_K, 30),
570 BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, 0xffffff, 1, 0),
571 BPF_STMT(BPF_RET | BPF_K, 30),
572 BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, 0xffffff, 1, 0),
573 BPF_STMT(BPF_RET | BPF_K, 30),
574 BPF_STMT(BPF_RET | BPF_K, MAX_K)
575 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200576 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700577 { 0, 0xAA, 0x55, 1 },
578 { { 4, 10 }, { 5, 20 }, { 6, MAX_K } },
579 },
580 {
Alexei Starovoitov64a89462014-05-08 14:10:52 -0700581 "tcpdump port 22",
Andrew Mortonece80492014-05-22 10:16:46 -0700582 .u.insns = {
Daniel Borkmannce25b682014-05-26 20:17:35 +0200583 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 12),
584 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x86dd, 0, 8), /* IPv6 */
585 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 20),
586 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x84, 2, 0),
587 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x6, 1, 0),
588 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x11, 0, 17),
589 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 54),
590 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 22, 14, 0),
591 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 56),
592 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 22, 12, 13),
593 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0800, 0, 12), /* IPv4 */
594 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 23),
595 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x84, 2, 0),
596 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x6, 1, 0),
597 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x11, 0, 8),
598 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 20),
599 BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, 0x1fff, 6, 0),
600 BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, 14),
601 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 14),
602 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 22, 2, 0),
603 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 16),
604 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 22, 0, 1),
605 BPF_STMT(BPF_RET | BPF_K, 0xffff),
606 BPF_STMT(BPF_RET | BPF_K, 0),
Alexei Starovoitov64a89462014-05-08 14:10:52 -0700607 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200608 CLASSIC,
Alexei Starovoitov64a89462014-05-08 14:10:52 -0700609 /* 3c:07:54:43:e5:76 > 10:bf:48:d6:43:d6, ethertype IPv4(0x0800)
610 * length 114: 10.1.1.149.49700 > 10.1.2.10.22: Flags [P.],
611 * seq 1305692979:1305693027, ack 3650467037, win 65535,
612 * options [nop,nop,TS val 2502645400 ecr 3971138], length 48
613 */
614 { 0x10, 0xbf, 0x48, 0xd6, 0x43, 0xd6,
615 0x3c, 0x07, 0x54, 0x43, 0xe5, 0x76,
616 0x08, 0x00,
617 0x45, 0x10, 0x00, 0x64, 0x75, 0xb5,
618 0x40, 0x00, 0x40, 0x06, 0xad, 0x2e, /* IP header */
619 0x0a, 0x01, 0x01, 0x95, /* ip src */
620 0x0a, 0x01, 0x02, 0x0a, /* ip dst */
621 0xc2, 0x24,
622 0x00, 0x16 /* dst port */ },
623 { { 10, 0 }, { 30, 0 }, { 100, 65535 } },
624 },
625 {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700626 "tcpdump complex",
Andrew Mortonece80492014-05-22 10:16:46 -0700627 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700628 /* tcpdump -nei eth0 'tcp port 22 and (((ip[2:2] -
629 * ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0) and
630 * (len > 115 or len < 30000000000)' -d
631 */
Daniel Borkmannce25b682014-05-26 20:17:35 +0200632 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 12),
633 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x86dd, 30, 0),
634 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x800, 0, 29),
635 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 23),
636 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x6, 0, 27),
637 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 20),
638 BPF_JUMP(BPF_JMP | BPF_JSET | BPF_K, 0x1fff, 25, 0),
639 BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, 14),
640 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 14),
641 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 22, 2, 0),
642 BPF_STMT(BPF_LD | BPF_H | BPF_IND, 16),
643 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 22, 0, 20),
644 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 16),
645 BPF_STMT(BPF_ST, 1),
646 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 14),
647 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xf),
648 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 2),
649 BPF_STMT(BPF_MISC | BPF_TAX, 0x5), /* libpcap emits K on TAX */
650 BPF_STMT(BPF_LD | BPF_MEM, 1),
651 BPF_STMT(BPF_ALU | BPF_SUB | BPF_X, 0),
652 BPF_STMT(BPF_ST, 5),
653 BPF_STMT(BPF_LDX | BPF_B | BPF_MSH, 14),
654 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 26),
655 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xf0),
656 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 2),
657 BPF_STMT(BPF_MISC | BPF_TAX, 0x9), /* libpcap emits K on TAX */
658 BPF_STMT(BPF_LD | BPF_MEM, 5),
659 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, 4, 0),
660 BPF_STMT(BPF_LD | BPF_LEN, 0),
661 BPF_JUMP(BPF_JMP | BPF_JGT | BPF_K, 0x73, 1, 0),
662 BPF_JUMP(BPF_JMP | BPF_JGE | BPF_K, 0xfc23ac00, 1, 0),
663 BPF_STMT(BPF_RET | BPF_K, 0xffff),
664 BPF_STMT(BPF_RET | BPF_K, 0),
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700665 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200666 CLASSIC,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700667 { 0x10, 0xbf, 0x48, 0xd6, 0x43, 0xd6,
668 0x3c, 0x07, 0x54, 0x43, 0xe5, 0x76,
669 0x08, 0x00,
670 0x45, 0x10, 0x00, 0x64, 0x75, 0xb5,
671 0x40, 0x00, 0x40, 0x06, 0xad, 0x2e, /* IP header */
672 0x0a, 0x01, 0x01, 0x95, /* ip src */
673 0x0a, 0x01, 0x02, 0x0a, /* ip dst */
674 0xc2, 0x24,
675 0x00, 0x16 /* dst port */ },
676 { { 10, 0 }, { 30, 0 }, { 100, 65535 } },
677 },
678 {
679 "RET_A",
Andrew Mortonece80492014-05-22 10:16:46 -0700680 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700681 /* check that unitialized X and A contain zeros */
682 BPF_STMT(BPF_MISC | BPF_TXA, 0),
683 BPF_STMT(BPF_RET | BPF_A, 0)
684 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200685 CLASSIC,
686 { },
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700687 { {1, 0}, {2, 0} },
688 },
689 {
690 "INT: ADD trivial",
Andrew Mortonece80492014-05-22 10:16:46 -0700691 .u.insns_int = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700692 BPF_ALU64_IMM(BPF_MOV, R1, 1),
693 BPF_ALU64_IMM(BPF_ADD, R1, 2),
694 BPF_ALU64_IMM(BPF_MOV, R2, 3),
695 BPF_ALU64_REG(BPF_SUB, R1, R2),
696 BPF_ALU64_IMM(BPF_ADD, R1, -1),
697 BPF_ALU64_IMM(BPF_MUL, R1, 3),
698 BPF_ALU64_REG(BPF_MOV, R0, R1),
699 BPF_EXIT_INSN(),
700 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200701 INTERNAL,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700702 { },
703 { { 0, 0xfffffffd } }
704 },
705 {
706 "INT: MUL_X",
Andrew Mortonece80492014-05-22 10:16:46 -0700707 .u.insns_int = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700708 BPF_ALU64_IMM(BPF_MOV, R0, -1),
709 BPF_ALU64_IMM(BPF_MOV, R1, -1),
710 BPF_ALU64_IMM(BPF_MOV, R2, 3),
711 BPF_ALU64_REG(BPF_MUL, R1, R2),
712 BPF_JMP_IMM(BPF_JEQ, R1, 0xfffffffd, 1),
713 BPF_EXIT_INSN(),
714 BPF_ALU64_IMM(BPF_MOV, R0, 1),
715 BPF_EXIT_INSN(),
716 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200717 INTERNAL,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700718 { },
719 { { 0, 1 } }
720 },
721 {
722 "INT: MUL_X2",
Andrew Mortonece80492014-05-22 10:16:46 -0700723 .u.insns_int = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700724 BPF_ALU32_IMM(BPF_MOV, R0, -1),
725 BPF_ALU32_IMM(BPF_MOV, R1, -1),
726 BPF_ALU32_IMM(BPF_MOV, R2, 3),
727 BPF_ALU64_REG(BPF_MUL, R1, R2),
728 BPF_ALU64_IMM(BPF_RSH, R1, 8),
729 BPF_JMP_IMM(BPF_JEQ, R1, 0x2ffffff, 1),
730 BPF_EXIT_INSN(),
731 BPF_ALU32_IMM(BPF_MOV, R0, 1),
732 BPF_EXIT_INSN(),
733 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200734 INTERNAL,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700735 { },
736 { { 0, 1 } }
737 },
738 {
739 "INT: MUL32_X",
Andrew Mortonece80492014-05-22 10:16:46 -0700740 .u.insns_int = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700741 BPF_ALU32_IMM(BPF_MOV, R0, -1),
742 BPF_ALU64_IMM(BPF_MOV, R1, -1),
743 BPF_ALU32_IMM(BPF_MOV, R2, 3),
744 BPF_ALU32_REG(BPF_MUL, R1, R2),
745 BPF_ALU64_IMM(BPF_RSH, R1, 8),
746 BPF_JMP_IMM(BPF_JEQ, R1, 0xffffff, 1),
747 BPF_EXIT_INSN(),
748 BPF_ALU32_IMM(BPF_MOV, R0, 1),
749 BPF_EXIT_INSN(),
750 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200751 INTERNAL,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700752 { },
753 { { 0, 1 } }
754 },
755 {
756 /* Have to test all register combinations, since
757 * JITing of different registers will produce
758 * different asm code.
759 */
760 "INT: ADD 64-bit",
Andrew Mortonece80492014-05-22 10:16:46 -0700761 .u.insns_int = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700762 BPF_ALU64_IMM(BPF_MOV, R0, 0),
763 BPF_ALU64_IMM(BPF_MOV, R1, 1),
764 BPF_ALU64_IMM(BPF_MOV, R2, 2),
765 BPF_ALU64_IMM(BPF_MOV, R3, 3),
766 BPF_ALU64_IMM(BPF_MOV, R4, 4),
767 BPF_ALU64_IMM(BPF_MOV, R5, 5),
768 BPF_ALU64_IMM(BPF_MOV, R6, 6),
769 BPF_ALU64_IMM(BPF_MOV, R7, 7),
770 BPF_ALU64_IMM(BPF_MOV, R8, 8),
771 BPF_ALU64_IMM(BPF_MOV, R9, 9),
772 BPF_ALU64_IMM(BPF_ADD, R0, 20),
773 BPF_ALU64_IMM(BPF_ADD, R1, 20),
774 BPF_ALU64_IMM(BPF_ADD, R2, 20),
775 BPF_ALU64_IMM(BPF_ADD, R3, 20),
776 BPF_ALU64_IMM(BPF_ADD, R4, 20),
777 BPF_ALU64_IMM(BPF_ADD, R5, 20),
778 BPF_ALU64_IMM(BPF_ADD, R6, 20),
779 BPF_ALU64_IMM(BPF_ADD, R7, 20),
780 BPF_ALU64_IMM(BPF_ADD, R8, 20),
781 BPF_ALU64_IMM(BPF_ADD, R9, 20),
782 BPF_ALU64_IMM(BPF_SUB, R0, 10),
783 BPF_ALU64_IMM(BPF_SUB, R1, 10),
784 BPF_ALU64_IMM(BPF_SUB, R2, 10),
785 BPF_ALU64_IMM(BPF_SUB, R3, 10),
786 BPF_ALU64_IMM(BPF_SUB, R4, 10),
787 BPF_ALU64_IMM(BPF_SUB, R5, 10),
788 BPF_ALU64_IMM(BPF_SUB, R6, 10),
789 BPF_ALU64_IMM(BPF_SUB, R7, 10),
790 BPF_ALU64_IMM(BPF_SUB, R8, 10),
791 BPF_ALU64_IMM(BPF_SUB, R9, 10),
792 BPF_ALU64_REG(BPF_ADD, R0, R0),
793 BPF_ALU64_REG(BPF_ADD, R0, R1),
794 BPF_ALU64_REG(BPF_ADD, R0, R2),
795 BPF_ALU64_REG(BPF_ADD, R0, R3),
796 BPF_ALU64_REG(BPF_ADD, R0, R4),
797 BPF_ALU64_REG(BPF_ADD, R0, R5),
798 BPF_ALU64_REG(BPF_ADD, R0, R6),
799 BPF_ALU64_REG(BPF_ADD, R0, R7),
800 BPF_ALU64_REG(BPF_ADD, R0, R8),
801 BPF_ALU64_REG(BPF_ADD, R0, R9), /* R0 == 155 */
802 BPF_JMP_IMM(BPF_JEQ, R0, 155, 1),
803 BPF_EXIT_INSN(),
804 BPF_ALU64_REG(BPF_ADD, R1, R0),
805 BPF_ALU64_REG(BPF_ADD, R1, R1),
806 BPF_ALU64_REG(BPF_ADD, R1, R2),
807 BPF_ALU64_REG(BPF_ADD, R1, R3),
808 BPF_ALU64_REG(BPF_ADD, R1, R4),
809 BPF_ALU64_REG(BPF_ADD, R1, R5),
810 BPF_ALU64_REG(BPF_ADD, R1, R6),
811 BPF_ALU64_REG(BPF_ADD, R1, R7),
812 BPF_ALU64_REG(BPF_ADD, R1, R8),
813 BPF_ALU64_REG(BPF_ADD, R1, R9), /* R1 == 456 */
814 BPF_JMP_IMM(BPF_JEQ, R1, 456, 1),
815 BPF_EXIT_INSN(),
816 BPF_ALU64_REG(BPF_ADD, R2, R0),
817 BPF_ALU64_REG(BPF_ADD, R2, R1),
818 BPF_ALU64_REG(BPF_ADD, R2, R2),
819 BPF_ALU64_REG(BPF_ADD, R2, R3),
820 BPF_ALU64_REG(BPF_ADD, R2, R4),
821 BPF_ALU64_REG(BPF_ADD, R2, R5),
822 BPF_ALU64_REG(BPF_ADD, R2, R6),
823 BPF_ALU64_REG(BPF_ADD, R2, R7),
824 BPF_ALU64_REG(BPF_ADD, R2, R8),
825 BPF_ALU64_REG(BPF_ADD, R2, R9), /* R2 == 1358 */
826 BPF_JMP_IMM(BPF_JEQ, R2, 1358, 1),
827 BPF_EXIT_INSN(),
828 BPF_ALU64_REG(BPF_ADD, R3, R0),
829 BPF_ALU64_REG(BPF_ADD, R3, R1),
830 BPF_ALU64_REG(BPF_ADD, R3, R2),
831 BPF_ALU64_REG(BPF_ADD, R3, R3),
832 BPF_ALU64_REG(BPF_ADD, R3, R4),
833 BPF_ALU64_REG(BPF_ADD, R3, R5),
834 BPF_ALU64_REG(BPF_ADD, R3, R6),
835 BPF_ALU64_REG(BPF_ADD, R3, R7),
836 BPF_ALU64_REG(BPF_ADD, R3, R8),
837 BPF_ALU64_REG(BPF_ADD, R3, R9), /* R3 == 4063 */
838 BPF_JMP_IMM(BPF_JEQ, R3, 4063, 1),
839 BPF_EXIT_INSN(),
840 BPF_ALU64_REG(BPF_ADD, R4, R0),
841 BPF_ALU64_REG(BPF_ADD, R4, R1),
842 BPF_ALU64_REG(BPF_ADD, R4, R2),
843 BPF_ALU64_REG(BPF_ADD, R4, R3),
844 BPF_ALU64_REG(BPF_ADD, R4, R4),
845 BPF_ALU64_REG(BPF_ADD, R4, R5),
846 BPF_ALU64_REG(BPF_ADD, R4, R6),
847 BPF_ALU64_REG(BPF_ADD, R4, R7),
848 BPF_ALU64_REG(BPF_ADD, R4, R8),
849 BPF_ALU64_REG(BPF_ADD, R4, R9), /* R4 == 12177 */
850 BPF_JMP_IMM(BPF_JEQ, R4, 12177, 1),
851 BPF_EXIT_INSN(),
852 BPF_ALU64_REG(BPF_ADD, R5, R0),
853 BPF_ALU64_REG(BPF_ADD, R5, R1),
854 BPF_ALU64_REG(BPF_ADD, R5, R2),
855 BPF_ALU64_REG(BPF_ADD, R5, R3),
856 BPF_ALU64_REG(BPF_ADD, R5, R4),
857 BPF_ALU64_REG(BPF_ADD, R5, R5),
858 BPF_ALU64_REG(BPF_ADD, R5, R6),
859 BPF_ALU64_REG(BPF_ADD, R5, R7),
860 BPF_ALU64_REG(BPF_ADD, R5, R8),
861 BPF_ALU64_REG(BPF_ADD, R5, R9), /* R5 == 36518 */
862 BPF_JMP_IMM(BPF_JEQ, R5, 36518, 1),
863 BPF_EXIT_INSN(),
864 BPF_ALU64_REG(BPF_ADD, R6, R0),
865 BPF_ALU64_REG(BPF_ADD, R6, R1),
866 BPF_ALU64_REG(BPF_ADD, R6, R2),
867 BPF_ALU64_REG(BPF_ADD, R6, R3),
868 BPF_ALU64_REG(BPF_ADD, R6, R4),
869 BPF_ALU64_REG(BPF_ADD, R6, R5),
870 BPF_ALU64_REG(BPF_ADD, R6, R6),
871 BPF_ALU64_REG(BPF_ADD, R6, R7),
872 BPF_ALU64_REG(BPF_ADD, R6, R8),
873 BPF_ALU64_REG(BPF_ADD, R6, R9), /* R6 == 109540 */
874 BPF_JMP_IMM(BPF_JEQ, R6, 109540, 1),
875 BPF_EXIT_INSN(),
876 BPF_ALU64_REG(BPF_ADD, R7, R0),
877 BPF_ALU64_REG(BPF_ADD, R7, R1),
878 BPF_ALU64_REG(BPF_ADD, R7, R2),
879 BPF_ALU64_REG(BPF_ADD, R7, R3),
880 BPF_ALU64_REG(BPF_ADD, R7, R4),
881 BPF_ALU64_REG(BPF_ADD, R7, R5),
882 BPF_ALU64_REG(BPF_ADD, R7, R6),
883 BPF_ALU64_REG(BPF_ADD, R7, R7),
884 BPF_ALU64_REG(BPF_ADD, R7, R8),
885 BPF_ALU64_REG(BPF_ADD, R7, R9), /* R7 == 328605 */
886 BPF_JMP_IMM(BPF_JEQ, R7, 328605, 1),
887 BPF_EXIT_INSN(),
888 BPF_ALU64_REG(BPF_ADD, R8, R0),
889 BPF_ALU64_REG(BPF_ADD, R8, R1),
890 BPF_ALU64_REG(BPF_ADD, R8, R2),
891 BPF_ALU64_REG(BPF_ADD, R8, R3),
892 BPF_ALU64_REG(BPF_ADD, R8, R4),
893 BPF_ALU64_REG(BPF_ADD, R8, R5),
894 BPF_ALU64_REG(BPF_ADD, R8, R6),
895 BPF_ALU64_REG(BPF_ADD, R8, R7),
896 BPF_ALU64_REG(BPF_ADD, R8, R8),
897 BPF_ALU64_REG(BPF_ADD, R8, R9), /* R8 == 985799 */
898 BPF_JMP_IMM(BPF_JEQ, R8, 985799, 1),
899 BPF_EXIT_INSN(),
900 BPF_ALU64_REG(BPF_ADD, R9, R0),
901 BPF_ALU64_REG(BPF_ADD, R9, R1),
902 BPF_ALU64_REG(BPF_ADD, R9, R2),
903 BPF_ALU64_REG(BPF_ADD, R9, R3),
904 BPF_ALU64_REG(BPF_ADD, R9, R4),
905 BPF_ALU64_REG(BPF_ADD, R9, R5),
906 BPF_ALU64_REG(BPF_ADD, R9, R6),
907 BPF_ALU64_REG(BPF_ADD, R9, R7),
908 BPF_ALU64_REG(BPF_ADD, R9, R8),
909 BPF_ALU64_REG(BPF_ADD, R9, R9), /* R9 == 2957380 */
910 BPF_ALU64_REG(BPF_MOV, R0, R9),
911 BPF_EXIT_INSN(),
912 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +0200913 INTERNAL,
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700914 { },
915 { { 0, 2957380 } }
916 },
917 {
918 "INT: ADD 32-bit",
Andrew Mortonece80492014-05-22 10:16:46 -0700919 .u.insns_int = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -0700920 BPF_ALU32_IMM(BPF_MOV, R0, 20),
921 BPF_ALU32_IMM(BPF_MOV, R1, 1),
922 BPF_ALU32_IMM(BPF_MOV, R2, 2),
923 BPF_ALU32_IMM(BPF_MOV, R3, 3),
924 BPF_ALU32_IMM(BPF_MOV, R4, 4),
925 BPF_ALU32_IMM(BPF_MOV, R5, 5),
926 BPF_ALU32_IMM(BPF_MOV, R6, 6),
927 BPF_ALU32_IMM(BPF_MOV, R7, 7),
928 BPF_ALU32_IMM(BPF_MOV, R8, 8),
929 BPF_ALU32_IMM(BPF_MOV, R9, 9),
930 BPF_ALU64_IMM(BPF_ADD, R1, 10),
931 BPF_ALU64_IMM(BPF_ADD, R2, 10),
932 BPF_ALU64_IMM(BPF_ADD, R3, 10),
933 BPF_ALU64_IMM(BPF_ADD, R4, 10),
934 BPF_ALU64_IMM(BPF_ADD, R5, 10),
935 BPF_ALU64_IMM(BPF_ADD, R6, 10),
936 BPF_ALU64_IMM(BPF_ADD, R7, 10),
937 BPF_ALU64_IMM(BPF_ADD, R8, 10),
938 BPF_ALU64_IMM(BPF_ADD, R9, 10),
939 BPF_ALU32_REG(BPF_ADD, R0, R1),
940 BPF_ALU32_REG(BPF_ADD, R0, R2),
941 BPF_ALU32_REG(BPF_ADD, R0, R3),
942 BPF_ALU32_REG(BPF_ADD, R0, R4),
943 BPF_ALU32_REG(BPF_ADD, R0, R5),
944 BPF_ALU32_REG(BPF_ADD, R0, R6),
945 BPF_ALU32_REG(BPF_ADD, R0, R7),
946 BPF_ALU32_REG(BPF_ADD, R0, R8),
947 BPF_ALU32_REG(BPF_ADD, R0, R9), /* R0 == 155 */
948 BPF_JMP_IMM(BPF_JEQ, R0, 155, 1),
949 BPF_EXIT_INSN(),
950 BPF_ALU32_REG(BPF_ADD, R1, R0),
951 BPF_ALU32_REG(BPF_ADD, R1, R1),
952 BPF_ALU32_REG(BPF_ADD, R1, R2),
953 BPF_ALU32_REG(BPF_ADD, R1, R3),
954 BPF_ALU32_REG(BPF_ADD, R1, R4),
955 BPF_ALU32_REG(BPF_ADD, R1, R5),
956 BPF_ALU32_REG(BPF_ADD, R1, R6),
957 BPF_ALU32_REG(BPF_ADD, R1, R7),
958 BPF_ALU32_REG(BPF_ADD, R1, R8),
959 BPF_ALU32_REG(BPF_ADD, R1, R9), /* R1 == 456 */
960 BPF_JMP_IMM(BPF_JEQ, R1, 456, 1),
961 BPF_EXIT_INSN(),
962 BPF_ALU32_REG(BPF_ADD, R2, R0),
963 BPF_ALU32_REG(BPF_ADD, R2, R1),
964 BPF_ALU32_REG(BPF_ADD, R2, R2),
965 BPF_ALU32_REG(BPF_ADD, R2, R3),
966 BPF_ALU32_REG(BPF_ADD, R2, R4),
967 BPF_ALU32_REG(BPF_ADD, R2, R5),
968 BPF_ALU32_REG(BPF_ADD, R2, R6),
969 BPF_ALU32_REG(BPF_ADD, R2, R7),
970 BPF_ALU32_REG(BPF_ADD, R2, R8),
971 BPF_ALU32_REG(BPF_ADD, R2, R9), /* R2 == 1358 */
972 BPF_JMP_IMM(BPF_JEQ, R2, 1358, 1),
973 BPF_EXIT_INSN(),
974 BPF_ALU32_REG(BPF_ADD, R3, R0),
975 BPF_ALU32_REG(BPF_ADD, R3, R1),
976 BPF_ALU32_REG(BPF_ADD, R3, R2),
977 BPF_ALU32_REG(BPF_ADD, R3, R3),
978 BPF_ALU32_REG(BPF_ADD, R3, R4),
979 BPF_ALU32_REG(BPF_ADD, R3, R5),
980 BPF_ALU32_REG(BPF_ADD, R3, R6),
981 BPF_ALU32_REG(BPF_ADD, R3, R7),
982 BPF_ALU32_REG(BPF_ADD, R3, R8),
983 BPF_ALU32_REG(BPF_ADD, R3, R9), /* R3 == 4063 */
984 BPF_JMP_IMM(BPF_JEQ, R3, 4063, 1),
985 BPF_EXIT_INSN(),
986 BPF_ALU32_REG(BPF_ADD, R4, R0),
987 BPF_ALU32_REG(BPF_ADD, R4, R1),
988 BPF_ALU32_REG(BPF_ADD, R4, R2),
989 BPF_ALU32_REG(BPF_ADD, R4, R3),
990 BPF_ALU32_REG(BPF_ADD, R4, R4),
991 BPF_ALU32_REG(BPF_ADD, R4, R5),
992 BPF_ALU32_REG(BPF_ADD, R4, R6),
993 BPF_ALU32_REG(BPF_ADD, R4, R7),
994 BPF_ALU32_REG(BPF_ADD, R4, R8),
995 BPF_ALU32_REG(BPF_ADD, R4, R9), /* R4 == 12177 */
996 BPF_JMP_IMM(BPF_JEQ, R4, 12177, 1),
997 BPF_EXIT_INSN(),
998 BPF_ALU32_REG(BPF_ADD, R5, R0),
999 BPF_ALU32_REG(BPF_ADD, R5, R1),
1000 BPF_ALU32_REG(BPF_ADD, R5, R2),
1001 BPF_ALU32_REG(BPF_ADD, R5, R3),
1002 BPF_ALU32_REG(BPF_ADD, R5, R4),
1003 BPF_ALU32_REG(BPF_ADD, R5, R5),
1004 BPF_ALU32_REG(BPF_ADD, R5, R6),
1005 BPF_ALU32_REG(BPF_ADD, R5, R7),
1006 BPF_ALU32_REG(BPF_ADD, R5, R8),
1007 BPF_ALU32_REG(BPF_ADD, R5, R9), /* R5 == 36518 */
1008 BPF_JMP_IMM(BPF_JEQ, R5, 36518, 1),
1009 BPF_EXIT_INSN(),
1010 BPF_ALU32_REG(BPF_ADD, R6, R0),
1011 BPF_ALU32_REG(BPF_ADD, R6, R1),
1012 BPF_ALU32_REG(BPF_ADD, R6, R2),
1013 BPF_ALU32_REG(BPF_ADD, R6, R3),
1014 BPF_ALU32_REG(BPF_ADD, R6, R4),
1015 BPF_ALU32_REG(BPF_ADD, R6, R5),
1016 BPF_ALU32_REG(BPF_ADD, R6, R6),
1017 BPF_ALU32_REG(BPF_ADD, R6, R7),
1018 BPF_ALU32_REG(BPF_ADD, R6, R8),
1019 BPF_ALU32_REG(BPF_ADD, R6, R9), /* R6 == 109540 */
1020 BPF_JMP_IMM(BPF_JEQ, R6, 109540, 1),
1021 BPF_EXIT_INSN(),
1022 BPF_ALU32_REG(BPF_ADD, R7, R0),
1023 BPF_ALU32_REG(BPF_ADD, R7, R1),
1024 BPF_ALU32_REG(BPF_ADD, R7, R2),
1025 BPF_ALU32_REG(BPF_ADD, R7, R3),
1026 BPF_ALU32_REG(BPF_ADD, R7, R4),
1027 BPF_ALU32_REG(BPF_ADD, R7, R5),
1028 BPF_ALU32_REG(BPF_ADD, R7, R6),
1029 BPF_ALU32_REG(BPF_ADD, R7, R7),
1030 BPF_ALU32_REG(BPF_ADD, R7, R8),
1031 BPF_ALU32_REG(BPF_ADD, R7, R9), /* R7 == 328605 */
1032 BPF_JMP_IMM(BPF_JEQ, R7, 328605, 1),
1033 BPF_EXIT_INSN(),
1034 BPF_ALU32_REG(BPF_ADD, R8, R0),
1035 BPF_ALU32_REG(BPF_ADD, R8, R1),
1036 BPF_ALU32_REG(BPF_ADD, R8, R2),
1037 BPF_ALU32_REG(BPF_ADD, R8, R3),
1038 BPF_ALU32_REG(BPF_ADD, R8, R4),
1039 BPF_ALU32_REG(BPF_ADD, R8, R5),
1040 BPF_ALU32_REG(BPF_ADD, R8, R6),
1041 BPF_ALU32_REG(BPF_ADD, R8, R7),
1042 BPF_ALU32_REG(BPF_ADD, R8, R8),
1043 BPF_ALU32_REG(BPF_ADD, R8, R9), /* R8 == 985799 */
1044 BPF_JMP_IMM(BPF_JEQ, R8, 985799, 1),
1045 BPF_EXIT_INSN(),
1046 BPF_ALU32_REG(BPF_ADD, R9, R0),
1047 BPF_ALU32_REG(BPF_ADD, R9, R1),
1048 BPF_ALU32_REG(BPF_ADD, R9, R2),
1049 BPF_ALU32_REG(BPF_ADD, R9, R3),
1050 BPF_ALU32_REG(BPF_ADD, R9, R4),
1051 BPF_ALU32_REG(BPF_ADD, R9, R5),
1052 BPF_ALU32_REG(BPF_ADD, R9, R6),
1053 BPF_ALU32_REG(BPF_ADD, R9, R7),
1054 BPF_ALU32_REG(BPF_ADD, R9, R8),
1055 BPF_ALU32_REG(BPF_ADD, R9, R9), /* R9 == 2957380 */
1056 BPF_ALU32_REG(BPF_MOV, R0, R9),
1057 BPF_EXIT_INSN(),
1058 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001059 INTERNAL,
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001060 { },
1061 { { 0, 2957380 } }
1062 },
1063 { /* Mainly checking JIT here. */
1064 "INT: SUB",
Andrew Mortonece80492014-05-22 10:16:46 -07001065 .u.insns_int = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001066 BPF_ALU64_IMM(BPF_MOV, R0, 0),
1067 BPF_ALU64_IMM(BPF_MOV, R1, 1),
1068 BPF_ALU64_IMM(BPF_MOV, R2, 2),
1069 BPF_ALU64_IMM(BPF_MOV, R3, 3),
1070 BPF_ALU64_IMM(BPF_MOV, R4, 4),
1071 BPF_ALU64_IMM(BPF_MOV, R5, 5),
1072 BPF_ALU64_IMM(BPF_MOV, R6, 6),
1073 BPF_ALU64_IMM(BPF_MOV, R7, 7),
1074 BPF_ALU64_IMM(BPF_MOV, R8, 8),
1075 BPF_ALU64_IMM(BPF_MOV, R9, 9),
1076 BPF_ALU64_REG(BPF_SUB, R0, R0),
1077 BPF_ALU64_REG(BPF_SUB, R0, R1),
1078 BPF_ALU64_REG(BPF_SUB, R0, R2),
1079 BPF_ALU64_REG(BPF_SUB, R0, R3),
1080 BPF_ALU64_REG(BPF_SUB, R0, R4),
1081 BPF_ALU64_REG(BPF_SUB, R0, R5),
1082 BPF_ALU64_REG(BPF_SUB, R0, R6),
1083 BPF_ALU64_REG(BPF_SUB, R0, R7),
1084 BPF_ALU64_REG(BPF_SUB, R0, R8),
1085 BPF_ALU64_REG(BPF_SUB, R0, R9),
1086 BPF_ALU64_IMM(BPF_SUB, R0, 10),
1087 BPF_JMP_IMM(BPF_JEQ, R0, -55, 1),
1088 BPF_EXIT_INSN(),
1089 BPF_ALU64_REG(BPF_SUB, R1, R0),
1090 BPF_ALU64_REG(BPF_SUB, R1, R2),
1091 BPF_ALU64_REG(BPF_SUB, R1, R3),
1092 BPF_ALU64_REG(BPF_SUB, R1, R4),
1093 BPF_ALU64_REG(BPF_SUB, R1, R5),
1094 BPF_ALU64_REG(BPF_SUB, R1, R6),
1095 BPF_ALU64_REG(BPF_SUB, R1, R7),
1096 BPF_ALU64_REG(BPF_SUB, R1, R8),
1097 BPF_ALU64_REG(BPF_SUB, R1, R9),
1098 BPF_ALU64_IMM(BPF_SUB, R1, 10),
1099 BPF_ALU64_REG(BPF_SUB, R2, R0),
1100 BPF_ALU64_REG(BPF_SUB, R2, R1),
1101 BPF_ALU64_REG(BPF_SUB, R2, R3),
1102 BPF_ALU64_REG(BPF_SUB, R2, R4),
1103 BPF_ALU64_REG(BPF_SUB, R2, R5),
1104 BPF_ALU64_REG(BPF_SUB, R2, R6),
1105 BPF_ALU64_REG(BPF_SUB, R2, R7),
1106 BPF_ALU64_REG(BPF_SUB, R2, R8),
1107 BPF_ALU64_REG(BPF_SUB, R2, R9),
1108 BPF_ALU64_IMM(BPF_SUB, R2, 10),
1109 BPF_ALU64_REG(BPF_SUB, R3, R0),
1110 BPF_ALU64_REG(BPF_SUB, R3, R1),
1111 BPF_ALU64_REG(BPF_SUB, R3, R2),
1112 BPF_ALU64_REG(BPF_SUB, R3, R4),
1113 BPF_ALU64_REG(BPF_SUB, R3, R5),
1114 BPF_ALU64_REG(BPF_SUB, R3, R6),
1115 BPF_ALU64_REG(BPF_SUB, R3, R7),
1116 BPF_ALU64_REG(BPF_SUB, R3, R8),
1117 BPF_ALU64_REG(BPF_SUB, R3, R9),
1118 BPF_ALU64_IMM(BPF_SUB, R3, 10),
1119 BPF_ALU64_REG(BPF_SUB, R4, R0),
1120 BPF_ALU64_REG(BPF_SUB, R4, R1),
1121 BPF_ALU64_REG(BPF_SUB, R4, R2),
1122 BPF_ALU64_REG(BPF_SUB, R4, R3),
1123 BPF_ALU64_REG(BPF_SUB, R4, R5),
1124 BPF_ALU64_REG(BPF_SUB, R4, R6),
1125 BPF_ALU64_REG(BPF_SUB, R4, R7),
1126 BPF_ALU64_REG(BPF_SUB, R4, R8),
1127 BPF_ALU64_REG(BPF_SUB, R4, R9),
1128 BPF_ALU64_IMM(BPF_SUB, R4, 10),
1129 BPF_ALU64_REG(BPF_SUB, R5, R0),
1130 BPF_ALU64_REG(BPF_SUB, R5, R1),
1131 BPF_ALU64_REG(BPF_SUB, R5, R2),
1132 BPF_ALU64_REG(BPF_SUB, R5, R3),
1133 BPF_ALU64_REG(BPF_SUB, R5, R4),
1134 BPF_ALU64_REG(BPF_SUB, R5, R6),
1135 BPF_ALU64_REG(BPF_SUB, R5, R7),
1136 BPF_ALU64_REG(BPF_SUB, R5, R8),
1137 BPF_ALU64_REG(BPF_SUB, R5, R9),
1138 BPF_ALU64_IMM(BPF_SUB, R5, 10),
1139 BPF_ALU64_REG(BPF_SUB, R6, R0),
1140 BPF_ALU64_REG(BPF_SUB, R6, R1),
1141 BPF_ALU64_REG(BPF_SUB, R6, R2),
1142 BPF_ALU64_REG(BPF_SUB, R6, R3),
1143 BPF_ALU64_REG(BPF_SUB, R6, R4),
1144 BPF_ALU64_REG(BPF_SUB, R6, R5),
1145 BPF_ALU64_REG(BPF_SUB, R6, R7),
1146 BPF_ALU64_REG(BPF_SUB, R6, R8),
1147 BPF_ALU64_REG(BPF_SUB, R6, R9),
1148 BPF_ALU64_IMM(BPF_SUB, R6, 10),
1149 BPF_ALU64_REG(BPF_SUB, R7, R0),
1150 BPF_ALU64_REG(BPF_SUB, R7, R1),
1151 BPF_ALU64_REG(BPF_SUB, R7, R2),
1152 BPF_ALU64_REG(BPF_SUB, R7, R3),
1153 BPF_ALU64_REG(BPF_SUB, R7, R4),
1154 BPF_ALU64_REG(BPF_SUB, R7, R5),
1155 BPF_ALU64_REG(BPF_SUB, R7, R6),
1156 BPF_ALU64_REG(BPF_SUB, R7, R8),
1157 BPF_ALU64_REG(BPF_SUB, R7, R9),
1158 BPF_ALU64_IMM(BPF_SUB, R7, 10),
1159 BPF_ALU64_REG(BPF_SUB, R8, R0),
1160 BPF_ALU64_REG(BPF_SUB, R8, R1),
1161 BPF_ALU64_REG(BPF_SUB, R8, R2),
1162 BPF_ALU64_REG(BPF_SUB, R8, R3),
1163 BPF_ALU64_REG(BPF_SUB, R8, R4),
1164 BPF_ALU64_REG(BPF_SUB, R8, R5),
1165 BPF_ALU64_REG(BPF_SUB, R8, R6),
1166 BPF_ALU64_REG(BPF_SUB, R8, R7),
1167 BPF_ALU64_REG(BPF_SUB, R8, R9),
1168 BPF_ALU64_IMM(BPF_SUB, R8, 10),
1169 BPF_ALU64_REG(BPF_SUB, R9, R0),
1170 BPF_ALU64_REG(BPF_SUB, R9, R1),
1171 BPF_ALU64_REG(BPF_SUB, R9, R2),
1172 BPF_ALU64_REG(BPF_SUB, R9, R3),
1173 BPF_ALU64_REG(BPF_SUB, R9, R4),
1174 BPF_ALU64_REG(BPF_SUB, R9, R5),
1175 BPF_ALU64_REG(BPF_SUB, R9, R6),
1176 BPF_ALU64_REG(BPF_SUB, R9, R7),
1177 BPF_ALU64_REG(BPF_SUB, R9, R8),
1178 BPF_ALU64_IMM(BPF_SUB, R9, 10),
1179 BPF_ALU64_IMM(BPF_SUB, R0, 10),
1180 BPF_ALU64_IMM(BPF_NEG, R0, 0),
1181 BPF_ALU64_REG(BPF_SUB, R0, R1),
1182 BPF_ALU64_REG(BPF_SUB, R0, R2),
1183 BPF_ALU64_REG(BPF_SUB, R0, R3),
1184 BPF_ALU64_REG(BPF_SUB, R0, R4),
1185 BPF_ALU64_REG(BPF_SUB, R0, R5),
1186 BPF_ALU64_REG(BPF_SUB, R0, R6),
1187 BPF_ALU64_REG(BPF_SUB, R0, R7),
1188 BPF_ALU64_REG(BPF_SUB, R0, R8),
1189 BPF_ALU64_REG(BPF_SUB, R0, R9),
1190 BPF_EXIT_INSN(),
1191 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001192 INTERNAL,
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001193 { },
1194 { { 0, 11 } }
1195 },
1196 { /* Mainly checking JIT here. */
1197 "INT: XOR",
Andrew Mortonece80492014-05-22 10:16:46 -07001198 .u.insns_int = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001199 BPF_ALU64_REG(BPF_SUB, R0, R0),
1200 BPF_ALU64_REG(BPF_XOR, R1, R1),
1201 BPF_JMP_REG(BPF_JEQ, R0, R1, 1),
1202 BPF_EXIT_INSN(),
1203 BPF_ALU64_IMM(BPF_MOV, R0, 10),
1204 BPF_ALU64_IMM(BPF_MOV, R1, -1),
1205 BPF_ALU64_REG(BPF_SUB, R1, R1),
1206 BPF_ALU64_REG(BPF_XOR, R2, R2),
1207 BPF_JMP_REG(BPF_JEQ, R1, R2, 1),
1208 BPF_EXIT_INSN(),
1209 BPF_ALU64_REG(BPF_SUB, R2, R2),
1210 BPF_ALU64_REG(BPF_XOR, R3, R3),
1211 BPF_ALU64_IMM(BPF_MOV, R0, 10),
1212 BPF_ALU64_IMM(BPF_MOV, R1, -1),
1213 BPF_JMP_REG(BPF_JEQ, R2, R3, 1),
1214 BPF_EXIT_INSN(),
1215 BPF_ALU64_REG(BPF_SUB, R3, R3),
1216 BPF_ALU64_REG(BPF_XOR, R4, R4),
1217 BPF_ALU64_IMM(BPF_MOV, R2, 1),
1218 BPF_ALU64_IMM(BPF_MOV, R5, -1),
1219 BPF_JMP_REG(BPF_JEQ, R3, R4, 1),
1220 BPF_EXIT_INSN(),
1221 BPF_ALU64_REG(BPF_SUB, R4, R4),
1222 BPF_ALU64_REG(BPF_XOR, R5, R5),
1223 BPF_ALU64_IMM(BPF_MOV, R3, 1),
1224 BPF_ALU64_IMM(BPF_MOV, R7, -1),
1225 BPF_JMP_REG(BPF_JEQ, R5, R4, 1),
1226 BPF_EXIT_INSN(),
1227 BPF_ALU64_IMM(BPF_MOV, R5, 1),
1228 BPF_ALU64_REG(BPF_SUB, R5, R5),
1229 BPF_ALU64_REG(BPF_XOR, R6, R6),
1230 BPF_ALU64_IMM(BPF_MOV, R1, 1),
1231 BPF_ALU64_IMM(BPF_MOV, R8, -1),
1232 BPF_JMP_REG(BPF_JEQ, R5, R6, 1),
1233 BPF_EXIT_INSN(),
1234 BPF_ALU64_REG(BPF_SUB, R6, R6),
1235 BPF_ALU64_REG(BPF_XOR, R7, R7),
1236 BPF_JMP_REG(BPF_JEQ, R7, R6, 1),
1237 BPF_EXIT_INSN(),
1238 BPF_ALU64_REG(BPF_SUB, R7, R7),
1239 BPF_ALU64_REG(BPF_XOR, R8, R8),
1240 BPF_JMP_REG(BPF_JEQ, R7, R8, 1),
1241 BPF_EXIT_INSN(),
1242 BPF_ALU64_REG(BPF_SUB, R8, R8),
1243 BPF_ALU64_REG(BPF_XOR, R9, R9),
1244 BPF_JMP_REG(BPF_JEQ, R9, R8, 1),
1245 BPF_EXIT_INSN(),
1246 BPF_ALU64_REG(BPF_SUB, R9, R9),
1247 BPF_ALU64_REG(BPF_XOR, R0, R0),
1248 BPF_JMP_REG(BPF_JEQ, R9, R0, 1),
1249 BPF_EXIT_INSN(),
1250 BPF_ALU64_REG(BPF_SUB, R1, R1),
1251 BPF_ALU64_REG(BPF_XOR, R0, R0),
1252 BPF_JMP_REG(BPF_JEQ, R9, R0, 2),
1253 BPF_ALU64_IMM(BPF_MOV, R0, 0),
1254 BPF_EXIT_INSN(),
1255 BPF_ALU64_IMM(BPF_MOV, R0, 1),
1256 BPF_EXIT_INSN(),
1257 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001258 INTERNAL,
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001259 { },
1260 { { 0, 1 } }
1261 },
1262 { /* Mainly checking JIT here. */
1263 "INT: MUL",
Andrew Mortonece80492014-05-22 10:16:46 -07001264 .u.insns_int = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001265 BPF_ALU64_IMM(BPF_MOV, R0, 11),
1266 BPF_ALU64_IMM(BPF_MOV, R1, 1),
1267 BPF_ALU64_IMM(BPF_MOV, R2, 2),
1268 BPF_ALU64_IMM(BPF_MOV, R3, 3),
1269 BPF_ALU64_IMM(BPF_MOV, R4, 4),
1270 BPF_ALU64_IMM(BPF_MOV, R5, 5),
1271 BPF_ALU64_IMM(BPF_MOV, R6, 6),
1272 BPF_ALU64_IMM(BPF_MOV, R7, 7),
1273 BPF_ALU64_IMM(BPF_MOV, R8, 8),
1274 BPF_ALU64_IMM(BPF_MOV, R9, 9),
1275 BPF_ALU64_REG(BPF_MUL, R0, R0),
1276 BPF_ALU64_REG(BPF_MUL, R0, R1),
1277 BPF_ALU64_REG(BPF_MUL, R0, R2),
1278 BPF_ALU64_REG(BPF_MUL, R0, R3),
1279 BPF_ALU64_REG(BPF_MUL, R0, R4),
1280 BPF_ALU64_REG(BPF_MUL, R0, R5),
1281 BPF_ALU64_REG(BPF_MUL, R0, R6),
1282 BPF_ALU64_REG(BPF_MUL, R0, R7),
1283 BPF_ALU64_REG(BPF_MUL, R0, R8),
1284 BPF_ALU64_REG(BPF_MUL, R0, R9),
1285 BPF_ALU64_IMM(BPF_MUL, R0, 10),
1286 BPF_JMP_IMM(BPF_JEQ, R0, 439084800, 1),
1287 BPF_EXIT_INSN(),
1288 BPF_ALU64_REG(BPF_MUL, R1, R0),
1289 BPF_ALU64_REG(BPF_MUL, R1, R2),
1290 BPF_ALU64_REG(BPF_MUL, R1, R3),
1291 BPF_ALU64_REG(BPF_MUL, R1, R4),
1292 BPF_ALU64_REG(BPF_MUL, R1, R5),
1293 BPF_ALU64_REG(BPF_MUL, R1, R6),
1294 BPF_ALU64_REG(BPF_MUL, R1, R7),
1295 BPF_ALU64_REG(BPF_MUL, R1, R8),
1296 BPF_ALU64_REG(BPF_MUL, R1, R9),
1297 BPF_ALU64_IMM(BPF_MUL, R1, 10),
1298 BPF_ALU64_REG(BPF_MOV, R2, R1),
1299 BPF_ALU64_IMM(BPF_RSH, R2, 32),
1300 BPF_JMP_IMM(BPF_JEQ, R2, 0x5a924, 1),
1301 BPF_EXIT_INSN(),
1302 BPF_ALU64_IMM(BPF_LSH, R1, 32),
1303 BPF_ALU64_IMM(BPF_ARSH, R1, 32),
1304 BPF_JMP_IMM(BPF_JEQ, R1, 0xebb90000, 1),
1305 BPF_EXIT_INSN(),
1306 BPF_ALU64_REG(BPF_MUL, R2, R0),
1307 BPF_ALU64_REG(BPF_MUL, R2, R1),
1308 BPF_ALU64_REG(BPF_MUL, R2, R3),
1309 BPF_ALU64_REG(BPF_MUL, R2, R4),
1310 BPF_ALU64_REG(BPF_MUL, R2, R5),
1311 BPF_ALU64_REG(BPF_MUL, R2, R6),
1312 BPF_ALU64_REG(BPF_MUL, R2, R7),
1313 BPF_ALU64_REG(BPF_MUL, R2, R8),
1314 BPF_ALU64_REG(BPF_MUL, R2, R9),
1315 BPF_ALU64_IMM(BPF_MUL, R2, 10),
1316 BPF_ALU64_IMM(BPF_RSH, R2, 32),
1317 BPF_ALU64_REG(BPF_MOV, R0, R2),
1318 BPF_EXIT_INSN(),
1319 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001320 INTERNAL,
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001321 { },
1322 { { 0, 0x35d97ef2 } }
1323 },
1324 {
1325 "INT: ALU MIX",
Andrew Mortonece80492014-05-22 10:16:46 -07001326 .u.insns_int = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001327 BPF_ALU64_IMM(BPF_MOV, R0, 11),
1328 BPF_ALU64_IMM(BPF_ADD, R0, -1),
1329 BPF_ALU64_IMM(BPF_MOV, R2, 2),
1330 BPF_ALU64_IMM(BPF_XOR, R2, 3),
1331 BPF_ALU64_REG(BPF_DIV, R0, R2),
1332 BPF_JMP_IMM(BPF_JEQ, R0, 10, 1),
1333 BPF_EXIT_INSN(),
1334 BPF_ALU64_IMM(BPF_MOD, R0, 3),
1335 BPF_JMP_IMM(BPF_JEQ, R0, 1, 1),
1336 BPF_EXIT_INSN(),
1337 BPF_ALU64_IMM(BPF_MOV, R0, -1),
1338 BPF_EXIT_INSN(),
1339 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001340 INTERNAL,
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001341 { },
1342 { { 0, -1 } }
1343 },
1344 {
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001345 "INT: DIV + ABS",
Andrew Mortonece80492014-05-22 10:16:46 -07001346 .u.insns_int = {
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001347 BPF_ALU64_REG(BPF_MOV, R6, R1),
1348 BPF_LD_ABS(BPF_B, 3),
1349 BPF_ALU64_IMM(BPF_MOV, R2, 2),
1350 BPF_ALU32_REG(BPF_DIV, R0, R2),
1351 BPF_ALU64_REG(BPF_MOV, R8, R0),
1352 BPF_LD_ABS(BPF_B, 4),
1353 BPF_ALU64_REG(BPF_ADD, R8, R0),
1354 BPF_LD_IND(BPF_B, R8, -70),
1355 BPF_EXIT_INSN(),
1356 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001357 INTERNAL,
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001358 { 10, 20, 30, 40, 50 },
1359 { { 4, 0 }, { 5, 10 } }
1360 },
1361 {
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001362 "INT: DIV by zero",
Andrew Mortonece80492014-05-22 10:16:46 -07001363 .u.insns_int = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001364 BPF_ALU64_REG(BPF_MOV, R6, R1),
1365 BPF_ALU64_IMM(BPF_MOV, R7, 0),
1366 BPF_LD_ABS(BPF_B, 3),
1367 BPF_ALU32_REG(BPF_DIV, R0, R7),
1368 BPF_EXIT_INSN(),
1369 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001370 INTERNAL,
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001371 { 10, 20, 30, 40, 50 },
1372 { { 3, 0 }, { 4, 0 } }
1373 },
1374 {
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001375 "check: missing ret",
Andrew Mortonece80492014-05-22 10:16:46 -07001376 .u.insns = {
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001377 BPF_STMT(BPF_LD | BPF_IMM, 1),
1378 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001379 CLASSIC | FLAG_NO_DATA | FLAG_EXPECTED_FAIL,
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001380 { },
1381 { }
1382 },
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001383 {
1384 "check: div_k_0",
Andrew Mortonece80492014-05-22 10:16:46 -07001385 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001386 BPF_STMT(BPF_ALU | BPF_DIV | BPF_K, 0),
1387 BPF_STMT(BPF_RET | BPF_K, 0)
1388 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001389 CLASSIC | FLAG_NO_DATA | FLAG_EXPECTED_FAIL,
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001390 { },
1391 { }
1392 },
1393 {
1394 "check: unknown insn",
Andrew Mortonece80492014-05-22 10:16:46 -07001395 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001396 /* seccomp insn, rejected in socket filter */
1397 BPF_STMT(BPF_LDX | BPF_W | BPF_ABS, 0),
1398 BPF_STMT(BPF_RET | BPF_K, 0)
1399 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001400 CLASSIC | FLAG_EXPECTED_FAIL,
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001401 { },
1402 { }
1403 },
1404 {
1405 "check: out of range spill/fill",
Andrew Mortonece80492014-05-22 10:16:46 -07001406 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001407 BPF_STMT(BPF_STX, 16),
1408 BPF_STMT(BPF_RET | BPF_K, 0)
1409 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001410 CLASSIC | FLAG_NO_DATA | FLAG_EXPECTED_FAIL,
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001411 { },
1412 { }
1413 },
Daniel Borkmann2e8a83c2014-05-23 18:44:01 +02001414 {
1415 "JUMPS + HOLES",
1416 .u.insns = {
1417 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1418 BPF_JUMP(BPF_JMP | BPF_JGE, 0, 13, 15),
1419 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1420 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1421 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1422 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1423 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1424 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1425 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1426 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1427 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1428 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1429 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1430 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1431 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1432 BPF_JUMP(BPF_JMP | BPF_JEQ, 0x90c2894d, 3, 4),
1433 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1434 BPF_JUMP(BPF_JMP | BPF_JEQ, 0x90c2894d, 1, 2),
1435 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1436 BPF_JUMP(BPF_JMP | BPF_JGE, 0, 14, 15),
1437 BPF_JUMP(BPF_JMP | BPF_JGE, 0, 13, 14),
1438 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1439 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1440 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1441 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1442 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1443 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1444 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1445 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1446 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1447 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1448 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1449 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1450 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1451 BPF_JUMP(BPF_JMP | BPF_JEQ, 0x2ac28349, 2, 3),
1452 BPF_JUMP(BPF_JMP | BPF_JEQ, 0x2ac28349, 1, 2),
1453 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1454 BPF_JUMP(BPF_JMP | BPF_JGE, 0, 14, 15),
1455 BPF_JUMP(BPF_JMP | BPF_JGE, 0, 13, 14),
1456 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1457 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1458 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1459 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1460 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1461 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1462 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1463 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1464 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1465 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1466 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1467 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1468 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1469 BPF_JUMP(BPF_JMP | BPF_JEQ, 0x90d2ff41, 2, 3),
1470 BPF_JUMP(BPF_JMP | BPF_JEQ, 0x90d2ff41, 1, 2),
1471 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1472 BPF_STMT(BPF_RET | BPF_A, 0),
1473 BPF_STMT(BPF_RET | BPF_A, 0),
1474 },
1475 CLASSIC,
Daniel Borkmannce25b682014-05-26 20:17:35 +02001476 { 0x00, 0x1b, 0x21, 0x3c, 0x9d, 0xf8,
1477 0x90, 0xe2, 0xba, 0x0a, 0x56, 0xb4,
1478 0x08, 0x00,
1479 0x45, 0x00, 0x00, 0x28, 0x00, 0x00,
1480 0x20, 0x00, 0x40, 0x11, 0x00, 0x00, /* IP header */
1481 0xc0, 0xa8, 0x33, 0x01,
1482 0xc0, 0xa8, 0x33, 0x02,
1483 0xbb, 0xb6,
1484 0xa9, 0xfa,
1485 0x00, 0x14, 0x00, 0x00,
1486 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
1487 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
1488 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
1489 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
1490 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
1491 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
1492 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
1493 0xcc, 0xcc, 0xcc, 0xcc },
Daniel Borkmann2e8a83c2014-05-23 18:44:01 +02001494 { { 88, 0x001b } }
1495 },
1496 {
1497 "check: RET X",
1498 .u.insns = {
1499 BPF_STMT(BPF_RET | BPF_X, 0),
1500 },
1501 CLASSIC | FLAG_NO_DATA | FLAG_EXPECTED_FAIL,
1502 { },
1503 { },
1504 },
1505 {
1506 "check: LDX + RET X",
1507 .u.insns = {
1508 BPF_STMT(BPF_LDX | BPF_IMM, 42),
1509 BPF_STMT(BPF_RET | BPF_X, 0),
1510 },
1511 CLASSIC | FLAG_NO_DATA | FLAG_EXPECTED_FAIL,
1512 { },
1513 { },
1514 },
Daniel Borkmann108cc222014-05-26 20:17:34 +02001515 { /* Mainly checking JIT here. */
Daniel Borkmann9fe13ba2014-05-29 10:22:48 +02001516 "M[]: alt STX + LDX",
Daniel Borkmann108cc222014-05-26 20:17:34 +02001517 .u.insns = {
1518 BPF_STMT(BPF_LDX | BPF_IMM, 100),
1519 BPF_STMT(BPF_STX, 0),
1520 BPF_STMT(BPF_LDX | BPF_MEM, 0),
1521 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1522 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1523 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1524 BPF_STMT(BPF_STX, 1),
1525 BPF_STMT(BPF_LDX | BPF_MEM, 1),
1526 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1527 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1528 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1529 BPF_STMT(BPF_STX, 2),
1530 BPF_STMT(BPF_LDX | BPF_MEM, 2),
1531 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1532 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1533 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1534 BPF_STMT(BPF_STX, 3),
1535 BPF_STMT(BPF_LDX | BPF_MEM, 3),
1536 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1537 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1538 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1539 BPF_STMT(BPF_STX, 4),
1540 BPF_STMT(BPF_LDX | BPF_MEM, 4),
1541 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1542 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1543 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1544 BPF_STMT(BPF_STX, 5),
1545 BPF_STMT(BPF_LDX | BPF_MEM, 5),
1546 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1547 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1548 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1549 BPF_STMT(BPF_STX, 6),
1550 BPF_STMT(BPF_LDX | BPF_MEM, 6),
1551 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1552 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1553 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1554 BPF_STMT(BPF_STX, 7),
1555 BPF_STMT(BPF_LDX | BPF_MEM, 7),
1556 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1557 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1558 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1559 BPF_STMT(BPF_STX, 8),
1560 BPF_STMT(BPF_LDX | BPF_MEM, 8),
1561 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1562 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1563 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1564 BPF_STMT(BPF_STX, 9),
1565 BPF_STMT(BPF_LDX | BPF_MEM, 9),
1566 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1567 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1568 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1569 BPF_STMT(BPF_STX, 10),
1570 BPF_STMT(BPF_LDX | BPF_MEM, 10),
1571 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1572 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1573 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1574 BPF_STMT(BPF_STX, 11),
1575 BPF_STMT(BPF_LDX | BPF_MEM, 11),
1576 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1577 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1578 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1579 BPF_STMT(BPF_STX, 12),
1580 BPF_STMT(BPF_LDX | BPF_MEM, 12),
1581 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1582 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1583 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1584 BPF_STMT(BPF_STX, 13),
1585 BPF_STMT(BPF_LDX | BPF_MEM, 13),
1586 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1587 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1588 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1589 BPF_STMT(BPF_STX, 14),
1590 BPF_STMT(BPF_LDX | BPF_MEM, 14),
1591 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1592 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1593 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1594 BPF_STMT(BPF_STX, 15),
1595 BPF_STMT(BPF_LDX | BPF_MEM, 15),
1596 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1597 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1598 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1599 BPF_STMT(BPF_RET | BPF_A, 0),
1600 },
1601 CLASSIC | FLAG_NO_DATA,
1602 { },
1603 { { 0, 116 } },
1604 },
Daniel Borkmann9fe13ba2014-05-29 10:22:48 +02001605 { /* Mainly checking JIT here. */
1606 "M[]: full STX + full LDX",
1607 .u.insns = {
1608 BPF_STMT(BPF_LDX | BPF_IMM, 0xbadfeedb),
1609 BPF_STMT(BPF_STX, 0),
1610 BPF_STMT(BPF_LDX | BPF_IMM, 0xecabedae),
1611 BPF_STMT(BPF_STX, 1),
1612 BPF_STMT(BPF_LDX | BPF_IMM, 0xafccfeaf),
1613 BPF_STMT(BPF_STX, 2),
1614 BPF_STMT(BPF_LDX | BPF_IMM, 0xbffdcedc),
1615 BPF_STMT(BPF_STX, 3),
1616 BPF_STMT(BPF_LDX | BPF_IMM, 0xfbbbdccb),
1617 BPF_STMT(BPF_STX, 4),
1618 BPF_STMT(BPF_LDX | BPF_IMM, 0xfbabcbda),
1619 BPF_STMT(BPF_STX, 5),
1620 BPF_STMT(BPF_LDX | BPF_IMM, 0xaedecbdb),
1621 BPF_STMT(BPF_STX, 6),
1622 BPF_STMT(BPF_LDX | BPF_IMM, 0xadebbade),
1623 BPF_STMT(BPF_STX, 7),
1624 BPF_STMT(BPF_LDX | BPF_IMM, 0xfcfcfaec),
1625 BPF_STMT(BPF_STX, 8),
1626 BPF_STMT(BPF_LDX | BPF_IMM, 0xbcdddbdc),
1627 BPF_STMT(BPF_STX, 9),
1628 BPF_STMT(BPF_LDX | BPF_IMM, 0xfeefdfac),
1629 BPF_STMT(BPF_STX, 10),
1630 BPF_STMT(BPF_LDX | BPF_IMM, 0xcddcdeea),
1631 BPF_STMT(BPF_STX, 11),
1632 BPF_STMT(BPF_LDX | BPF_IMM, 0xaccfaebb),
1633 BPF_STMT(BPF_STX, 12),
1634 BPF_STMT(BPF_LDX | BPF_IMM, 0xbdcccdcf),
1635 BPF_STMT(BPF_STX, 13),
1636 BPF_STMT(BPF_LDX | BPF_IMM, 0xaaedecde),
1637 BPF_STMT(BPF_STX, 14),
1638 BPF_STMT(BPF_LDX | BPF_IMM, 0xfaeacdad),
1639 BPF_STMT(BPF_STX, 15),
1640 BPF_STMT(BPF_LDX | BPF_MEM, 0),
1641 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1642 BPF_STMT(BPF_LDX | BPF_MEM, 1),
1643 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1644 BPF_STMT(BPF_LDX | BPF_MEM, 2),
1645 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1646 BPF_STMT(BPF_LDX | BPF_MEM, 3),
1647 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1648 BPF_STMT(BPF_LDX | BPF_MEM, 4),
1649 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1650 BPF_STMT(BPF_LDX | BPF_MEM, 5),
1651 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1652 BPF_STMT(BPF_LDX | BPF_MEM, 6),
1653 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1654 BPF_STMT(BPF_LDX | BPF_MEM, 7),
1655 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1656 BPF_STMT(BPF_LDX | BPF_MEM, 8),
1657 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1658 BPF_STMT(BPF_LDX | BPF_MEM, 9),
1659 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1660 BPF_STMT(BPF_LDX | BPF_MEM, 10),
1661 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1662 BPF_STMT(BPF_LDX | BPF_MEM, 11),
1663 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1664 BPF_STMT(BPF_LDX | BPF_MEM, 12),
1665 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1666 BPF_STMT(BPF_LDX | BPF_MEM, 13),
1667 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1668 BPF_STMT(BPF_LDX | BPF_MEM, 14),
1669 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1670 BPF_STMT(BPF_LDX | BPF_MEM, 15),
1671 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1672 BPF_STMT(BPF_RET | BPF_A, 0),
1673 },
1674 CLASSIC | FLAG_NO_DATA,
1675 { },
1676 { { 0, 0x2a5a5e5 } },
1677 },
Daniel Borkmannd50bc152014-05-29 10:22:49 +02001678 {
1679 "check: SKF_AD_MAX",
1680 .u.insns = {
1681 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
1682 SKF_AD_OFF + SKF_AD_MAX),
1683 BPF_STMT(BPF_RET | BPF_A, 0),
1684 },
1685 CLASSIC | FLAG_NO_DATA | FLAG_EXPECTED_FAIL,
1686 { },
1687 { },
1688 },
1689 { /* Passes checker but fails during runtime. */
1690 "LD [SKF_AD_OFF-1]",
1691 .u.insns = {
1692 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
1693 SKF_AD_OFF - 1),
1694 BPF_STMT(BPF_RET | BPF_K, 1),
1695 },
1696 CLASSIC,
1697 { },
1698 { { 1, 0 } },
1699 },
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001700};
1701
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001702static struct net_device dev;
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001703
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001704static struct sk_buff *populate_skb(char *buf, int size)
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001705{
1706 struct sk_buff *skb;
1707
1708 if (size >= MAX_DATA)
1709 return NULL;
1710
1711 skb = alloc_skb(MAX_DATA, GFP_KERNEL);
1712 if (!skb)
1713 return NULL;
1714
1715 memcpy(__skb_put(skb, size), buf, size);
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001716
1717 /* Initialize a fake skb with test pattern. */
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001718 skb_reset_mac_header(skb);
1719 skb->protocol = htons(ETH_P_IP);
1720 skb->pkt_type = SKB_TYPE;
1721 skb->mark = SKB_MARK;
1722 skb->hash = SKB_HASH;
1723 skb->queue_mapping = SKB_QUEUE_MAP;
1724 skb->vlan_tci = SKB_VLAN_TCI;
1725 skb->dev = &dev;
1726 skb->dev->ifindex = SKB_DEV_IFINDEX;
1727 skb->dev->type = SKB_DEV_TYPE;
1728 skb_set_network_header(skb, min(size, ETH_HLEN));
1729
1730 return skb;
1731}
1732
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001733static void *generate_test_data(struct bpf_test *test, int sub)
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001734{
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001735 if (test->aux & FLAG_NO_DATA)
1736 return NULL;
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001737
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001738 /* Test case expects an skb, so populate one. Various
1739 * subtests generate skbs of different sizes based on
1740 * the same data.
1741 */
1742 return populate_skb(test->data, test->test[sub].data_size);
1743}
1744
1745static void release_test_data(const struct bpf_test *test, void *data)
1746{
1747 if (test->aux & FLAG_NO_DATA)
1748 return;
1749
1750 kfree_skb(data);
1751}
1752
1753static int probe_filter_length(struct sock_filter *fp)
1754{
1755 int len = 0;
1756
Chema Gonzaleze9d94502014-05-30 10:15:12 -07001757 for (len = MAX_INSNS - 1; len > 0; --len)
1758 if (fp[len].code != 0 || fp[len].k != 0)
1759 break;
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001760
Chema Gonzaleze9d94502014-05-30 10:15:12 -07001761 return len + 1;
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001762}
1763
1764static struct sk_filter *generate_filter(int which, int *err)
1765{
1766 struct sk_filter *fp;
1767 struct sock_fprog_kern fprog;
1768 unsigned int flen = probe_filter_length(tests[which].u.insns);
1769 __u8 test_type = tests[which].aux & TEST_TYPE_MASK;
1770
1771 switch (test_type) {
1772 case CLASSIC:
1773 fprog.filter = tests[which].u.insns;
1774 fprog.len = flen;
1775
1776 *err = sk_unattached_filter_create(&fp, &fprog);
1777 if (tests[which].aux & FLAG_EXPECTED_FAIL) {
1778 if (*err == -EINVAL) {
1779 pr_cont("PASS\n");
1780 /* Verifier rejected filter as expected. */
1781 *err = 0;
1782 return NULL;
1783 } else {
1784 pr_cont("UNEXPECTED_PASS\n");
1785 /* Verifier didn't reject the test that's
1786 * bad enough, just return!
1787 */
1788 *err = -EINVAL;
1789 return NULL;
1790 }
1791 }
1792 /* We don't expect to fail. */
1793 if (*err) {
1794 pr_cont("FAIL to attach err=%d len=%d\n",
1795 *err, fprog.len);
1796 return NULL;
1797 }
1798 break;
1799
1800 case INTERNAL:
1801 fp = kzalloc(sk_filter_size(flen), GFP_KERNEL);
1802 if (fp == NULL) {
1803 pr_cont("UNEXPECTED_FAIL no memory left\n");
1804 *err = -ENOMEM;
1805 return NULL;
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001806 }
1807
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001808 fp->len = flen;
1809 memcpy(fp->insnsi, tests[which].u.insns_int,
1810 fp->len * sizeof(struct sock_filter_int));
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001811
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001812 sk_filter_select_runtime(fp);
1813 break;
1814 }
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001815
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001816 *err = 0;
1817 return fp;
1818}
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001819
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001820static void release_filter(struct sk_filter *fp, int which)
1821{
1822 __u8 test_type = tests[which].aux & TEST_TYPE_MASK;
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001823
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001824 switch (test_type) {
1825 case CLASSIC:
1826 sk_unattached_filter_destroy(fp);
1827 break;
1828 case INTERNAL:
1829 sk_filter_free(fp);
1830 break;
1831 }
1832}
1833
1834static int __run_one(const struct sk_filter *fp, const void *data,
1835 int runs, u64 *duration)
1836{
1837 u64 start, finish;
1838 int ret, i;
1839
1840 start = ktime_to_us(ktime_get());
1841
1842 for (i = 0; i < runs; i++)
1843 ret = SK_RUN_FILTER(fp, data);
1844
1845 finish = ktime_to_us(ktime_get());
1846
1847 *duration = (finish - start) * 1000ULL;
1848 do_div(*duration, runs);
1849
1850 return ret;
1851}
1852
1853static int run_one(const struct sk_filter *fp, struct bpf_test *test)
1854{
1855 int err_cnt = 0, i, runs = MAX_TESTRUNS;
1856
1857 for (i = 0; i < MAX_SUBTESTS; i++) {
1858 void *data;
1859 u64 duration;
1860 u32 ret;
1861
1862 if (test->test[i].data_size == 0 &&
1863 test->test[i].result == 0)
1864 break;
1865
1866 data = generate_test_data(test, i);
1867 ret = __run_one(fp, data, runs, &duration);
1868 release_test_data(test, data);
1869
1870 if (ret == test->test[i].result) {
1871 pr_cont("%lld ", duration);
1872 } else {
1873 pr_cont("ret %d != %d ", ret,
1874 test->test[i].result);
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001875 err_cnt++;
1876 }
1877 }
1878
1879 return err_cnt;
1880}
1881
1882static __init int test_bpf(void)
1883{
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001884 int i, err_cnt = 0, pass_cnt = 0;
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001885
1886 for (i = 0; i < ARRAY_SIZE(tests); i++) {
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001887 struct sk_filter *fp;
1888 int err;
1889
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001890 pr_info("#%d %s ", i, tests[i].descr);
1891
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001892 fp = generate_filter(i, &err);
1893 if (fp == NULL) {
1894 if (err == 0) {
1895 pass_cnt++;
1896 continue;
1897 }
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001898
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001899 return err;
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001900 }
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001901 err = run_one(fp, &tests[i]);
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001902 release_filter(fp, i);
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001903
1904 if (err) {
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001905 pr_cont("FAIL (%d times)\n", err);
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001906 err_cnt++;
1907 } else {
1908 pr_cont("PASS\n");
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001909 pass_cnt++;
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001910 }
1911 }
1912
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001913 pr_info("Summary: %d PASSED, %d FAILED\n", pass_cnt, err_cnt);
1914 return err_cnt ? -EINVAL : 0;
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001915}
1916
1917static int __init test_bpf_init(void)
1918{
1919 return test_bpf();
1920}
1921
1922static void __exit test_bpf_exit(void)
1923{
1924}
1925
1926module_init(test_bpf_init);
1927module_exit(test_bpf_exit);
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001928
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001929MODULE_LICENSE("GPL");