-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtc-limit.sh
77 lines (66 loc) · 2.02 KB
/
tc-limit.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
#!/bin/bash
# Full path to tc binary
i_eth=eth0
i_ibf=ifb211
# Define the upload and download speed limit, follow units can be
# passed as a parameter:
# kbps: Kilobytes per second
# mbps: Megabytes per second
# kbit: kilobits per second
# mbit: megabits per second
# bps: Bytes per second
download_limit=70mbit
function start_tc {
stop_tc
ip link add $i_ibf type ifb
ip link set dev $i_ibf up
tc qdisc add dev $i_eth ingress
tc filter add dev $i_eth parent ffff: protocol ip u32 match u32 0 0 flowid 1a64: action mirred egress redirect dev $i_ibf
tc qdisc add dev $i_ibf root handle 1a64: htb default 1
tc class add dev $i_ibf parent 1a64: classid 1a64:1 htb rate 32000000.0kbit
tc class add dev $i_ibf parent 1a64: classid 1a64:81 htb rate $download_limit ceil $download_limit burst 1250.0KB cburst 1250.0KB
tc qdisc add dev $i_ibf parent 1a64:81 handle 2fbe: netem
tc filter add dev $i_ibf protocol ip parent 1a64: prio 5 u32 match ip dst 0.0.0.0/0 match ip src 0.0.0.0/0 flowid 1a64:81
}
#
# Removes the network speed limiting and restores the default TC configuration
#
function stop_tc {
#tc qdisc del dev $i_eth root
tc qdisc del dev $i_eth ingress
tc qdisc del dev $i_ibf root
ip link set dev $i_ibf down
ip link delete $i_ibf type ifb
}
function show_status {
tc -s qdisc ls dev $i_ibf
tc class show dev $i_ibf
tc filter show dev $i_ibf
tc qdisc show dev $i_ibf
tc -s qdisc ls dev $i_eth
tc class show dev $i_eth
tc filter show dev $i_eth
tc qdisc show dev $i_eth
}
#
# Display help
#
function display_help {
echo "Usage: limit [OPTION] [BANDWIDTH_RATE]"
echo -e "\tstart BANDWIDTH_RATE - Apply the tc limit (limit start 100mbit)"
echo -e "\tstop - Remove the tc limit"
echo -e "\tstatus - Show status"
}
# Start
if [ -n "$2" ]; then
download_limit=$2
fi
if [ -z "$1" ]; then
display_help
elif [ "$1" == "start" ]; then
start_tc
elif [ "$1" == "stop" ]; then
stop_tc
elif [ "$1" == "status" ]; then
show_status
fi