Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

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