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.
43 JumpEqual JumpTest = iota
60 // An Extension is a function call provided by the kernel that
61 // performs advanced operations that are expensive or impossible
62 // within the BPF virtual machine.
64 // Extensions are only implemented by the Linux kernel.
66 // TODO: should we prune this list? Some of these extensions seem
67 // either broken or near-impossible to use correctly, whereas other
68 // (len, random, ifindex) are quite useful.
71 // Extension functions available in the Linux kernel.
73 // extOffset is the negative maximum number of instructions used
74 // to load instructions by overloading the K argument.
76 // ExtLen returns the length of the packet.
78 // ExtProto returns the packet's L3 protocol type.
79 ExtProto Extension = 0
80 // ExtType returns the packet's type (skb->pkt_type in the kernel)
82 // TODO: better documentation. How nice an API do we want to
83 // provide for these esoteric extensions?
85 // ExtPayloadOffset returns the offset of the packet payload, or
86 // the first protocol header that the kernel does not know how to
88 ExtPayloadOffset Extension = 52
89 // ExtInterfaceIndex returns the index of the interface on which
90 // the packet was received.
91 ExtInterfaceIndex Extension = 8
92 // ExtNetlinkAttr returns the netlink attribute of type X at
94 ExtNetlinkAttr Extension = 12
95 // ExtNetlinkAttrNested returns the nested netlink attribute of
96 // type X at offset A.
97 ExtNetlinkAttrNested Extension = 16
98 // ExtMark returns the packet's mark value.
99 ExtMark Extension = 20
100 // ExtQueue returns the packet's assigned hardware queue.
101 ExtQueue Extension = 24
102 // ExtLinkLayerType returns the packet's hardware address type
103 // (e.g. Ethernet, Infiniband).
104 ExtLinkLayerType Extension = 28
105 // ExtRXHash returns the packets receive hash.
107 // TODO: figure out what this rxhash actually is.
108 ExtRXHash Extension = 32
109 // ExtCPUID returns the ID of the CPU processing the current
111 ExtCPUID Extension = 36
112 // ExtVLANTag returns the packet's VLAN tag.
113 ExtVLANTag Extension = 44
114 // ExtVLANTagPresent returns non-zero if the packet has a VLAN
117 // TODO: I think this might be a lie: it reads bit 0x1000 of the
118 // VLAN header, which changed meaning in recent revisions of the
119 // spec - this extension may now return meaningless information.
120 ExtVLANTagPresent Extension = 48
121 // ExtVLANProto returns 0x8100 if the frame has a VLAN header,
122 // 0x88a8 if the frame has a "Q-in-Q" double VLAN header, or some
123 // other value if no VLAN information is present.
124 ExtVLANProto Extension = 60
125 // ExtRand returns a uniformly random uint32.
126 ExtRand Extension = 56
129 // The following gives names to various bit patterns used in opcode construction.
132 opMaskCls uint16 = 0x7
134 opMaskLoadDest = 0x01
135 opMaskLoadWidth = 0x18
136 opMaskLoadMode = 0xe0
138 opMaskOperandSrc = 0x08
139 opMaskOperator = 0xf0
141 opMaskJumpConst = 0x0f
142 opMaskJumpCond = 0xf0
146 // +---------------+-----------------+---+---+---+
147 // | AddrMode (3b) | LoadWidth (2b) | 0 | 0 | 0 |
148 // +---------------+-----------------+---+---+---+
149 opClsLoadA uint16 = iota
150 // +---------------+-----------------+---+---+---+
151 // | AddrMode (3b) | LoadWidth (2b) | 0 | 0 | 1 |
152 // +---------------+-----------------+---+---+---+
154 // +---+---+---+---+---+---+---+---+
155 // | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
156 // +---+---+---+---+---+---+---+---+
158 // +---+---+---+---+---+---+---+---+
159 // | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
160 // +---+---+---+---+---+---+---+---+
162 // +---------------+-----------------+---+---+---+
163 // | Operator (4b) | OperandSrc (1b) | 1 | 0 | 0 |
164 // +---------------+-----------------+---+---+---+
166 // +-----------------------------+---+---+---+---+
167 // | TestOperator (4b) | 0 | 1 | 0 | 1 |
168 // +-----------------------------+---+---+---+---+
170 // +---+-------------------------+---+---+---+---+
171 // | 0 | 0 | 0 | RetSrc (1b) | 0 | 1 | 1 | 0 |
172 // +---+-------------------------+---+---+---+---+
174 // +---+-------------------------+---+---+---+---+
175 // | 0 | 0 | 0 | TXAorTAX (1b) | 0 | 1 | 1 | 1 |
176 // +---+-------------------------+---+---+---+---+
181 opAddrModeImmediate uint16 = iota << 5
185 opAddrModePacketLen // actually an extension, not an addressing mode.
190 opLoadWidth4 uint16 = iota << 3
195 // Operator defined by ALUOp*
198 opALUSrcConstant uint16 = iota << 3
203 opJumpAlways = iota << 4
211 opRetSrcConstant uint16 = iota << 4