Skip to content

Automatically adjust CAKE qdisc RTT parameter based on measured network conditions

License

Notifications You must be signed in to change notification settings

galpt/cake-autortt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

24 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

cake-autortt

🌐 Language / 语言 / Bahasa / Π―Π·Ρ‹ΠΊ / 言θͺž / NgΓ΄n ngα»― / Dil / Ψ§Ω„Ω„ΨΊΨ©

English | δΈ­ζ–‡ | Bahasa Indonesia | Русский | ζ—₯本θͺž | TiαΊΏng Việt | TΓΌrkΓ§e | Ψ§Ω„ΨΉΨ±Ψ¨ΩŠΨ©


Note

If you're looking for the Ubuntu/Debian version, check the ubuntu-debian/ folder. However, please note that only the OpenWrt version is personally tested daily - the Ubuntu/Debian port is provided as-is for the community.

Automatically adjust CAKE qdisc RTT parameter based on measured network conditions

cake-autortt is an OpenWrt service that intelligently monitors active network connections and automatically adjusts the RTT (Round Trip Time) parameter of CAKE qdisc on both ingress and egress interfaces for optimal network performance.

🌍 Why This Matters for Your Internet Experience

Most users are familiar with the fast loading times of major websites like YouTube, Netflix, and Google - these sites use Content Delivery Networks (CDNs) that place servers very close to users, typically resulting in response times under 50-100ms. However, the internet is much larger than these big platforms.

When you browse beyond the major CDN-backed sites, you encounter a diverse world of servers:

  • Local/Regional Services: Small businesses, local news sites, community forums, and regional services often have servers within your country or region (10-50ms RTT)
  • International Content: Specialized websites, academic resources, gaming servers, and niche services may be hosted continents away (100-500ms RTT)
  • Remote Infrastructure: Some services, particularly in developing regions or specialized applications, may have significantly higher latencies

The CAKE RTT parameter controls how aggressively the queue management algorithm responds to congestion. By default, CAKE uses a 100ms RTT assumption that works reasonably well for general internet traffic. However:

  • Too Low RTT Setting: If CAKE thinks the network RTT is shorter than reality, it becomes too aggressive in dropping packets when queues build up, potentially reducing throughput for distant servers
  • Too High RTT Setting: If CAKE thinks the network RTT is longer than reality, it becomes too conservative and allows larger queues to build up, creating unnecessary latency for nearby servers

Real-World Impact Examples:

  • Singapore User β†’ German Server: Without RTT adjustment, a user in Singapore accessing a German website (β‰ˆ180ms RTT) might experience reduced throughput because CAKE's default 100ms setting causes premature packet drops
  • Rural US β†’ Regional Server: A user in rural US accessing a regional server (β‰ˆ25ms RTT) might experience higher latency than necessary because CAKE's default 100ms setting allows queues to grow larger than needed
  • Gaming/Real-time Applications: Applications sensitive to both latency and throughput benefit significantly from RTT tuning that matches actual network conditions

How cake-autortt Helps: By automatically measuring the actual RTT to the servers you're communicating with and adjusting CAKE's parameters accordingly, you get:

  • Snappier response when accessing nearby servers (shorter RTT β†’ more aggressive queue management)
  • Better throughput when accessing distant servers (longer RTT β†’ more patient queue management)
  • Optimal bufferbloat control that adapts to real network conditions rather than assumptions

This is particularly valuable for users who regularly access diverse content sources, work with international services, or live in areas where internet traffic frequently traverses long distances.

πŸš€ Features

  • Automatic RTT Detection: Monitors active connections via /proc/net/nf_conntrack and measures RTT to external hosts
  • Smart Host Filtering: Automatically filters out LAN addresses and focuses on external hosts
  • Smart RTT Algorithm: Uses built-in ping command to measure RTT to each host individually (3 pings per host), then intelligently selects between average and worst-case RTT for optimal performance
  • Interface Auto-Detection: Automatically detects CAKE-enabled interfaces (prefers ifb-* for download, physical interfaces for upload)
  • OpenWrt Service Integration: Runs as a proper OpenWrt service with automatic startup and process management
  • Configurable Parameters: All timing and behavior parameters can be customized via UCI configuration
  • Robust Error Handling: Gracefully handles missing dependencies, network issues, and interface changes
  • Minimal Dependencies: Only requires ping and tc - no additional packages needed, uses built-in utilities available on all systems
  • High Precision RTT: Supports fractional RTT values (e.g., 100.23ms) for precise network timing adjustments

πŸ”§ Compatibility

Tested and Working:

  • OpenWrt 24.10.1, r28597-0425664679, Target Platform x86/64

Expected Compatibility:

  • Previous OpenWrt versions (21.02+) with CAKE qdisc support
  • Future OpenWrt releases as long as required dependencies are available
  • All target architectures supported by OpenWrt (ARM, MIPS, x86, etc.)

Requirements for Compatibility:

  • CAKE qdisc kernel module
  • ping utility (included in all standard Linux distributions)
  • Standard tc (traffic control) utilities
  • /proc/net/nf_conntrack support (netfilter conntrack)

πŸ“‹ Requirements

Dependencies

  • ping: Standard ping utility for measuring RTT (included in all Linux distributions)
  • tc: Traffic control utility (part of iproute2)
  • CAKE qdisc: Must be configured on target interfaces

Installation of Dependencies

# ping is included by default in OpenWrt
# No additional packages needed for ping functionality

# CAKE qdisc is typically available in modern OpenWrt versions
# Check if tc supports CAKE:
tc qdisc help | grep cake

πŸ”§ Installation

Important

Before running the installation script, you MUST edit the configuration file to set the correct interface names for your system.

  1. Edit the configuration file:
# Edit the config file to match your interface names
nano etc/config/cake-autortt
  1. Configure your interface names:

Update the dl_interface (download) and ul_interface (upload) settings to match your network setup:

# Example configurations for different setups:

# For typical OpenWrt setup with SQM using ifb interfaces:
option dl_interface 'ifb-wan'      # Download interface (usually ifb-*)
option ul_interface 'wan'          # Upload interface (usually wan, eth0, etc.)

# For direct interface setup:
option dl_interface 'eth0'         # Your WAN interface
option ul_interface 'eth0'         # Same interface for both directions

# For custom interface names:
option dl_interface 'ifb4eth1'     # Your specific download interface
option ul_interface 'eth1'         # Your specific upload interface

How to find your interface names:

# List interfaces with CAKE qdisc
tc qdisc show | grep cake

# List all network interfaces
ip link show

# Check SQM interface configuration (if using SQM)
uci show sqm

Quick Installation

  1. Configure interfaces (see above section)

  2. Run the installation script:

# Make install script executable and run
chmod +x install.sh
./install.sh

Manual Installation

  1. Copy the service files to your OpenWrt router:
# Copy the main executable
cp usr/bin/cake-autortt /usr/bin/
chmod +x /usr/bin/cake-autortt

# Copy the init script
cp etc/init.d/cake-autortt /etc/init.d/
chmod +x /etc/init.d/cake-autortt

# Copy the configuration file
cp etc/config/cake-autortt /etc/config/

# Copy the hotplug script
cp etc/hotplug.d/iface/99-cake-autortt /etc/hotplug.d/iface/
chmod +x /etc/hotplug.d/iface/99-cake-autortt
  1. Enable and start the service:
# Enable the service to start automatically on boot
/etc/init.d/cake-autortt enable

# Start the service
/etc/init.d/cake-autortt start

πŸ—‘οΈ Uninstallation

To remove cake-autortt from your system:

# Make uninstall script executable and run
chmod +x uninstall.sh
./uninstall.sh

The uninstall script will:

  • Stop and disable the service
  • Remove all installed files
  • Optionally remove configuration and backup files
  • Clean up temporary files

βš™οΈ Configuration

πŸ”§ Interface Configuration (REQUIRED)

The most critical configuration step is setting the correct interface names. The service will not work properly without the correct interface names.

# View current configuration
uci show cake-autortt

# REQUIRED: Set your interface names
uci set cake-autortt.global.dl_interface='your-download-interface'
uci set cake-autortt.global.ul_interface='your-upload-interface'
uci commit cake-autortt

# Other optional configuration changes
uci set cake-autortt.global.rtt_update_interval='5'
uci set cake-autortt.global.debug='1'
uci commit cake-autortt

# Restart service to apply changes
/etc/init.d/cake-autortt restart

The service is configured through UCI. Edit /etc/config/cake-autortt or use the uci command.

Configuration Parameters

Parameter Default Description
dl_interface auto Download interface name (e.g., 'ifb-wan', 'ifb4eth1')
ul_interface auto Upload interface name (e.g., 'wan', 'eth1')
rtt_update_interval 5 Seconds between qdisc RTT parameter updates
min_hosts 3 Minimum number of hosts required for RTT calculation
max_hosts 100 Maximum number of hosts to probe sequentially
rtt_margin_percent 10 Safety margin added to measured RTT (percentage)
default_rtt_ms 100 Default RTT when insufficient hosts available
debug 0 Enable debug logging (0=disabled, 1=enabled)

Note

While the interface parameters have "auto" as default, auto-detection may not work reliably in all configurations. It is strongly recommended to explicitly set these values.

Tip

For high-activity networks (e.g., university campuses, public networks with many active users), consider adjusting rtt_update_interval based on your network's characteristics. The default 5 seconds works well for most scenarios, but you may increase it to 10-15 seconds for more stable networks or decrease it to 3 seconds for very dynamic environments.

πŸ” How It Works

  1. Connection Monitoring: Periodically parses /proc/net/nf_conntrack to identify active network connections
  2. Host Filtering: Extracts destination IP addresses and filters out private/LAN addresses
  3. RTT Measurement: Uses ping to measure RTT to each external host individually (3 pings per host)
  4. Smart RTT Selection: Pings hosts one by one to prevent network overload, calculates both average and worst-case RTT, then uses the higher value to ensure optimal performance for all connections
  5. Safety Margin: Adds a configurable margin to the measured RTT to ensure adequate buffering
  6. qdisc Update: Updates the CAKE qdisc RTT parameter on both download and upload interfaces

🧠 Smart RTT Algorithm

Starting with version 1.2.0, cake-autortt implements an intelligent RTT selection algorithm based on recommendations from Dave TΓ€ht (co-author of CAKE):

The Problem: Using only average RTT can be problematic when some hosts have significantly higher latency than others. For example, if you have 100 hosts with an average RTT of 40ms, but 2 hosts have RTTs of 234ms and 240ms, using the 40ms average could cause performance issues for those high-latency connections.

The Solution: The algorithm now:

  1. Calculates both average and worst-case RTT from all responsive hosts
  2. Compares the two values and intelligently selects the appropriate one
  3. Uses worst RTT when it's significantly higher than average to ensure all connections perform well
  4. Uses average RTT when worst RTT is close to average to avoid over-conservative settings

Why This Matters: According to Dave TΓ€ht, "it is better, especially when inbound shaping, to be using your typical RTT as the estimate so as to get control of the queue before it flows into the ISP shaper you are defeating." However, if the actual RTT to any host is longer than the RTT set on CAKE interfaces, performance can suffer significantly.

Real-World Example:

  • 98 hosts with RTT 30-50ms (average: 42ms)
  • 2 hosts with RTT 200ms+ (worst: 234ms)
  • Old algorithm: Would use 45ms average, causing issues for the 200ms+ hosts
  • New algorithm: Uses 234ms worst RTT, ensuring optimal performance for all connections

Example Connection Flow

[LAN Device] β†’ [Router with CAKE] β†’ [Internet]
                       ↑
                 cake-autortt monitors
                 active connections and
                 adjusts RTT parameter

πŸ“Š Expected Behavior

After installation and startup, you should observe:

Immediate Effects

  • Service starts automatically and begins monitoring connections
  • RTT measurements logged to system log (if debug enabled)
  • CAKE qdisc RTT parameter updated every 5 seconds based on measured network conditions
  • High precision RTT values (e.g., 44.89ms) applied to CAKE qdisc

Long-term Benefits

  • Improved Responsiveness: RTT parameter stays current with actual network conditions
  • Better Bufferbloat Control: CAKE can make more informed decisions about queue management
  • Adaptive Performance: Automatically adjusts to changing network conditions (satellite, cellular, congested links)
  • Higher Accuracy: Samples up to 100 hosts (configurable) for better representation of network conditions

Monitoring

# Check service status
/etc/init.d/cake-autortt status

# View service logs
logread | grep cake-autortt

# Monitor CAKE qdisc parameters
tc qdisc show | grep cake

# Debug mode for detailed logging
uci set cake-autortt.global.debug='1'
uci commit cake-autortt
/etc/init.d/cake-autortt restart

πŸ”§ Troubleshooting

Common Issues

  1. Service won't start

    # Check dependencies
    which ping tc
    
    # Check for CAKE interfaces
    tc qdisc show | grep cake
  2. No RTT updates

    # Enable debug mode
    uci set cake-autortt.global.debug='1'
    uci commit cake-autortt
    /etc/init.d/cake-autortt restart
    
    # Check logs
    logread | grep cake-autortt
  3. Interface detection fails

    # Manually specify interfaces
    uci set cake-autortt.global.dl_interface='ifb-wan'
    uci set cake-autortt.global.ul_interface='wan'
    uci commit cake-autortt
    /etc/init.d/cake-autortt restart

Debug Information

With debug enabled (uci set cake-autortt.global.debug='1'), the service provides detailed logging:

Example debug output:

[2025-01-09 18:34:22] cake-autortt DEBUG: Extracting hosts from conntrack
[2025-01-09 18:34:22] cake-autortt DEBUG: Found 35 non-LAN hosts
[2025-01-09 18:34:22] cake-autortt DEBUG: Measuring RTT using ping for 35 hosts (3 pings each)
[2025-01-09 18:34:25] cake-autortt DEBUG: ping summary: 28/35 hosts alive
[2025-01-09 18:34:25] cake-autortt DEBUG: Using average RTT: 45.2ms (avg: 45.2ms, worst: 89.1ms)
[2025-01-09 18:34:25] cake-autortt DEBUG: Using measured RTT: 45.2ms
[2025-01-09 18:34:35] cake-autortt INFO: Adjusting CAKE RTT to 49.72ms (49720us)
[2025-01-09 18:34:35] cake-autortt DEBUG: Updated RTT on download interface ifb-wan
[2025-01-09 18:34:35] cake-autortt DEBUG: Updated RTT on upload interface wan

Note

Memory-Efficient Logging: Debug logging is optimized to prevent log flooding. Individual host RTT measurements are not logged to reduce memory usage and disk writes. Only summary information is logged, making it suitable for continuous operation without excessive log growth.

πŸ“„ License

This project is licensed under the GNU General Public License v2.0 - see the LICENSE file for details.

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

About

Automatically adjust CAKE qdisc RTT parameter based on measured network conditions

Topics

Resources

License

Stars

Watchers

Forks

Languages