constants.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 opClsMask uint16 = 0x7
  118. const (
  119. // +---------------+-----------------+---+---+---+
  120. // | AddrMode (3b) | LoadWidth (2b) | 0 | 0 | 0 |
  121. // +---------------+-----------------+---+---+---+
  122. opClsLoadA uint16 = iota
  123. // +---------------+-----------------+---+---+---+
  124. // | AddrMode (3b) | LoadWidth (2b) | 0 | 0 | 1 |
  125. // +---------------+-----------------+---+---+---+
  126. opClsLoadX
  127. // +---+---+---+---+---+---+---+---+
  128. // | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
  129. // +---+---+---+---+---+---+---+---+
  130. opClsStoreA
  131. // +---+---+---+---+---+---+---+---+
  132. // | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
  133. // +---+---+---+---+---+---+---+---+
  134. opClsStoreX
  135. // +---------------+-----------------+---+---+---+
  136. // | Operator (4b) | OperandSrc (1b) | 1 | 0 | 0 |
  137. // +---------------+-----------------+---+---+---+
  138. opClsALU
  139. // +-----------------------------+---+---+---+---+
  140. // | TestOperator (4b) | 0 | 1 | 0 | 1 |
  141. // +-----------------------------+---+---+---+---+
  142. opClsJump
  143. // +---+-------------------------+---+---+---+---+
  144. // | 0 | 0 | 0 | RetSrc (1b) | 0 | 1 | 1 | 0 |
  145. // +---+-------------------------+---+---+---+---+
  146. opClsReturn
  147. // +---+-------------------------+---+---+---+---+
  148. // | 0 | 0 | 0 | TXAorTAX (1b) | 0 | 1 | 1 | 1 |
  149. // +---+-------------------------+---+---+---+---+
  150. opClsMisc
  151. )
  152. const (
  153. opAddrModeImmediate uint16 = iota << 5
  154. opAddrModeAbsolute
  155. opAddrModeIndirect
  156. opAddrModeScratch
  157. // These are actually extensions, not addressing modes.
  158. opAddrModePacketLen
  159. opAddrModeIPv4HeaderLen
  160. )
  161. const (
  162. opLoadWidth4 uint16 = iota << 3
  163. opLoadWidth2
  164. opLoadWidth1
  165. )
  166. // Operator defined by ALUOp*
  167. const opALUOpMask = 0xf0
  168. const opALUSrcMask = 0x08
  169. const (
  170. opALUSrcConstant uint16 = iota << 3
  171. opALUSrcX
  172. )
  173. const (
  174. opJumpAlways = iota << 4
  175. opJumpEqual
  176. opJumpGT
  177. opJumpGE
  178. opJumpSet
  179. )
  180. const (
  181. opRetSrcConstant uint16 = iota << 4
  182. opRetSrcA
  183. )
  184. const (
  185. opMiscTAX = 0x00
  186. opMiscTXA = 0x80
  187. )