You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
5.9 KiB

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