Matteo Scandolo | 6288d5a | 2017-08-08 13:05:26 -0700 | [diff] [blame] | 1 | |
| 2 | {# |
| 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 | #} |
| 17 | |
| 18 | |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 19 | #!/bin/bash |
| 20 | # tc uses the following units when passed as a parameter. |
| 21 | # kbps: Kilobytes per second |
| 22 | # mbps: Megabytes per second |
| 23 | # kbit: Kilobits per second |
| 24 | # mbit: Megabits per second |
| 25 | # bps: Bytes per second |
| 26 | # Amounts of data can be specified in: |
| 27 | # kb or k: Kilobytes |
| 28 | # mb or m: Megabytes |
| 29 | # mbit: Megabits |
| 30 | # kbit: Kilobits |
| 31 | # To get the byte figure from bits, divide the number by 8 bit |
| 32 | # |
| 33 | |
| 34 | TC=/sbin/tc |
| 35 | |
| 36 | WAN=eth0 # External (WAN side) interface |
| 37 | LAN=eth1 # Customer (LAN side) interface |
| 38 | |
| 39 | MAXRATE=10gbit # Maximum upload/download rate |
| 40 | DNLD={{ downlink_speed }} # DOWNLOAD Limit |
| 41 | UPLD={{ uplink_speed }} # UPLOAD Limit |
| 42 | |
| 43 | [ "$DNLD" == "None" ] && DNLD=$MAXRATE |
| 44 | [ "$UPLD" == "None" ] && UPLD=$MAXRATE |
| 45 | |
| 46 | start() { |
| 47 | |
| 48 | # We'll use Hierarchical Token Bucket (HTB) to shape bandwidth. |
| 49 | # For detailed configuration options, please consult Linux man |
| 50 | # page. |
| 51 | |
| 52 | # |
| 53 | # WAN side (upload limiting) |
| 54 | # |
| 55 | $TC qdisc add dev $WAN root handle 1: htb default 30 |
| 56 | $TC class add dev $WAN parent 1: classid 1:1 htb rate $MAXRATE burst 15k |
| 57 | |
| 58 | # The default class |
| 59 | $TC class add dev $WAN parent 1:1 classid 1:30 htb rate 1kbit ceil $UPLD burst 15k |
| 60 | $TC qdisc add dev $WAN parent 1:30 handle 30: sfq perturb 10 |
| 61 | |
| 62 | # This class is exempt from the upload limit |
| 63 | $TC class add dev $WAN parent 1:1 classid 1:50 htb rate 1kbit ceil $MAXRATE burst 15k |
| 64 | $TC qdisc add dev $WAN parent 1:50 handle 50: sfq perturb 10 |
| 65 | |
| 66 | # |
| 67 | # LAN side (download limiting) |
| 68 | # |
| 69 | $TC qdisc add dev $LAN root handle 1: htb default 30 |
| 70 | $TC class add dev $LAN parent 1: classid 1:1 htb rate $MAXRATE burst 15k |
| 71 | |
| 72 | # The default class |
| 73 | $TC class add dev $LAN parent 1:1 classid 1:30 htb rate 1kbit ceil $DNLD burst 15k |
| 74 | $TC qdisc add dev $LAN parent 1:30 handle 30: sfq perturb 10 |
| 75 | |
| 76 | # This class is exempt from the download limit |
| 77 | $TC class add dev $LAN parent 1:1 classid 1:50 htb rate 1kbit ceil $MAXRATE burst 15k |
| 78 | $TC qdisc add dev $LAN parent 1:50 handle 50: sfq perturb 10 |
| 79 | |
| 80 | } |
| 81 | |
| 82 | stop() { |
| 83 | |
| 84 | # Stop the bandwidth shaping. |
| 85 | $TC qdisc del dev $WAN root |
| 86 | $TC qdisc del dev $LAN root |
| 87 | |
| 88 | } |
| 89 | |
| 90 | restart() { |
| 91 | |
| 92 | # Self-explanatory. |
| 93 | stop |
| 94 | sleep 1 |
| 95 | start |
| 96 | |
| 97 | } |
| 98 | |
| 99 | show() { |
| 100 | |
| 101 | # Display status of traffic control status. |
| 102 | echo "Download ($LAN):" |
| 103 | $TC -s class show dev $LAN |
| 104 | |
| 105 | echo "" |
| 106 | echo "Upload ($WAN):" |
| 107 | $TC -s class show dev $WAN |
| 108 | |
| 109 | } |
| 110 | |
| 111 | case "$1" in |
| 112 | |
| 113 | start) |
| 114 | |
| 115 | echo -n "Starting bandwidth shaping: " |
| 116 | start |
| 117 | echo "done" |
| 118 | ;; |
| 119 | |
| 120 | stop) |
| 121 | |
| 122 | echo -n "Stopping bandwidth shaping: " |
| 123 | stop |
| 124 | echo "done" |
| 125 | ;; |
| 126 | |
| 127 | restart) |
| 128 | |
| 129 | echo -n "Restarting bandwidth shaping: " |
| 130 | restart |
| 131 | echo "done" |
| 132 | ;; |
| 133 | |
| 134 | show) |
| 135 | |
| 136 | echo "Bandwidth shaping status:" |
| 137 | show |
| 138 | echo "" |
| 139 | ;; |
| 140 | |
| 141 | *) |
| 142 | |
| 143 | pwd=$(pwd) |
| 144 | echo "Usage: tc.bash {start|stop|restart|show}" |
| 145 | ;; |
| 146 | |
| 147 | esac |
| 148 | |
| 149 | exit 0 |