net_softnet.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2019 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. "io"
  19. "strconv"
  20. "strings"
  21. "github.com/prometheus/procfs/internal/util"
  22. )
  23. // For the proc file format details,
  24. // See:
  25. // * Linux 2.6.23 https://elixir.bootlin.com/linux/v2.6.23/source/net/core/dev.c#L2343
  26. // * Linux 4.17 https://elixir.bootlin.com/linux/v4.17/source/net/core/net-procfs.c#L162
  27. // and https://elixir.bootlin.com/linux/v4.17/source/include/linux/netdevice.h#L2810.
  28. // SoftnetStat contains a single row of data from /proc/net/softnet_stat.
  29. type SoftnetStat struct {
  30. // Number of processed packets.
  31. Processed uint32
  32. // Number of dropped packets.
  33. Dropped uint32
  34. // Number of times processing packets ran out of quota.
  35. TimeSqueezed uint32
  36. }
  37. var softNetProcFile = "net/softnet_stat"
  38. // NetSoftnetStat reads data from /proc/net/softnet_stat.
  39. func (fs FS) NetSoftnetStat() ([]SoftnetStat, error) {
  40. b, err := util.ReadFileNoStat(fs.proc.Path(softNetProcFile))
  41. if err != nil {
  42. return nil, err
  43. }
  44. entries, err := parseSoftnet(bytes.NewReader(b))
  45. if err != nil {
  46. return nil, fmt.Errorf("failed to parse /proc/net/softnet_stat: %w", err)
  47. }
  48. return entries, nil
  49. }
  50. func parseSoftnet(r io.Reader) ([]SoftnetStat, error) {
  51. const minColumns = 9
  52. s := bufio.NewScanner(r)
  53. var stats []SoftnetStat
  54. for s.Scan() {
  55. columns := strings.Fields(s.Text())
  56. width := len(columns)
  57. if width < minColumns {
  58. return nil, fmt.Errorf("%d columns were detected, but at least %d were expected", width, minColumns)
  59. }
  60. // We only parse the first three columns at the moment.
  61. us, err := parseHexUint32s(columns[0:3])
  62. if err != nil {
  63. return nil, err
  64. }
  65. stats = append(stats, SoftnetStat{
  66. Processed: us[0],
  67. Dropped: us[1],
  68. TimeSqueezed: us[2],
  69. })
  70. }
  71. return stats, nil
  72. }
  73. func parseHexUint32s(ss []string) ([]uint32, error) {
  74. us := make([]uint32, 0, len(ss))
  75. for _, s := range ss {
  76. u, err := strconv.ParseUint(s, 16, 32)
  77. if err != nil {
  78. return nil, err
  79. }
  80. us = append(us, uint32(u))
  81. }
  82. return us, nil
  83. }