net_route.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2023 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. const (
  24. blackholeRepresentation string = "*"
  25. blackholeIfaceName string = "blackhole"
  26. routeLineColumns int = 11
  27. )
  28. // A NetRouteLine represents one line from net/route.
  29. type NetRouteLine struct {
  30. Iface string
  31. Destination uint32
  32. Gateway uint32
  33. Flags uint32
  34. RefCnt uint32
  35. Use uint32
  36. Metric uint32
  37. Mask uint32
  38. MTU uint32
  39. Window uint32
  40. IRTT uint32
  41. }
  42. func (fs FS) NetRoute() ([]NetRouteLine, error) {
  43. return readNetRoute(fs.proc.Path("net", "route"))
  44. }
  45. func readNetRoute(path string) ([]NetRouteLine, error) {
  46. b, err := util.ReadFileNoStat(path)
  47. if err != nil {
  48. return nil, err
  49. }
  50. routelines, err := parseNetRoute(bytes.NewReader(b))
  51. if err != nil {
  52. return nil, fmt.Errorf("failed to read net route from %s: %w", path, err)
  53. }
  54. return routelines, nil
  55. }
  56. func parseNetRoute(r io.Reader) ([]NetRouteLine, error) {
  57. var routelines []NetRouteLine
  58. scanner := bufio.NewScanner(r)
  59. scanner.Scan()
  60. for scanner.Scan() {
  61. fields := strings.Fields(scanner.Text())
  62. routeline, err := parseNetRouteLine(fields)
  63. if err != nil {
  64. return nil, err
  65. }
  66. routelines = append(routelines, *routeline)
  67. }
  68. return routelines, nil
  69. }
  70. func parseNetRouteLine(fields []string) (*NetRouteLine, error) {
  71. if len(fields) != routeLineColumns {
  72. return nil, fmt.Errorf("invalid routeline, num of digits: %d", len(fields))
  73. }
  74. iface := fields[0]
  75. if iface == blackholeRepresentation {
  76. iface = blackholeIfaceName
  77. }
  78. destination, err := strconv.ParseUint(fields[1], 16, 32)
  79. if err != nil {
  80. return nil, err
  81. }
  82. gateway, err := strconv.ParseUint(fields[2], 16, 32)
  83. if err != nil {
  84. return nil, err
  85. }
  86. flags, err := strconv.ParseUint(fields[3], 10, 32)
  87. if err != nil {
  88. return nil, err
  89. }
  90. refcnt, err := strconv.ParseUint(fields[4], 10, 32)
  91. if err != nil {
  92. return nil, err
  93. }
  94. use, err := strconv.ParseUint(fields[5], 10, 32)
  95. if err != nil {
  96. return nil, err
  97. }
  98. metric, err := strconv.ParseUint(fields[6], 10, 32)
  99. if err != nil {
  100. return nil, err
  101. }
  102. mask, err := strconv.ParseUint(fields[7], 16, 32)
  103. if err != nil {
  104. return nil, err
  105. }
  106. mtu, err := strconv.ParseUint(fields[8], 10, 32)
  107. if err != nil {
  108. return nil, err
  109. }
  110. window, err := strconv.ParseUint(fields[9], 10, 32)
  111. if err != nil {
  112. return nil, err
  113. }
  114. irtt, err := strconv.ParseUint(fields[10], 10, 32)
  115. if err != nil {
  116. return nil, err
  117. }
  118. routeline := &NetRouteLine{
  119. Iface: iface,
  120. Destination: uint32(destination),
  121. Gateway: uint32(gateway),
  122. Flags: uint32(flags),
  123. RefCnt: uint32(refcnt),
  124. Use: uint32(use),
  125. Metric: uint32(metric),
  126. Mask: uint32(mask),
  127. MTU: uint32(mtu),
  128. Window: uint32(window),
  129. IRTT: uint32(irtt),
  130. }
  131. return routeline, nil
  132. }