Skip to content

NetBSD: read the routing table from the kernel, not netstat - #3742

Merged
dbwiddis merged 3 commits into
oshi:masterfrom
dbwiddis:netbsd-route-dump
Sep 20, 2026
Merged

dbwiddis merged 3 commits into
oshi:masterfrom
dbwiddis:netbsd-route-dump

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Sep 20, 2026 •

Copy link
Copy Markdown
Member

NetworkParams.getRoutes() on NetBSD was falling back to netstat even where the JNA native library is available, so its routes differed from every other BSD's.

Cause

NetBsdNetworkParamsJNA fetched NET_RT_DUMP through the generic NetBsdSysctlUtil.sysctl(int[]), which sizes its buffer from the first sysctl call and returns all of it, ignoring the byte count the second call reports. The kernel over-estimates a routing dump, so the buffer ended in uninitialized memory; RouteTableDump.parse then failed on the garbage and getRoutes() fell through to netstat -rn.

The fallback is not equivalent: NetBSD's netstat leaves the interface index embedded in interface-local multicast addresses, printing ff01:1::/32 where the kernel dump yields ff01::/32 on the right interface.

This fix uses the shared BsdRouteDumpJNA, as FreeBSD and OpenBSD already do; it trims to the length the second call reports. The FFM twin was already correct, through BsdRouteDumpFFM.

Testing

Verified on a NetBSD 10.1 VM with java-jna installed: getRoutes() now returns the kernel's table, and NativeComparisonTest agrees with the FFM implementation, which is how the bug surfaced.

Note for reviewers: the underlying helper, SysctlUtilJNA.sysctl(call, name, log, level), still returns the untrimmed buffer. Its only other NetBSD caller asks for kern.cp_time, which is fixed-size and unaffected, so this change stays scoped to the routing dump rather than altering a helper every BSD shares.

Summary by CodeRabbit

  • Bug Fixes
    • Improved NetBSD network route detection when native support is available by reading routes directly from the operating system.
    • Corrected interface-local multicast route reporting, preventing addresses from appearing in the incorrect ff01:1:: format.
    • Added reliable fallback route discovery when native support is unavailable, ensuring route information remains available across supported configurations.

Pre-existing bug, found by NativeComparisonTest now running on NetBSD.
NetBsdNetworkParamsJNA fetched NET_RT_DUMP through the generic
NetBsdSysctlUtil.sysctl(int[]), which returns the whole buffer sized by the
first call and ignores the byte count the second call reports. The kernel
over-estimates a routing dump, so the tail was uninitialized memory; the parse
then failed and getRoutes() silently fell back to netstat, whose output keeps
the embedded interface index in interface-local multicast (ff01:1::/32).

Use the shared BsdRouteDumpJNA, as FreeBSD and OpenBSD already do; it trims to
the length the second call reports. The FFM twin already did the same through
BsdRouteDumpFFM.
@coderabbitai

coderabbitai Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview 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: cf88e889-fec4-43c5-8498-678d4a362da3

📥 Commits

Reviewing files that changed from the base of the PR and between c26f452 and ee10c42.

📒 Files selected for processing (1)
  • CHANGELOG.md
🚧 Files skipped from review as they are similar to previous changes (1)
  • CHANGELOG.md

Included review availability: 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. (13)
  • GitHub Check: Test JDK 25, OpenIndiana
  • GitHub Check: Test JDK 25, FreeBSD
  • GitHub Check: Test JDK 25, OpenBSD
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Analyze (java)
  • GitHub Check: Test JDK 25, windows-latest
  • GitHub Check: Error Prone
  • GitHub Check: Test JDK 25, macos-latest
  • GitHub Check: Lint (Spotless, Checkstyle)
  • GitHub Check: Test JDK 26, ubuntu-latest
  • GitHub Check: Test JDK 21, ubuntu-latest
  • GitHub Check: Test JDK 25, ubuntu-latest
  • GitHub Check: Lint (ForbiddenAPIs, Javadoc, SLF4J)

📝 Walkthrough

Walkthrough

NetBSD route queries now delegate to the shared BSD JNA route-dump implementation when available. They use the superclass fallback when JNA is unavailable. The changelog documents the correction.

Changes

NetBSD route dump delegation

Layer / File(s) Summary
Route dump delegation and documentation
oshi-core/src/main/java/oshi/software/os/unix/netbsd/NetBsdNetworkParamsJNA.java, CHANGELOG.md
queryRouteDump() now uses BsdRouteDumpJNA with NetBsdLibc.INSTANCE when JNA is available. It calls the superclass implementation when JNA is unavailable. The changelog records the previous buffer-sizing behavior and its fallback result.

Priority: ➖ Normal

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

Change: Bug fix

Suggested reviewers: krillsson

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files. (1 skipped: 1 … Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ 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: NetBSD now reads the routing table from the kernel instead of using netstat.
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 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

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 routes at dawn
The old loose buffer tail is gone
BSD lends a steadier way
NetBSD follows it today
And fallback paths remain in play

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

@codecov

codecov Bot commented Sep 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.22%. Comparing base (3fe3954) to head (ee10c42).
⚠️ Report is 6 commits behind head on master.

Files with missing lines Patch % Lines
...oftware/os/unix/netbsd/NetBsdNetworkParamsJNA.java 0.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3742      +/-   ##
============================================
- Coverage     80.32%   80.22%   -0.10%     
- Complexity       83     4714    +4631     
============================================
  Files           711      711              
  Lines         25602    25598       -4     
  Branches       4320     4319       -1     
============================================
- Hits          20565    20537      -28     
- Misses         2821     2841      +20     
- Partials       2216     2220       +4     
Flag Coverage Δ
freebsd 35.83% <0.00%> (-0.01%) ⬇️
linux 39.69% <0.00%> (+<0.01%) ⬆️
macos 40.52% <0.00%> (-0.04%) ⬇️
openbsd 35.06% <0.00%> (-0.01%) ⬇️
solaris 36.42% <0.00%> (+0.01%) ⬆️
windows 46.82% <0.00%> (-0.01%) ⬇️

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 bce604d into oshi:master Sep 20, 2026
18 checks passed
@dbwiddis
dbwiddis deleted the netbsd-route-dump branch September 20, 2026 03:21
dbwiddis added a commit that referenced this pull request Oct 16, 2026
The NET_RT_DUMP request named protocol family 17, which is PF_ROUTE on
macOS, FreeBSD, DragonFly BSD and OpenBSD but the obsolete AF_OROUTE on
NetBSD, where PF_ROUTE is 34. On NetBSD every call failed, logged an
error and fell back to running netstat, so the native routing table
added in #3742 was never read there.

The value now sits in RouteTableDump.Layout beside the platform's other
routing message values, and the JNA and FFM drivers take the layout from
their callers.

RouteComparisonTest now covers NetBSD as well, with the interface name
in column 6 of its netstat output. Verified on a NetBSD 10.1 aarch64 VM:
JNA and FFM read the same 45 routes netstat -rn lists, and the full
-Paggregate-coverage run passes with all four comparison tests.
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