Skip to content

Commit adee02f

Browse files
authored
Parse /proc/net/protocols (#347)
This patch adds support for extracting system socket metrics found only in /proc/net/protocols. The most interesting metrics found here are per-protocol socket counts along with the total memory in pages used by each. For TCP connections this data can be used to tune/monitor a system against the thresholds set in net.ipv4.tcp_mem. This file also exposes a pressure metric which indicates whether or not a protocol like TCP has entered memory pressure, which again is useful in troubleshooting things like memory pressure induced packet drops on loopback. Some additional albeit less interesting metrics that come out of this file include the size of each protocol's struct, and the maximum header size the kernel is willing to allocate. The additional columns enumerate capabilities per-protocol and are encoded as bools. Signed-off-by: Juan Bran <[email protected]>
1 parent f159672 commit adee02f

File tree

3 files changed

+281
-0
lines changed

3 files changed

+281
-0
lines changed

fixtures.ttar

+18
Original file line numberDiff line numberDiff line change
@@ -2129,6 +2129,24 @@ Lines: 6
21292129
4 1FB3C 0 1282A8F 0
21302130
Mode: 644
21312131
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2132+
Path: fixtures/proc/net/protocols
2133+
Lines: 14
2134+
protocol size sockets memory press maxhdr slab module cl co di ac io in de sh ss gs se re sp bi br ha uh gp em
2135+
PACKET 1344 2 -1 NI 0 no kernel n n n n n n n n n n n n n n n n n n n
2136+
PINGv6 1112 0 -1 NI 0 yes kernel y y y n n y n n y y y y n y y y y y n
2137+
RAWv6 1112 1 -1 NI 0 yes kernel y y y n y y y n y y y y n y y y y n n
2138+
UDPLITEv6 1216 0 57 NI 0 yes kernel y y y n y y y n y y y y n n n y y y n
2139+
UDPv6 1216 10 57 NI 0 yes kernel y y y n y y y n y y y y n n n y y y n
2140+
TCPv6 2144 1937 1225378 no 320 yes kernel y y y y y y y y y y y y y n y y y y y
2141+
UNIX 1024 120 -1 NI 0 yes kernel n n n n n n n n n n n n n n n n n n n
2142+
UDP-Lite 1024 0 57 NI 0 yes kernel y y y n y y y n y y y y y n n y y y n
2143+
PING 904 0 -1 NI 0 yes kernel y y y n n y n n y y y y n y y y y y n
2144+
RAW 912 0 -1 NI 0 yes kernel y y y n y y y n y y y y n y y y y n n
2145+
UDP 1024 73 57 NI 0 yes kernel y y y n y y y n y y y y y n n y y y n
2146+
TCP 1984 93064 1225378 yes 320 yes kernel y y y y y y y y y y y y y n y y y y y
2147+
NETLINK 1040 16 -1 NI 0 no kernel n n n n n n n n n n n n n n n n n n n
2148+
Mode: 444
2149+
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
21322150
Directory: fixtures/proc/net/rpc
21332151
Mode: 755
21342152
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

net_protocols.go

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

net_protocols_test.go

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
15+
import (
16+
"strings"
17+
"testing"
18+
)
19+
20+
func TestParseCapabilities(t *testing.T) {
21+
rawStr := "y y y y y y y y y y y y y n y y y y y\n"
22+
have := NetProtocolCapabilities{}
23+
err := have.parseCapabilities(strings.Fields(rawStr))
24+
if err != nil {
25+
t.Fatal(err)
26+
}
27+
28+
want := NetProtocolCapabilities{true, true, true, true, true, true, true, true, true, true, true, true, true, false, true, true, true, true, true}
29+
if want != have {
30+
t.Errorf("want %+v\nhave %+v\n", want, have)
31+
}
32+
}
33+
34+
func TestProtocolsParseLine(t *testing.T) {
35+
rawStr := "TCP 1984 93064 1225378 no 320 yes kernel y y y y y y y y y y y y y n y y y y y\n"
36+
protocols := NetProtocolStats{}
37+
have, err := protocols.parseLine(rawStr)
38+
if err != nil {
39+
t.Fatal(err)
40+
}
41+
42+
want := NetProtocolStatLine{"TCP", 1984, 93064, 1225378, 0, 320, true, "kernel", NetProtocolCapabilities{true, true, true, true, true, true, true, true, true, true, true, true, true, false, true, true, true, true, true}}
43+
if want != *have {
44+
t.Errorf("want %+v\nhave %+v\n", want, have)
45+
}
46+
}
47+
48+
func TestProtocolsParseProtocols(t *testing.T) {
49+
fs, err := NewFS(procTestFixtures)
50+
if err != nil {
51+
t.Fatal(err)
52+
}
53+
54+
protocolStats, err := fs.NetProtocols()
55+
if err != nil {
56+
t.Fatal(err)
57+
}
58+
59+
lines := map[string]NetProtocolStatLine{
60+
"PACKET": {"PACKET", 1344, 2, -1, -1, 0, false, "kernel", NetProtocolCapabilities{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false}},
61+
"PINGv6": {"PINGv6", 1112, 0, -1, -1, 0, true, "kernel", NetProtocolCapabilities{true, true, true, false, false, true, false, false, true, true, true, true, false, true, true, true, true, true, false}},
62+
"RAWv6": {"RAWv6", 1112, 1, -1, -1, 0, true, "kernel", NetProtocolCapabilities{true, true, true, false, true, true, true, false, true, true, true, true, false, true, true, true, true, false, false}},
63+
"UDPLITEv6": {"UDPLITEv6", 1216, 0, 57, -1, 0, true, "kernel", NetProtocolCapabilities{true, true, true, false, true, true, true, false, true, true, true, true, false, false, false, true, true, true, false}},
64+
"UDPv6": {"UDPv6", 1216, 10, 57, -1, 0, true, "kernel", NetProtocolCapabilities{true, true, true, false, true, true, true, false, true, true, true, true, false, false, false, true, true, true, false}},
65+
"TCPv6": {"TCPv6", 2144, 1937, 1225378, 0, 320, true, "kernel", NetProtocolCapabilities{true, true, true, true, true, true, true, true, true, true, true, true, true, false, true, true, true, true, true}},
66+
"UNIX": {"UNIX", 1024, 120, -1, -1, 0, true, "kernel", NetProtocolCapabilities{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false}},
67+
"UDP-Lite": {"UDP-Lite", 1024, 0, 57, -1, 0, true, "kernel", NetProtocolCapabilities{true, true, true, false, true, true, true, false, true, true, true, true, true, false, false, true, true, true, false}},
68+
"PING": {"PING", 904, 0, -1, -1, 0, true, "kernel", NetProtocolCapabilities{true, true, true, false, false, true, false, false, true, true, true, true, false, true, true, true, true, true, false}},
69+
"RAW": {"RAW", 912, 0, -1, -1, 0, true, "kernel", NetProtocolCapabilities{true, true, true, false, true, true, true, false, true, true, true, true, false, true, true, true, true, false, false}},
70+
"UDP": {"UDP", 1024, 73, 57, -1, 0, true, "kernel", NetProtocolCapabilities{true, true, true, false, true, true, true, false, true, true, true, true, true, false, false, true, true, true, false}},
71+
"TCP": {"TCP", 1984, 93064, 1225378, 1, 320, true, "kernel", NetProtocolCapabilities{true, true, true, true, true, true, true, true, true, true, true, true, true, false, true, true, true, true, true}},
72+
"NETLINK": {"NETLINK", 1040, 16, -1, -1, 0, false, "kernel", NetProtocolCapabilities{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false}},
73+
}
74+
75+
if want, have := len(lines), len(protocolStats); want != have {
76+
t.Errorf("want %d parsed net/protocols lines, have %d", want, have)
77+
}
78+
for _, line := range protocolStats {
79+
if want, have := lines[line.Name], line; want != have {
80+
t.Errorf("%s: want %v, have %v", line.Name, want, have)
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)