David K. Bainbridge | 215e024 | 2017-09-05 23:18:24 -0700 | [diff] [blame] | 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 | const ( |
| 42 | // K == A |
| 43 | JumpEqual JumpTest = iota |
| 44 | // K != A |
| 45 | JumpNotEqual |
| 46 | // K > A |
| 47 | JumpGreaterThan |
| 48 | // K < A |
| 49 | JumpLessThan |
| 50 | // K >= A |
| 51 | JumpGreaterOrEqual |
| 52 | // K <= A |
| 53 | JumpLessOrEqual |
| 54 | // K & A != 0 |
| 55 | JumpBitsSet |
| 56 | // K & A == 0 |
| 57 | JumpBitsNotSet |
| 58 | ) |
| 59 | |
| 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. |
| 63 | // |
| 64 | // Extensions are only implemented by the Linux kernel. |
| 65 | // |
| 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. |
| 69 | type Extension int |
| 70 | |
| 71 | // Extension functions available in the Linux kernel. |
| 72 | const ( |
| 73 | // extOffset is the negative maximum number of instructions used |
| 74 | // to load instructions by overloading the K argument. |
| 75 | extOffset = -0x1000 |
| 76 | // ExtLen returns the length of the packet. |
| 77 | ExtLen Extension = 1 |
| 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) |
| 81 | // |
| 82 | // TODO: better documentation. How nice an API do we want to |
| 83 | // provide for these esoteric extensions? |
| 84 | ExtType Extension = 4 |
| 85 | // ExtPayloadOffset returns the offset of the packet payload, or |
| 86 | // the first protocol header that the kernel does not know how to |
| 87 | // parse. |
| 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 |
| 93 | // offset A. |
| 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. |
| 106 | // |
| 107 | // TODO: figure out what this rxhash actually is. |
| 108 | ExtRXHash Extension = 32 |
| 109 | // ExtCPUID returns the ID of the CPU processing the current |
| 110 | // packet. |
| 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 |
| 115 | // tag. |
| 116 | // |
| 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 |
| 127 | ) |
| 128 | |
| 129 | // The following gives names to various bit patterns used in opcode construction. |
| 130 | |
| 131 | const ( |
| 132 | opMaskCls uint16 = 0x7 |
| 133 | // opClsLoad masks |
| 134 | opMaskLoadDest = 0x01 |
| 135 | opMaskLoadWidth = 0x18 |
| 136 | opMaskLoadMode = 0xe0 |
| 137 | // opClsALU |
| 138 | opMaskOperandSrc = 0x08 |
| 139 | opMaskOperator = 0xf0 |
| 140 | // opClsJump |
| 141 | opMaskJumpConst = 0x0f |
| 142 | opMaskJumpCond = 0xf0 |
| 143 | ) |
| 144 | |
| 145 | const ( |
| 146 | // +---------------+-----------------+---+---+---+ |
| 147 | // | AddrMode (3b) | LoadWidth (2b) | 0 | 0 | 0 | |
| 148 | // +---------------+-----------------+---+---+---+ |
| 149 | opClsLoadA uint16 = iota |
| 150 | // +---------------+-----------------+---+---+---+ |
| 151 | // | AddrMode (3b) | LoadWidth (2b) | 0 | 0 | 1 | |
| 152 | // +---------------+-----------------+---+---+---+ |
| 153 | opClsLoadX |
| 154 | // +---+---+---+---+---+---+---+---+ |
| 155 | // | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | |
| 156 | // +---+---+---+---+---+---+---+---+ |
| 157 | opClsStoreA |
| 158 | // +---+---+---+---+---+---+---+---+ |
| 159 | // | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | |
| 160 | // +---+---+---+---+---+---+---+---+ |
| 161 | opClsStoreX |
| 162 | // +---------------+-----------------+---+---+---+ |
| 163 | // | Operator (4b) | OperandSrc (1b) | 1 | 0 | 0 | |
| 164 | // +---------------+-----------------+---+---+---+ |
| 165 | opClsALU |
| 166 | // +-----------------------------+---+---+---+---+ |
| 167 | // | TestOperator (4b) | 0 | 1 | 0 | 1 | |
| 168 | // +-----------------------------+---+---+---+---+ |
| 169 | opClsJump |
| 170 | // +---+-------------------------+---+---+---+---+ |
| 171 | // | 0 | 0 | 0 | RetSrc (1b) | 0 | 1 | 1 | 0 | |
| 172 | // +---+-------------------------+---+---+---+---+ |
| 173 | opClsReturn |
| 174 | // +---+-------------------------+---+---+---+---+ |
| 175 | // | 0 | 0 | 0 | TXAorTAX (1b) | 0 | 1 | 1 | 1 | |
| 176 | // +---+-------------------------+---+---+---+---+ |
| 177 | opClsMisc |
| 178 | ) |
| 179 | |
| 180 | const ( |
| 181 | opAddrModeImmediate uint16 = iota << 5 |
| 182 | opAddrModeAbsolute |
| 183 | opAddrModeIndirect |
| 184 | opAddrModeScratch |
| 185 | opAddrModePacketLen // actually an extension, not an addressing mode. |
| 186 | opAddrModeMemShift |
| 187 | ) |
| 188 | |
| 189 | const ( |
| 190 | opLoadWidth4 uint16 = iota << 3 |
| 191 | opLoadWidth2 |
| 192 | opLoadWidth1 |
| 193 | ) |
| 194 | |
| 195 | // Operator defined by ALUOp* |
| 196 | |
| 197 | const ( |
| 198 | opALUSrcConstant uint16 = iota << 3 |
| 199 | opALUSrcX |
| 200 | ) |
| 201 | |
| 202 | const ( |
| 203 | opJumpAlways = iota << 4 |
| 204 | opJumpEqual |
| 205 | opJumpGT |
| 206 | opJumpGE |
| 207 | opJumpSet |
| 208 | ) |
| 209 | |
| 210 | const ( |
| 211 | opRetSrcConstant uint16 = iota << 4 |
| 212 | opRetSrcA |
| 213 | ) |
| 214 | |
| 215 | const ( |
| 216 | opMiscTAX = 0x00 |
| 217 | opMiscTXA = 0x80 |
| 218 | ) |