Skip to content

Instantly share code, notes, and snippets.

@shakibamoshiri
Forked from TechNickAI/limit_bandwidth.bash
Created September 29, 2024 11:57
Show Gist options
  • Save shakibamoshiri/8b3b65bbb1fd440fbc0135085c47dccf to your computer and use it in GitHub Desktop.
Save shakibamoshiri/8b3b65bbb1fd440fbc0135085c47dccf to your computer and use it in GitHub Desktop.
Limit Bandwidth by port
#!/bin/bash
# A script to limit the outgoing bandwidth of the consensus and execution layers by port
# We do this because the highest cost for running a node in AWS is the outgoing bandwidth
# Define your ports and their corresponding rate limits
declare -A ports=([30303]="500kbit" [9000]="1mbit")
# Define arbitrary class IDs for each port
declare -A class_ids=([30303]=1 [9000]=2)
# Delete any existing qdisc
tc qdisc del dev ens5 root 2>/dev/null || true
# Add the qdisc to the root of ens5
tc qdisc add dev ens5 root handle 1: htb default 10
# Loop over the ports
for port in "${!ports[@]}"; do
# Get the rate limit for this port
rate=${ports[$port]}
# Get the class ID for this port
class_id=${class_ids[$port]}
# Add a class with the rate limit under the root qdisc
tc class add dev ens5 parent 1: classid 1:$class_id htb rate $rate
# Add a filter that matches the port to the class
tc filter add dev ens5 protocol ip parent 1:0 prio 1 u32 match ip dport $port 0xffff flowid 1:$class_id
# Print a message
echo "Limited bandwidth of port $port to $rate."
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment