Skip to content

Estimate macOS TCP segments sent from IP packets sent, not delivered - #3822

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:mac-tcp-segments-sent
Oct 17, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:mac-tcp-segments-sent

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

Follow-up to a finding raised in #3801.

Without elevated permissions, macOS returns zeros from net.inet.tcp.stats, so getTCPv4Stats() estimates its counts from the IP and UDP totals. The segments-sent estimate subtracted the UDP datagrams sent from ips_delivered. That field is "datagrams delivered to upper level", an inbound count.

Change

The estimate now uses ips_localout, "total ip packets generated here", instead of ips_delivered. The offsets come from the macOS SDK's netinet/ip_var.h: ips_delivered is at 56 and ips_localout at 60. Both twins now read offset 60, and require a 64-byte buffer instead of 60.

Verification

On macOS 26.6.2, as an unprivileged user:

Value
netstat: packets sent from this host 258,570,565
netstat: UDP datagrams output 6,116,717
OSHI getSegmentsSent(), after 252,453,848 (the difference of the two)
OSHI getSegmentsSent(), before (delivered less UDP output) about 214.7 million

There's no TCP figure to compare against directly, because unprivileged netstat -s -p tcp reports zeros as well.

MacInternetProtocolStatsTest runs these counters through the unprivileged path. The full -Paggregate-coverage gate passes on this Mac, including the JNA-vs-FFM comparison tests.

Not changed

  • Segments received: the estimate still starts from all IP packets received (ips_total) rather than those delivered to this host. On this machine the two differ by about 4,000 in 220 million.
  • UDP families: getUDPv4Stats() counts both IPv4 and IPv6 datagrams. macOS keeps no per-family UDP datagram counter, so there's nothing to fix there.

Summary by CodeRabbit

  • Bug Fixes
    • Improved estimated TCPv4 segments-sent statistics on macOS when running without elevated permissions, using outbound IP packets while accounting for UDP traffic.

Without elevated permissions net.inet.tcp.stats reads as zeros on macOS,
so getTCPv4Stats() estimates from the IP and UDP totals. The segments-sent
estimate subtracted the UDP datagrams sent from ips_delivered, "datagrams
delivered to upper level", which counts inbound packets. Use
ips_localout, "total ip packets generated here", instead.

The offsets come from the macOS SDK's netinet/ip_var.h: ips_delivered is
at 56 and ips_localout at 60. Both twins now read offset 60, and require
64 bytes rather than 60.

On macOS 26.6.2, unprivileged, the estimate is 252453848: netstat's
258570565 packets sent from this host less 6116717 UDP datagrams output.
The old one would have been about 214.7 million.
@coderabbitai

coderabbitai Bot commented Oct 17, 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: ac9a30b5-1aad-4687-94bb-83c6ade0a659
📥 Commits

Reviewing files that changed from the base of the PR and between 72186ba and 3384087.

📒 Files selected for processing: 5
  • CHANGELOG.md
  • oshi-common/src/main/java/oshi/software/common/os/mac/MacInternetProtocolStats.java
  • oshi-common/src/test/java/oshi/software/common/os/mac/MacInternetProtocolStatsTest.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: 6
  • GitHub Check: Test JDK 25, windows-latest
  • GitHub Check: Test JDK 25, macos-latest
  • GitHub Check: Lint (Spotless, Checkstyle)
  • GitHub Check: Lint (ForbiddenAPIs, Javadoc, SLF4J)
  • 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
  • oshi-common/src/test/java/oshi/software/common/os/mac/MacInternetProtocolStatsTest.java
Applicability reference: AGENTS.md: # AGENTS.md Instructions for AI coding agents working in the OSHI repository.

📄 CodeRabbit inference engine (CONTRIBUTING.md)

Files:

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

92-92: LGTM!

228-248: LGTM!

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

239-241: LGTM!

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

149-151: LGTM!

oshi-common/src/test/java/oshi/software/common/os/mac/MacInternetProtocolStatsTest.java: 1

1-56: LGTM!

CHANGELOG.md: 1

29-29: LGTM!

📝 Walkthrough

On macOS, the unprivileged TCPv4 sent-segment estimate now uses the local-output IP packet count minus UDP output packets. Both native query implementations read the counter at offset 60. A test checks the sent and received estimates.

Changes: macOS TCPv4 statistics

Layer / File(s) Summary
Read the local-output IP counter
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
BsdIpstat stores the local-output counter. The FFM and JNA implementations require a buffer of at least 64 bytes and read the counter at offset 60.
Update the TCPv4 estimate
oshi-common/src/main/java/oshi/software/common/os/mac/MacInternetProtocolStats.java, oshi-common/src/test/java/oshi/software/common/os/mac/MacInternetProtocolStatsTest.java, CHANGELOG.md
The unprivileged sent-count estimate subtracts UDP output packets from the local-output count. The test checks estimated sent and received counts. The changelog records the correction.

Priority: ⬇️ Low

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

Change: Bug fix · Severity of issue fixed: Low

Merge Risk: ⚪ Minimal · up to 33840

This change corrects the macOS unprivileged TCPv4 segments-sent estimate to use the locally generated IP packet counter. No actionable merge-blocking risk remains.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 10.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 10 functions across 4 files. (1 skipped: … 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: estimating macOS TCP segments sent from locally sent IP packets instead of delivered packets.
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 10.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 10 functions across 4 files. (1 skipped: 1 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

  • 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 bright,
Outbound packets hop in sight.
UDP steps aside from TCP’s score,
Tests count the packets, then some more.
The changelog tells what changed today.

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

@codecov

codecov Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 71.42857% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.93%. Comparing base (72186ba) to head (3384087).

Files with missing lines Patch % Lines
...i/software/os/mac/MacInternetProtocolStatsFFM.java 50.00% 0 Missing and 1 partial ⚠️
...i/software/os/mac/MacInternetProtocolStatsJNA.java 50.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3822      +/-   ##
============================================
- Coverage     80.99%   80.93%   -0.06%     
- Complexity      661      722      +61     
============================================
  Files           725      725              
  Lines         26148    26148              
  Branches       4444     4444              
============================================
- Hits          21178    21163      -15     
- Misses         2758     2771      +13     
- Partials       2212     2214       +2     
Flag Coverage Δ
linux 41.60% <42.85%> (+0.20%) ⬆️
macos 42.54% <71.42%> (-0.05%) ⬇️
windows 48.98% <42.85%> (+0.26%) ⬆️

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 61aafad into oshi:master Oct 17, 2026
19 checks passed
@dbwiddis
dbwiddis deleted the mac-tcp-segments-sent branch October 17, 2026 20:04
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