Skip to content

Commit 6d8714c

Browse files
authored
Add support for for /proc/net/tls_stat kTLS stats (#579)
Signed-off-by: Felix Aronsson <[email protected]>
1 parent 0f527e6 commit 6d8714c

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed

Diff for: net_tls_stat.go

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

Diff for: net_tls_stat_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
14+
package procfs
15+
16+
import (
17+
"testing"
18+
)
19+
20+
func TestTLSStat(t *testing.T) {
21+
tlsStats, err := getProcFixtures(t).NewTLSStat()
22+
if err != nil {
23+
t.Fatal(err)
24+
}
25+
26+
for _, test := range []struct {
27+
name string
28+
want int
29+
got int
30+
}{
31+
{name: "TLSCurrTxSw", want: 5, got: tlsStats.TLSCurrTxSw},
32+
{name: "TLSCurrRxSw", want: 5, got: tlsStats.TLSCurrRxSw},
33+
{name: "TLSCurrTxDevice", want: 0, got: tlsStats.TLSCurrTxDevice},
34+
{name: "TLSCurrRxDevice", want: 0, got: tlsStats.TLSCurrRxDevice},
35+
{name: "TLSTxSw", want: 8711, got: tlsStats.TLSTxSw},
36+
{name: "TLSTxSw", want: 8711, got: tlsStats.TLSRxSw},
37+
{name: "TLSTxDevice", want: 0, got: tlsStats.TLSTxDevice},
38+
{name: "TLSRxDevice", want: 0, got: tlsStats.TLSRxDevice},
39+
{name: "TLSDecryptError", want: 13, got: tlsStats.TLSDecryptError},
40+
{name: "TLSRxDeviceResync", want: 0, got: tlsStats.TLSRxDeviceResync},
41+
{name: "TLSDecryptRetry", want: 0, got: tlsStats.TLSDecryptRetry},
42+
{name: "TLSRxNoPadViolation", want: 0, got: tlsStats.TLSRxNoPadViolation},
43+
} {
44+
if test.want != test.got {
45+
t.Errorf("Want %s %d, have %d", test.name, test.want, test.got)
46+
}
47+
}
48+
}

Diff for: testdata/fixtures.ttar

+16
Original file line numberDiff line numberDiff line change
@@ -2528,6 +2528,22 @@ Lines: 3
25282528
6073: 000080FE00000000FFADE15609667CFE:C781 00000000000000000000000000000000:0000 07 00000000:00000000 00:00000000 00000000 1000 0 11337031 2 00000000b9256fdd 0
25292529
Mode: 644
25302530
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2531+
Path: fixtures/proc/net/tls_stat
2532+
Lines: 12
2533+
TlsCurrTxSw 5
2534+
TlsCurrRxSw 5
2535+
TlsCurrTxDevice 0
2536+
TlsCurrRxDevice 0
2537+
TlsTxSw 8711
2538+
TlsRxSw 8711
2539+
TlsTxDevice 0
2540+
TlsRxDevice 0
2541+
TlsDecryptError 13
2542+
TlsRxDeviceResync 0
2543+
TlsDecryptRetry 0
2544+
TlsRxNoPadViolation 0
2545+
Mode: 644
2546+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
25312547
Path: fixtures/proc/net/udp
25322548
Lines: 4
25332549
sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode

0 commit comments

Comments
 (0)