blob: 0fd8b9495110b8e5acfdd32223102500b0155cb8 [file] [log] [blame]
Andrea Campanellaedfdbca2017-02-01 17:33:47 -08001#!/bin/bash
Scott Baker3b8ceca2017-10-12 11:38:50 -07002
3# Copyright 2017-present Open Networking Foundation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080017# tc uses the following units when passed as a parameter.
18# kbps: Kilobytes per second
19# mbps: Megabytes per second
20# kbit: Kilobits per second
21# mbit: Megabits per second
22# bps: Bytes per second
23# Amounts of data can be specified in:
24# kb or k: Kilobytes
25# mb or m: Megabytes
26# mbit: Megabits
27# kbit: Kilobits
28# To get the byte figure from bits, divide the number by 8 bit
29#
30
31TC=/sbin/tc
32
33WAN=eth0 # External (WAN side) interface
34LAN=eth1 # Customer (LAN side) interface
35
36MAXRATE=10gbit # Maximum upload/download rate
37DNLD={{ downlink_speed }} # DOWNLOAD Limit
38UPLD={{ uplink_speed }} # UPLOAD Limit
39
40[ "$DNLD" == "None" ] && DNLD=$MAXRATE
41[ "$UPLD" == "None" ] && UPLD=$MAXRATE
42
43start() {
44
45# We'll use Hierarchical Token Bucket (HTB) to shape bandwidth.
46# For detailed configuration options, please consult Linux man
47# page.
48
49 #
50 # WAN side (upload limiting)
51 #
52 $TC qdisc add dev $WAN root handle 1: htb default 30
53 $TC class add dev $WAN parent 1: classid 1:1 htb rate $MAXRATE burst 15k
54
55 # The default class
56 $TC class add dev $WAN parent 1:1 classid 1:30 htb rate 1kbit ceil $UPLD burst 15k
57 $TC qdisc add dev $WAN parent 1:30 handle 30: sfq perturb 10
58
59 # This class is exempt from the upload limit
60 $TC class add dev $WAN parent 1:1 classid 1:50 htb rate 1kbit ceil $MAXRATE burst 15k
61 $TC qdisc add dev $WAN parent 1:50 handle 50: sfq perturb 10
62
63 #
64 # LAN side (download limiting)
65 #
66 $TC qdisc add dev $LAN root handle 1: htb default 30
67 $TC class add dev $LAN parent 1: classid 1:1 htb rate $MAXRATE burst 15k
68
69 # The default class
70 $TC class add dev $LAN parent 1:1 classid 1:30 htb rate 1kbit ceil $DNLD burst 15k
71 $TC qdisc add dev $LAN parent 1:30 handle 30: sfq perturb 10
72
73 # This class is exempt from the download limit
74 $TC class add dev $LAN parent 1:1 classid 1:50 htb rate 1kbit ceil $MAXRATE burst 15k
75 $TC qdisc add dev $LAN parent 1:50 handle 50: sfq perturb 10
76
77}
78
79stop() {
80
81# Stop the bandwidth shaping.
82 $TC qdisc del dev $WAN root
83 $TC qdisc del dev $LAN root
84
85}
86
87restart() {
88
89# Self-explanatory.
90 stop
91 sleep 1
92 start
93
94}
95
96show() {
97
98# Display status of traffic control status.
99 echo "Download ($LAN):"
100 $TC -s class show dev $LAN
101
102 echo ""
103 echo "Upload ($WAN):"
104 $TC -s class show dev $WAN
105
106}
107
108case "$1" in
109
110 start)
111
112 echo -n "Starting bandwidth shaping: "
113 start
114 echo "done"
115 ;;
116
117 stop)
118
119 echo -n "Stopping bandwidth shaping: "
120 stop
121 echo "done"
122 ;;
123
124 restart)
125
126 echo -n "Restarting bandwidth shaping: "
127 restart
128 echo "done"
129 ;;
130
131 show)
132
133 echo "Bandwidth shaping status:"
134 show
135 echo ""
136 ;;
137
138 *)
139
140 pwd=$(pwd)
141 echo "Usage: tc.bash {start|stop|restart|show}"
142 ;;
143
144esac
145
146exit 0