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