You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
5.4 KiB

  1. // Copyright 2020 The Prometheus Authors
  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. "bytes"
  17. "fmt"
  18. "strconv"
  19. "strings"
  20. "github.com/prometheus/procfs/internal/util"
  21. )
  22. // NetProtocolStats stores the contents from /proc/net/protocols.
  23. type NetProtocolStats map[string]NetProtocolStatLine
  24. // NetProtocolStatLine contains a single line parsed from /proc/net/protocols. We
  25. // only care about the first six columns as the rest are not likely to change
  26. // and only serve to provide a set of capabilities for each protocol.
  27. type NetProtocolStatLine struct {
  28. Name string // 0 The name of the protocol
  29. Size uint64 // 1 The size, in bytes, of a given protocol structure. e.g. sizeof(struct tcp_sock) or sizeof(struct unix_sock)
  30. Sockets int64 // 2 Number of sockets in use by this protocol
  31. Memory int64 // 3 Number of 4KB pages allocated by all sockets of this protocol
  32. Pressure int // 4 This is either yes, no, or NI (not implemented). For the sake of simplicity we treat NI as not experiencing memory pressure.
  33. MaxHeader uint64 // 5 Protocol specific max header size
  34. Slab bool // 6 Indicates whether or not memory is allocated from the SLAB
  35. ModuleName string // 7 The name of the module that implemented this protocol or "kernel" if not from a module
  36. Capabilities NetProtocolCapabilities
  37. }
  38. // NetProtocolCapabilities contains a list of capabilities for each protocol.
  39. type NetProtocolCapabilities struct {
  40. Close bool // 8
  41. Connect bool // 9
  42. Disconnect bool // 10
  43. Accept bool // 11
  44. IoCtl bool // 12
  45. Init bool // 13
  46. Destroy bool // 14
  47. Shutdown bool // 15
  48. SetSockOpt bool // 16
  49. GetSockOpt bool // 17
  50. SendMsg bool // 18
  51. RecvMsg bool // 19
  52. SendPage bool // 20
  53. Bind bool // 21
  54. BacklogRcv bool // 22
  55. Hash bool // 23
  56. UnHash bool // 24
  57. GetPort bool // 25
  58. EnterMemoryPressure bool // 26
  59. }
  60. // NetProtocols reads stats from /proc/net/protocols and returns a map of
  61. // PortocolStatLine entries. As of this writing no official Linux Documentation
  62. // exists, however the source is fairly self-explanatory and the format seems
  63. // stable since its introduction in 2.6.12-rc2
  64. // Linux 2.6.12-rc2 - https://elixir.bootlin.com/linux/v2.6.12-rc2/source/net/core/sock.c#L1452
  65. // Linux 5.10 - https://elixir.bootlin.com/linux/v5.10.4/source/net/core/sock.c#L3586
  66. func (fs FS) NetProtocols() (NetProtocolStats, error) {
  67. data, err := util.ReadFileNoStat(fs.proc.Path("net/protocols"))
  68. if err != nil {
  69. return NetProtocolStats{}, err
  70. }
  71. return parseNetProtocols(bufio.NewScanner(bytes.NewReader(data)))
  72. }
  73. func parseNetProtocols(s *bufio.Scanner) (NetProtocolStats, error) {
  74. nps := NetProtocolStats{}
  75. // Skip the header line
  76. s.Scan()
  77. for s.Scan() {
  78. line, err := nps.parseLine(s.Text())
  79. if err != nil {
  80. return NetProtocolStats{}, err
  81. }
  82. nps[line.Name] = *line
  83. }
  84. return nps, nil
  85. }
  86. func (ps NetProtocolStats) parseLine(rawLine string) (*NetProtocolStatLine, error) {
  87. line := &NetProtocolStatLine{Capabilities: NetProtocolCapabilities{}}
  88. var err error
  89. const enabled = "yes"
  90. const disabled = "no"
  91. fields := strings.Fields(rawLine)
  92. line.Name = fields[0]
  93. line.Size, err = strconv.ParseUint(fields[1], 10, 64)
  94. if err != nil {
  95. return nil, err
  96. }
  97. line.Sockets, err = strconv.ParseInt(fields[2], 10, 64)
  98. if err != nil {
  99. return nil, err
  100. }
  101. line.Memory, err = strconv.ParseInt(fields[3], 10, 64)
  102. if err != nil {
  103. return nil, err
  104. }
  105. if fields[4] == enabled {
  106. line.Pressure = 1
  107. } else if fields[4] == disabled {
  108. line.Pressure = 0
  109. } else {
  110. line.Pressure = -1
  111. }
  112. line.MaxHeader, err = strconv.ParseUint(fields[5], 10, 64)
  113. if err != nil {
  114. return nil, err
  115. }
  116. if fields[6] == enabled {
  117. line.Slab = true
  118. } else if fields[6] == disabled {
  119. line.Slab = false
  120. } else {
  121. return nil, fmt.Errorf("unable to parse capability for protocol: %s", line.Name)
  122. }
  123. line.ModuleName = fields[7]
  124. err = line.Capabilities.parseCapabilities(fields[8:])
  125. if err != nil {
  126. return nil, err
  127. }
  128. return line, nil
  129. }
  130. func (pc *NetProtocolCapabilities) parseCapabilities(capabilities []string) error {
  131. // The capabilities are all bools so we can loop over to map them
  132. capabilityFields := [...]*bool{
  133. &pc.Close,
  134. &pc.Connect,
  135. &pc.Disconnect,
  136. &pc.Accept,
  137. &pc.IoCtl,
  138. &pc.Init,
  139. &pc.Destroy,
  140. &pc.Shutdown,
  141. &pc.SetSockOpt,
  142. &pc.GetSockOpt,
  143. &pc.SendMsg,
  144. &pc.RecvMsg,
  145. &pc.SendPage,
  146. &pc.Bind,
  147. &pc.BacklogRcv,
  148. &pc.Hash,
  149. &pc.UnHash,
  150. &pc.GetPort,
  151. &pc.EnterMemoryPressure,
  152. }
  153. for i := 0; i < len(capabilities); i++ {
  154. if capabilities[i] == "y" {
  155. *capabilityFields[i] = true
  156. } else if capabilities[i] == "n" {
  157. *capabilityFields[i] = false
  158. } else {
  159. return fmt.Errorf("unable to parse capability block for protocol: position %d", i)
  160. }
  161. }
  162. return nil
  163. }