Skip to content

Dynamically adjusts the TCP stack, congestion control algorithm, buffers, and queues based on real-time network metrics such as latency, packet loss, jitter, throughput, queue drops, out-of-order segments, FIN-WAIT-1/FIN-WAIT-2 sockets, and NIC speed. It is designed to optimize FreeBSD networking performance in diverse and changing conditions.

License

Notifications You must be signed in to change notification settings

geobarrod/FBSD-Net-Autotuner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FreeBSD Network Autotuner

Purpose

Dynamically adjusts the TCP stack, congestion control algorithm, buffers, and queues based on real-time network metrics such as latency, packet loss, jitter, throughput, queue drops, out-of-order segments, FIN-WAIT-1/FIN-WAIT-2 sockets, and NIC speed. It is designed to optimize FreeBSD networking performance in diverse and changing conditions.


Usage

sudo fbsd-net-autotuner [-d] [-l]

Options

  • -d → Add date and time to the event log.
  • -l → Log events to the file /var/log/fbsd-net-autotuner.log.

Configuration Variables

You can override these defaults by exporting environment variables before running the script:

Variable Default Description
TARGET_HOST auto Host used for ping measurements. Selected dynamically from public resolvers (1.1.1.1, 1.0.0.1, 8.8.8.8, 8.8.4.4, 9.9.9.9) based on lowest RTT.
IFACE auto Network interface to monitor. Auto-detected from default route or falls back to the first interface.
PING_COUNT 10 Number of pings per round
INTERVAL_SEC 60 Interval between measurements (sec)

Features

  • Auto-selects the most responsive TARGET_HOST from a resilient list of public resolvers.
  • Detects NIC speed robustly (validates numeric values, defaults to 1000 Mbps).
  • Classifies network conditions into tiers: very_slow, slow, medium, fast, very_fast.
  • Dynamically selects TCP congestion control algorithm and stack:
    • Algorithms: cubic, chd, htcp, dctcp, cdg, vegas.
    • Stacks: freebsd, bbr, rack.
  • Adjusts:
    • Buffer sizes (recv/send, maxsockbuf).
    • ECN (Explicit Congestion Notification).
    • Keepalive.
    • Delayed ACK (tier-based with jitter override).
    • SACK (Selective ACK).
    • Initial congestion window (initcwnd_segments).
    • TSO (toggle based on kernel drops).
    • ABC (net.inet.tcp.abc_l_var) → controls cwnd growth aggressiveness.
    • Reassembly queue (net.inet.tcp.reass.maxqueuelen) → adapts to out-of-order segments.
    • Enables net.inet.tcp.fast_finwait2_recycle only if FIN-WAIT-2 exceeds 5% of ESTABLISHED.
  • Monitors:
    • RTT (latency).
    • Packet loss.
    • Jitter.
    • Throughput.
    • Kernel intr_queue_drops.
    • Out-of-order segments.
    • FIN-WAIT-1 sockets (diagnostic only, logged).
    • FIN-WAIT-2 sockets (dynamic tuning).
  • Tunes mbuf clusters and jumbo buffers based on NIC speed (1G, 10G, 40G).
  • Tunes UDP receive buffer size based on NIC speed.
  • Applies changes immediately via sysctl and persists them in /etc/sysctl.conf.
  • Configures ISR tunables (maxthreads, bindthreads, defaultqlimit, maxqlimit) via /boot/loader.conf (requires reboot).
  • Adds automatic comments with timestamp in /etc/sysctl.conf and /boot/loader.conf to mark lines generated by the autotuner for traceability.
  • Flexible logging:
    • Terminal only.
    • Terminal + date.
    • File logging.

Network Tier Classification

The script evaluates RTT (latency), packet loss, throughput, and also jitter/out-of-order to classify the connection:

Tier Conditions (examples) Behavior
very_slow RTT ≥ 250ms or loss ≥ 5% Conservative buffers, Vegas/CHD
slow RTT ≥ 150ms or loss ≥ 2% or throughput < 2 Mbps Larger buffers, Vegas/HTCP
medium RTT ≥ 80ms or throughput < 20 Mbps Balanced buffers, HTCP/Cubic
fast RTT ≥ 30ms or throughput < 200 Mbps Aggressive buffers, DCTCP/HTCP
very_fast RTT < 30ms and throughput ≥ 1 Gbps Maximum buffers, BBR/RACK

Requirements

  • FreeBSD system with root privileges.
  • Utilities: sysctl, ping, netstat.
  • Reboot required for loader.conf tunables (net.isr.*).

Monitored Metrics

The net-autotuner continuously measures and logs the following metrics to guide dynamic tuning decisions:

Metric Source/Method Purpose / Usage
RTT (latency) ping statistics Classify network tier (very_slow → very_fast).
Packet loss ping statistics Detect unstable links, adjust buffers and congestion control.
Jitter Min/Max RTT difference Override delayed ACK when jitter is high.
Throughput netstat -I <iface> bytes per second Classify tier, estimate Bandwidth-Delay Product (BDP).
Intr queue drops sysctl net.inet.ip.intr_queue_drops Increase intr_queue_maxlen, toggle TSO if persistent.
Out-of-order segs netstat -s Adjust TCP reassembly queue length (reass.maxqueuelen).
FIN-WAIT-1 conns `netstat -an grep FIN_WAIT_1`
FIN-WAIT-2 conns `netstat -an grep FIN_WAIT_2`
ESTABLISHED conns `netstat -an grep ESTABLISHED`

Notes

  • The script runs in an infinite loop. Stop it with Ctrl+C.
  • Ensure /etc/sysctl.conf and /boot/loader.conf is writable for persistence.
  • Ensure that the congestion control algorithms (cubic, chd, htcp, dctcp, cdg, vegas) can load.
  • Ensure that the TCP stacks (freebsd, bbr, rack) can load.
  • delayed_ack tier setting may be overridden by jitter detection.
  • abc_l_var and reass.maxqueuelen are tuned dynamically based on stability and out-of-order metrics.
  • FIN-WAIT-1: monitored only, useful for diagnostics, no sysctl tuning available.
  • FIN-WAIT-2: dynamically managed; fast_finwait2_recycle enabled only if >5% of ESTABLISHED.
  • All metrics are logged in extended output for traceability and performance analysis.

Example

Run with logging to file and timestamps:

sudo fbsd-net-autotuner -d -l

Installation

Clone the repository and run the installer:

git clone https://github.com/geobarrod/FBSD-Net-Autotuner.git
cd FBSD-Net-Autotuner
sudo make install

Uninstallation

To remove the fbsd-net-autotuner and service:

cd FBSD-Net-Autotuner
sudo make uninstall

Changelog

v1.0 — 2025-12-05

  • Initial release of FreeBSD Network Autotuner.

v1.1 — 2025-12-10

  • Added BDP estimation and buffer tuning.

v1.2 — 2025-12-15

  • Added jitter measurement and override for delayed ACK.

v1.3 — 2025-12-20

  • Added out-of-order segment measurement and dynamic tuning for ABC and reassembly queue.

v1.4 — 2025-12-25

  • Refactored loss_pct extraction.
  • Improved persistence logic in set_sysctl_if_needed.

v1.5 — 2026-01-09

  • Automatic selection of TARGET_HOST.
  • Robust NIC speed detection.
  • Mbuf cluster and UDP buffer tuning.
  • ISR tuning via /boot/loader.conf.

v1.6 — 2026-01-11

  • Added monitoring of FIN-WAIT-1 sockets as a diagnostic metric (logged, no dynamic tuning).
  • Integrated dynamic tuning for FIN-WAIT-2 sockets:
    • Uses net.inet.tcp.fast_finwait2_recycle.
    • Enabled only if FIN-WAIT-2 exceeds 5% of ESTABLISHED connections.
    • Restores default when ratio ≤5%.
    • Idempotent logging: changes are logged only when state transitions occur.
  • Extended log output to include finwait1_conns, finwait2_conns, and est_conns.

v1.7 — 2026-01-14

  • Introduced adaptive congestion control selection:
    • Chooses CC algorithm dynamically (Vegas, HTCP, DCTCP, CDG, CHD/HD, Cubic) based on RTT, loss, jitter, and throughput.
  • Introduced adaptive TCP stack selection:
    • Chooses stack dynamically (BBR, RACK, FreeBSD) based on throughput, RTT, loss, and jitter.

About

Dynamically adjusts the TCP stack, congestion control algorithm, buffers, and queues based on real-time network metrics such as latency, packet loss, jitter, throughput, queue drops, out-of-order segments, FIN-WAIT-1/FIN-WAIT-2 sockets, and NIC speed. It is designed to optimize FreeBSD networking performance in diverse and changing conditions.

Resources

License

Stars

Watchers

Forks