├── .gitignore ├── LICENSE ├── README.md ├── kernel ├── linux-headers-6.1.55-magictcp001_6.1.55-magictcp001-4_amd64.deb ├── linux-image-6.1.55-magictcp001_6.1.55-magictcp001-4_amd64.deb └── linux-libc-dev_6.1.55-magictcp001-4_amd64.deb └── main.sh /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/* 2 | .DS_STORE 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 nexstorm 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # magicTCP 2 | 3 | This script aims to optimise TCP performance between two hosts. 4 | 5 | ## Usage 6 | ```shell 7 | bash <(curl -sSL https://raw.githubusercontent.com/nexstorm/magicTCP/main/main.sh) 8 | ``` 9 | 10 | ## Minimum requirements 11 | 12 | - Debian 11 13 | - 500MB of free disk space 14 | - 1GB of RAM 15 | 16 | ## What this script does 17 | 18 | ### magicTCP kernel 19 | 20 | This is a kernel that is optimised for TCP throughput. It is based on (usually) the latest LTS kernel with patches that optimise parts of the kernel and enable some of the tweaks used. 21 | 22 | ### Tuning TCP via sysctl.conf 23 | 24 | We apply the following changes to your sysctl file when you run the script 25 | 26 | ```sysctl.conf 27 | net.ipv4.tcp_rmem = 8192 262144 536870912 28 | net.ipv4.tcp_wmem = 8192 262144 536870912 29 | net.ipv4.tcp_adv_win_scale = -2 30 | net.ipv4.tcp_collapse_max_bytes = 6291456 31 | net.ipv4.tcp_notsent_lowat = 131072 32 | net.ipv4.tcp_window_scaling = 1 33 | net.core.default_qdisc = fq 34 | net.ipv4.tcp_congestion_control = bbr 35 | ``` 36 | 37 | 1. **net.ipv4.tcp_rmem = 8192 262144 536870912** 38 | 39 | This line specifies the minimum, default, and maximum receive socket buffer sizes for TCP connections. Increasing buffer sizes can help improve performance for high-throughput applications. 40 | 41 | 2. **net.ipv4.tcp_wmem = 8192 262144 536870912** 42 | 43 | Similar to the previous line, this one sets the minimum, default, and maximum send socket buffer sizes for TCP connections. Increasing these values can allow for larger send buffers, improving the performance of applications that send data over TCP. 44 | 45 | 3. **net.ipv4.tcp_adv_win_scale = -2** 46 | 47 | Set to 0.25x of memory in the receive buffer to account for the overhead when processing packets. -2 is used to reduce the frequency of TCP collapse 48 | 49 | 4. **net.ipv4.tcp_collapse_max_bytes = 6291456** 50 | 51 | From [Cloudflare's blog](https://blog.cloudflare.com/optimizing-tcp-for-high-throughput-and-low-latency/) 52 | 53 | 5. **net.ipv4.tcp_notsent_lowat = 131072** 54 | 55 | This sets the low-water mark for the amount of unsent data in TCP sockets. Raising this threshold can reduce the number of small packets sent. 56 | 57 | 6. **net.ipv4.tcp_window_scaling = 1** 58 | 59 | Enables TCP window scaling 60 | 61 | 7. **net.core.default_qdisc = fq** 62 | 63 | Uses fair-queue as the queuing discipline 64 | 65 | 8. **net.ipv4.tcp_congestion_control = bbr** 66 | 67 | Uses BBR as the CCA. BBR is a Google algorithm that generally has the best throughput. We use it basically because it is unfairly aggressive. 68 | 69 | This script also does some tuning to UDP and also enables forwarding. 70 | -------------------------------------------------------------------------------- /kernel/linux-headers-6.1.55-magictcp001_6.1.55-magictcp001-4_amd64.deb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nexstorm/magicTCP/36feaa1a6318dc50bf015c7ed7f6d4cb21524af9/kernel/linux-headers-6.1.55-magictcp001_6.1.55-magictcp001-4_amd64.deb -------------------------------------------------------------------------------- /kernel/linux-image-6.1.55-magictcp001_6.1.55-magictcp001-4_amd64.deb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nexstorm/magicTCP/36feaa1a6318dc50bf015c7ed7f6d4cb21524af9/kernel/linux-image-6.1.55-magictcp001_6.1.55-magictcp001-4_amd64.deb -------------------------------------------------------------------------------- /kernel/linux-libc-dev_6.1.55-magictcp001-4_amd64.deb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nexstorm/magicTCP/36feaa1a6318dc50bf015c7ed7f6d4cb21524af9/kernel/linux-libc-dev_6.1.55-magictcp001-4_amd64.deb -------------------------------------------------------------------------------- /main.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo """\ 4 | __ 5 | ____ ___ _ _______/ /_____ _________ ___ 6 | / __ \/ _ \| |/_/ ___/ __/ __ \/ ___/ __ \__ \\ 7 | / / / / __/> <(__ ) /_/ /_/ / / / / / / / / 8 | /_/ /_/\___/_/|_/____/\__/\____/_/ /_/ /_/ /_/ 9 | _________________________________________________ 10 | magicTCP v0.1 | 25/09/2023 edition 11 | 12 | """ 13 | 14 | install_magictcp_kernel(){ 15 | magictcp_kernel_version="6.1.55-magictcp001" 16 | apt install -y wget 17 | wget "https://raw.githubusercontent.com/nexstorm/magicTCP/main/kernel/linux-headers-${magictcp_kernel_version}_${magictcp_kernel_version}-4_amd64.deb" -O "linux-headers-${magictcp_kernel_version}.deb" 18 | wget "https://raw.githubusercontent.com/nexstorm/magicTCP/main/kernel/linux-image-${magictcp_kernel_version}_${magictcp_kernel_version}-4_amd64.deb" -O "linux-image-${magictcp_kernel_version}.deb" 19 | dpkg -i "linux-headers-${magictcp_kernel_version}.deb" 20 | dpkg -i "linux-image-${magictcp_kernel_version}.deb" 21 | rm -rf "linux-headers-${magictcp_kernel_version}.deb" 22 | rm -rf "linux-image-${magictcp_kernel_version}.deb" 23 | 24 | update-initramfs -c -k ${magictcp_kernel_version} 25 | update-grub 26 | reboot 27 | } 28 | 29 | apply_tcp_optimization(){ 30 | declare -A params=( 31 | ["net.ipv4.tcp_rmem"]="8192 262144 536870912" 32 | ["net.ipv4.tcp_wmem"]="8192 262144 536870912" 33 | ["net.ipv4.tcp_collapse_max_bytes"]="6291456" 34 | ["net.ipv4.tcp_notsent_lowat"]="131072" 35 | ["net.ipv4.tcp_adv_win_scale"]="1" 36 | ["net.core.default_qdisc"]="fq" 37 | ["net.ipv4.tcp_congestion_control"]="bbr" 38 | ["net.ipv4.tcp_window_scaling"]="1" 39 | ["net.ipv4.conf.all.route_localnet"]="1" 40 | ["net.ipv4.ip_forward"]="1" 41 | ["net.ipv4.conf.all.forwarding"]="1" 42 | ["net.ipv4.conf.default.forwarding"]="1" 43 | # experimental UDP optimisation 44 | ["net.ipv4.udp_rmem_min"]="16384" 45 | ["net.ipv4.udp_wmem_min"]="16384" 46 | ["net.core.rmem_default"]="26214400" 47 | ["net.core.rmem_max"]="26214400" 48 | ["net.core.optmem_max"]="65535" 49 | ["net.ipv4.udp_mem"]="8192 262144 536870912" 50 | ["net.core.netdev_max_backlog"]="30000" 51 | 52 | ) 53 | 54 | cp /etc/sysctl.conf /etc/sysctl.conf.bak 55 | for param in "${!params[@]}"; do 56 | if grep -q "^$param" /etc/sysctl.conf; then 57 | sed -i "s|^$param.*|$param = ${params[$param]}|" /etc/sysctl.conf 58 | else 59 | # If the parameter doesn't exist, add it 60 | echo "$param = ${params[$param]}" >> /etc/sysctl.conf 61 | fi 62 | done 63 | 64 | sysctl -p 65 | } 66 | 67 | echo "1. Install magicTCP kernel" 68 | echo "2. Apply TCP optimization" 69 | 70 | read -p "Please select :" num 71 | case "$num" in 72 | 1) 73 | install_magictcp_kernel 74 | ;; 75 | 2) 76 | apply_tcp_optimization 77 | ;; 78 | *) 79 | clear 80 | echo -e "${Error}:Please select a valid option [1, 2]" 81 | exit 1 82 | ;; 83 | esac 84 | 85 | --------------------------------------------------------------------------------