Repository navigation
Read FreeBSD TCP and UDP statistics as 64-bit counters - #3795
Conversation
Every counter in struct tcpstat and struct udpstat has been a uint64_t since FreeBSD 10.0, but OSHI read them as 32-bit values at the macOS offsets, so most TCP and UDP statistics were wrong: passive opens 0, segments received a different counter, UDP datagrams sent 0. The structs carry no version field, and FreeBSD 14.0 inserted tcps_progdrops mid-struct, moving every TCP counter from tcps_sndtotal on by 8 bytes. The size the sysctl returns identifies the layout: 880 or 904 bytes before the insertion, 984 or more after it. Any other size falls back to netstat -s. Offsets were checked against tcp_var.h and udp_var.h from 10.0, 11.4, 12.4, 13.5, 14.0, 14.3, 15.0 and main. The parsing moves to FreeBsdInternetProtocolStats in oshi-common; the JNA and FFM twins only fetch the bytes. UDP no-port now counts udps_noport, udps_noportbcast and udps_noportmcast, which netstat reports together. UDPv6 reports zeros instead of the IPv6 software-checksum counters it showed. The now-unused stat carriers in CLibrary are removed. IpStatsComparisonTest checks the native counters against netstat -s on FreeBSD and macOS, bracketing each netstat count between two native reads. It fails on master and passes here on FreeBSD 16-CURRENT.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📜 Recent review details
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to FreeBSD TCP and UDP statistics are now read as 64-bit counters at layout-appropriate offsets, and unrecognized layouts fall back to Pre-merge checks ·
|
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #3795 +/- ##
============================================
- Coverage 80.66% 80.58% -0.08%
- Complexity 83 561 +478
============================================
Files 725 725
Lines 25991 25944 -47
Branches 4399 4399
============================================
- Hits 20965 20908 -57
- Misses 2785 2793 +8
- Partials 2241 2243 +2
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:
|
* Count every macOS datagram with no socket on its port 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 #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. * Stop reporting macOS checksum counters as UDPv6 datagrams 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.
Fixes the TCP and UDP statistics from
InternetProtocolStatson FreeBSD, found by comparing an OSHI report against the OS's own tools on cfarm430 (FreeBSD 16-CURRENT).The bug
Every counter in
struct tcpstatandstruct udpstathas been auint64_tsince FreeBSD 10.0, but OSHI read them as 32-bit values at the macOS offsets. On cfarm430 that gave passive opens 0 (netstat: 5133), segments received 171 (827k), UDP datagrams sent 0 (448k), and so on.The fix
The structs have no version field, and FreeBSD 14.0 inserted
tcps_progdropsmid-struct, moving every TCP counter fromtcps_sndtotalon by 8 bytes. The size the sysctl returns identifies the layout:tcpstatsizetcps_progdropstcps_progdropsnetstat -sfallbackOffsets were checked against
tcp_var.handudp_var.hfrom 10.0, 11.4, 12.4, 13.5, 14.0, 14.3, 15.0 and main.udpstatis unchanged since 10.0 (112 bytes).Parsing moves to
FreeBsdInternetProtocolStatsinoshi-common; the JNA and FFM twins only fetch the bytes.UDP no-port now counts
udps_noport + udps_noportbcast + udps_noportmcast, matching whatnetstatprints.UDPv6 reports zeros instead of the IPv6 software-checksum counters it showed; FreeBSD does not count IPv6 datagrams separately in this struct.
The now-unused stat carriers in
CLibraryare removed (macOS has its own inoshi-common).New comparison test
IpStatsComparisonTest(inoshi.comparison, run by-Paggregate-coverage) checks the native counters againstnetstat -son FreeBSD and macOS. Counters only grow, so eachnetstatvalue must lie between a native read taken before it and one taken after. It parsesnetstatitself rather than through the production parser. The FreeBSD and macOS CI jobs already run this profile, so a future release that moves the offsets again fails CI.On macOS, TCP counters are zero without root and some fields are estimated from IP counters, so macOS compares only the fields read directly; UDP no-port is skipped there because macOS still reports only
udps_noportmcast(follow-up).Testing
netstat -s.-Perror-prone, and-Pnative-comparisongreen.Unrelated:
DiskDriversTest.testDiskDriversfails on cfarm430 on master too (a multi-disk VM); that is a separate partition-listing bug, tracked separately.Summary by CodeRabbit
netstatoutput on macOS and FreeBSD.