Skip to content

Commit 6adea56

Browse files
authored
Count IPv6 traffic in network protocol statistics (#969)
The ip, udp and icmp protocol counters were read only from /proc/net/snmp, which is IPv4-only, so IPv6 traffic was never counted. In a dual-stack setup, TCP over IPv6 didn't move the ip byte counters, and IPv6 UDP datagrams and ping6 didn't move the udp and icmp ones. Also read /proc/<pid>/net/snmp6 and add the Ip6, Udp6 and Icmp6 counters to their IPv4 counterparts: packets, ip octets (Ip6InOctets/Ip6OutOctets), and the matching drop and error counters so they cover the same traffic as the packet counts. TCP is unchanged because the kernel's Tcp counters already cover both families. If snmp6 is missing (IPv6 disabled in the guest), only the IPv4 counters are used. Add unit tests for merging IPv4 and IPv6 counters, IPv6-only input and a missing snmp6.
1 parent cc8d321 commit 6adea56

8 files changed

Lines changed: 354 additions & 15 deletions

File tree

‎Sources/Containerization/ContainerStatistics.swift‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,17 +263,37 @@ public struct ContainerStatistics: Sendable {
263263
/// Per-protocol traffic for the container's network namespace (not per interface; includes loopback).
264264
/// Containers sharing a namespace, as all containers in a VM do by default, report identical values.
265265
public struct NetworkProtocolStatistics: Sendable {
266+
/// Covers both IPv4 and IPv6: the kernel shares its TCP counters between the two families.
266267
public var tcp: NetworkProtocolCounters
268+
/// IPv4 only; see `udp6`.
267269
public var udp: NetworkProtocolCounters
270+
/// IPv4 only; see `icmp6`.
268271
public var icmp: NetworkProtocolCounters
269-
/// All IP traffic. What is not tcp/udp/icmp (raw IP, other protocols) is the remainder.
272+
/// All IPv4 traffic. What is not tcp/udp/icmp (raw IP, other protocols) is the remainder. See `ip6` for IPv6.
270273
public var ip: NetworkProtocolCounters
274+
/// IPv6 UDP. `nil` when the guest has no IPv6 statistics.
275+
public var udp6: NetworkProtocolCounters?
276+
/// IPv6 ICMP. `nil` when the guest has no IPv6 statistics.
277+
public var icmp6: NetworkProtocolCounters?
278+
/// All IPv6 traffic. `nil` when the guest has no IPv6 statistics.
279+
public var ip6: NetworkProtocolCounters?
271280

272-
public init(tcp: NetworkProtocolCounters, udp: NetworkProtocolCounters, icmp: NetworkProtocolCounters, ip: NetworkProtocolCounters) {
281+
public init(
282+
tcp: NetworkProtocolCounters,
283+
udp: NetworkProtocolCounters,
284+
icmp: NetworkProtocolCounters,
285+
ip: NetworkProtocolCounters,
286+
udp6: NetworkProtocolCounters? = nil,
287+
icmp6: NetworkProtocolCounters? = nil,
288+
ip6: NetworkProtocolCounters? = nil
289+
) {
273290
self.tcp = tcp
274291
self.udp = udp
275292
self.icmp = icmp
276293
self.ip = ip
294+
self.udp6 = udp6
295+
self.icmp6 = icmp6
296+
self.ip6 = ip6
277297
}
278298
}
279299

‎Sources/Containerization/SandboxContext/SandboxContext.pb.swift‎

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1905,6 +1905,37 @@ public nonisolated struct Com_Apple_Containerization_Sandbox_V3_NetworkProtocolS
19051905
/// Clears the value of `ip`. Subsequent reads from it will return its default value.
19061906
public mutating func clearIp() {_uniqueStorage()._ip = nil}
19071907

1908+
/// IPv6 counterparts of udp, icmp and ip, from /proc/net/snmp6. The IPv4 fields
1909+
/// above do not include IPv6 traffic. There is no tcp6: the kernel shares its TCP
1910+
/// counters between both families, so tcp already covers IPv6. Unset when the
1911+
/// guest has no IPv6 statistics.
1912+
public var udp6: Com_Apple_Containerization_Sandbox_V3_NetworkProtocolCounters {
1913+
get {_storage._udp6 ?? Com_Apple_Containerization_Sandbox_V3_NetworkProtocolCounters()}
1914+
set {_uniqueStorage()._udp6 = newValue}
1915+
}
1916+
/// Returns true if `udp6` has been explicitly set.
1917+
public var hasUdp6: Bool {_storage._udp6 != nil}
1918+
/// Clears the value of `udp6`. Subsequent reads from it will return its default value.
1919+
public mutating func clearUdp6() {_uniqueStorage()._udp6 = nil}
1920+
1921+
public var icmp6: Com_Apple_Containerization_Sandbox_V3_NetworkProtocolCounters {
1922+
get {_storage._icmp6 ?? Com_Apple_Containerization_Sandbox_V3_NetworkProtocolCounters()}
1923+
set {_uniqueStorage()._icmp6 = newValue}
1924+
}
1925+
/// Returns true if `icmp6` has been explicitly set.
1926+
public var hasIcmp6: Bool {_storage._icmp6 != nil}
1927+
/// Clears the value of `icmp6`. Subsequent reads from it will return its default value.
1928+
public mutating func clearIcmp6() {_uniqueStorage()._icmp6 = nil}
1929+
1930+
public var ip6: Com_Apple_Containerization_Sandbox_V3_NetworkProtocolCounters {
1931+
get {_storage._ip6 ?? Com_Apple_Containerization_Sandbox_V3_NetworkProtocolCounters()}
1932+
set {_uniqueStorage()._ip6 = newValue}
1933+
}
1934+
/// Returns true if `ip6` has been explicitly set.
1935+
public var hasIp6: Bool {_storage._ip6 != nil}
1936+
/// Clears the value of `ip6`. Subsequent reads from it will return its default value.
1937+
public mutating func clearIp6() {_uniqueStorage()._ip6 = nil}
1938+
19081939
public var unknownFields = SwiftProtobuf.UnknownStorage()
19091940

19101941
public init() {}
@@ -4980,13 +5011,16 @@ nonisolated extension Com_Apple_Containerization_Sandbox_V3_NetworkProtocolCount
49805011

49815012
nonisolated extension Com_Apple_Containerization_Sandbox_V3_NetworkProtocolStats: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
49825013
public static let protoMessageName: String = _protobuf_package + ".NetworkProtocolStats"
4983-
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{1}tcp\0\u{1}udp\0\u{1}icmp\0\u{1}ip\0")
5014+
public static let _protobuf_nameMap = SwiftProtobuf._NameMap(bytecode: "\0\u{1}tcp\0\u{1}udp\0\u{1}icmp\0\u{1}ip\0\u{1}udp6\0\u{1}icmp6\0\u{1}ip6\0")
49845015

49855016
fileprivate class _StorageClass {
49865017
var _tcp: Com_Apple_Containerization_Sandbox_V3_NetworkProtocolCounters? = nil
49875018
var _udp: Com_Apple_Containerization_Sandbox_V3_NetworkProtocolCounters? = nil
49885019
var _icmp: Com_Apple_Containerization_Sandbox_V3_NetworkProtocolCounters? = nil
49895020
var _ip: Com_Apple_Containerization_Sandbox_V3_NetworkProtocolCounters? = nil
5021+
var _udp6: Com_Apple_Containerization_Sandbox_V3_NetworkProtocolCounters? = nil
5022+
var _icmp6: Com_Apple_Containerization_Sandbox_V3_NetworkProtocolCounters? = nil
5023+
var _ip6: Com_Apple_Containerization_Sandbox_V3_NetworkProtocolCounters? = nil
49905024

49915025
// This property is used as the initial default value for new instances of the type.
49925026
// The type itself is protecting the reference to its storage via CoW semantics.
@@ -5001,6 +5035,9 @@ nonisolated extension Com_Apple_Containerization_Sandbox_V3_NetworkProtocolStats
50015035
_udp = source._udp
50025036
_icmp = source._icmp
50035037
_ip = source._ip
5038+
_udp6 = source._udp6
5039+
_icmp6 = source._icmp6
5040+
_ip6 = source._ip6
50045041
}
50055042
}
50065043

@@ -5023,6 +5060,9 @@ nonisolated extension Com_Apple_Containerization_Sandbox_V3_NetworkProtocolStats
50235060
case 2: try { try decoder.decodeSingularMessageField(value: &_storage._udp) }()
50245061
case 3: try { try decoder.decodeSingularMessageField(value: &_storage._icmp) }()
50255062
case 4: try { try decoder.decodeSingularMessageField(value: &_storage._ip) }()
5063+
case 5: try { try decoder.decodeSingularMessageField(value: &_storage._udp6) }()
5064+
case 6: try { try decoder.decodeSingularMessageField(value: &_storage._icmp6) }()
5065+
case 7: try { try decoder.decodeSingularMessageField(value: &_storage._ip6) }()
50265066
default: break
50275067
}
50285068
}
@@ -5047,6 +5087,15 @@ nonisolated extension Com_Apple_Containerization_Sandbox_V3_NetworkProtocolStats
50475087
try { if let v = _storage._ip {
50485088
try visitor.visitSingularMessageField(value: v, fieldNumber: 4)
50495089
} }()
5090+
try { if let v = _storage._udp6 {
5091+
try visitor.visitSingularMessageField(value: v, fieldNumber: 5)
5092+
} }()
5093+
try { if let v = _storage._icmp6 {
5094+
try visitor.visitSingularMessageField(value: v, fieldNumber: 6)
5095+
} }()
5096+
try { if let v = _storage._ip6 {
5097+
try visitor.visitSingularMessageField(value: v, fieldNumber: 7)
5098+
} }()
50505099
}
50515100
try unknownFields.traverse(visitor: &visitor)
50525101
}
@@ -5060,6 +5109,9 @@ nonisolated extension Com_Apple_Containerization_Sandbox_V3_NetworkProtocolStats
50605109
if _storage._udp != rhs_storage._udp {return false}
50615110
if _storage._icmp != rhs_storage._icmp {return false}
50625111
if _storage._ip != rhs_storage._ip {return false}
5112+
if _storage._udp6 != rhs_storage._udp6 {return false}
5113+
if _storage._icmp6 != rhs_storage._icmp6 {return false}
5114+
if _storage._ip6 != rhs_storage._ip6 {return false}
50635115
return true
50645116
}
50655117
if !storagesAreEqual {return false}

‎Sources/Containerization/SandboxContext/SandboxContext.proto‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,13 @@ message NetworkProtocolStats {
534534
NetworkProtocolCounters icmp = 3;
535535
// All IP traffic. Whatever is not tcp/udp/icmp (raw IP, other protocols) is the remainder.
536536
NetworkProtocolCounters ip = 4;
537+
// IPv6 counterparts of udp, icmp and ip, from /proc/net/snmp6. The IPv4 fields
538+
// above do not include IPv6 traffic. There is no tcp6: the kernel shares its TCP
539+
// counters between both families, so tcp already covers IPv6. Unset when the
540+
// guest has no IPv6 statistics.
541+
NetworkProtocolCounters udp6 = 5;
542+
NetworkProtocolCounters icmp6 = 6;
543+
NetworkProtocolCounters ip6 = 7;
537544
}
538545

539546
// Memory event counters from cgroup2's memory.events file.

‎Sources/Containerization/Vminitd.swift‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,14 @@ extension ContainerStatistics.NetworkProtocolCounters {
688688

689689
extension ContainerStatistics.NetworkProtocolStatistics {
690690
init(_ proto: Com_Apple_Containerization_Sandbox_V3_NetworkProtocolStats) {
691-
self.init(tcp: .init(proto.tcp), udp: .init(proto.udp), icmp: .init(proto.icmp), ip: .init(proto.ip))
691+
self.init(
692+
tcp: .init(proto.tcp),
693+
udp: .init(proto.udp),
694+
icmp: .init(proto.icmp),
695+
ip: .init(proto.ip),
696+
udp6: proto.hasUdp6 ? .init(proto.udp6) : nil,
697+
icmp6: proto.hasIcmp6 ? .init(proto.icmp6) : nil,
698+
ip6: proto.hasIp6 ? .init(proto.ip6) : nil
699+
)
692700
}
693701
}

‎Sources/ContainerizationOS/Linux/ProcNetSnmp.swift‎

Lines changed: 88 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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).
3030
public 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

Comments
 (0)