forked from nokia/intel-nuc-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbwshaping.sh
executable file
·153 lines (118 loc) · 3.83 KB
/
bwshaping.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/bash
# Enable/disable debugging
#set -x
# Name of the traffic control command.
TC_CMD=/sbin/tc
TC="sudo $TC_CMD"
if [ ! -x $TC_CMD ]
then
yum -y install iproute
fi
# The network interfaces we're planning on limiting bandwidth.
if [ -f .nic.internal ] && [ -f .nic.external ]
then
# Need to perform shaping in both directions
# Shaping rules have to be applied on two interfaces
UP_IF=`cat .nic.external`
# Specify the external subnet to be rate-limited/throttled
UP_IP=`ip addr show $UP_IF | grep inet | grep -v inet6 | awk '{print $2}'`
echo "Using external interface: $UP_IF with address $UP_IP"
DL_IF=`cat .nic.internal`
# Specify the internal subnet to be rate-limited/throttled
DL_IP=`ip addr show $DL_IF | grep inet | grep -v inet6 | awk '{print $2}'`
echo "Using internal interface: $DL_IF with address $DL_IP"
else
echo "Error: unable to identify active interfaces"
echo "Specify external interface in file: .nic.external"
echo "Specify internal interface in file: .nic.internal"
fi
echo ""
start() {
# tc uses the following units when passed as a parameter.
echo "Traffic shaping bandwidth can be specified as:"
echo " kbps: Kilobytes per second"
echo " mbps: Megabytes per second"
echo " kbit: Kilobits per second"
echo " mbit: Megabits per second"
echo " bps: Bytes per second"
echo ""
echo -n "Specify upload bandwidth: "; read UP_BW
echo -n "Specify download bandwidth: "; read DL_BW
echo ""
# Amounts of data can be specified in:
# kb or k: Kilobytes
# kbit: Kilobits
# To get the byte figure from bits, divide the number by 8 bit
# We'll use Hierarchical Token Bucket (HTB) to shape bandwidth.
# For detailed configuration options, please consult Linux man
# page.
$TC qdisc add dev $UP_IF root handle 1: htb default 30
$TC class add dev $UP_IF parent 1: classid 1:1 htb rate $UP_BW
$TC class add dev $UP_IF parent 1: classid 1:2 htb rate $UP_BW
$TC qdisc add dev $DL_IF root handle 1: htb default 30
$TC class add dev $DL_IF parent 1: classid 1:1 htb rate $DL_BW
$TC class add dev $DL_IF parent 1: classid 1:2 htb rate $DL_BW
# Filter options for limiting the intended interface.
$TC filter add dev $UP_IF protocol ip parent 1:0 prio 1 u32 match ip src $UP_IP flowid 1:1
$TC filter add dev $UP_IF protocol ip parent 1:0 prio 1 u32 match ip dst $DL_IP flowid 1:2
$TC filter add dev $DL_IF protocol ip parent 1:0 prio 1 u32 match ip src $UP_IP flowid 1:1
$TC filter add dev $DL_IF protocol ip parent 1:0 prio 1 u32 match ip dst $DL_IP flowid 1:2
# For each interface...
#
# The first line creates the root qdisc, and the next two lines
# create two child qdisc that are to be used to shape download
# and upload bandwidth.
#
# The 4th and 5th line creates the filter to match the interface.
# The 'dst' IP address is used to limit download speed, and the
# 'src' IP address is used to limit upload speed.
}
stop() {
# Stop the bandwidth shaping.
if ($TC qdisc del dev $UP_IF root >/dev/null 2>&1) && ($TC qdisc del dev $DL_IF root >/dev/null 2>&1)
then
return
else
echo "ERROR"
echo "Shaping is not currently configured"; exit 1
fi
}
restart() {
# Self-explanatory.
stop
sleep 1
start
}
show() {
# Display status of traffic control status.
echo "Bandwidth shaping rules for external interface: $UP_IF"
$TC -s qdisc ls dev $UP_IF
echo ""
echo "Bandwidth shaping rules for external interface: $DL_IF"
$TC -s qdisc ls dev $DL_IF
}
case "$1" in
start)
start
echo "Traffic shaping rules configured"
;;
stop)
echo -n "Stopping bandwidth shaping: "
stop
echo "done"
;;
restart)
echo -n "Restarting bandwidth shaping: "
restart
echo "done"
;;
show)
show
echo ""
;;
*)
pwd=$(pwd)
echo "Usage: bandwidth-shaping.sh {start|stop|restart|show}"
;;
esac
exit 0