Repository navigation
Fix the macOS UDP statistics - #3801
Conversation
getUDPv4Stats() read udps_noportmcast, which holds only the IPv6 multicast datagrams that found no socket. MIB-II's udpNoPorts, and what netstat -s -p udp prints, also covers the unicast and the IPv4 broadcast/multicast ones, so the count was short by orders of magnitude: 12 against netstat's 47782 on a desktop with a week of uptime. Probing the kernel's counters settles how they partition, which the header comments describe loosely as "of above": case noport noportbcast noportmcast IPv4 unicast, no socket +1 0 0 IPv4 multicast, no socket 0 +1 0 IPv6 unicast, no socket +1 0 0 IPv6 multicast, no socket +1 0 +1 So udps_noportbcast is disjoint from udps_noport and must be added, while udps_noportmcast is a subset of it and must not be, or every IPv6 multicast datagram counts twice. macOS therefore differs from FreeBSD, where oshi#3795 adds all three. BsdUdpstat carries udps_noport and udps_noportbcast, read at offsets 16 and 20, within the size the existing guard already requires. It no longer carries udps_noportmcast, which nothing reads now. Offsets verified field by field against netinet/udp_var.h. With the count correct, the macOS exclusion comes off the no-port check in IpStatsComparisonTest, which brackets netstat's figure between two native reads. It passes, as does NativeComparisonTest.internetProtocolStats, confirming the JNA and FFM twins read the new offsets alike.
getUDPv6Stats() returned udps_snd6_swcsum and udps_rcv6_swcsum as the datagrams sent and received. Those count datagrams whose checksum was computed in software, not datagrams. Probing them shows how far apart those are: traffic generated snd6_swcsum rcv6_swcsum 20 UDP datagrams over ::1 +0 +0 25 UDP datagrams out en0 +25 +0 So the sent figure matches off-box traffic but counts no loopback datagram, and the received figure does not increment at all: it reads 0 against 6441827 IPv6 packets received on a desktop with a week of uptime. No better counter exists. macOS has no net.inet6.udp6 sysctl, and udps_ipackets and udps_opackets provably count both families, so there is nothing per-family to read. The override is therefore removed in favor of the zeros AbstractInternetProtocolStats already returns for an OS that keeps no separate UDPv6 statistics, which is where FreeBSD landed in getTCPv6Stats() keeps subtracting the same two fields from the IPv6 packet totals and is left alone: as a correction term, not a count, the sent one removes 610176 of 2734898 packets of real UDPv6 traffic from the estimate and earns its place. Its comment now records what the fields are, that the received side is uncorrected because udps_rcv6_swcsum never increments, and why they are not reported as datagram counts, so this does not come back.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📜 Recent review details
📝 Walkthrough
Merge Risk: 🔵 Low · up to
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3801 +/- ##
============================================
- Coverage 80.69% 80.63% -0.06%
- Complexity 83 634 +551
============================================
Files 725 725
Lines 25928 25928
Branches 4404 4404
============================================
- Hits 20922 20907 -15
- Misses 2775 2787 +12
- Partials 2231 2234 +3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Two fixes to
InternetProtocolStatson macOS, both in the UDP counters.getDatagramsNoPort()counted only IPv6 multicastIt read
udps_noportmcast. MIB-II'sudpNoPorts, and whatnetstat -s -p udpprints, also covers the unicast and the IPv4 broadcast/multicast datagrams that find no socket, so the count was short by orders of magnitude — 12 against netstat's 47782 on my desktop.Which of the three counters to add is not something the header tells you: it describes
udps_noportbcastas "of above, arrived as broadcast" andudps_noportmcastas "of no socket on port, multicast", which reads like both are subsets ofudps_noport. Probing the kernel says otherwise:noportnoportbcastnoportmcastSo
udps_noportbcastis disjoint fromudps_noportand has to be added, whileudps_noportmcastreally is a subset and must not be, or every IPv6 multicast datagram is counted twice. macOS therefore differs from FreeBSD, where #3795 adds all three.BsdUdpstatnow carriesudps_noportandudps_noportbcast, read at offsets 16 and 20 — inside the size the existing guards already require, so no guard changes. It no longer carriesudps_noportmcast, which nothing reads now. Offsets verified field by field againstnetinet/udp_var.h.With the count correct, the macOS exclusion comes off the no-port check in
IpStatsComparisonTest, so that assertion now covers both platforms.getUDPv6Stats()reported software-checksum counters as datagramsIt returned
udps_snd6_swcsumandudps_rcv6_swcsumas the datagrams sent and received. Those count datagrams whose checksum was computed in software, which is not the same thing at all:snd6_swcsumrcv6_swcsum::1en0The sent figure matches off-box traffic but counts no loopback datagram, and the received figure does not increment at all — it reads 0 against 6441827 IPv6 packets received.
There is no better counter to switch to. macOS has no
net.inet6.udp6sysctl, andudps_ipackets/udps_opacketsprovably count both families, so nothing per-family exists to read. The override is removed in favor of the zerosAbstractInternetProtocolStatsalready returns for an OS that keeps no separate UDPv6 statistics — where FreeBSD landed in #3795.getTCPv6Stats()keeps subtracting those same two fields from the IPv6 packet totals, and I left it alone deliberately. As a correction term rather than a count, the sent one removes 610176 of 2734898 packets of real UDPv6 traffic from the estimate, so it earns its place even though the raw value is a poor datagram count. Its comment now records what the fields are, that the received side is uncorrected becauseudps_rcv6_swcsumnever increments, and why they are not reported as datagram counts.Verification
On an M-series Mac:
IpStatsComparisonTest.testUdpAgreesWithNetstatpasses with the no-port check now enabled, bracketing netstat's figure between two native reads, andNativeComparisonTest.internetProtocolStatspasses, confirming the JNA and FFM twins read the new offsets alike. Gate clean onoshi-common,oshi-core,oshi-core-ffm -amincluding-Paggregate-coverageand-Perror-prone.Not in this PR
Two adjacent things the probing turned up, which I'd rather raise than fold in:
getUDPv4Stats()reports both families.udps_ipackets/udps_opacketsare combined, so what OSHI labels UDPv4 is really UDP total; about 12.5% of the "sent" figure here is IPv6. Subtractingudps_snd6_swcsumwould improve the sent side, but nothing is available for the received side.getTCPv4Stats()'s unprivilegedsegmentsSentmixes directions. It computesips_delivered - udps_opackets, butips_deliveredis inbound ("packets for this host"); the outbound counter isips_localout, at offset 60, which OSHI doesn't read. It reads plausibly on a symmetric desktop — 29.96M against a correct ~29.34M — and would be badly wrong on an asymmetric workload. It's also unverified, since the comparison test's TCP sent/received checks are gated to FreeBSD only.Summary by CodeRabbit
netstat.