blob: 413890815d3e7f247ead0aacda4937194ee5fcd4 [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];
Alexei Starovoitov2695fb52014-07-24 16:38:21 -070069 struct bpf_insn 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 Starovoitov72b603e2014-08-25 12:27:02 -07001345 "INT: shifts by register",
1346 .u.insns_int = {
1347 BPF_MOV64_IMM(R0, -1234),
1348 BPF_MOV64_IMM(R1, 1),
1349 BPF_ALU32_REG(BPF_RSH, R0, R1),
1350 BPF_JMP_IMM(BPF_JEQ, R0, 0x7ffffd97, 1),
1351 BPF_EXIT_INSN(),
1352 BPF_MOV64_IMM(R2, 1),
1353 BPF_ALU64_REG(BPF_LSH, R0, R2),
1354 BPF_MOV32_IMM(R4, -1234),
1355 BPF_JMP_REG(BPF_JEQ, R0, R4, 1),
1356 BPF_EXIT_INSN(),
1357 BPF_ALU64_IMM(BPF_AND, R4, 63),
1358 BPF_ALU64_REG(BPF_LSH, R0, R4), /* R0 <= 46 */
1359 BPF_MOV64_IMM(R3, 47),
1360 BPF_ALU64_REG(BPF_ARSH, R0, R3),
1361 BPF_JMP_IMM(BPF_JEQ, R0, -617, 1),
1362 BPF_EXIT_INSN(),
1363 BPF_MOV64_IMM(R2, 1),
1364 BPF_ALU64_REG(BPF_LSH, R4, R2), /* R4 = 46 << 1 */
1365 BPF_JMP_IMM(BPF_JEQ, R4, 92, 1),
1366 BPF_EXIT_INSN(),
1367 BPF_MOV64_IMM(R4, 4),
1368 BPF_ALU64_REG(BPF_LSH, R4, R4), /* R4 = 4 << 4 */
1369 BPF_JMP_IMM(BPF_JEQ, R4, 64, 1),
1370 BPF_EXIT_INSN(),
1371 BPF_MOV64_IMM(R4, 5),
1372 BPF_ALU32_REG(BPF_LSH, R4, R4), /* R4 = 5 << 5 */
1373 BPF_JMP_IMM(BPF_JEQ, R4, 160, 1),
1374 BPF_EXIT_INSN(),
1375 BPF_MOV64_IMM(R0, -1),
1376 BPF_EXIT_INSN(),
1377 },
1378 INTERNAL,
1379 { },
1380 { { 0, -1 } }
1381 },
1382 {
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001383 "INT: DIV + ABS",
Andrew Mortonece80492014-05-22 10:16:46 -07001384 .u.insns_int = {
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001385 BPF_ALU64_REG(BPF_MOV, R6, R1),
1386 BPF_LD_ABS(BPF_B, 3),
1387 BPF_ALU64_IMM(BPF_MOV, R2, 2),
1388 BPF_ALU32_REG(BPF_DIV, R0, R2),
1389 BPF_ALU64_REG(BPF_MOV, R8, R0),
1390 BPF_LD_ABS(BPF_B, 4),
1391 BPF_ALU64_REG(BPF_ADD, R8, R0),
1392 BPF_LD_IND(BPF_B, R8, -70),
1393 BPF_EXIT_INSN(),
1394 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001395 INTERNAL,
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001396 { 10, 20, 30, 40, 50 },
1397 { { 4, 0 }, { 5, 10 } }
1398 },
1399 {
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001400 "INT: DIV by zero",
Andrew Mortonece80492014-05-22 10:16:46 -07001401 .u.insns_int = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001402 BPF_ALU64_REG(BPF_MOV, R6, R1),
1403 BPF_ALU64_IMM(BPF_MOV, R7, 0),
1404 BPF_LD_ABS(BPF_B, 3),
1405 BPF_ALU32_REG(BPF_DIV, R0, R7),
1406 BPF_EXIT_INSN(),
1407 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001408 INTERNAL,
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001409 { 10, 20, 30, 40, 50 },
1410 { { 3, 0 }, { 4, 0 } }
1411 },
1412 {
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001413 "check: missing ret",
Andrew Mortonece80492014-05-22 10:16:46 -07001414 .u.insns = {
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001415 BPF_STMT(BPF_LD | BPF_IMM, 1),
1416 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001417 CLASSIC | FLAG_NO_DATA | FLAG_EXPECTED_FAIL,
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001418 { },
1419 { }
1420 },
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001421 {
1422 "check: div_k_0",
Andrew Mortonece80492014-05-22 10:16:46 -07001423 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001424 BPF_STMT(BPF_ALU | BPF_DIV | BPF_K, 0),
1425 BPF_STMT(BPF_RET | BPF_K, 0)
1426 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001427 CLASSIC | FLAG_NO_DATA | FLAG_EXPECTED_FAIL,
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001428 { },
1429 { }
1430 },
1431 {
1432 "check: unknown insn",
Andrew Mortonece80492014-05-22 10:16:46 -07001433 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001434 /* seccomp insn, rejected in socket filter */
1435 BPF_STMT(BPF_LDX | BPF_W | BPF_ABS, 0),
1436 BPF_STMT(BPF_RET | BPF_K, 0)
1437 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001438 CLASSIC | FLAG_EXPECTED_FAIL,
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001439 { },
1440 { }
1441 },
1442 {
1443 "check: out of range spill/fill",
Andrew Mortonece80492014-05-22 10:16:46 -07001444 .u.insns = {
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001445 BPF_STMT(BPF_STX, 16),
1446 BPF_STMT(BPF_RET | BPF_K, 0)
1447 },
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001448 CLASSIC | FLAG_NO_DATA | FLAG_EXPECTED_FAIL,
Alexei Starovoitov9def6242014-05-08 14:10:53 -07001449 { },
1450 { }
1451 },
Daniel Borkmann2e8a83c2014-05-23 18:44:01 +02001452 {
1453 "JUMPS + HOLES",
1454 .u.insns = {
1455 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1456 BPF_JUMP(BPF_JMP | BPF_JGE, 0, 13, 15),
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_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1470 BPF_JUMP(BPF_JMP | BPF_JEQ, 0x90c2894d, 3, 4),
1471 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1472 BPF_JUMP(BPF_JMP | BPF_JEQ, 0x90c2894d, 1, 2),
1473 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1474 BPF_JUMP(BPF_JMP | BPF_JGE, 0, 14, 15),
1475 BPF_JUMP(BPF_JMP | BPF_JGE, 0, 13, 14),
1476 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1477 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1478 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1479 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1480 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1481 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1482 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1483 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1484 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1485 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1486 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1487 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1488 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1489 BPF_JUMP(BPF_JMP | BPF_JEQ, 0x2ac28349, 2, 3),
1490 BPF_JUMP(BPF_JMP | BPF_JEQ, 0x2ac28349, 1, 2),
1491 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1492 BPF_JUMP(BPF_JMP | BPF_JGE, 0, 14, 15),
1493 BPF_JUMP(BPF_JMP | BPF_JGE, 0, 13, 14),
1494 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1495 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1496 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1497 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1498 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1499 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1500 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1501 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1502 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1503 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1504 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1505 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1506 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1507 BPF_JUMP(BPF_JMP | BPF_JEQ, 0x90d2ff41, 2, 3),
1508 BPF_JUMP(BPF_JMP | BPF_JEQ, 0x90d2ff41, 1, 2),
1509 BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 0),
1510 BPF_STMT(BPF_RET | BPF_A, 0),
1511 BPF_STMT(BPF_RET | BPF_A, 0),
1512 },
1513 CLASSIC,
Daniel Borkmannce25b682014-05-26 20:17:35 +02001514 { 0x00, 0x1b, 0x21, 0x3c, 0x9d, 0xf8,
1515 0x90, 0xe2, 0xba, 0x0a, 0x56, 0xb4,
1516 0x08, 0x00,
1517 0x45, 0x00, 0x00, 0x28, 0x00, 0x00,
1518 0x20, 0x00, 0x40, 0x11, 0x00, 0x00, /* IP header */
1519 0xc0, 0xa8, 0x33, 0x01,
1520 0xc0, 0xa8, 0x33, 0x02,
1521 0xbb, 0xb6,
1522 0xa9, 0xfa,
1523 0x00, 0x14, 0x00, 0x00,
1524 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
1525 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
1526 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
1527 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
1528 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
1529 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
1530 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc,
1531 0xcc, 0xcc, 0xcc, 0xcc },
Daniel Borkmann2e8a83c2014-05-23 18:44:01 +02001532 { { 88, 0x001b } }
1533 },
1534 {
1535 "check: RET X",
1536 .u.insns = {
1537 BPF_STMT(BPF_RET | BPF_X, 0),
1538 },
1539 CLASSIC | FLAG_NO_DATA | FLAG_EXPECTED_FAIL,
1540 { },
1541 { },
1542 },
1543 {
1544 "check: LDX + RET X",
1545 .u.insns = {
1546 BPF_STMT(BPF_LDX | BPF_IMM, 42),
1547 BPF_STMT(BPF_RET | BPF_X, 0),
1548 },
1549 CLASSIC | FLAG_NO_DATA | FLAG_EXPECTED_FAIL,
1550 { },
1551 { },
1552 },
Daniel Borkmann108cc222014-05-26 20:17:34 +02001553 { /* Mainly checking JIT here. */
Daniel Borkmann9fe13ba2014-05-29 10:22:48 +02001554 "M[]: alt STX + LDX",
Daniel Borkmann108cc222014-05-26 20:17:34 +02001555 .u.insns = {
1556 BPF_STMT(BPF_LDX | BPF_IMM, 100),
1557 BPF_STMT(BPF_STX, 0),
1558 BPF_STMT(BPF_LDX | BPF_MEM, 0),
1559 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1560 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1561 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1562 BPF_STMT(BPF_STX, 1),
1563 BPF_STMT(BPF_LDX | BPF_MEM, 1),
1564 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1565 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1566 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1567 BPF_STMT(BPF_STX, 2),
1568 BPF_STMT(BPF_LDX | BPF_MEM, 2),
1569 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1570 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1571 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1572 BPF_STMT(BPF_STX, 3),
1573 BPF_STMT(BPF_LDX | BPF_MEM, 3),
1574 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1575 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1576 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1577 BPF_STMT(BPF_STX, 4),
1578 BPF_STMT(BPF_LDX | BPF_MEM, 4),
1579 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1580 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1581 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1582 BPF_STMT(BPF_STX, 5),
1583 BPF_STMT(BPF_LDX | BPF_MEM, 5),
1584 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1585 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1586 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1587 BPF_STMT(BPF_STX, 6),
1588 BPF_STMT(BPF_LDX | BPF_MEM, 6),
1589 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1590 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1591 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1592 BPF_STMT(BPF_STX, 7),
1593 BPF_STMT(BPF_LDX | BPF_MEM, 7),
1594 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1595 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1596 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1597 BPF_STMT(BPF_STX, 8),
1598 BPF_STMT(BPF_LDX | BPF_MEM, 8),
1599 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1600 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1601 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1602 BPF_STMT(BPF_STX, 9),
1603 BPF_STMT(BPF_LDX | BPF_MEM, 9),
1604 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1605 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1606 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1607 BPF_STMT(BPF_STX, 10),
1608 BPF_STMT(BPF_LDX | BPF_MEM, 10),
1609 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1610 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1611 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1612 BPF_STMT(BPF_STX, 11),
1613 BPF_STMT(BPF_LDX | BPF_MEM, 11),
1614 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1615 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1616 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1617 BPF_STMT(BPF_STX, 12),
1618 BPF_STMT(BPF_LDX | BPF_MEM, 12),
1619 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1620 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1621 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1622 BPF_STMT(BPF_STX, 13),
1623 BPF_STMT(BPF_LDX | BPF_MEM, 13),
1624 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1625 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1626 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1627 BPF_STMT(BPF_STX, 14),
1628 BPF_STMT(BPF_LDX | BPF_MEM, 14),
1629 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1630 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1631 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1632 BPF_STMT(BPF_STX, 15),
1633 BPF_STMT(BPF_LDX | BPF_MEM, 15),
1634 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1635 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 1),
1636 BPF_STMT(BPF_MISC | BPF_TAX, 0),
1637 BPF_STMT(BPF_RET | BPF_A, 0),
1638 },
1639 CLASSIC | FLAG_NO_DATA,
1640 { },
1641 { { 0, 116 } },
1642 },
Daniel Borkmann9fe13ba2014-05-29 10:22:48 +02001643 { /* Mainly checking JIT here. */
1644 "M[]: full STX + full LDX",
1645 .u.insns = {
1646 BPF_STMT(BPF_LDX | BPF_IMM, 0xbadfeedb),
1647 BPF_STMT(BPF_STX, 0),
1648 BPF_STMT(BPF_LDX | BPF_IMM, 0xecabedae),
1649 BPF_STMT(BPF_STX, 1),
1650 BPF_STMT(BPF_LDX | BPF_IMM, 0xafccfeaf),
1651 BPF_STMT(BPF_STX, 2),
1652 BPF_STMT(BPF_LDX | BPF_IMM, 0xbffdcedc),
1653 BPF_STMT(BPF_STX, 3),
1654 BPF_STMT(BPF_LDX | BPF_IMM, 0xfbbbdccb),
1655 BPF_STMT(BPF_STX, 4),
1656 BPF_STMT(BPF_LDX | BPF_IMM, 0xfbabcbda),
1657 BPF_STMT(BPF_STX, 5),
1658 BPF_STMT(BPF_LDX | BPF_IMM, 0xaedecbdb),
1659 BPF_STMT(BPF_STX, 6),
1660 BPF_STMT(BPF_LDX | BPF_IMM, 0xadebbade),
1661 BPF_STMT(BPF_STX, 7),
1662 BPF_STMT(BPF_LDX | BPF_IMM, 0xfcfcfaec),
1663 BPF_STMT(BPF_STX, 8),
1664 BPF_STMT(BPF_LDX | BPF_IMM, 0xbcdddbdc),
1665 BPF_STMT(BPF_STX, 9),
1666 BPF_STMT(BPF_LDX | BPF_IMM, 0xfeefdfac),
1667 BPF_STMT(BPF_STX, 10),
1668 BPF_STMT(BPF_LDX | BPF_IMM, 0xcddcdeea),
1669 BPF_STMT(BPF_STX, 11),
1670 BPF_STMT(BPF_LDX | BPF_IMM, 0xaccfaebb),
1671 BPF_STMT(BPF_STX, 12),
1672 BPF_STMT(BPF_LDX | BPF_IMM, 0xbdcccdcf),
1673 BPF_STMT(BPF_STX, 13),
1674 BPF_STMT(BPF_LDX | BPF_IMM, 0xaaedecde),
1675 BPF_STMT(BPF_STX, 14),
1676 BPF_STMT(BPF_LDX | BPF_IMM, 0xfaeacdad),
1677 BPF_STMT(BPF_STX, 15),
1678 BPF_STMT(BPF_LDX | BPF_MEM, 0),
1679 BPF_STMT(BPF_MISC | BPF_TXA, 0),
1680 BPF_STMT(BPF_LDX | BPF_MEM, 1),
1681 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1682 BPF_STMT(BPF_LDX | BPF_MEM, 2),
1683 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1684 BPF_STMT(BPF_LDX | BPF_MEM, 3),
1685 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1686 BPF_STMT(BPF_LDX | BPF_MEM, 4),
1687 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1688 BPF_STMT(BPF_LDX | BPF_MEM, 5),
1689 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1690 BPF_STMT(BPF_LDX | BPF_MEM, 6),
1691 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1692 BPF_STMT(BPF_LDX | BPF_MEM, 7),
1693 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1694 BPF_STMT(BPF_LDX | BPF_MEM, 8),
1695 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1696 BPF_STMT(BPF_LDX | BPF_MEM, 9),
1697 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1698 BPF_STMT(BPF_LDX | BPF_MEM, 10),
1699 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1700 BPF_STMT(BPF_LDX | BPF_MEM, 11),
1701 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1702 BPF_STMT(BPF_LDX | BPF_MEM, 12),
1703 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1704 BPF_STMT(BPF_LDX | BPF_MEM, 13),
1705 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1706 BPF_STMT(BPF_LDX | BPF_MEM, 14),
1707 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1708 BPF_STMT(BPF_LDX | BPF_MEM, 15),
1709 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
1710 BPF_STMT(BPF_RET | BPF_A, 0),
1711 },
1712 CLASSIC | FLAG_NO_DATA,
1713 { },
1714 { { 0, 0x2a5a5e5 } },
1715 },
Daniel Borkmannd50bc152014-05-29 10:22:49 +02001716 {
1717 "check: SKF_AD_MAX",
1718 .u.insns = {
1719 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
1720 SKF_AD_OFF + SKF_AD_MAX),
1721 BPF_STMT(BPF_RET | BPF_A, 0),
1722 },
1723 CLASSIC | FLAG_NO_DATA | FLAG_EXPECTED_FAIL,
1724 { },
1725 { },
1726 },
1727 { /* Passes checker but fails during runtime. */
1728 "LD [SKF_AD_OFF-1]",
1729 .u.insns = {
1730 BPF_STMT(BPF_LD | BPF_W | BPF_ABS,
1731 SKF_AD_OFF - 1),
1732 BPF_STMT(BPF_RET | BPF_K, 1),
1733 },
1734 CLASSIC,
1735 { },
1736 { { 1, 0 } },
1737 },
Alexei Starovoitov02ab6952014-09-04 22:17:17 -07001738 {
1739 "load 64-bit immediate",
1740 .u.insns_int = {
1741 BPF_LD_IMM64(R1, 0x567800001234L),
1742 BPF_MOV64_REG(R2, R1),
1743 BPF_MOV64_REG(R3, R2),
1744 BPF_ALU64_IMM(BPF_RSH, R2, 32),
1745 BPF_ALU64_IMM(BPF_LSH, R3, 32),
1746 BPF_ALU64_IMM(BPF_RSH, R3, 32),
1747 BPF_ALU64_IMM(BPF_MOV, R0, 0),
1748 BPF_JMP_IMM(BPF_JEQ, R2, 0x5678, 1),
1749 BPF_EXIT_INSN(),
1750 BPF_JMP_IMM(BPF_JEQ, R3, 0x1234, 1),
1751 BPF_EXIT_INSN(),
1752 BPF_ALU64_IMM(BPF_MOV, R0, 1),
1753 BPF_EXIT_INSN(),
1754 },
1755 INTERNAL,
1756 { },
1757 { { 0, 1 } }
1758 },
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001759};
1760
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001761static struct net_device dev;
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001762
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001763static struct sk_buff *populate_skb(char *buf, int size)
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001764{
1765 struct sk_buff *skb;
1766
1767 if (size >= MAX_DATA)
1768 return NULL;
1769
1770 skb = alloc_skb(MAX_DATA, GFP_KERNEL);
1771 if (!skb)
1772 return NULL;
1773
1774 memcpy(__skb_put(skb, size), buf, size);
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001775
1776 /* Initialize a fake skb with test pattern. */
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001777 skb_reset_mac_header(skb);
1778 skb->protocol = htons(ETH_P_IP);
1779 skb->pkt_type = SKB_TYPE;
1780 skb->mark = SKB_MARK;
1781 skb->hash = SKB_HASH;
1782 skb->queue_mapping = SKB_QUEUE_MAP;
1783 skb->vlan_tci = SKB_VLAN_TCI;
1784 skb->dev = &dev;
1785 skb->dev->ifindex = SKB_DEV_IFINDEX;
1786 skb->dev->type = SKB_DEV_TYPE;
1787 skb_set_network_header(skb, min(size, ETH_HLEN));
1788
1789 return skb;
1790}
1791
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001792static void *generate_test_data(struct bpf_test *test, int sub)
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001793{
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001794 if (test->aux & FLAG_NO_DATA)
1795 return NULL;
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001796
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001797 /* Test case expects an skb, so populate one. Various
1798 * subtests generate skbs of different sizes based on
1799 * the same data.
1800 */
1801 return populate_skb(test->data, test->test[sub].data_size);
1802}
1803
1804static void release_test_data(const struct bpf_test *test, void *data)
1805{
1806 if (test->aux & FLAG_NO_DATA)
1807 return;
1808
1809 kfree_skb(data);
1810}
1811
1812static int probe_filter_length(struct sock_filter *fp)
1813{
1814 int len = 0;
1815
Chema Gonzaleze9d94502014-05-30 10:15:12 -07001816 for (len = MAX_INSNS - 1; len > 0; --len)
1817 if (fp[len].code != 0 || fp[len].k != 0)
1818 break;
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001819
Chema Gonzaleze9d94502014-05-30 10:15:12 -07001820 return len + 1;
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001821}
1822
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001823static struct bpf_prog *generate_filter(int which, int *err)
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001824{
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001825 struct bpf_prog *fp;
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001826 struct sock_fprog_kern fprog;
1827 unsigned int flen = probe_filter_length(tests[which].u.insns);
1828 __u8 test_type = tests[which].aux & TEST_TYPE_MASK;
1829
1830 switch (test_type) {
1831 case CLASSIC:
1832 fprog.filter = tests[which].u.insns;
1833 fprog.len = flen;
1834
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001835 *err = bpf_prog_create(&fp, &fprog);
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001836 if (tests[which].aux & FLAG_EXPECTED_FAIL) {
1837 if (*err == -EINVAL) {
1838 pr_cont("PASS\n");
1839 /* Verifier rejected filter as expected. */
1840 *err = 0;
1841 return NULL;
1842 } else {
1843 pr_cont("UNEXPECTED_PASS\n");
1844 /* Verifier didn't reject the test that's
1845 * bad enough, just return!
1846 */
1847 *err = -EINVAL;
1848 return NULL;
1849 }
1850 }
1851 /* We don't expect to fail. */
1852 if (*err) {
1853 pr_cont("FAIL to attach err=%d len=%d\n",
1854 *err, fprog.len);
1855 return NULL;
1856 }
1857 break;
1858
1859 case INTERNAL:
Daniel Borkmann60a3b222014-09-02 22:53:44 +02001860 fp = bpf_prog_alloc(bpf_prog_size(flen), 0);
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001861 if (fp == NULL) {
1862 pr_cont("UNEXPECTED_FAIL no memory left\n");
1863 *err = -ENOMEM;
1864 return NULL;
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001865 }
1866
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001867 fp->len = flen;
1868 memcpy(fp->insnsi, tests[which].u.insns_int,
Alexei Starovoitov2695fb52014-07-24 16:38:21 -07001869 fp->len * sizeof(struct bpf_insn));
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001870
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001871 bpf_prog_select_runtime(fp);
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001872 break;
1873 }
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001874
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001875 *err = 0;
1876 return fp;
1877}
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001878
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001879static void release_filter(struct bpf_prog *fp, int which)
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001880{
1881 __u8 test_type = tests[which].aux & TEST_TYPE_MASK;
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001882
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001883 switch (test_type) {
1884 case CLASSIC:
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001885 bpf_prog_destroy(fp);
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001886 break;
1887 case INTERNAL:
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001888 bpf_prog_free(fp);
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001889 break;
1890 }
1891}
1892
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001893static int __run_one(const struct bpf_prog *fp, const void *data,
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001894 int runs, u64 *duration)
1895{
1896 u64 start, finish;
1897 int ret, i;
1898
1899 start = ktime_to_us(ktime_get());
1900
1901 for (i = 0; i < runs; i++)
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001902 ret = BPF_PROG_RUN(fp, data);
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001903
1904 finish = ktime_to_us(ktime_get());
1905
1906 *duration = (finish - start) * 1000ULL;
1907 do_div(*duration, runs);
1908
1909 return ret;
1910}
1911
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001912static int run_one(const struct bpf_prog *fp, struct bpf_test *test)
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001913{
1914 int err_cnt = 0, i, runs = MAX_TESTRUNS;
1915
1916 for (i = 0; i < MAX_SUBTESTS; i++) {
1917 void *data;
1918 u64 duration;
1919 u32 ret;
1920
1921 if (test->test[i].data_size == 0 &&
1922 test->test[i].result == 0)
1923 break;
1924
1925 data = generate_test_data(test, i);
1926 ret = __run_one(fp, data, runs, &duration);
1927 release_test_data(test, data);
1928
1929 if (ret == test->test[i].result) {
1930 pr_cont("%lld ", duration);
1931 } else {
1932 pr_cont("ret %d != %d ", ret,
1933 test->test[i].result);
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001934 err_cnt++;
1935 }
1936 }
1937
1938 return err_cnt;
1939}
1940
1941static __init int test_bpf(void)
1942{
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001943 int i, err_cnt = 0, pass_cnt = 0;
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001944
1945 for (i = 0; i < ARRAY_SIZE(tests); i++) {
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -07001946 struct bpf_prog *fp;
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001947 int err;
1948
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001949 pr_info("#%d %s ", i, tests[i].descr);
1950
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001951 fp = generate_filter(i, &err);
1952 if (fp == NULL) {
1953 if (err == 0) {
1954 pass_cnt++;
1955 continue;
1956 }
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001957
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001958 return err;
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001959 }
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001960 err = run_one(fp, &tests[i]);
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001961 release_filter(fp, i);
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001962
1963 if (err) {
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001964 pr_cont("FAIL (%d times)\n", err);
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001965 err_cnt++;
1966 } else {
1967 pr_cont("PASS\n");
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001968 pass_cnt++;
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001969 }
1970 }
1971
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001972 pr_info("Summary: %d PASSED, %d FAILED\n", pass_cnt, err_cnt);
1973 return err_cnt ? -EINVAL : 0;
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001974}
1975
1976static int __init test_bpf_init(void)
1977{
1978 return test_bpf();
1979}
1980
1981static void __exit test_bpf_exit(void)
1982{
1983}
1984
1985module_init(test_bpf_init);
1986module_exit(test_bpf_exit);
Daniel Borkmann10f18e02014-05-23 18:44:00 +02001987
Alexei Starovoitov64a89462014-05-08 14:10:52 -07001988MODULE_LICENSE("GPL");