1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4 5 package bpf
6 7 // A Register is a register of the BPF virtual machine.
8 type Register uint16
9 10 const (
11 // RegA is the accumulator register. RegA is always the
12 // destination register of ALU operations.
13 RegA Register = iota
14 // RegX is the indirection register, used by LoadIndirect
15 // operations.
16 RegX
17 )
18 19 // An ALUOp is an arithmetic or logic operation.
20 type ALUOp uint16
21 22 // ALU binary operation types.
23 const (
24 ALUOpAdd ALUOp = iota << 4
25 ALUOpSub
26 ALUOpMul
27 ALUOpDiv
28 ALUOpOr
29 ALUOpAnd
30 ALUOpShiftLeft
31 ALUOpShiftRight
32 aluOpNeg // Not exported because it's the only unary ALU operation, and gets its own instruction type.
33 ALUOpMod
34 ALUOpXor
35 )
36 37 // A JumpTest is a comparison operator used in conditional jumps.
38 type JumpTest uint16
39 40 // Supported operators for conditional jumps.
41 // K can be RegX for JumpIfX
42 const (
43 // K == A
44 JumpEqual JumpTest = iota
45 // K != A
46 JumpNotEqual
47 // K > A
48 JumpGreaterThan
49 // K < A
50 JumpLessThan
51 // K >= A
52 JumpGreaterOrEqual
53 // K <= A
54 JumpLessOrEqual
55 // K & A != 0
56 JumpBitsSet
57 // K & A == 0
58 JumpBitsNotSet
59 )
60 61 // An Extension is a function call provided by the kernel that
62 // performs advanced operations that are expensive or impossible
63 // within the BPF virtual machine.
64 //
65 // Extensions are only implemented by the Linux kernel.
66 //
67 // TODO: should we prune this list? Some of these extensions seem
68 // either broken or near-impossible to use correctly, whereas other
69 // (len, random, ifindex) are quite useful.
70 type Extension int
71 72 // Extension functions available in the Linux kernel.
73 const (
74 // extOffset is the negative maximum number of instructions used
75 // to load instructions by overloading the K argument.
76 extOffset = -0x1000
77 // ExtLen returns the length of the packet.
78 ExtLen Extension = 1
79 // ExtProto returns the packet's L3 protocol type.
80 ExtProto Extension = 0
81 // ExtType returns the packet's type (skb->pkt_type in the kernel)
82 //
83 // TODO: better documentation. How nice an API do we want to
84 // provide for these esoteric extensions?
85 ExtType Extension = 4
86 // ExtPayloadOffset returns the offset of the packet payload, or
87 // the first protocol header that the kernel does not know how to
88 // parse.
89 ExtPayloadOffset Extension = 52
90 // ExtInterfaceIndex returns the index of the interface on which
91 // the packet was received.
92 ExtInterfaceIndex Extension = 8
93 // ExtNetlinkAttr returns the netlink attribute of type X at
94 // offset A.
95 ExtNetlinkAttr Extension = 12
96 // ExtNetlinkAttrNested returns the nested netlink attribute of
97 // type X at offset A.
98 ExtNetlinkAttrNested Extension = 16
99 // ExtMark returns the packet's mark value.
100 ExtMark Extension = 20
101 // ExtQueue returns the packet's assigned hardware queue.
102 ExtQueue Extension = 24
103 // ExtLinkLayerType returns the packet's hardware address type
104 // (e.g. Ethernet, Infiniband).
105 ExtLinkLayerType Extension = 28
106 // ExtRXHash returns the packets receive hash.
107 //
108 // TODO: figure out what this rxhash actually is.
109 ExtRXHash Extension = 32
110 // ExtCPUID returns the ID of the CPU processing the current
111 // packet.
112 ExtCPUID Extension = 36
113 // ExtVLANTag returns the packet's VLAN tag.
114 ExtVLANTag Extension = 44
115 // ExtVLANTagPresent returns non-zero if the packet has a VLAN
116 // tag.
117 //
118 // TODO: I think this might be a lie: it reads bit 0x1000 of the
119 // VLAN header, which changed meaning in recent revisions of the
120 // spec - this extension may now return meaningless information.
121 ExtVLANTagPresent Extension = 48
122 // ExtVLANProto returns 0x8100 if the frame has a VLAN header,
123 // 0x88a8 if the frame has a "Q-in-Q" double VLAN header, or some
124 // other value if no VLAN information is present.
125 ExtVLANProto Extension = 60
126 // ExtRand returns a uniformly random uint32.
127 ExtRand Extension = 56
128 )
129 130 // The following gives names to various bit patterns used in opcode construction.
131 132 const (
133 opMaskCls uint16 = 0x7
134 // opClsLoad masks
135 opMaskLoadDest = 0x01
136 opMaskLoadWidth = 0x18
137 opMaskLoadMode = 0xe0
138 // opClsALU & opClsJump
139 opMaskOperand = 0x08
140 opMaskOperator = 0xf0
141 )
142 143 const (
144 // +---------------+-----------------+---+---+---+
145 // | AddrMode (3b) | LoadWidth (2b) | 0 | 0 | 0 |
146 // +---------------+-----------------+---+---+---+
147 opClsLoadA uint16 = iota
148 // +---------------+-----------------+---+---+---+
149 // | AddrMode (3b) | LoadWidth (2b) | 0 | 0 | 1 |
150 // +---------------+-----------------+---+---+---+
151 opClsLoadX
152 // +---+---+---+---+---+---+---+---+
153 // | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
154 // +---+---+---+---+---+---+---+---+
155 opClsStoreA
156 // +---+---+---+---+---+---+---+---+
157 // | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
158 // +---+---+---+---+---+---+---+---+
159 opClsStoreX
160 // +---------------+-----------------+---+---+---+
161 // | Operator (4b) | OperandSrc (1b) | 1 | 0 | 0 |
162 // +---------------+-----------------+---+---+---+
163 opClsALU
164 // +-----------------------------+---+---+---+---+
165 // | TestOperator (4b) | 0 | 1 | 0 | 1 |
166 // +-----------------------------+---+---+---+---+
167 opClsJump
168 // +---+-------------------------+---+---+---+---+
169 // | 0 | 0 | 0 | RetSrc (1b) | 0 | 1 | 1 | 0 |
170 // +---+-------------------------+---+---+---+---+
171 opClsReturn
172 // +---+-------------------------+---+---+---+---+
173 // | 0 | 0 | 0 | TXAorTAX (1b) | 0 | 1 | 1 | 1 |
174 // +---+-------------------------+---+---+---+---+
175 opClsMisc
176 )
177 178 const (
179 opAddrModeImmediate uint16 = iota << 5
180 opAddrModeAbsolute
181 opAddrModeIndirect
182 opAddrModeScratch
183 opAddrModePacketLen // actually an extension, not an addressing mode.
184 opAddrModeMemShift
185 )
186 187 const (
188 opLoadWidth4 uint16 = iota << 3
189 opLoadWidth2
190 opLoadWidth1
191 )
192 193 // Operand for ALU and Jump instructions
194 type opOperand uint16
195 196 // Supported operand sources.
197 const (
198 opOperandConstant opOperand = iota << 3
199 opOperandX
200 )
201 202 // An jumpOp is a conditional jump condition.
203 type jumpOp uint16
204 205 // Supported jump conditions.
206 const (
207 opJumpAlways jumpOp = iota << 4
208 opJumpEqual
209 opJumpGT
210 opJumpGE
211 opJumpSet
212 )
213 214 const (
215 opRetSrcConstant uint16 = iota << 4
216 opRetSrcA
217 )
218 219 const (
220 opMiscTAX = 0x00
221 opMiscTXA = 0x80
222 )
223