Skip to content

Read BSD TCP statistics from the lines netstat prints - #3803

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:bsd-netstat-tcp-stats
Oct 14, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:bsd-netstat-tcp-stats

Conversation

@dbwiddis

Copy link
Copy Markdown
Member

OpenBSD, NetBSD and DragonFly BSD get InternetProtocolStats from the shared netstat -s parser in NetStat. Checked against real output from all four BSDs (wording is identical across them), most of the TCP fields were wrong:

field before now
active / passive openings always 0, no BSD line matched connection requests / connection accepts
connections established connections established (including accepts), the count ever established (88159 on the OpenBSD box) IPv4 connections that netstat -n -f inet lists as ESTABLISHED, as FreeBSD and macOS already do
connection failures bad connection attempts (bad SYNs) embryonic connections dropped, the tcps_conndrops FreeBSD's native path reads
connections reset dropped due to RST, a counter nested under the SYN cache the D in connections closed (including D drops), FreeBSD's tcps_drops
segments retransmitted also added DragonFly's retransmitted by SACK only data packets (B bytes) retransmitted

UDP: receive errors no longer include with no checksum (a checksum is optional over IPv4, and FreeBSD's native path does not count it), and FreeBSD's broadcast/multicast datagram(s) undelivered wording now counts as no port, so the FreeBSD netstat fallback agrees with its sysctl path. Singular forms, which netstat prints for a count of 1, are matched throughout.

Code changes:

  • NetStat.queryTcpStats(String, long) takes the current established count from the caller; FreeBSD's fallback uses it instead of rebuilding TcpStats by hand.
  • NetStat.queryBsdTcpStats() combines it with netstat -n -f inet; OpenBSD, NetBSD and DragonFly call it.
  • queryTcpnetstat counts plain tcp rows as IPv4, which is how OpenBSD and NetBSD label them.
  • The invented BSD fixtures in NetStatTest are replaced with real output from OpenBSD 7.9, DragonFly 6.4 and FreeBSD 16, keeping the lines that used to be misread.

Verification

  • Gate on oshi-common clean, including -Perror-prone.
  • DragonFly 6.4 (cfarm152): getTCPv4Stats() and getUDPv4Stats() agree with live netstat -s on every field, and established (2) with netstat -n -f inet.
  • OpenBSD 7.9 and NetBSD 10.1: fixture-tested only, from output captured on cfarm220 and cfarm428, which have no JDK. netstat -n -f inet format confirmed on both.

OpenBSD, NetBSD and DragonFly BSD read TCP and UDP statistics through the
shared netstat -s parser, which matched none of their connection lines and
mapped others to unrelated counters:

- active and passive openings were always 0; they are now read from
  "connection requests" and "connection accepts"
- connections established held the count ever established; it is now the
  number of IPv4 connections netstat -n -f inet lists as established, as on
  FreeBSD and macOS
- failures came from "bad connection attempts" (bad SYNs) and resets from
  the SYN cache's "dropped due to RST"; they now come from "embryonic
  connections dropped" and the drops among "connections closed", the
  counters FreeBSD's native path reads
- retransmissions no longer add DragonFly's "retransmitted by SACK" line
- UDP receive errors no longer include "with no checksum", which is valid
  over IPv4, and FreeBSD's "undelivered" wording now counts as no port

Singular forms, printed for a count of 1, are matched too. The invented BSD
fixtures are replaced with real output from OpenBSD 7.9, DragonFly 6.4 and
FreeBSD 16. Verified against live netstat on DragonFly 6.4.
@codecov

codecov Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.81818% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.69%. Comparing base (7a27392) to head (9dddf98).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...n/src/main/java/oshi/util/driver/unix/NetStat.java 88.88% 0 Missing and 2 partials ⚠️
...ragonflybsd/DragonFlyBsdInternetProtocolStats.java 0.00% 1 Missing ⚠️
.../os/unix/freebsd/FreeBsdInternetProtocolStats.java 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3803      +/-   ##
============================================
+ Coverage     80.68%   80.69%   +0.01%     
- Complexity      404      633     +229     
============================================
  Files           725      725              
  Lines         25928    25936       +8     
  Branches       4404     4406       +2     
============================================
+ Hits          20920    20930      +10     
+ Misses         2778     2777       -1     
+ Partials       2230     2229       -1     
Flag Coverage Δ
freebsd 36.97% <40.90%> (+<0.01%) ⬆️
linux 41.00% <40.90%> (+0.01%) ⬆️
macos 42.12% <40.90%> (-0.04%) ⬇️
netbsd 36.23% <77.27%> (+0.01%) ⬆️
openbsd 36.83% <77.27%> (+0.03%) ⬆️
solaris 38.13% <40.90%> (+<0.01%) ⬆️
windows 48.39% <40.90%> (+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 c36f940 into oshi:master Oct 14, 2026
20 checks passed
@dbwiddis
dbwiddis deleted the bsd-netstat-tcp-stats branch October 14, 2026 01:32
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