@@ -21,12 +21,12 @@ import Foundation
2121#endif
2222
2323/// Protocol-level traffic counters for a network namespace, parsed from the
24- /// kernel's `/proc/net/snmp` and `/proc/net/netstat `.
24+ /// kernel's `/proc/net/snmp`, `/proc/net/netstat` and `/proc/net/snmp6 `.
2525///
2626/// These counters are per network namespace, not per interface, and count
2727/// loopback traffic. Containers that share a network namespace (the default
2828/// in a VM, where all containers share the VM's) therefore see the same values. The kernel keeps no per-protocol byte counters, so bytes
29- /// are only available for IP as a whole (`IpExt: InOctets/OutOctets`).
29+ /// are only available for IP as a whole (`IpExt: InOctets/OutOctets` for IPv4, `Ip6InOctets/Ip6OutOctets` for IPv6 ).
3030public struct ProcNetSnmp : Sendable , Equatable {
3131 public struct Counters : Sendable , Equatable {
3232 public var receivedPackets : UInt64 = 0
@@ -46,22 +46,41 @@ public struct ProcNetSnmp: Sendable, Equatable {
4646 public init ( ) { }
4747 }
4848
49- /// TCP segments (`Tcp: InSegs/OutSegs`). No byte counters.
49+ /// TCP segments (`Tcp: InSegs/OutSegs`). No byte counters. The kernel shares its TCP
50+ /// counters between IPv4 and IPv6, so these cover both families and there is no `tcp6`.
5051 public var tcp = Counters ( )
51- /// UDP datagrams (`Udp: InDatagrams/OutDatagrams`). No byte counters.
52+ /// IPv4 UDP datagrams (`Udp: InDatagrams/OutDatagrams`). No byte counters.
5253 public var udp = Counters ( )
53- /// ICMP messages (`Icmp: InMsgs/OutMsgs`). No byte counters.
54+ /// IPv4 ICMP messages (`Icmp: InMsgs/OutMsgs`). No byte counters.
5455 public var icmp = Counters ( )
55- /// All IP packets (`Ip: InReceives/OutRequests`) and octets (`IpExt: InOctets/OutOctets`).
56+ /// All IPv4 packets (`Ip: InReceives/OutRequests`) and octets (`IpExt: InOctets/OutOctets`).
5657 public var ip = Counters ( )
58+ /// IPv6 UDP datagrams (`Udp6InDatagrams/Udp6OutDatagrams`). `nil` when `/proc/net/snmp6` is unavailable.
59+ public var udp6 : Counters ?
60+ /// IPv6 ICMP messages (`Icmp6InMsgs/Icmp6OutMsgs`). `nil` when `/proc/net/snmp6` is unavailable.
61+ public var icmp6 : Counters ?
62+ /// All IPv6 packets (`Ip6InReceives/Ip6OutRequests`) and octets (`Ip6InOctets/Ip6OutOctets`).
63+ /// `nil` when `/proc/net/snmp6` is unavailable.
64+ public var ip6 : Counters ?
5765
5866 public init ( ) { }
5967
60- /// Parse the contents of `/proc/net/snmp` and, optionally, `/proc/net/netstat`.
68+ /// Parse the contents of `/proc/net/snmp` and, optionally, `/proc/net/netstat` and `/proc/net/snmp6` .
6169 ///
62- /// Both files alternate a header line (`Proto: name name ...`) with a value line
63- /// (`Proto: 1 2 ...`). Fields are matched by name so new kernel columns don't break parsing.
64- public init ( snmp : String , netstat : String ? = nil ) {
70+ /// `snmp` and `netstat` alternate a header line of field names with a line of values for the
71+ /// same protocol. For example, `snmp` might contain
72+ /// ```
73+ /// Udp: InDatagrams NoPorts InErrors OutDatagrams
74+ /// Udp: 300 1 0 250
75+ /// Tcp: RtoAlgorithm MaxConn InSegs OutSegs
76+ /// Tcp: 1 -1 600 500
77+ /// ```
78+ /// Fields are matched by name, so columns added by newer kernels don't break parsing.
79+ ///
80+ /// `snmp6` holds the IPv6 counters, one `Name value` pair per line, where the name starts
81+ /// with its protocol (`Udp6InDatagrams 60`). They go to `udp6`, `icmp6` and `ip6`, separate
82+ /// from the IPv4 ones, and stay `nil` when `snmp6` is absent.
83+ public init ( snmp : String , netstat : String ? = nil , snmp6 : String ? = nil ) {
6584 let snmpTables = Self . tables ( snmp )
6685 let netstatTables = netstat . map ( Self . tables ) ?? [ : ]
6786
@@ -101,6 +120,38 @@ public struct ProcNetSnmp: Sendable, Equatable {
101120 ip . receivedErrors = sum ( ipTable , " InHdrErrors " , " InAddrErrors " , " InUnknownProtos " )
102121 ip . transmittedDrops = sum ( ipTable , " OutDiscards " , " OutNoRoutes " )
103122 ip . transmittedErrors = ipTable ? [ " FragFails " ] ?? 0
123+
124+ guard let snmp6 else { return }
125+
126+ // The same mapping as IPv4, but every IPv6 counter is in snmp6, including octets and InNoRoutes.
127+ let udp6Table = Self . snmp6Fields ( snmp6 , prefix : " Udp6 " )
128+ var udp6 = Counters ( )
129+ udp6 . receivedPackets = udp6Table [ " InDatagrams " ] ?? 0
130+ udp6 . transmittedPackets = udp6Table [ " OutDatagrams " ] ?? 0
131+ udp6 . receivedDrops = sum ( udp6Table , " RcvbufErrors " , " NoPorts " )
132+ udp6 . receivedErrors = ( udp6Table [ " InErrors " ] ?? 0 ) . subtractingClamped ( udp6Table [ " RcvbufErrors " ] ?? 0 )
133+ udp6 . transmittedDrops = udp6Table [ " SndbufErrors " ] ?? 0
134+ self . udp6 = udp6
135+
136+ let icmp6Table = Self . snmp6Fields ( snmp6 , prefix : " Icmp6 " )
137+ var icmp6 = Counters ( )
138+ icmp6 . receivedPackets = icmp6Table [ " InMsgs " ] ?? 0
139+ icmp6 . transmittedPackets = icmp6Table [ " OutMsgs " ] ?? 0
140+ icmp6 . receivedErrors = icmp6Table [ " InErrors " ] ?? 0
141+ icmp6 . transmittedErrors = icmp6Table [ " OutErrors " ] ?? 0
142+ self . icmp6 = icmp6
143+
144+ let ip6Table = Self . snmp6Fields ( snmp6 , prefix : " Ip6 " )
145+ var ip6 = Counters ( )
146+ ip6 . receivedPackets = ip6Table [ " InReceives " ] ?? 0
147+ ip6 . transmittedPackets = ip6Table [ " OutRequests " ] ?? 0
148+ ip6 . receivedBytes = ip6Table [ " InOctets " ] ?? 0
149+ ip6 . transmittedBytes = ip6Table [ " OutOctets " ] ?? 0
150+ ip6 . receivedDrops = sum ( ip6Table , " InDiscards " , " InNoRoutes " )
151+ ip6 . receivedErrors = sum ( ip6Table , " InHdrErrors " , " InAddrErrors " , " InUnknownProtos " )
152+ ip6 . transmittedDrops = sum ( ip6Table , " OutDiscards " , " OutNoRoutes " )
153+ ip6 . transmittedErrors = ip6Table [ " FragFails " ] ?? 0
154+ self . ip6 = ip6
104155 }
105156
106157 /// Read the counters for the network namespace of process `pid`, or of the
@@ -109,7 +160,33 @@ public struct ProcNetSnmp: Sendable, Equatable {
109160 let base = pid . map { " /proc/ \( $0 ) /net " } ?? " /proc/thread-self/net "
110161 let snmp = try String ( contentsOfFile : " \( base ) /snmp " , encoding : . utf8 )
111162 let netstat = try? String ( contentsOfFile : " \( base ) /netstat " , encoding : . utf8 )
112- return ProcNetSnmp ( snmp : snmp , netstat : netstat )
163+ // Absent when IPv6 is disabled in the guest kernel.
164+ let snmp6 = try? String ( contentsOfFile : " \( base ) /snmp6 " , encoding : . utf8 )
165+ return ProcNetSnmp ( snmp : snmp , netstat : netstat , snmp6 : snmp6 )
166+ }
167+
168+ /// Returns the `/proc/net/snmp6` fields starting with `prefix`, keyed by the name after it.
169+ ///
170+ /// Unlike `snmp` and `netstat`, each line holds one `Name value` pair, separated by spaces or tabs.
171+ /// Given
172+ /// ```
173+ /// Ip6InReceives 50
174+ /// Ip6InOctets 1000
175+ /// Udp6InDatagrams 60
176+ /// Udp6NoPorts 5
177+ /// Icmp6InMsgs 6
178+ /// ```
179+ /// and prefix `Udp6`, this returns `["InDatagrams": 60, "NoPorts": 5]`. Lines for other
180+ /// prefixes are skipped, as are values that don't parse as unsigned.
181+ private static func snmp6Fields ( _ contents : String ? , prefix : String ) -> [ String : UInt64 ] {
182+ var result : [ String : UInt64 ] = [ : ]
183+ guard let contents else { return result }
184+ for line in contents . split ( separator : " \n " ) {
185+ let parts = line . split ( whereSeparator : { $0 == " " || $0 == " \t " } )
186+ guard parts . count == 2 , parts [ 0 ] . hasPrefix ( prefix ) , let value = UInt64 ( parts [ 1 ] ) else { continue }
187+ result [ String ( parts [ 0 ] . dropFirst ( prefix . count ) ) ] = value
188+ }
189+ return result
113190 }
114191
115192 /// Returns `[protocol: [field: value]]` from alternating header/value lines.
0 commit comments