source: lib/bpf-jit/bpf-opcodes.c @ e966aa8

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since e966aa8 was d48008d, checked in by Perry Lorier <perry@…>, 11 years ago

Add JITting of BPF bytecode using LLVM

  • Property mode set to 100644
File size: 4.0 KB
Line 
1/*
2 * This file is not linked into libtrace in the normal way you would expect.
3 *
4 * It is passed to llvm-gcc, which generates .bc, and then through llc which
5 * generates a .cc file of the llvm code to /generate/ this file.  This is
6 * then included into bpf-jit.
7 */
8#include <inttypes.h>
9#include <arpa/inet.h>
10#define MEMLEN 16
11
12typedef struct bpf_state_t {
13        unsigned int A;
14        unsigned int X;
15        const unsigned char *P;
16        uint32_t len;
17        unsigned int mem[MEMLEN];
18} bpf_state_t;
19
20#define OPCODE(name) \
21static unsigned int name(bpf_state_t *state, unsigned char jt, unsigned char jf, unsigned long k)
22
23#define P_WORD(x)  ntohl(*(uint32_t*)&state->P[x])
24#define P_HWORD(x) ntohs(*(uint16_t*)&state->P[x])
25#define P_BYTE(x) (state->P[(x)])
26
27OPCODE(bpf_ldw_abs) { 
28        if (k+3>=state->len)
29                return 0;       /* Read past end of packet: Fail */
30        state->A = P_WORD(k); 
31        return ~0U;
32}
33OPCODE(bpf_ldh_abs) { 
34        if (k+1>=state->len)
35                return 0;
36        state->A = P_HWORD(k); 
37        return ~0U;
38}
39OPCODE(bpf_ldb_abs) { 
40        if (k>=state->len)
41                return 0;
42        state->A = P_BYTE(k); 
43        return ~0U;
44}
45OPCODE(bpf_ldw_ind) { 
46        if (k+state->X+3 >= state->len)
47                return 0;
48        state->A = P_WORD(k+state->X); 
49        return ~0U;
50}
51OPCODE(bpf_ldh_ind) { 
52        if (k+state->X+1 >= state->len)
53                return 0;
54        state->A = P_HWORD(k+state->X); 
55        return ~0U;
56}
57OPCODE(bpf_ldb_ind) { 
58        if (k+state->X >= state->len)
59                return 0;
60        state->A = P_BYTE(k+state->X); 
61        return ~0U;
62}
63OPCODE(bbf_ldw_ind) { state->A = state->len; return ~0;}
64OPCODE(bpf_ld_imm)  { state->A = k; return ~0;}
65OPCODE(bpf_ld_mem)  { 
66        if (k>=MEMLEN)
67                return 0; /* Fail Immediately */
68        state->A = state->mem[k]; 
69        return ~0;
70}
71
72OPCODE(bpf_ldx_imm) { state->X = k; return ~0;}
73OPCODE(bpf_ldx_mem) { 
74        if (k>=MEMLEN)
75                return 0; /* Fail Immediately */
76        state->X = state->mem[k]; 
77        return ~0;
78}
79OPCODE(bpf_ldx_len) { state->X = state->len; return ~0;}
80OPCODE(bpf_ldx_msh) { 
81        if (k>=state->len)
82                return 0; /* Read past end of packet: Fail */
83        state->X = 4*(P_BYTE(k)&0x0F); 
84        return ~0;
85}
86
87OPCODE(bpf_sd) { 
88        if (k>=MEMLEN)
89                return 0; /* Fail Immediately */
90        state->mem[k] = state->A; 
91        return ~0;
92}
93OPCODE(bpf_sdx) { 
94        if (k>=MEMLEN)
95                return 0; /* Fail Immediately */
96        state->mem[k] = state->X; 
97        return ~0;
98}
99
100OPCODE(bpf_alu_add_k) { state->A += k; return ~0;}
101OPCODE(bpf_alu_sub_k) { state->A -= k; return ~0;}
102OPCODE(bpf_alu_mul_k) { state->A *= k; return ~0;}
103OPCODE(bpf_alu_div_k) { state->A /= k; return ~0;}
104OPCODE(bpf_alu_and_k) { state->A &= k; return ~0;}
105OPCODE(bpf_alu_or_k)  { state->A |= k; return ~0;}
106OPCODE(bpf_alu_lsh_k) { state->A = state->A << k; return ~0;}
107OPCODE(bpf_alu_rsh_k) { state->A = state->A >> k; return ~0;}
108OPCODE(bpf_alu_neg) { state->A = -state->A; return ~0;}
109
110OPCODE(bpf_alu_add_x) { state->A += state->X; return ~0;}
111OPCODE(bpf_alu_sub_x) { state->A -= state->X; return ~0;}
112OPCODE(bpf_alu_mul_x) { state->A *= state->X; return ~0;}
113OPCODE(bpf_alu_div_x) { state->A /= state->X; return ~0;}
114OPCODE(bpf_alu_and_x) { state->A &= state->X; return ~0;}
115OPCODE(bpf_alu_or_x)  { state->A |= state->X; return ~0;}
116OPCODE(bpf_alu_lsh_x) { state->A = state->A << state->X; return ~0;}
117OPCODE(bpf_alu_rsh_x) { state->A = state->A >> state->X; return ~0;}
118
119/* These are created by code
120OPCODE(bpf_ja)    { state->pc += k; return ~0;}
121OPCODE(bpf_gt_k)  { state->pc += (state->A > k) ? jt : jf; return ~0;}
122OPCODE(bpf_ge_k)  { state->pc += (state->A >= k) ? jt : jf; return ~0;}
123OPCODE(bpf_eq_k)  { state->pc += (state->A == k) ? jt : jf; return ~0;}
124OPCODE(bpf_set_k) { state->pc += (state->A & k) ? jt : jf; return ~0;}
125OPCODE(bpf_gt_x)  { state->pc += (state->A > state->X) ? jt : jf; return ~0;}
126OPCODE(bpf_ge_x)  { state->pc += (state->A >= state->X) ? jt : jf; return ~0;}
127OPCODE(bpf_eq_x)  { state->pc += (state->A == state->X) ? jt : jf; return ~0;}
128OPCODE(bpf_set_x) { state->pc += (state->A & state->X) ? jt : jf; return ~0;}
129*/
130
131OPCODE(bpf_ret_a) { return state->A; }
132OPCODE(bpf_ret_k) { return k; }
133
134OPCODE(bpf_tax) { state->X = state->A; return ~0; }
135OPCODE(bpf_txa) { state->A = state->X; return ~0; }
136
Note: See TracBrowser for help on using the repository browser.