Skip to content

Instantly share code, notes, and snippets.

@Lakshanz
Last active April 16, 2024 14:04
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save Lakshanz/19613830e5c6f233754e12b25408cc51 to your computer and use it in GitHub Desktop.
Save Lakshanz/19613830e5c6f233754e12b25408cc51 to your computer and use it in GitHub Desktop.
TC limiting : Helps to set maximum upload/download speed limit to your linux server/pc's selected network interface.
#!/bin/bash
# Full path to tc binary
TC=$(which tc)
#
# NETWORK CONFIGURATION
# interface - name of your interface device
# interface_speed - speed in mbit of your $interface
# ip - IP address of your server, change this if you don't want to use
# the default catch all filters.
#
interface=eth0
interface_speed=100mbit
ip=4.1.2.3 # The IP address bound to the interface
# Define the upload and download speed limit, follow units can be
# passed as a parameter:
# kbps: Kilobytes per second
# mbps: Megabytes per second
# kbit: kilobits per second
# mbit: megabits per second
# bps: Bytes per second
download_limit=512kbit
upload_limit=10mbit
# Filter options for limiting the intended interface.
FILTER="$TC filter add dev $interface protocol ip parent 1: prio 1 u32"
#
# This function starts the TC rules and limits the upload and download speed
# per already configured earlier.
#
function start_tc {
tc qdisc show dev $interface | grep -q "qdisc pfifo_fast 0"
[ "$?" -gt "0" ] && tc qdisc del dev $interface root; sleep 1
# start the tc configuration
$TC qdisc add dev $interface root handle 1: htb default 30
$TC class add dev $interface parent 1: classid 1:1 htb rate $interface_speed burst 15k
$TC class add dev $interface parent 1:1 classid 1:10 htb rate $download_limit burst 15k
$TC class add dev $interface parent 1:1 classid 1:20 htb rate $upload_limit burst 15k
$TC qdisc add dev $interface parent 1:10 handle 10: sfq perturb 10
$TC qdisc add dev $interface parent 1:20 handle 20: sfq perturb 10
# Apply the filter rules
# Catch-all IP rules, which will set global limit on the server
# for all IP addresses on the server.
$FILTER match ip dst 0.0.0.0/0 flowid 1:10
$FILTER match ip src 0.0.0.0/0 flowid 1:20
# If you want to limit the upload/download limit based on specific IP address
# you can comment the above catch-all filter and uncomment these:
#
# $FILTER match ip dst $ip/32 flowid 1:10
# $FILTER match ip src $ip/32 flowid 1:20
}
#
# Removes the network speed limiting and restores the default TC configuration
#
function stop_tc {
tc qdisc show dev $interface | grep -q "qdisc pfifo_fast 0"
[ "$?" -gt "0" ] && tc qdisc del dev $interface root
}
function show_status {
$TC -s qdisc ls dev $interface
}
#
# Display help
#
function display_help {
echo "Usage: tc [OPTION]"
echo -e "\tstart - Apply the tc limit"
echo -e "\tstop - Remove the tc limit"
echo -e "\tstatus - Show status"
}
# Start
if [ -z "$1" ]; then
display_help
elif [ "$1" == "start" ]; then
start_tc
elif [ "$1" == "stop" ]; then
stop_tc
elif [ "$1" == "status" ]; then
show_status
fi
@IgnacioARD
Copy link

Hi! I just saw your code and it is amazing! I wanted to ask you if when using the global limit on the server (using the code just as it is displayed here) it limits the interface to have a top upload and download speed or if it limits any IP on that interface to have that limit, what I mean is, if you limit top download to lets say 1mbit, every IP will have 1mbit or if there are 2 IPs it will give each IP a part of that 1mbit

@theluke
Copy link

theluke commented Jan 3, 2022

Hi @Lakshanz , pretty cool script. Can you please help me customize it for my use case, where I need to apply the rules to the wwan0 port of the router.
This port is used when the main internet connection fails and the router falls back to the mobile data connection.
This means that normally the wwan0 port does not have any ip.

@faisaljaved0483
Copy link

Awesome ... working as was required
Thanks a lot for such a nice effort.
Thanks

@roachme
Copy link

roachme commented Aug 27, 2023

it does not limit download speed the way I expected. Only upload did. I've noticed part of code burst 15k impacts what i wanna ideally get.

@wuwenxux
Copy link

it does not limit download speed the way I expected. Only upload did. I've noticed part of code burst 15k impacts what i wanna ideally get.

How to limit download speed, reduce the burst value?

@whr819987540
Copy link

it does not limit download speed the way I expected. Only upload did. I've noticed part of code burst 15k impacts what i wanna ideally get.

Same as you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment