cregit-Linux how code gets into the kernel

Release 4.7 lib/test_bpf.c

Directory: lib
/*
 * Testsuite for BPF interpreter and BPF JIT compiler
 *
 * Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 */


#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/init.h>
#include <linux/module.h>
#include <linux/filter.h>
#include <linux/bpf.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/if_vlan.h>
#include <linux/random.h>
#include <linux/highmem.h>

/* General test specific settings */

#define MAX_SUBTESTS	3

#define MAX_TESTRUNS	10000

#define MAX_DATA	128

#define MAX_INSNS	512

#define MAX_K		0xffffFFFF

/* Few constants used to init test 'skb' */

#define SKB_TYPE	3

#define SKB_MARK	0x1234aaaa

#define SKB_HASH	0x1234aaab

#define SKB_QUEUE_MAP	123

#define SKB_VLAN_TCI	0xffff

#define SKB_DEV_IFINDEX	577

#define SKB_DEV_TYPE	588

/* Redefine REGs to make tests less verbose */

#define R0		BPF_REG_0

#define R1		BPF_REG_1

#define R2		BPF_REG_2

#define R3		BPF_REG_3

#define R4		BPF_REG_4

#define R5		BPF_REG_5

#define R6		BPF_REG_6

#define R7		BPF_REG_7

#define R8		BPF_REG_8

#define R9		BPF_REG_9

#define R10		BPF_REG_10

/* Flags that can be passed to test cases */

#define FLAG_NO_DATA		BIT(0)

#define FLAG_EXPECTED_FAIL	BIT(1)

#define FLAG_SKB_FRAG		BIT(2)

enum {
	
CLASSIC  = BIT(6),	/* Old BPF instructions only. */
	
INTERNAL = BIT(7),	/* Extended instruction set.  */
};


#define TEST_TYPE_MASK		(CLASSIC | INTERNAL)


struct bpf_test {
	
const char *descr;
	union {
		
struct sock_filter insns[MAX_INSNS];
		
struct bpf_insn insns_int[MAX_INSNS];
		struct {
			
void *insns;
			
unsigned int len;
		} 
ptr;
	} 
u;
	
__u8 aux;
	
__u8 data[MAX_DATA];
	struct {
		
int data_size;
		
__u32 result;
	} 
test[MAX_SUBTESTS];
	
int (*fill_helper)(struct bpf_test *self);
	
__u8 frag_data[MAX_DATA];
};

/* Large test cases need separate allocation and fill handler. */


static int bpf_fill_maxinsns1(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; __u32 k = ~0; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; for (i = 0; i < len; i++, k--) insn[i] = __BPF_STMT(BPF_RET | BPF_K, k); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
daniel borkmanndaniel borkmann108100.00%1100.00%
Total108100.00%1100.00%


static int bpf_fill_maxinsns2(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; for (i = 0; i < len; i++) insn[i] = __BPF_STMT(BPF_RET | BPF_K, 0xfefefefe); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
daniel borkmanndaniel borkmann99100.00%1100.00%
Total99100.00%1100.00%


static int bpf_fill_maxinsns3(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; struct rnd_state rnd; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; prandom_seed_state(&rnd, 3141592653589793238ULL); for (i = 0; i < len - 1; i++) { __u32 k = prandom_u32_state(&rnd); insn[i] = __BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, k); } insn[len - 1] = __BPF_STMT(BPF_RET | BPF_A, 0); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
daniel borkmanndaniel borkmann142100.00%1100.00%
Total142100.00%1100.00%


static int bpf_fill_maxinsns4(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS + 1; struct sock_filter *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; for (i = 0; i < len; i++) insn[i] = __BPF_STMT(BPF_RET | BPF_K, 0xfefefefe); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
daniel borkmanndaniel borkmann101100.00%1100.00%
Total101100.00%1100.00%


static int bpf_fill_maxinsns5(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; insn[0] = __BPF_JUMP(BPF_JMP | BPF_JA, len - 2, 0, 0); for (i = 1; i < len - 1; i++) insn[i] = __BPF_STMT(BPF_RET | BPF_K, 0xfefefefe); insn[len - 1] = __BPF_STMT(BPF_RET | BPF_K, 0xabababab); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
daniel borkmanndaniel borkmann137100.00%1100.00%
Total137100.00%1100.00%


static int bpf_fill_maxinsns6(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; for (i = 0; i < len - 1; i++) insn[i] = __BPF_STMT(BPF_LD | BPF_W | BPF_ABS, SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT); insn[len - 1] = __BPF_STMT(BPF_RET | BPF_A, 0); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
daniel borkmanndaniel borkmann121100.00%1100.00%
Total121100.00%1100.00%


static int bpf_fill_maxinsns7(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; for (i = 0; i < len - 4; i++) insn[i] = __BPF_STMT(BPF_LD | BPF_W | BPF_ABS, SKF_AD_OFF + SKF_AD_CPU); insn[len - 4] = __BPF_STMT(BPF_MISC | BPF_TAX, 0); insn[len - 3] = __BPF_STMT(BPF_LD | BPF_W | BPF_ABS, SKF_AD_OFF + SKF_AD_CPU); insn[len - 2] = __BPF_STMT(BPF_ALU | BPF_SUB | BPF_X, 0); insn[len - 1] = __BPF_STMT(BPF_RET | BPF_A, 0); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
daniel borkmanndaniel borkmann175100.00%1100.00%
Total175100.00%1100.00%


static int bpf_fill_maxinsns8(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; int i, jmp_off = len - 3; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; insn[0] = __BPF_STMT(BPF_LD | BPF_IMM, 0xffffffff); for (i = 1; i < len - 1; i++) insn[i] = __BPF_JUMP(BPF_JMP | BPF_JGT, 0xffffffff, jmp_off--, 0); insn[len - 1] = __BPF_STMT(BPF_RET | BPF_A, 0); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
daniel borkmanndaniel borkmann14199.30%150.00%
alexei starovoitovalexei starovoitov10.70%150.00%
Total142100.00%2100.00%


static int bpf_fill_maxinsns9(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS; struct bpf_insn *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; insn[0] = BPF_JMP_IMM(BPF_JA, 0, 0, len - 2); insn[1] = BPF_ALU32_IMM(BPF_MOV, R0, 0xcbababab); insn[2] = BPF_EXIT_INSN(); for (i = 3; i < len - 2; i++) insn[i] = BPF_ALU32_IMM(BPF_MOV, R0, 0xfefefefe); insn[len - 2] = BPF_EXIT_INSN(); insn[len - 1] = BPF_JMP_IMM(BPF_JA, 0, 0, -(len - 1)); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
daniel borkmanndaniel borkmann174100.00%1100.00%
Total174100.00%1100.00%


static int bpf_fill_maxinsns10(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS, hlen = len - 2; struct bpf_insn *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; for (i = 0; i < hlen / 2; i++) insn[i] = BPF_JMP_IMM(BPF_JA, 0, 0, hlen - 2 - 2 * i); for (i = hlen - 1; i > hlen / 2; i--) insn[i] = BPF_JMP_IMM(BPF_JA, 0, 0, hlen - 1 - 2 * i); insn[hlen / 2] = BPF_JMP_IMM(BPF_JA, 0, 0, hlen / 2 - 1); insn[hlen] = BPF_ALU32_IMM(BPF_MOV, R0, 0xabababac); insn[hlen + 1] = BPF_EXIT_INSN(); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
daniel borkmanndaniel borkmann200100.00%1100.00%
Total200100.00%1100.00%


static int __bpf_fill_ja(struct bpf_test *self, unsigned int len, unsigned int plen) { struct sock_filter *insn; unsigned int rlen; int i, j; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; rlen = (len % plen) - 1; for (i = 0; i + plen < len; i += plen) for (j = 0; j < plen; j++) insn[i + j] = __BPF_JUMP(BPF_JMP | BPF_JA, plen - 1 - j, 0, 0); for (j = 0; j < rlen; j++) insn[i + j] = __BPF_JUMP(BPF_JMP | BPF_JA, rlen - 1 - j, 0, 0); insn[len - 1] = __BPF_STMT(BPF_RET | BPF_K, 0xababcbac); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
alexei starovoitovalexei starovoitov12563.78%150.00%
daniel borkmanndaniel borkmann7136.22%150.00%
Total196100.00%2100.00%


static int bpf_fill_maxinsns11(struct bpf_test *self) { /* Hits 70 passes on x86_64, so cannot get JITed there. */ return __bpf_fill_ja(self, BPF_MAXINSNS, 68); }

Contributors

PersonTokensPropCommitsCommitProp
alexei starovoitovalexei starovoitov22100.00%1100.00%
Total22100.00%1100.00%


static int bpf_fill_ja(struct bpf_test *self) { /* Hits exactly 11 passes on x86_64 JIT. */ return __bpf_fill_ja(self, 12, 9); }

Contributors

PersonTokensPropCommitsCommitProp
alexei starovoitovalexei starovoitov22100.00%1100.00%
Total22100.00%1100.00%


static int bpf_fill_ld_abs_get_processor_id(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS; struct sock_filter *insn; int i; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; for (i = 0; i < len - 1; i += 2) { insn[i] = __BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 0); insn[i + 1] = __BPF_STMT(BPF_LD | BPF_W | BPF_ABS, SKF_AD_OFF + SKF_AD_CPU); } insn[len - 1] = __BPF_STMT(BPF_RET | BPF_K, 0xbee); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
alexei starovoitovalexei starovoitov142100.00%1100.00%
Total142100.00%1100.00%

#define PUSH_CNT 68 /* test: {skb->data[0], vlan_push} x 68 + {skb->data[0], vlan_pop} x 68 */
static int bpf_fill_ld_abs_vlan_push_pop(struct bpf_test *self) { unsigned int len = BPF_MAXINSNS; struct bpf_insn *insn; int i = 0, j, k = 0; insn = kmalloc_array(len, sizeof(*insn), GFP_KERNEL); if (!insn) return -ENOMEM; insn[i++] = BPF_MOV64_REG(R6, R1); loop: for (j = 0; j < PUSH_CNT; j++) { insn[i++] = BPF_LD_ABS(BPF_B, 0); insn[i] = BPF_JMP_IMM(BPF_JNE, R0, 0x34, len - i - 2); i++; insn[i++] = BPF_MOV64_REG(R1, R6); insn[i++] = BPF_MOV64_IMM(R2, 1); insn[i++] = BPF_MOV64_IMM(R3, 2); insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, bpf_skb_vlan_push_proto.func - __bpf_call_base); insn[i] = BPF_JMP_IMM(BPF_JNE, R0, 0, len - i - 2); i++; } for (j = 0; j < PUSH_CNT; j++) { insn[i++] = BPF_LD_ABS(BPF_B, 0); insn[i] = BPF_JMP_IMM(BPF_JNE, R0, 0x34, len - i - 2); i++; insn[i++] = BPF_MOV64_REG(R1, R6); insn[i++] = BPF_RAW_INSN(BPF_JMP | BPF_CALL, 0, 0, 0, bpf_skb_vlan_pop_proto.func - __bpf_call_base); insn[i] = BPF_JMP_IMM(BPF_JNE, R0, 0, len - i - 2); i++; } if (++k < 5) goto loop; for (; i < len - 1; i++) insn[i] = BPF_ALU32_IMM(BPF_MOV, R0, 0xbef); insn[len - 1] = BPF_EXIT_INSN(); self->u.ptr.insns = insn; self->u.ptr.len = len; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
alexei starovoitovalexei starovoitov31380.05%150.00%
daniel borkmanndaniel borkmann7819.95%150.00%
Total391100.00%2100.00%

static struct bpf_test tests[] = { { "TAX", .u.insns = { BPF_STMT(BPF_LD | BPF_IMM, 1), BPF_STMT(BPF_MISC | BPF_TAX, 0), BPF_STMT(BPF_LD | BPF_IMM, 2), BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0), BPF_STMT(BPF_ALU | BPF_NEG, 0), /* A == -3 */ BPF_STMT(BPF_MISC | BPF_TAX, 0), BPF_STMT(BPF_LD | BPF_LEN, 0), BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0), BPF_STMT(BPF_MISC | BPF_TAX, 0), /* X == len - 3 */ BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1), BPF_STMT(BPF_RET | BPF_A, 0) }, CLASSIC, { 10, 20, 30, 40, 50 }, { { 2, 10 }, { 3, 20 }, { 4, 30 } }, }, { "TXA", .u.insns = { BPF_STMT(BPF_LDX | BPF_LEN, 0), BPF_STMT(BPF_MISC | BPF_TXA, 0), BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0), BPF_STMT(BPF_RET | BPF_A, 0) /* A == len * 2 */ }, CLASSIC, { 10, 20, 30, 40, 50 }, { { 1, 2 }, { 3, 6 }, { 4, 8 } }, }, { "ADD_SUB_MUL_K", .u.insns = { BPF_STMT(BPF_LD | BPF_IMM, 1), BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 2), BPF_STMT(BPF_LDX | BPF_IMM, 3), BPF_STMT(BPF_ALU | BPF_SUB | BPF_X, 0), BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 0xffffffff), BPF_STMT(BPF_ALU | BPF_MUL | BPF_K, 3), BPF_STMT(BPF_RET | BPF_A, 0) }, CLASSIC | FLAG_NO_DATA, { }, { { 0, 0xfffffffd } } }, { "DIV_MOD_KX", .u.insns = { BPF_STMT(BPF_LD | BPF_IMM, 8), BPF_STMT(BPF_ALU | BPF_DIV | BPF_K, 2), BPF_STMT(BPF_MISC | BPF_TAX, 0), BPF_STMT(BPF_LD | BPF_IMM, 0xffffffff), BPF_STMT(BPF_ALU | BPF_DIV | BPF_X, 0), BPF_STMT(BPF_MISC | BPF_TAX, 0), BPF_STMT(BPF_LD | BPF_IMM, 0xffffffff), BPF_STMT(BPF_ALU | BPF_DIV | BPF_K, 0x70000000), BPF_STMT(BPF_MISC | BPF_TAX, 0), BPF_STMT(BPF_LD | BPF_IMM, 0xffffffff), BPF_STMT(BPF_ALU | BPF_MOD | BPF_X, 0), BPF_STMT(BPF_MISC | BPF_TAX, 0), BPF_STMT(BPF_LD | BPF_IMM, 0xffffffff), BPF_STMT(BPF_ALU | BPF_MOD | BPF_K, 0x70000000), BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0), BPF_STMT(BPF_RET | BPF_A, 0) }, CLASSIC | FLAG_NO_DATA, { }, { { 0, 0x20000000 } } }, { "AND_OR_LSH_K", .u.insns = { BPF_STMT(BPF_LD | BPF_IMM, 0xff),