blob: aa4d55e8f74bb05e511e9a6d87d3071a1e825ca2 [file] [log] [blame]
{#
Copyright 2017-present Open Networking Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
#}
#!/bin/bash
# tc uses the following units when passed as a parameter.
# kbps: Kilobytes per second
# mbps: Megabytes per second
# kbit: Kilobits per second
# mbit: Megabits per second
# bps: Bytes per second
# Amounts of data can be specified in:
# kb or k: Kilobytes
# mb or m: Megabytes
# mbit: Megabits
# kbit: Kilobits
# To get the byte figure from bits, divide the number by 8 bit
#
TC=/sbin/tc
WAN=eth0 # External (WAN side) interface
LAN=eth1 # Customer (LAN side) interface
MAXRATE=10gbit # Maximum upload/download rate
DNLD={{ downlink_speed }} # DOWNLOAD Limit
UPLD={{ uplink_speed }} # UPLOAD Limit
[ "$DNLD" == "None" ] && DNLD=$MAXRATE
[ "$UPLD" == "None" ] && UPLD=$MAXRATE
start() {
# We'll use Hierarchical Token Bucket (HTB) to shape bandwidth.
# For detailed configuration options, please consult Linux man
# page.
#
# WAN side (upload limiting)
#
$TC qdisc add dev $WAN root handle 1: htb default 30
$TC class add dev $WAN parent 1: classid 1:1 htb rate $MAXRATE burst 15k
# The default class
$TC class add dev $WAN parent 1:1 classid 1:30 htb rate 1kbit ceil $UPLD burst 15k
$TC qdisc add dev $WAN parent 1:30 handle 30: sfq perturb 10
# This class is exempt from the upload limit
$TC class add dev $WAN parent 1:1 classid 1:50 htb rate 1kbit ceil $MAXRATE burst 15k
$TC qdisc add dev $WAN parent 1:50 handle 50: sfq perturb 10
#
# LAN side (download limiting)
#
$TC qdisc add dev $LAN root handle 1: htb default 30
$TC class add dev $LAN parent 1: classid 1:1 htb rate $MAXRATE burst 15k
# The default class
$TC class add dev $LAN parent 1:1 classid 1:30 htb rate 1kbit ceil $DNLD burst 15k
$TC qdisc add dev $LAN parent 1:30 handle 30: sfq perturb 10
# This class is exempt from the download limit
$TC class add dev $LAN parent 1:1 classid 1:50 htb rate 1kbit ceil $MAXRATE burst 15k
$TC qdisc add dev $LAN parent 1:50 handle 50: sfq perturb 10
}
stop() {
# Stop the bandwidth shaping.
$TC qdisc del dev $WAN root
$TC qdisc del dev $LAN root
}
restart() {
# Self-explanatory.
stop
sleep 1
start
}
show() {
# Display status of traffic control status.
echo "Download ($LAN):"
$TC -s class show dev $LAN
echo ""
echo "Upload ($WAN):"
$TC -s class show dev $WAN
}
case "$1" in
start)
echo -n "Starting bandwidth shaping: "
start
echo "done"
;;
stop)
echo -n "Stopping bandwidth shaping: "
stop
echo "done"
;;
restart)
echo -n "Restarting bandwidth shaping: "
restart
echo "done"
;;
show)
echo "Bandwidth shaping status:"
show
echo ""
;;
*)
pwd=$(pwd)
echo "Usage: tc.bash {start|stop|restart|show}"
;;
esac
exit 0