Skip to content

Fix the macOS UDP statistics - #3801

Merged
dbwiddis merged 2 commits into
oshi:masterfrom
dbwiddis:mac-udp-noport
Oct 14, 2026
Merged

dbwiddis merged 2 commits into
oshi:masterfrom
dbwiddis:mac-udp-noport

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Two fixes to InternetProtocolStats on macOS, both in the UDP counters.

getDatagramsNoPort() counted only IPv6 multicast

It read udps_noportmcast. MIB-II's udpNoPorts, and what netstat -s -p udp prints, 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_noportbcast as "of above, arrived as broadcast" and udps_noportmcast as "of no socket on port, multicast", which reads like both are subsets of udps_noport. Probing the kernel says otherwise:

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 has to be added, while udps_noportmcast really 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.

BsdUdpstat now carries udps_noport and udps_noportbcast, read at offsets 16 and 20 — inside the size the existing guards already require, so no guard changes. 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, so that assertion now covers both platforms.

getUDPv6Stats() reported software-checksum counters as datagrams

It returned udps_snd6_swcsum and udps_rcv6_swcsum as the datagrams sent and received. Those count datagrams whose checksum was computed in software, which is not the same thing at all:

traffic generated snd6_swcsum rcv6_swcsum
20 UDP datagrams over ::1 +0 +0
25 UDP datagrams out en0 +25 +0

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.

There is no better counter to switch to. macOS has no net.inet6.udp6 sysctl, and udps_ipackets/udps_opackets provably count both families, so nothing per-family exists to read. The override is removed in favor of the zeros AbstractInternetProtocolStats already 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 because udps_rcv6_swcsum never increments, and why they are not reported as datagram counts.

Verification

On an M-series Mac: IpStatsComparisonTest.testUdpAgreesWithNetstat passes with the no-port check now enabled, bracketing netstat's figure between two native reads, and NativeComparisonTest.internetProtocolStats passes, confirming the JNA and FFM twins read the new offsets alike. Gate clean on oshi-common,oshi-core,oshi-core-ffm -am including -Paggregate-coverage and -Perror-prone.

Not in this PR

Two adjacent things the probing turned up, which I'd rather raise than fold in:

  1. getUDPv4Stats() reports both families. udps_ipackets/udps_opackets are combined, so what OSHI labels UDPv4 is really UDP total; about 12.5% of the "sent" figure here is IPv6. Subtracting udps_snd6_swcsum would improve the sent side, but nothing is available for the received side.
  2. getTCPv4Stats()'s unprivileged segmentsSent mixes directions. It computes ips_delivered - udps_opackets, but ips_delivered is inbound ("packets for this host"); the outbound counter is ips_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

  • Bug Fixes
    • Corrected macOS UDP no-port statistics to include both undelivered unicast and broadcast/multicast datagrams, aligning the count with netstat.
    • macOS IPv6 UDP statistics now report zero instead of unrelated software-checksum counts.
    • Improved the accuracy of macOS network statistics comparisons across supported platforms.

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.
@coderabbitai

coderabbitai Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2aaab76f-e1d4-40ce-9eb4-776a0f664bd0
📥 Commits

Reviewing files that changed from the base of the PR and between 3d57a4d and bfe3cb1.

📒 Files selected for processing: 5
  • CHANGELOG.md
  • oshi-benchmark/src/test/java/oshi/comparison/IpStatsComparisonTest.java
  • oshi-common/src/main/java/oshi/software/common/os/mac/MacInternetProtocolStats.java
  • oshi-core-ffm/src/main/java/oshi/software/os/mac/MacInternetProtocolStatsFFM.java
  • oshi-core/src/main/java/oshi/software/os/mac/MacInternetProtocolStatsJNA.java

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

📜 Recent review details
⏰ Context from checks skipped due to timeout: 12
  • GitHub Check: Test JDK 25, FreeBSD
  • GitHub Check: Test JDK 25, OpenBSD
  • GitHub Check: Test JDK 25, NetBSD
  • GitHub Check: Test JDK 25, OpenIndiana
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 25, macos-latest
  • GitHub Check: Test JDK 25, ubuntu-latest
  • GitHub Check: Lint (ForbiddenAPIs, Javadoc, SLF4J)
  • GitHub Check: Test JDK 25, windows-latest
  • GitHub Check: Lint (Spotless, Checkstyle)
  • GitHub Check: Error Prone
  • GitHub Check: Analyze (java)
🧰 Additional context used
📚 Code guidelines: 2
AGENTS.md — auto-discovered
CONTRIBUTING.md — auto-discovered
📓 Path-based instructions: 3
Source excerpt: **Banned APIs** (`config/forbidden-apis.txt` and friends, enforced by `forbiddenapis:check`): `oshi-core-ffm` may not reference `com.sun.jna.**`.

📄 CodeRabbit inference engine (AGENTS.md)

Files:

  • oshi-core-ffm/src/main/java/oshi/software/os/mac/MacInternetProtocolStatsFFM.java
Source excerpt: **Banned APIs** (`config/forbidden-apis.txt` and friends, enforced by `forbiddenapis:check`): `oshi-common` may not reference `com.sun.jna.**`, `java.lang.foreign.**`, or any `System.load`/`loadLibrary`.

📄 CodeRabbit inference engine (AGENTS.md)

Files:

  • oshi-common/src/main/java/oshi/software/common/os/mac/MacInternetProtocolStats.java
Source excerpt: The Changelog lets users know what's changed.

📄 CodeRabbit inference engine (CONTRIBUTING.md)

Files:

  • CHANGELOG.md
🔇 Additional comments: 5
oshi-common/src/main/java/oshi/software/common/os/mac/MacInternetProtocolStats.java: 1

104-110: LGTM!

Also applies to: 119-126, 187-188, 200-201, 206-206, 212-213

oshi-core/src/main/java/oshi/software/os/mac/MacInternetProtocolStatsJNA.java: 1

171-172: LGTM!

Also applies to: 175-175

oshi-core-ffm/src/main/java/oshi/software/os/mac/MacInternetProtocolStatsFFM.java: 1

230-231: LGTM!

Also applies to: 233-233

oshi-benchmark/src/test/java/oshi/comparison/IpStatsComparisonTest.java: 1

148-149: LGTM!

CHANGELOG.md: 1

15-15: LGTM!

📝 Walkthrough

The change updates macOS UDP counter decoding and reporting. IPv4 no-port statistics now combine two counters, and IPv6 UDP statistics use the superclass implementation. The benchmark comparison now checks the combined no-port count on macOS and FreeBSD.

Changes: macOS UDP statistics

Layer / File(s) Summary
UDP counter decoding
oshi-common/src/main/java/oshi/software/common/os/mac/MacInternetProtocolStats.java, oshi-core/src/main/java/oshi/software/os/mac/MacInternetProtocolStatsJNA.java, oshi-core-ffm/src/main/java/oshi/software/os/mac/MacInternetProtocolStatsFFM.java
BsdUdpstat now stores separate no-port and broadcast/multicast counters. The JNA and FFM implementations read the updated fields from their respective buffers.
UDP statistics reporting and comparison
oshi-common/src/main/java/oshi/software/common/os/mac/MacInternetProtocolStats.java, oshi-benchmark/src/test/java/oshi/comparison/IpStatsComparisonTest.java, CHANGELOG.md
macOS IPv4 UDP statistics sum the no-port counters, and the macOS IPv6 UDP override is removed. The comparison checks the combined no-port count on macOS and FreeBSD. The changelog records the statistics changes.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~15 minutes

Change: Bug fix

Merge Risk: 🔵 Low · up to bfe3c

The macOS no-port statistic can overcount some datagrams. The impact is limited to this reported statistic, but correcting it requires more than removing one counter from the sum.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 4 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: correcting macOS UDP statistics.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 4 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit checks the counters at dawn,
Two no-port totals now join as one.
The IPv6 override hops away,
While native reads find fields in place.
The burrow logs the change today.

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.63%. Comparing base (3d57a4d) to head (bfe3cb1).

Files with missing lines Patch % Lines
...i/software/os/mac/MacInternetProtocolStatsFFM.java 66.66% 1 Missing ⚠️
...i/software/os/mac/MacInternetProtocolStatsJNA.java 66.66% 1 Missing ⚠️
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     
Flag Coverage Δ
freebsd 36.96% <0.00%> (ø)
linux 40.98% <0.00%> (ø)
macos 42.11% <80.00%> (-0.04%) ⬇️
netbsd 36.21% <0.00%> (+0.02%) ⬆️
openbsd 36.80% <0.00%> (-0.01%) ⬇️
solaris 38.11% <0.00%> (ø)
windows 48.39% <0.00%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dbwiddis
dbwiddis merged commit 7a27392 into oshi:master Oct 14, 2026
19 checks passed
@dbwiddis
dbwiddis deleted the mac-udp-noport branch October 14, 2026 01:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant