net_tls_stat.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2023 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. // TLSStat struct represents data in /proc/net/tls_stat.
  22. // See https://docs.kernel.org/networking/tls.html#statistics
  23. type TLSStat struct {
  24. // number of TX sessions currently installed where host handles cryptography
  25. TLSCurrTxSw int
  26. // number of RX sessions currently installed where host handles cryptography
  27. TLSCurrRxSw int
  28. // number of TX sessions currently installed where NIC handles cryptography
  29. TLSCurrTxDevice int
  30. // number of RX sessions currently installed where NIC handles cryptography
  31. TLSCurrRxDevice int
  32. //number of TX sessions opened with host cryptography
  33. TLSTxSw int
  34. //number of RX sessions opened with host cryptography
  35. TLSRxSw int
  36. // number of TX sessions opened with NIC cryptography
  37. TLSTxDevice int
  38. // number of RX sessions opened with NIC cryptography
  39. TLSRxDevice int
  40. // record decryption failed (e.g. due to incorrect authentication tag)
  41. TLSDecryptError int
  42. // number of RX resyncs sent to NICs handling cryptography
  43. TLSRxDeviceResync int
  44. // number of RX records which had to be re-decrypted due to TLS_RX_EXPECT_NO_PAD mis-prediction. Note that this counter will also increment for non-data records.
  45. TLSDecryptRetry int
  46. // number of data RX records which had to be re-decrypted due to TLS_RX_EXPECT_NO_PAD mis-prediction.
  47. TLSRxNoPadViolation int
  48. }
  49. // NewTLSStat reads the tls_stat statistics.
  50. func NewTLSStat() (TLSStat, error) {
  51. fs, err := NewFS(DefaultMountPoint)
  52. if err != nil {
  53. return TLSStat{}, err
  54. }
  55. return fs.NewTLSStat()
  56. }
  57. // NewTLSStat reads the tls_stat statistics.
  58. func (fs FS) NewTLSStat() (TLSStat, error) {
  59. file, err := os.Open(fs.proc.Path("net/tls_stat"))
  60. if err != nil {
  61. return TLSStat{}, err
  62. }
  63. defer file.Close()
  64. var (
  65. tlsstat = TLSStat{}
  66. s = bufio.NewScanner(file)
  67. )
  68. for s.Scan() {
  69. fields := strings.Fields(s.Text())
  70. if len(fields) != 2 {
  71. return TLSStat{}, fmt.Errorf("%w: %q line %q", ErrFileParse, file.Name(), s.Text())
  72. }
  73. name := fields[0]
  74. value, err := strconv.Atoi(fields[1])
  75. if err != nil {
  76. return TLSStat{}, err
  77. }
  78. switch name {
  79. case "TlsCurrTxSw":
  80. tlsstat.TLSCurrTxSw = value
  81. case "TlsCurrRxSw":
  82. tlsstat.TLSCurrRxSw = value
  83. case "TlsCurrTxDevice":
  84. tlsstat.TLSCurrTxDevice = value
  85. case "TlsCurrRxDevice":
  86. tlsstat.TLSCurrRxDevice = value
  87. case "TlsTxSw":
  88. tlsstat.TLSTxSw = value
  89. case "TlsRxSw":
  90. tlsstat.TLSRxSw = value
  91. case "TlsTxDevice":
  92. tlsstat.TLSTxDevice = value
  93. case "TlsRxDevice":
  94. tlsstat.TLSRxDevice = value
  95. case "TlsDecryptError":
  96. tlsstat.TLSDecryptError = value
  97. case "TlsRxDeviceResync":
  98. tlsstat.TLSRxDeviceResync = value
  99. case "TlsDecryptRetry":
  100. tlsstat.TLSDecryptRetry = value
  101. case "TlsRxNoPadViolation":
  102. tlsstat.TLSRxNoPadViolation = value
  103. }
  104. }
  105. return tlsstat, s.Err()
  106. }