constants.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. // ExtLen returns the length of the packet.
  65. ExtLen Extension = 1
  66. // ExtProto returns the packet's L3 protocol type.
  67. ExtProto = 0
  68. // ExtType returns the packet's type (skb->pkt_type in the kernel)
  69. //
  70. // TODO: better documentation. How nice an API do we want to
  71. // provide for these esoteric extensions?
  72. ExtType = 4
  73. // ExtPayloadOffset returns the offset of the packet payload, or
  74. // the first protocol header that the kernel does not know how to
  75. // parse.
  76. ExtPayloadOffset = 52
  77. // ExtInterfaceIndex returns the index of the interface on which
  78. // the packet was received.
  79. ExtInterfaceIndex = 8
  80. // ExtNetlinkAttr returns the netlink attribute of type X at
  81. // offset A.
  82. ExtNetlinkAttr = 12
  83. // ExtNetlinkAttrNested returns the nested netlink attribute of
  84. // type X at offset A.
  85. ExtNetlinkAttrNested = 16
  86. // ExtMark returns the packet's mark value.
  87. ExtMark = 20
  88. // ExtQueue returns the packet's assigned hardware queue.
  89. ExtQueue = 24
  90. // ExtLinkLayerType returns the packet's hardware address type
  91. // (e.g. Ethernet, Infiniband).
  92. ExtLinkLayerType = 28
  93. // ExtRXHash returns the packets receive hash.
  94. //
  95. // TODO: figure out what this rxhash actually is.
  96. ExtRXHash = 32
  97. // ExtCPUID returns the ID of the CPU processing the current
  98. // packet.
  99. ExtCPUID = 36
  100. // ExtVLANTag returns the packet's VLAN tag.
  101. ExtVLANTag = 44
  102. // ExtVLANTagPresent returns non-zero if the packet has a VLAN
  103. // tag.
  104. //
  105. // TODO: I think this might be a lie: it reads bit 0x1000 of the
  106. // VLAN header, which changed meaning in recent revisions of the
  107. // spec - this extension may now return meaningless information.
  108. ExtVLANTagPresent = 48
  109. // ExtVLANProto returns 0x8100 if the frame has a VLAN header,
  110. // 0x88a8 if the frame has a "Q-in-Q" double VLAN header, or some
  111. // other value if no VLAN information is present.
  112. ExtVLANProto = 60
  113. // ExtRand returns a uniformly random uint32.
  114. ExtRand = 56
  115. )
  116. // The following gives names to various bit patterns used in opcode construction.
  117. const (
  118. opMaskCls uint16 = 0x7
  119. // opClsLoad masks
  120. opMaskLoadDest = 0x01
  121. opMaskLoadWidth = 0x18
  122. opMaskLoadMode = 0xe0
  123. // opClsALU
  124. opMaskOperandSrc = 0x08
  125. opMaskOperator = 0xf0
  126. // opClsJump
  127. opMaskJumpConst = 0x0f
  128. opMaskJumpCond = 0xf0
  129. )
  130. const (
  131. // +---------------+-----------------+---+---+---+
  132. // | AddrMode (3b) | LoadWidth (2b) | 0 | 0 | 0 |
  133. // +---------------+-----------------+---+---+---+
  134. opClsLoadA uint16 = iota
  135. // +---------------+-----------------+---+---+---+
  136. // | AddrMode (3b) | LoadWidth (2b) | 0 | 0 | 1 |
  137. // +---------------+-----------------+---+---+---+
  138. opClsLoadX
  139. // +---+---+---+---+---+---+---+---+
  140. // | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
  141. // +---+---+---+---+---+---+---+---+
  142. opClsStoreA
  143. // +---+---+---+---+---+---+---+---+
  144. // | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
  145. // +---+---+---+---+---+---+---+---+
  146. opClsStoreX
  147. // +---------------+-----------------+---+---+---+
  148. // | Operator (4b) | OperandSrc (1b) | 1 | 0 | 0 |
  149. // +---------------+-----------------+---+---+---+
  150. opClsALU
  151. // +-----------------------------+---+---+---+---+
  152. // | TestOperator (4b) | 0 | 1 | 0 | 1 |
  153. // +-----------------------------+---+---+---+---+
  154. opClsJump
  155. // +---+-------------------------+---+---+---+---+
  156. // | 0 | 0 | 0 | RetSrc (1b) | 0 | 1 | 1 | 0 |
  157. // +---+-------------------------+---+---+---+---+
  158. opClsReturn
  159. // +---+-------------------------+---+---+---+---+
  160. // | 0 | 0 | 0 | TXAorTAX (1b) | 0 | 1 | 1 | 1 |
  161. // +---+-------------------------+---+---+---+---+
  162. opClsMisc
  163. )
  164. const (
  165. opAddrModeImmediate uint16 = iota << 5
  166. opAddrModeAbsolute
  167. opAddrModeIndirect
  168. opAddrModeScratch
  169. opAddrModePacketLen // actually an extension, not an addressing mode.
  170. opAddrModeMemShift
  171. )
  172. const (
  173. opLoadWidth4 uint16 = iota << 3
  174. opLoadWidth2
  175. opLoadWidth1
  176. )
  177. // Operator defined by ALUOp*
  178. const (
  179. opALUSrcConstant uint16 = iota << 3
  180. opALUSrcX
  181. )
  182. const (
  183. opJumpAlways = iota << 4
  184. opJumpEqual
  185. opJumpGT
  186. opJumpGE
  187. opJumpSet
  188. )
  189. const (
  190. opRetSrcConstant uint16 = iota << 4
  191. opRetSrcA
  192. )
  193. const (
  194. opMiscTAX = 0x00
  195. opMiscTXA = 0x80
  196. )