net_xfrm.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2017 Prometheus Team
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package procfs
  14. import (
  15. "bufio"
  16. "fmt"
  17. "os"
  18. "strconv"
  19. "strings"
  20. )
  21. // XfrmStat models the contents of /proc/net/xfrm_stat.
  22. type XfrmStat struct {
  23. // All errors which are not matched by other
  24. XfrmInError int
  25. // No buffer is left
  26. XfrmInBufferError int
  27. // Header Error
  28. XfrmInHdrError int
  29. // No state found
  30. // i.e. either inbound SPI, address, or IPSEC protocol at SA is wrong
  31. XfrmInNoStates int
  32. // Transformation protocol specific error
  33. // e.g. SA Key is wrong
  34. XfrmInStateProtoError int
  35. // Transformation mode specific error
  36. XfrmInStateModeError int
  37. // Sequence error
  38. // e.g. sequence number is out of window
  39. XfrmInStateSeqError int
  40. // State is expired
  41. XfrmInStateExpired int
  42. // State has mismatch option
  43. // e.g. UDP encapsulation type is mismatched
  44. XfrmInStateMismatch int
  45. // State is invalid
  46. XfrmInStateInvalid int
  47. // No matching template for states
  48. // e.g. Inbound SAs are correct but SP rule is wrong
  49. XfrmInTmplMismatch int
  50. // No policy is found for states
  51. // e.g. Inbound SAs are correct but no SP is found
  52. XfrmInNoPols int
  53. // Policy discards
  54. XfrmInPolBlock int
  55. // Policy error
  56. XfrmInPolError int
  57. // All errors which are not matched by others
  58. XfrmOutError int
  59. // Bundle generation error
  60. XfrmOutBundleGenError int
  61. // Bundle check error
  62. XfrmOutBundleCheckError int
  63. // No state was found
  64. XfrmOutNoStates int
  65. // Transformation protocol specific error
  66. XfrmOutStateProtoError int
  67. // Transportation mode specific error
  68. XfrmOutStateModeError int
  69. // Sequence error
  70. // i.e sequence number overflow
  71. XfrmOutStateSeqError int
  72. // State is expired
  73. XfrmOutStateExpired int
  74. // Policy discads
  75. XfrmOutPolBlock int
  76. // Policy is dead
  77. XfrmOutPolDead int
  78. // Policy Error
  79. XfrmOutPolError int
  80. // Forward routing of a packet is not allowed
  81. XfrmFwdHdrError int
  82. // State is invalid, perhaps expired
  83. XfrmOutStateInvalid int
  84. // State hasn’t been fully acquired before use
  85. XfrmAcquireError int
  86. }
  87. // NewXfrmStat reads the xfrm_stat statistics.
  88. func NewXfrmStat() (XfrmStat, error) {
  89. fs, err := NewFS(DefaultMountPoint)
  90. if err != nil {
  91. return XfrmStat{}, err
  92. }
  93. return fs.NewXfrmStat()
  94. }
  95. // NewXfrmStat reads the xfrm_stat statistics from the 'proc' filesystem.
  96. func (fs FS) NewXfrmStat() (XfrmStat, error) {
  97. file, err := os.Open(fs.proc.Path("net/xfrm_stat"))
  98. if err != nil {
  99. return XfrmStat{}, err
  100. }
  101. defer file.Close()
  102. var (
  103. x = XfrmStat{}
  104. s = bufio.NewScanner(file)
  105. )
  106. for s.Scan() {
  107. fields := strings.Fields(s.Text())
  108. if len(fields) != 2 {
  109. return XfrmStat{}, fmt.Errorf("%w: %q line %q", ErrFileParse, file.Name(), s.Text())
  110. }
  111. name := fields[0]
  112. value, err := strconv.Atoi(fields[1])
  113. if err != nil {
  114. return XfrmStat{}, err
  115. }
  116. switch name {
  117. case "XfrmInError":
  118. x.XfrmInError = value
  119. case "XfrmInBufferError":
  120. x.XfrmInBufferError = value
  121. case "XfrmInHdrError":
  122. x.XfrmInHdrError = value
  123. case "XfrmInNoStates":
  124. x.XfrmInNoStates = value
  125. case "XfrmInStateProtoError":
  126. x.XfrmInStateProtoError = value
  127. case "XfrmInStateModeError":
  128. x.XfrmInStateModeError = value
  129. case "XfrmInStateSeqError":
  130. x.XfrmInStateSeqError = value
  131. case "XfrmInStateExpired":
  132. x.XfrmInStateExpired = value
  133. case "XfrmInStateInvalid":
  134. x.XfrmInStateInvalid = value
  135. case "XfrmInTmplMismatch":
  136. x.XfrmInTmplMismatch = value
  137. case "XfrmInNoPols":
  138. x.XfrmInNoPols = value
  139. case "XfrmInPolBlock":
  140. x.XfrmInPolBlock = value
  141. case "XfrmInPolError":
  142. x.XfrmInPolError = value
  143. case "XfrmOutError":
  144. x.XfrmOutError = value
  145. case "XfrmInStateMismatch":
  146. x.XfrmInStateMismatch = value
  147. case "XfrmOutBundleGenError":
  148. x.XfrmOutBundleGenError = value
  149. case "XfrmOutBundleCheckError":
  150. x.XfrmOutBundleCheckError = value
  151. case "XfrmOutNoStates":
  152. x.XfrmOutNoStates = value
  153. case "XfrmOutStateProtoError":
  154. x.XfrmOutStateProtoError = value
  155. case "XfrmOutStateModeError":
  156. x.XfrmOutStateModeError = value
  157. case "XfrmOutStateSeqError":
  158. x.XfrmOutStateSeqError = value
  159. case "XfrmOutStateExpired":
  160. x.XfrmOutStateExpired = value
  161. case "XfrmOutPolBlock":
  162. x.XfrmOutPolBlock = value
  163. case "XfrmOutPolDead":
  164. x.XfrmOutPolDead = value
  165. case "XfrmOutPolError":
  166. x.XfrmOutPolError = value
  167. case "XfrmFwdHdrError":
  168. x.XfrmFwdHdrError = value
  169. case "XfrmOutStateInvalid":
  170. x.XfrmOutStateInvalid = value
  171. case "XfrmAcquireError":
  172. x.XfrmAcquireError = value
  173. }
  174. }
  175. return x, s.Err()
  176. }