xfrm.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. XfrmFwdHdrError int
  81. XfrmOutStateInvalid int
  82. XfrmAcquireError int
  83. }
  84. // NewXfrmStat reads the xfrm_stat statistics.
  85. func NewXfrmStat() (XfrmStat, error) {
  86. fs, err := NewFS(DefaultMountPoint)
  87. if err != nil {
  88. return XfrmStat{}, err
  89. }
  90. return fs.NewXfrmStat()
  91. }
  92. // NewXfrmStat reads the xfrm_stat statistics from the 'proc' filesystem.
  93. func (fs FS) NewXfrmStat() (XfrmStat, error) {
  94. file, err := os.Open(fs.proc.Path("net/xfrm_stat"))
  95. if err != nil {
  96. return XfrmStat{}, err
  97. }
  98. defer file.Close()
  99. var (
  100. x = XfrmStat{}
  101. s = bufio.NewScanner(file)
  102. )
  103. for s.Scan() {
  104. fields := strings.Fields(s.Text())
  105. if len(fields) != 2 {
  106. return XfrmStat{}, fmt.Errorf(
  107. "couldn't parse %s line %s", file.Name(), s.Text())
  108. }
  109. name := fields[0]
  110. value, err := strconv.Atoi(fields[1])
  111. if err != nil {
  112. return XfrmStat{}, err
  113. }
  114. switch name {
  115. case "XfrmInError":
  116. x.XfrmInError = value
  117. case "XfrmInBufferError":
  118. x.XfrmInBufferError = value
  119. case "XfrmInHdrError":
  120. x.XfrmInHdrError = value
  121. case "XfrmInNoStates":
  122. x.XfrmInNoStates = value
  123. case "XfrmInStateProtoError":
  124. x.XfrmInStateProtoError = value
  125. case "XfrmInStateModeError":
  126. x.XfrmInStateModeError = value
  127. case "XfrmInStateSeqError":
  128. x.XfrmInStateSeqError = value
  129. case "XfrmInStateExpired":
  130. x.XfrmInStateExpired = value
  131. case "XfrmInStateInvalid":
  132. x.XfrmInStateInvalid = value
  133. case "XfrmInTmplMismatch":
  134. x.XfrmInTmplMismatch = value
  135. case "XfrmInNoPols":
  136. x.XfrmInNoPols = value
  137. case "XfrmInPolBlock":
  138. x.XfrmInPolBlock = value
  139. case "XfrmInPolError":
  140. x.XfrmInPolError = value
  141. case "XfrmOutError":
  142. x.XfrmOutError = value
  143. case "XfrmInStateMismatch":
  144. x.XfrmInStateMismatch = value
  145. case "XfrmOutBundleGenError":
  146. x.XfrmOutBundleGenError = value
  147. case "XfrmOutBundleCheckError":
  148. x.XfrmOutBundleCheckError = value
  149. case "XfrmOutNoStates":
  150. x.XfrmOutNoStates = value
  151. case "XfrmOutStateProtoError":
  152. x.XfrmOutStateProtoError = value
  153. case "XfrmOutStateModeError":
  154. x.XfrmOutStateModeError = value
  155. case "XfrmOutStateSeqError":
  156. x.XfrmOutStateSeqError = value
  157. case "XfrmOutStateExpired":
  158. x.XfrmOutStateExpired = value
  159. case "XfrmOutPolBlock":
  160. x.XfrmOutPolBlock = value
  161. case "XfrmOutPolDead":
  162. x.XfrmOutPolDead = value
  163. case "XfrmOutPolError":
  164. x.XfrmOutPolError = value
  165. case "XfrmFwdHdrError":
  166. x.XfrmFwdHdrError = value
  167. case "XfrmOutStateInvalid":
  168. x.XfrmOutStateInvalid = value
  169. case "XfrmAcquireError":
  170. x.XfrmAcquireError = value
  171. }
  172. }
  173. return x, s.Err()
  174. }