Skip to content

Instantly share code, notes, and snippets.

@LukasRypl
Last active November 21, 2017 08:58
Show Gist options
  • Save LukasRypl/5de40b5544a2c38d49c0b9590b774840 to your computer and use it in GitHub Desktop.
Save LukasRypl/5de40b5544a2c38d49c0b9590b774840 to your computer and use it in GitHub Desktop.
Linux TC
#!/bin/bash
#
###############################################################################
# Script used for setting bandwidth and delay. #
###############################################################################
#
# bandwidth in kilobytes
BANDWIDTH="640"
# time to reach other server (RTT will be 2xlonger=40ms)
DELAY="250"
# network to set (default is for all IPs)
network="0/0"
# interface on which we set bandwidth and delay
interface="bond0"
STOP_ACTION="stop"
START_ACTION="start"
STATUS_ACTION="status"
function setBandwidth() {
tc qdisc add dev $interface root handle 1:0 htb
tc class add dev $interface parent 1: classid 1:1 htb rate ${BANDWIDTH}kbps
tc filter add dev $interface parent 1:0 prio 1 protocol ip u32 match ip dst $network flowid 1:1
# last rule for limiting per outgoing destination port:
# tc filter add dev eth0 parent 1:0 prio 1 u32 match ip dport ${destination} 0xffff flowid 1:1
}
function unsetBandwidth() {
tc filter del dev $interface parent 1:0 prio 1 protocol ip u32 match ip dst $network flowid 1:1
tc class del dev $interface parent 1: classid 1:1 htb rate ${BANDWIDTH}kbps
tc qdisc del dev $interface root handle 1:0 htb
}
function setDelay() {
tc qdisc add dev $interface parent 1:1 netem delay ${DELAY}ms
}
function unsetDelay() {
tc qdisc del dev $interface parent 1:1 netem delay ${DELAY}ms
}
function showStatus() {
tc qdisc
tc class show dev $interface
tc filter show dev $interface
}
function setParameters() {
# check if network address is given as parameter
if [ -z "$1" ]; then
echo "No parameters specified. Using default network address=$network and default interface $interface."
else
network=$1
if [ -z "$2" ]; then
echo "No interface specified. Using default interface $interface."
else
interface=$2
fi
fi
}
function usage() {
cat <<EOF
Usage: $0 [$START_ACTION|$STOP_ACTION|$STATUS_ACTION] <ip> <interface>
<ip> the network address where limits will be applied
example: 172.16.100.0/24 -- network
0/0 -- all IP traffic
Script does not do any checks so be sure about what are you doing!
EOF
}
###############################################################################
# Start of script. #
###############################################################################
action=$1
case $action in
$STOP_ACTION)
setParameters $2 $3
unsetDelay
unsetBandwidth
;;
$START_ACTION)
setParameters $2 $3
echo -e "Limiting connection via $interface for all traffic going to $network \nbandwidth = $BANDWIDTH kBytes/s delay = $DELAY ms"
setBandwidth
setDelay
;;
$STATUS_ACTION)
setParameters $2 $3
showStatus
;;
*)
usage
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment