Warning
If you know english better than me, fix errors and send them to me. Maybe in the future, when I am going to be rich, I will invest in my education, but not now.
Versions:
In the beginning I'd like to say that this software is in beta version so it means that shaperd is insufficiently tested and, in some cases, can make problems. Probably it will be upgraded more times.
After appearing of a new user in network and beginning data transfer from internet (local transfer don't allocate bandwidth), shaperd is testing user's connections and compares it with list of exceptions (/etc/shaper/ignore file). If the daemon can't find connection in exception list then allocate bandwidth for this user. Size of bandwidth results from simple dividing maximum incoming speed of link by number of currently working users.
After specified time (default: 10 seconds) - shaperd checks utilization of user's allocated bandwidth. If user utilized less than 50% of allocated bandwidth, daemon reduces allocation about 25%. If user utilized more than 75% then shaperd increases bandwidth about minimum guaranteed bandwidth (e.g. 8000 bit/s that is 1 KB/s). If user utilized bandwidth between 50% and 75% then allocation is unchanged. Rule is to allocate bandwidth in sequence for: "new" users, low allocation users and rest of unused bandwidth is assigned to high allocated users. I hope it's clear :)
Thanks to it shaperd prevents the connection from being took over by single maniacs using P2P software or idiot opening 30 sessions of FTP in the same moment,
I wrote Shaperd in C language and funcionally match to shaper_cbq shell script (also made by me (as you see - you can't choose :)
Still exists insoluble problem in 2.4.x kernels, which cause that some old masqueraded connections are hanging on by 5 days. As a solution shaperd ignore connections with last activity time bigger than 600 seconds.
Autor of this software will not respond for any damages, destructions or errors, caused by this software, description and anything else. Everythink you do - you do It on your own responsibility.
This software is tested on systems based on 2.4.8, 2.4.12, 2.4.18, 2.4.20, 2.4.21, 2.4.24, 2.6.8 kernels, ipchains and iptables firewalls (I'm using Linux Mandrake 9.1 and 10.1 for developing).
tc -d qdiscIf you'll get nothing as a result then your kernel support QoS (CBQ).
ipchains_path=/sbin/ipchains iptables_path=/sbin/iptables tc_path=/sbin/tc timeout_ipchains_22=3600 timeout_ipchains_24=7200 timeout_iptables_24=432000 timeout_global=300 start_speed=0 even_division=no continuous_control=yes squid_support=no squid_port=8080 divide_upload=yes extended_queue=no nomasq=yes check_sum_of_bandwidth=yes class_type=cbq upload_class_type=cbq check_download_factor_cnt=5 check_upload_factor_cnt=5 remove_dead_class=yes refresh_dead_fwrule=yes burst_type=maximum cburst_type=maximum htb_burst=8 htb_cburst=8 jump_type=5 nop_factor=60 add_factor=80 speed_ext=bit delay_mesg=yes mask_mesg=yes check_firewall_counters=12 debug=0 delay=10 write_delay=1 delay_pools_download=0/100,5000/75,20000/50 delay_pools_upload=0/100,5000/75,20000/50 inter_int=ppp0;8000;92000;1000;60000;auto;1 local_int=eth0;10485760;192.168.0.0/16;192.168.1.0/24;2Principies:
divide_upload=yes class_type=cbq upload_class_type=cbq jump_type=5 speed_ext=Kbit delay=10 write_delay=0 inter_int=ppp0;16;900;1;300;auto;1 local_int=eth0;10240;192.168.0.0/16;192.168.1.0/24;2 local_int=eth1;102400;192.168.0.0/16;192.168.2.0/24;3
divide_upload=yes class_type=cbq upload_class_type=cbq jump_type=5 speed_ext=Kbit debug=0 delay=10 write_delay=0 inter_int=eth0;8;440;1;100;auto;1 inter_int=ppp0;8;92;1;60;auto;2 local_int=eth1;10240;192.168.0.0/16;192.168.1.0/24;3 local_int=eth2;102400;192.168.0.0/16;192.168.2.0/24;4 local_int=ppp2;768;192.168.3.0/24;192.168.3.0/24;5
cd /usr/src/shaperd makeor in older versions:
gcc /usr/src/shaperd/shaperd.c -o /sbin/shaperd
192.168.1.2=eth0 ppp0 192.168.1.3=eth0 ppp0 192.168.1.4=eth0 ppp0 8000 16000 8000 16000 192.168.1.5=eth0 ppp0 192.168.2.2=eth1 ppp0 192.168.2.3=eth1 ppp0 192.168.2.4=eth1 ppp1 192.168.2.5=eth1 ppp1I introduced possibility of placing of individual limits for individual IP numbers additionally. In above been mentioned example for IP number 192.168.1.4. I put following forks: 8000 (1KB of - guaranteed minimum) and 16000 (2KB - maximum assigned speed) and second pair for upload control. Remember, that value this such myself are passed in unit of speed how put unit in file shaper.X.cfg in parameter speed_ext.
192.168.1.2=eth0 ppp0 192.168.1.3=eth0 ppp0 # IP numbers: 192.168.1.4 and 192.168.1.5 have one common allotment of bandwidth 192.168.1.4/31=eth0 ppp0 192.168.2.2=eth1 ppp0 192.168.2.3=eth1 ppp0 192.168.2.4=eth1 ppp1 192.168.2.5=eth1 ppp1Another example:
192.168.1.0/24=eth0 ppp0 192.168.2.0/24=eth1 ppp0In case of lack of registrations shaperd accepts values written in file shaper.X.cfg for every number.
lospeed = hispeed / max_number_of_IPswhere:
tc -s class show dev eth1and for HTB you can see:
class htb 2:5 parent 2:1 prio 3 rate 214Kbit ceil 214Kbit burst 27391b cburst 1872b Sent 27025335 bytes 34960 pkts (dropped 0, overlimits 0) rate 21702bps 28pps lended: 0 borrowed: 0 giants: 0 injects: 0 tokens: 778767 ctokens: 15581and for upload:
tc -s class show dev eth0for CBQ you can see:
class cbq 1:5 parent 1:1 rate 14Kbit (bounded) prio 5 Sent 554775 bytes 4674 pkts (dropped 0, overlimits 0) borrowed 30 overactions 0 avgidle 379301 undertime 0Additional 4 classes for queueing has a numbers based on rule X:Y where X is class identifier and Y = (4*(A+4))+PRIO
# download # highest priority for download - class 2:120 iptables -t mangle -A FORWARD -d 192.168.1.2 -m tos --tos 0x10 -j MARK --set-mark 2120 # medium priority for download - class 2:121 iptables -t mangle -A FORWARD -d 192.168.1.2 -m tos --tos 0x08 -j MARK --set-mark 2121 # low priority for download - 2:122 iptables -t mangle -A FORWARD -d 192.168.1.2 -m tos --tos 0x04 -j MARK --set-mark 2122 # Rest unclassified traffic will go to 2:123 class.Now example script for creating firewall rules based on /etc/shaper/queue_download_high.0 file: Analogically ypu should do the same for other queue files except queue_upload_server_* files which are still used by shaperd:
#!/bin/sh extqueue=`grep extended_queue=yes /etc/shaper/shaper.0.cfg | wc -l` IFS=';' if [ $extqueue -eq 1 ];then grep -E -v "^#|^$" /etc/shaper/queue_download_high.0 | while read prot src sport dst dport do if [ "$prot" == "*" ];then prot="!icmp" fi if [ "$src" == "*" ];then src="0/0" fi if [ "$dst" == "*" ];then dst="0/0" fi if [ "$sport" == "*" ];then sport="0:65535" fi if [ "$dport" == "*" ];then dport="0:65535" fi if [ "$prot" != "icmp" ];then iptables -t mangle -I FORWARD -p $prot -s $src --sport $sport -d $dst --dport $dport -j TOS --set-tos 0x10 else iptables -t mangle -I FORWARD -p $prot -s $src -d $dst -j TOS --set-tos 0x10 fi done fiMARK we will count from formula
M = (1000 * X) + PRIO + (4 * (A + 4))where M - Mark, X - class identifier, A - ip line position in iplist file, PRIO - 100, 101 or 102
tc -s filter show dev eth1 | grep 848and we will get as a result:
filter parent 2: protocol ip pref 10 fw handle 0x848 classid 2:120So it's correct. All packets with 0x848 MARK will go to 2:120 class. Now we can observe traffic going through this class:
watch tc -s class show dev eth1And we can see (if something was sent by this class) - for HTB:
class htb 2:120 parent 2:5 leaf a688: prio 0 rate 273Kbit ceil 273Kbit burst 15Kb cburst 1948b Sent 10644 bytes 133 pkts (dropped 0, overlimits 0) rate 11bps lended: 133 borrowed: 0 giants: 0 injects: 0 tokens: 315623 ctokens: 39721Now we create firewall rules for traffic to internet (upload):
# upload # lowest priority for upload - class 1:123 iptables -t mangle -A FORWARD -s 192.168.1.2 -d ! 192.168.0.0/16 -j MARK --set-mark 1123 # highest priority for upload - class 1:120 iptables -t mangle -A FORWARD -s 192.168.1.2 -d ! 192.168.0.0/16 -m tos --tos 0x10 -j MARK --set-mark 1120 # medium priority for upload - class 1:121 iptables -t mangle -A FORWARD -s 192.168.1.2 -d ! 192.168.0.0/16 -m tos --tos 0x08 -j MARK --set-mark 1121 # low priority for upload - class 1:122 iptables -t mangle -A FORWARD -s 192.168.1.2 -d ! 192.168.0.0/16 -m tos --tos 0x10 -j MARK --set-mark 1122The formula for counting MARK is the same as for download. You should know that upload traffic is limited on internet interface - not on local. So MARK for 192.168.1.2 and 100 priority (first IP in iplist file) is 1120 (in Hexadecimal system 0x460):
tc -s filter show dev eth0 | grep 460and we will see:
filter parent 1: protocol ip pref 10 fw handle 0x460 classid 1:120So it's ok. Now we will see traffic in this class:
watch tc -s class show dev eth0and we see:
class cbq 1:120 parent 1:5 leaf a69c: rate 18Kbit prio 3 Sent 12122 bytes 208 pkts (dropped 0, overlimits 0) borrowed 0 overactions 0 avgidle 8.07342e+06 undertime 0
iptables -t mangle -vxL POSTROUTING -n | grep 8080and
iptables -vxL squid0 -n
iptables -vxL squid0 -n | grep -E -v "destination|squid" | awk '{print $9,"\t",$2}'And if squid correctly mark TOS filed you will see (e.g.):
192.168.1.3 135967 0.0.0.0/0 2074683 192.168.1.7 2432944 192.168.1.6 18121074If all rules for users will have 0 and only 0.0.0.0/0 will count then you have problem - recompile your squid.
modprobe sch_cbq modprobe sch_tbf modprobe cls_u32For HTB you should load following modules:
modprobe sch_htb modprobe sch_sfq modprobe cls_u32
/sbin/shaperd shownat
192.168.1.2 40000 192.168.1.9 58000or if speed_ext is set as Kbit:
192.168.1.2 40 Kbit 192.168.1.9 58 Kbit
tc -s qdiscResults of his working can look so:
qdisc tbf d09f: dev eth0 rate 5430bps burst 10Kb lat 1.2s Sent 108930 bytes 73 pkts (dropped 0, overlimits 0) qdisc tbf d09e: dev eth0 rate 5430bps burst 10Kb lat 1.2s Sent 126618 bytes 89 pkts (dropped 0, overlimits 0) qdisc tbf d09d: dev eth0 rate 1638bps burst 10Kb lat 3.8s Sent 54699 bytes 65 pkts (dropped 0, overlimits 321) backlog 6110b 5p qdisc tbf d09c: dev eth0 rate 10Mbit burst 10Kb lat 4.8ms Sent 0 bytes 0 pkts (dropped 0, overlimits 0) qdisc tbf d09b: dev eth0 rate 10Mbit burst 10Kb lat 4.8ms Sent 5690 bytes 53 pkts (dropped 0, overlimits 0) qdisc cbq 10: dev eth0 rate 10Mbit (bounded,isolated) prio no-transmit Sent 310829 bytes 302 pkts (dropped 0, overlimits 1555) backlog 5p borrowed 0 overactions 0 avgidle 624 undertime 0Look on line from rate different from 10Mbit (or different in case of local link about different maximum speeds). In this of case of three first class illustrate transfers of three different computers in local net. Check, whether registered size of sent datas Sent are. If so this it means, that CBQ work correctly and cuts transfer.
<p><a href="https://cbq.trzepak.net/"> <img src="https://cbq.trzepak.net/linux/pics/shaperd.gif" border="0" width="102" height="47" alt="Powered by Shaper CBQ"></a></p>
Problems
To avoid itself problems, which they can result in track of normal exploitation of shaperd you have to warn some rules
ipchains -L output -ntable shaperX should be on the top position
Chain output (policy ACCEPT): target prot opt source destination ports shaout0 all ------ 0.0.0.0/0 0.0.0.0/0 n/a shaper0 all ------ 0.0.0.0/0 0.0.0.0/0 n/a ppp-out all ------ 0.0.0.0/0 0.0.0.0/0 n/a eth-out all ------ 0.0.0.0/0 0.0.0.0/0 n/a
ipchains -L shaper0 -nIn this table should be rules for every IP numbers from file /etc/shaper/iplist.0 Example:
Chain shaper0 (1 references): target prot opt source destination ports RETURN tcp ------ !192.168.1.1 192.168.1.2 * -> * RETURN tcp ------ !192.168.1.1 192.168.1.3 * -> * RETURN tcp ------ !192.168.1.1 192.168.1.4 * -> * RETURN tcp ------ !192.168.1.1 192.168.1.5 * -> * RETURN tcp ------ !192.168.1.1 192.168.1.6 * -> * RETURN tcp ------ !192.168.1.1 192.168.1.7 * -> * RETURN tcp ------ !192.168.1.1 192.168.1.8 * -> *
watch ipchains -vxL shaper0 -nIt will permit onto observation, do rules count quantity downloaded datas for individual IP numbers. Generally - if some number at present something download from internet, his rule has to count. If it does not make this - restart shaper.
iptables -L FORWARD -n --line-numbers | grep shatable shaperX should to have No 1 position:
1 shaout0 all -- 0.0.0.0/0 0.0.0.0/0
2 shaper0 all -- 0.0.0.0/0 0.0.0.0/0
iptables -L shaper0 -nIn this table should be rules for every IP numbers from file /etc/shaper/iplist.0 Example:
Chain shaper0 (1 references) target prot opt source destination RETURN all -- !192.168.0.0/16 192.168.1.2 RETURN all -- !192.168.0.0/16 192.168.1.3 RETURN all -- !192.168.0.0/16 192.168.1.4 RETURN all -- !192.168.0.0/16 192.168.1.5 RETURN all -- !192.168.0.0/16 192.168.1.6 RETURN all -- !192.168.0.0/16 192.168.1.7 RETURN all -- !192.168.0.0/16 192.168.1.8 RETURN all -- !192.168.0.0/16 192.168.1.9 RETURN all -- !192.168.0.0/16 192.168.1.10
watch iptables -vxL shaper0 -nIt will permit onto observation, do rules count quantity downloaded datas for individual IP numbers. Generally - if some number at present something download from internet, his rule has to count. If it does not make this - restart shaper.
Back | Linux |