birmingham@ic.fbi.gov , phoenix@ic.fbi.gov , fbise@leo.gov
NickSoft Linux Cookbook Index NickSoft Linux Cookbook
Quick howto's, Real live examples.
 
 FAQFAQ   SearchSearch   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Simple shaper [beta]

 
Post new topic   Reply to topic    NickSoft Linux Cookbook Index -> Bandwidth control
View previous topic :: View next topic  

Vote for this article
1 - useless
0%
 0%  [ 0 ]
2 - bad
0%
 0%  [ 0 ]
3 - not so bad
0%
 0%  [ 0 ]
4 - good
0%
 0%  [ 0 ]
5 - Excelent
0%
 0%  [ 0 ]
Total Votes : 0

Author Message
NickSoft
Site Admin


Joined: 13 Nov 2006
Posts: 22

PostPosted: Sat Nov 25, 2006 6:38 pm    Post subject: Simple shaper [beta] Reply with quote

This howto is not confirmed by users. Please write to Support forum if you have troubles or find any mistakes.

This is a simple script to control traffic for your users.
Let's say that you have a router with 2 network cards (NIC):
Code:
eth0 - connected to internet
eth1 - private network (your clients)

let's say that you use single class C network:
192.168.1.0/24

We assume that you use default kernel which has HTB and SFQ modules enabled by default and you have iproute2 installed (we need the tc utility.

Here is a simple script to set bandwidth limit for your users:
shaper.sh.gz

First you need to edit the file and set variables to match your settings. If you have users with IPs below x.x.x.11 you must alter START_IP variable. NET variable is your network. If your network is 10.1.1.0/24 you must NET will have this value:
NET='10.1.1.' #(not ip/mask format)

Important note:
This script controls only incoming speed (download). I'll rewrite the script soon to support upload speed control. If you want to control upload speed you must not use NAT on the same server that shaper is working:
users -> shaper and control -> NAT server

There is a way to use upload bandwidth control and NAT on the same machine, but this will be covered in another howto. To do this you'll need 2.6.x kernel or patch'o'matic for 2.4 kernel

Here is the source of this script:
Code:

#/bin/bash

# all speeds are in kilobytes per second
#speed of network interface - 100 MBit, about 10 MB/s (1024*10 KB/s)
let ALL=1024*10
# limit local traffic to leave have space for internet
let LAN_MIN=1024*8
let LAN_MAX=1024*9
#total internet connection - 2 MBit
let INET=256
#per user speed
let USERS_MIN=20
let USERS_MAX=60

# leave ips 192.168.1.2 to 192.168.1.10 for servers - mail server, file server, backup ....
let START_IP=11
EXTDEV='eth0'
INTDEV='eth1'
NET='192.168.1.'

TC=/sbin/tc

#show status
  if [ "$1" = "status" ] ; then
    $TC -s qdisc ls dev $INTDEV
    $TC -s class ls dev $INTDEV
    exit
  fi

#remove old settings
$TC qdisc del dev $INTDEV root 2> /dev/null > /dev/null
  if [ "$1" = "stop" ] ; then
    echo "Shaper is off"
    exit
  fi


$TC qdisc add dev $INTDEV root handle 1 htb default 0 r2q 1
$TC class add dev $INTDEV parent 1: classid 1:10 htb rate $[$ALL*8]Kbit ceil 95Mbit burst 15k prio 5 quantum 15000
$TC class add dev $INTDEV parent 1:10 classid 1:20 htb rate $[$LAN_MIN*8]Kbit ceil $[$LAN_MAX*8]Kbit burst 15k prio 5 quantum 15000
$TC class add dev $INTDEV parent 1:10 classid 1:30 htb rate $[$INET*8]Kbit ceil $[$INET*8]Kbit burst 15k prio 6 quantum 15000

#Set filters for local traffic
$TC filter add dev $INTDEV parent 1: protocol ip prio 100 u32 match ip dst 192.168.0.0/16 match ip src 192.168.0.0/16 classid 1:20
$TC filter add dev $INTDEV parent 1: protocol ip prio 100 u32 match ip dst 172.16.0.0/12 match ip src 172.16.0.0/12 classid 1:20
$TC filter add dev $INTDEV parent 1: protocol ip prio 100 u32 match ip src 10.0.0.0/8 match ip dst 10.0.0.0/8 classid 1:20


function setUser(){
# $1 - IP
# $2 - class id
# $3 - min speed in kbytes
# $4 - max speed in kbytes
  QUANTUM=""
  ip=$1
  cid=$2
  rate=$3
  ceil=$4
  let rate=rate*8
  let ceil=ceil*8
  if [ $rate -le 16 ] ; then
    QUANTUM="quantum 1500"
  fi
  if [ $rate -gt 512 ] ; then
    QUANTUM="quantum 15000"
  fi
  let brust=ceil/80

#  sfqclass=`echo $2|awk -F ":" '{print $2 ":"}'`

  $TC class add dev $INTDEV parent 1:30 classid 1:${cid} htb rate ${rate}Kbit ceil ${ceil}Kbit burst $brust prio 6 $QUANTUM
#  echo "$TC qdisc add dev $DEV parent $2 handle $sfqclass sfq perturb 10"
  $TC qdisc add dev $INTDEV parent 1:${cid} handle ${cid} sfq perturb 10
  $TC filter add dev $INTDEV parent 1:  protocol ip prio 100 u32 match ip dst ${ip} classid 1:${cid}
}

#start class for HTB
let n=100
for i in `seq $START_IP 254` ; do
  echo "${n}. setting ip ${NET}${i}"
  setUser "${NET}${i}" $n $USERS_MIN $USERS_MAX
  let n=n+1
done
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    NickSoft Linux Cookbook Index -> Bandwidth control All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




MLDb.org Magic Eight Ball Croler Web Search Croler Web Directory Bianca Ryan MyBestMatch.net Microlab.info Digger Services Sofia

Powered by 220V

AbuseIPDB Contributor Badge