nitred / nr-wg-mtu-finder

MIT License
202 stars 22 forks source link

About

A python project to help find the optimal MTU values that maximizes the upload or download speeds between a peer and server. It also helps find bandwidth dead zones caused due to a poor choice of MTUs.

I built the project to help myself find the right MTU values for my WG server and peer. I inadvertently found that the default MTU values for the server and peer in my case put my WG connection in a bandwidth dead zone. Related reddit post.

You can have a look at the real-world heatmaps which are posted by users in the issue Post your MTU heatmaps here! If you happen to successfully use nr-wg-mtu-finder and are able to generate a heatmap, please post your heatmap in the issue if possible.

Lastly, please read the following documentation carefully!

Project Version

0.2.1

Example Bandwidth Plot

Bandwidth Plot

Warning

WARNING: This project contains scripts that run shell commands using root access. DO NOT USE IN PRODUCTION.

WARNING: This project tears down and spins up the Wireguard interface in the order of a thousand times. DO NOT USE IN PRODUCTION.

That being said, if you're an experienced python developer, please go through the code to verify that it meets your security standards.

Installation

The project has been built and tested on Ubuntu 20.04 & Python 3.9.

Install the following on both the WG server and WG peer

Usage

Prerequisites

  1. Follow the installation instructions above for both WG server and WG peer
  2. The project assumes that you already have a working WG installation on both the WG peer and WG server.
  3. The project assumes that you already have a WG interface like wg0.
  4. The project assumes that you already have a WG conf file like /etc/wireguard/wg0.conf. Take a backup of these files.
  5. Before running the following scripts, the WG interface is expected to be active/online such that the peer is able to ping the server. Use wg-quick up INTERFACE on both the WG server and WG peer to activate the connection.
  6. Start the WG server script before the WG peer script

On the WG Server

  1. Let your firewall accept connections on port 5201 from IPs within your WG interface. This port is used by the iperf3 server.
    # Replace 10.2.0.0/24 with your interface's IP range
    ufw allow proto tcp from 10.2.0.0/24 to any port 5201
  2. Let your firewall accept connections on port 5000 from IPs within your WG interface. This port is used by the flask server.
    # Replace 10.2.0.0/24 with your interface's IP range
    ufw allow proto tcp from 10.2.0.0/24 to any port 5000
  3. Add the MTU setting to the WG conf file i.e. /etc/wireguard/wg0.conf. Choose any random MTU, it will be replaced by the script anyway:

    [Interface]
    ...
    MTU = 1420  # <----- ADD THIS LINE IF NOT ALREADY EXISTS
    
    [Peer]
    ...
  4. Start the server script with the following command.
    # Example: The script loops through server MTUs from 1280 to 1290 in steps of 2
    nr-wg-mtu-finder --mode server --mtu-min 1280 --mtu-max 1290 --mtu-step 2 --server-ip 10.2.0.1

On the WG Peer

  1. Add the MTU setting to the WG conf file i.e. /etc/wireguard/wg0.conf. Choose any random MTU, it will be replaced by the script anyway:

    [Interface]
    ...
    MTU = 1420  # <----- ADD THIS LINE IF NOT ALREADY EXISTS
    
    [Peer]
    ...
  2. Start the peer script with the following command.
    # Example: The script cycles through peer MTUs from 1280 to 1290 in steps of 2
    nr-wg-mtu-finder --mode peer --mtu-min 1280 --mtu-max 1290 --mtu-step 2 --server-ip 10.2.0.1

How it works?

How does the server script work?

  1. The flow for the server script is defined in the method MTUFinder.run_server_mode().
  2. First, a flask server called a sync_server is run is the background on a separate process.
    • The sync_server listens for requests and commands from the peer script so that they can synchronize with each other.
    • The peer script waits for the sync_server to be available before running any upload or download tests.
    • The peer script gets the status and MTU of the server script from the sync_server.
    • The peer script tells the sync_server that it is done with its looping through all of its MTUs and is ready for the server script to change its MTU so that it can start a fresh cycle.
    • The sync_server informs the peer script that the server script is finished with looping through all MTUs and that it is going to shut itself down. The peer script uses this signal to shut itself down as well.
  3. When the server script receives an INITIALIZE signal, it runs the following shell commands
    • First, terminate an iperf3 server process if it is already running.
    • Spin down the WG interface
      wg-quick down wg0
    • Replace the MTU in the WG conf file with the next MTU in the list
      # 1421 is the new MTU
      sed -i s/MTU.*/MTU = 1421/ /etc/wireguard/wg0.conf
    • Spin up the WG interface
      wg-quick up wg0
    • Run iperf3 in server mode
      iperf3 -s
  4. If the server has finishing cycling through all of its MTUs and then receives a request from peer script that it is ready for a new cycle, then the server sends a SHUTDOWN signal to the peer script via the sync_server.

How does the peer script work?

How is the MTU heatmap generated?

So if you successfully ran the server and peer script, you should find two new files (one csv and one png) generated in the same directory where you ran the peer script on the WG-peer server.

CLI Options

nr-wg-mtu-finder

$ nr-wg-mtu-finder --help
usage: nr-wg-mtu-finder [-h] --mode MODE --mtu-min MTU_MIN --mtu-max MTU_MAX --mtu-step
                        MTU_STEP --server-ip SERVER_IP [--server-port SERVER_PORT]
                        [--interface INTERFACE] [--conf-file CONF_FILE]
                        [--peer-skip-errors PEER_SKIP_ERRORS]

nr-wg-mtu-finder - Helps find the optimal Wireguard MTU between a WG Server and a WG Peer.

optional arguments:
  -h, --help            show this help message and exit
  --mode MODE           Mode should be 'server' if you are running this script on the WG
                        Server. Mode should be 'peer' if you are running this script on
                        the WG Peer.
  --mtu-min MTU_MIN     Min MTU. Must be in the range [1280, 1500].
  --mtu-max MTU_MAX     Max MTU. Must be in the range [1280, 1500].
  --mtu-step MTU_STEP   By how much to increment the MTU between loops.
  --server-ip SERVER_IP
                        The IP address of the WG server and flask server.
  --server-port SERVER_PORT
                        The port for the flask server.
  --interface INTERFACE
                        The WG interface name. Default: 'wg0'
  --conf-file CONF_FILE
                        The path to the interface config file. Default:
                        '/etc/wireguard/wg0.conf'
  --peer-skip-errors PEER_SKIP_ERRORS
                        Skip errors when known errors occur in 'peer' mode during the MTU
                        loop. The known errors are logged and the loop continues without
                        crashing. Default: 'True'. Example usage: --peer-skip-errors False

nr-wg-mtu-finder-heatmap

$ nr-wg-mtu-finder-heatmap --help
usage: nr-wg-mtu-finder-heatmap [-h] --log-filepath LOG_FILEPATH --heatmap-filepath
                                HEATMAP_FILEPATH

nr-wg-mtu-finder-heatmap - Generate a heatmap file (png) from a log file (csv) that was
created by the `nr-wg-mtu-finder` script. This is useful in case the original script file
crashed midway.

optional arguments:
  -h, --help            show this help message and exit
  --log-filepath LOG_FILEPATH
                        Absolute path to the log file (csv) that was created by the `nr-wg-
                        mtu-finder` script.
  --heatmap-filepath HEATMAP_FILEPATH
                        Absolute path to the heatmap file (png) which will be created from
                        the log file (csv).

Development

Publish to pypi.org

License

MIT