-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtclimit
executable file
·107 lines (92 loc) · 2.94 KB
/
tclimit
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
#!/bin/sh
if [ "$1" = "" -o "$2" = "" ]; then
exit;
fi
id=$3;
# Display status of traffic control status.
# $TC -s qdisc ls dev $IF
# these do not change
#
# Name of the traffic control command.
TC=/sbin/tc
START=1
# The network interface we're planning on limiting bandwidth.
#this in the vnetX
IF=$2 # Interface (changes)
hostname=`hostname`
if [ "$hostname" = "kvm1.trouble-free.net" -o "$hostname" = "kvm2.interserver.net" -o "$hostname" = "kvm50.interserver.net" ]; then
eth=eth1;
elif [ -e /etc/debian_version ]; then
if [ -e /sys/class/net/p2p1 ]; then
eth=p2p1;
elif [ -e /sys/class/net/em1 ]; then
eth=em1;
else
eth=eth0;
fi
eth=$(ip route|grep default|sed s#"^.*dev \([^ ]*\) .*$"#"\1"#g)
if [ "$eth" = "br0" ]; then
eth=$(brctl show $eth |grep -v interface|head -n 1|awk '{ print $4 }')
fi;
elif [ -e /etc/redhat-release ]; then
if [ -e /sys/class/net/enp9s0f0 ]; then
eth=enp9s0f0;
elif [ -e /sys/class/net/enp11s0f0 ]; then
eth=enp11s0f0;
else
eth=eth0;
fi
else
eth=eth0;
fi
OF=$eth
# Filter options for limiting the intended interface.
U32IF="$TC filter add dev $IF protocol ip parent 1:0 prio 1 u32"
U32OF="$TC filter add dev $OF protocol ip parent 1:0 prio 1 u32"
skip=0;
if [ -e /tools/bandwidth/$id/skip ]; then
echo "Skipping $id";
skip=1;
elif [ -e /tools/bandwidth/$id/80 ]; then
# Download limit (in mega bits)
DNLD=80mbit # DOWNLOAD Limit
# Upload limit (in mega bits)
UPLD=80mbit
elif [ -e /tools/bandwidth/$id/20 ]; then
# Download limit (in mega bits)
DNLD=20mbit # DOWNLOAD Limit
# Upload limit (in mega bits)
UPLD=20mbit
elif [ -e /tools/bandwidth/$id/10 ]; then
# Download limit (in mega bits)
DNLD=10mbit # DOWNLOAD Limit
# Upload limit (in mega bits)
UPLD=10mbit
# just in case nothing passed
else
# Download limit (in mega bits)
DNLD=100mbit # DOWNLOAD Limit
# Upload limit (in mega bits)
UPLD=100mbit # UPLOAD Limit
fi
# IP address of the machine we are controlling
IP=$1 # Host IP
# delete old rules
$TC qdisc del dev $IF root
$TC qdisc del dev $OF root
if [ -e /nobwlimit ]; then
echo "no bw limit set";
exit;
fi
if [ "$skip" = "0" ]; then
# create parent
$TC qdisc add dev $IF root handle 1:0 htb default 30 r2q 15
$TC qdisc add dev $OF root handle 1:0 htb default 30 r2q 15
# make rules
$TC class add dev $IF parent 1:0 classid 1:$START htb rate $DNLD
$TC class add dev $OF parent 1:0 classid 1:$START htb rate $UPLD
$U32IF match ip dst $IP/32 flowid 1:$START
$U32OF match ip src $IP/32 flowid 1:$START
# add support for multiple ips
START=`expr $START + 1`
fi