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.
7 // A Register is a register of the BPF virtual machine.
11 // RegA is the accumulator register. RegA is always the
12 // destination register of ALU operations.
14 // RegX is the indirection register, used by LoadIndirect
19 // An ALUOp is an arithmetic or logic operation.
22 // ALU binary operation types.
24 ALUOpAdd ALUOp = iota << 4
32 aluOpNeg // Not exported because it's the only unary ALU operation, and gets its own instruction type.
37 // A JumpTest is a comparison operator used in conditional jumps.
40 // Supported operators for conditional jumps.
41 // K can be RegX for JumpIfX
44 JumpEqual JumpTest = iota
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.
65 // Extensions are only implemented by the Linux kernel.
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.
72 // Extension functions available in the Linux kernel.
74 // extOffset is the negative maximum number of instructions used
75 // to load instructions by overloading the K argument.
77 // ExtLen returns the length of the packet.
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)
83 // TODO: better documentation. How nice an API do we want to
84 // provide for these esoteric extensions?
86 // ExtPayloadOffset returns the offset of the packet payload, or
87 // the first protocol header that the kernel does not know how to
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
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.
108 // TODO: figure out what this rxhash actually is.
109 ExtRXHash Extension = 32
110 // ExtCPUID returns the ID of the CPU processing the current
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
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
130 // The following gives names to various bit patterns used in opcode construction.
133 opMaskCls uint16 = 0x7
135 opMaskLoadDest = 0x01
136 opMaskLoadWidth = 0x18
137 opMaskLoadMode = 0xe0
138 // opClsALU & opClsJump
140 opMaskOperator = 0xf0
144 // +---------------+-----------------+---+---+---+
145 // | AddrMode (3b) | LoadWidth (2b) | 0 | 0 | 0 |
146 // +---------------+-----------------+---+---+---+
147 opClsLoadA uint16 = iota
148 // +---------------+-----------------+---+---+---+
149 // | AddrMode (3b) | LoadWidth (2b) | 0 | 0 | 1 |
150 // +---------------+-----------------+---+---+---+
152 // +---+---+---+---+---+---+---+---+
153 // | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
154 // +---+---+---+---+---+---+---+---+
156 // +---+---+---+---+---+---+---+---+
157 // | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
158 // +---+---+---+---+---+---+---+---+
160 // +---------------+-----------------+---+---+---+
161 // | Operator (4b) | OperandSrc (1b) | 1 | 0 | 0 |
162 // +---------------+-----------------+---+---+---+
164 // +-----------------------------+---+---+---+---+
165 // | TestOperator (4b) | 0 | 1 | 0 | 1 |
166 // +-----------------------------+---+---+---+---+
168 // +---+-------------------------+---+---+---+---+
169 // | 0 | 0 | 0 | RetSrc (1b) | 0 | 1 | 1 | 0 |
170 // +---+-------------------------+---+---+---+---+
172 // +---+-------------------------+---+---+---+---+
173 // | 0 | 0 | 0 | TXAorTAX (1b) | 0 | 1 | 1 | 1 |
174 // +---+-------------------------+---+---+---+---+
179 opAddrModeImmediate uint16 = iota << 5
183 opAddrModePacketLen // actually an extension, not an addressing mode.
188 opLoadWidth4 uint16 = iota << 3
193 // Operand for ALU and Jump instructions
194 type opOperand uint16
196 // Supported operand sources.
198 opOperandConstant opOperand = iota << 3
202 // An jumpOp is a conditional jump condition.
205 // Supported jump conditions.
207 opJumpAlways jumpOp = iota << 4
215 opRetSrcConstant uint16 = iota << 4