OpenVZ Forum


Home » General » Support » How to limit traffic per vps?
How to limit traffic per vps? [message #29335] Thu, 10 April 2008 19:46 Go to next message
silentninja is currently offline  silentninja
Messages: 37
Registered: September 2007
Member
Hello, I've recently read this message on the wiki:
http://wiki.openvz.org/Traffic_shaping_with_tc

But I've haven't undestood it totally.
My VPS Servers both work on CentOS and RedHat Enterprise servers, I think it would be great to limit each vps to 512kbps or 1024kbps outbound and inbound.

Is there any basic script to limit per each vps on the server at 512k and not all the venet at once?

[Updated on: Thu, 10 April 2008 20:08]

Report message to a moderator

Re: How to limit traffic per vps? [message #31363 is a reply to message #29335] Wed, 25 June 2008 18:33 Go to previous messageGo to next message
silentninja is currently offline  silentninja
Messages: 37
Registered: September 2007
Member
I know you should hate to answer silly questions to newbie users, but if you just give me the start point i could go on..

In this link:
http://wiki.openvz.org/Traffic_shaping_with_tc

There's a Script about limiting with an HTB and TC but I want to limit all traffic from that VPS to no more than 512 kbps (in / out) but on all examples shows 100mbit, 10mbit, 20mbit, 30mbit... and I don't understand which values I should use..

Or should I use the first two examples (incoming / outgoing limits) with which change to limit to 512 kbps ?
Re: How to limit traffic per vps? [message #31453 is a reply to message #31363] Tue, 01 July 2008 00:23 Go to previous messageGo to next message
swindmill is currently offline  swindmill
Messages: 57
Registered: April 2007
Member
Try something like this:

#!/bin/bash

IFSPEED="100mbit"
SPEEDLIMIT="512kbit"

SHAPEIPS=`echo 192.168.1.{10..20}`
STARTSHAPECLASS=10

addtc()
{
        tc qdisc del dev $DEV root

        tc qdisc add dev $DEV root handle 1: htb

        tc class add dev $DEV parent 1: classid 1:1 htb rate "$IFSPEED" burst 15k quantum 60000

        SHAPECLASS=$STARTSHAPECLASS

        for IP in `echo "$SHAPEIPS"`
        do
                tc class add dev $DEV parent 1:1 classid 1:"$SHAPECLASS" htb rate "$SPEEDLIMIT" quantum 3000
                SHAPECLASS=$(($SHAPECLASS+1))
        done

        SHAPECLASS=$STARTSHAPECLASS

        for IP in `echo "$SHAPEIPS"`
        do
                tc filter add dev $DEV protocol ip parent 1:1 prio 1 u32 match ip $1 "$IP" flowid 1:"$SHAPECLASS"
                SHAPECLASS=$(($SHAPECLASS+1))
        done

}


DEV="eth0"

addtc src

DEV="venet0"

addtc dst


This will limit each VE IP address in the range of 192.168.1.10 through 192.168.1.20 to 512kbit/sec in and out. I didn't actually test this but it does seem to produce valid commands and should serve your needs.

The commands this example executes are:

tc qdisc del dev eth0 root
tc qdisc add dev eth0 root handle 1: htb
tc class add dev eth0 parent 1: classid 1:1 htb rate 100mbit burst 15k quantum 60000
tc class add dev eth0 parent 1:1 classid 1:10 htb rate 512kbit quantum 3000
tc class add dev eth0 parent 1:1 classid 1:11 htb rate 512kbit quantum 3000
tc class add dev eth0 parent 1:1 classid 1:12 htb rate 512kbit quantum 3000
tc class add dev eth0 parent 1:1 classid 1:13 htb rate 512kbit quantum 3000
tc class add dev eth0 parent 1:1 classid 1:14 htb rate 512kbit quantum 3000
tc class add dev eth0 parent 1:1 classid 1:15 htb rate 512kbit quantum 3000
tc class add dev eth0 parent 1:1 classid 1:16 htb rate 512kbit quantum 3000
tc class add dev eth0 parent 1:1 classid 1:17 htb rate 512kbit quantum 3000
tc class add dev eth0 parent 1:1 classid 1:18 htb rate 512kbit quantum 3000
tc class add dev eth0 parent 1:1 classid 1:19 htb rate 512kbit quantum 3000
tc class add dev eth0 parent 1:1 classid 1:20 htb rate 512kbit quantum 3000
tc filter add dev eth0 protocol ip parent 1:1 prio 1 u32 match ip src 192.168.1.10 flowid 1:10
tc filter add dev eth0 protocol ip parent 1:1 prio 1 u32 match ip src 192.168.1.11 flowid 1:11
tc filter add dev eth0 protocol ip parent 1:1 prio 1 u32 match ip src 192.168.1.12 flowid 1:12
tc filter add dev eth0 protocol ip parent 1:1 prio 1 u32 match ip src 192.168.1.13 flowid 1:13
tc filter add dev eth0 protocol ip parent 1:1 prio 1 u32 match ip src 192.168.1.14 flowid 1:14
tc filter add dev eth0 protocol ip parent 1:1 prio 1 u32 match ip src 192.168.1.15 flowid 1:15
tc filter add dev eth0 protocol ip parent 1:1 prio 1 u32 match ip src 192.168.1.16 flowid 1:16
tc filter add dev eth0 protocol ip parent 1:1 prio 1 u32 match ip src 192.168.1.17 flowid 1:17
tc filter add dev eth0 protocol ip parent 1:1 prio 1 u32 match ip src 192.168.1.18 flowid 1:18
tc filter add dev eth0 protocol ip parent 1:1 prio 1 u32 match ip src 192.168.1.19 flowid 1:19
tc filter add dev eth0 protocol ip parent 1:1 prio 1 u32 match ip src 192.168.1.20 flowid 1:20
tc qdisc del dev venet0 root
tc qdisc add dev venet0 root handle 1: htb
tc class add dev venet0 parent 1: classid 1:1 htb rate 100mbit burst 15k quantum 60000
tc class add dev venet0 parent 1:1 classid 1:10 htb rate 512kbit quantum 3000
tc class add dev venet0 parent 1:1 classid 1:11 htb rate 512kbit quantum 3000
tc class add dev venet0 parent 1:1 classid 1:12 htb rate 512kbit quantum 3000
tc class add dev venet0 parent 1:1 classid 1:13 htb rate 512kbit quantum 3000
tc class add dev venet0 parent 1:1 classid 1:14 htb rate 512kbit quantum 3000
tc class add dev venet0 parent 1:1 classid 1:15 htb rate 512kbit quantum 3000
tc class add dev venet0 parent 1:1 classid 1:16 htb rate 512kbit quantum 3000
tc class add dev venet0 parent 1:1 classid 1:17 htb rate 512kbit quantum 3000
tc class add dev venet0 parent 1:1 classid 1:18 htb rate 512kbit quantum 3000
tc class add dev venet0 parent 1:1 classid 1:19 htb rate 512kbit quantum 3000
tc class add dev venet0 parent 1:1 classid 1:20 htb rate 512kbit quantum 3000
tc filter add dev venet0 protocol ip parent 1:1 prio 1 u32 match ip dst 192.168.1.10 flowid 1:10
tc filter add dev venet0 protocol ip parent 1:1 prio 1 u32 match ip dst 192.168.1.11 flowid 1:11
tc filter add dev venet0 protocol ip parent 1:1 prio 1 u32 match ip dst 192.168.1.12 flowid 1:12
tc filter add dev venet0 protocol ip parent 1:1 prio 1 u32 match ip dst 192.168.1.13 flowid 1:13
tc filter add dev venet0 protocol ip parent 1:1 prio 1 u32 match ip dst 192.168.1.14 flowid 1:14
tc filter add dev venet0 protocol ip parent 1:1 prio 1 u32 match ip dst 192.168.1.15 flowid 1:15
tc filter add dev venet0 protocol ip parent 1:1 prio 1 u32 match ip dst 192.168.1.16 flowid 1:16
tc filter add dev venet0 protocol ip parent 1:1 prio 1 u32 match ip dst 192.168.1.17 flowid 1:17
tc filter add dev venet0 protocol ip parent 1:1 prio 1 u32 match ip dst 192.168.1.18 flowid 1:18
tc filter add dev venet0 protocol ip parent 1:1 prio 1 u32 match ip dst 192.168.1.19 flowid 1:19
tc filter add dev venet0 protocol ip parent 1:1 prio 1 u32 match ip dst 192.168.1.20 flowid 1:20

[Updated on: Tue, 01 July 2008 02:56]

Report message to a moderator

Re: How to limit traffic per vps? [message #31454 is a reply to message #31453] Tue, 01 July 2008 01:15 Go to previous messageGo to next message
silentninja is currently offline  silentninja
Messages: 37
Registered: September 2007
Member
THANK YOU, SO MUCH Shocked Very Happy
Re: How to limit traffic per vps? [message #31455 is a reply to message #31454] Tue, 01 July 2008 02:28 Go to previous messageGo to next message
swindmill is currently offline  swindmill
Messages: 57
Registered: April 2007
Member
I made an error in the script and have modified it.

The bottom portion of the script should read:

DEV="eth0"

addtc src

DEV="venet0"

addtc dst

Re: How to limit traffic per vps? [message #31572 is a reply to message #29335] Thu, 03 July 2008 18:17 Go to previous messageGo to next message
silentninja is currently offline  silentninja
Messages: 37
Registered: September 2007
Member
It seems to work well since the tc filters and classes show up when I do the
tc filter show dev eth0 parent 1:1
command.. hard to type though, heh

(Before reading next lines "kb" means kbites, "KB" means kbYtes)

But.. it's not filtering anything, I've tried to download a file from one of the VPS limited to 512 kb and it's downloading at 380 KB per second.. it shouldn't go faster than 80 KB/s because it's been limited...

Check my show output (i've modified the script so some vps have a 256 kb limit and a 512 kb limit, the IFSPEED has been set up to 4mbit since I don't want them to get more than that.

[root@vps0 ~]# tc class show dev eth0 parent 1:1
class htb 1:11 parent 1:1 prio 0 rate 256000bit ceil 256000bit burst 1632b cburst 1632b 
class htb 1:1 root rate 4000Kbit ceil 4000Kbit burst 15Kb cburst 2100b 
class htb 1:10 parent 1:1 prio 0 rate 256000bit ceil 256000bit burst 1632b cburst 1632b 
class htb 1:13 parent 1:1 prio 0 rate 512000bit ceil 512000bit burst 1664b cburst 1664b 
class htb 1:12 parent 1:1 prio 0 rate 256000bit ceil 256000bit burst 1632b cburst 1632b 
class htb 1:15 parent 1:1 prio 0 rate 512000bit ceil 512000bit burst 1664b cburst 1664b 
class htb 1:14 parent 1:1 prio 0 rate 256000bit ceil 256000bit burst 1632b cburst 1632b 
class htb 1:16 parent 1:1 prio 0 rate 512000bit ceil 512000bit burst 1664b cburst 1664b 


And this are the filters...
[root@vps0 ~]# tc filter show dev eth0 parent 1:1
filter protocol ip pref 1 u32 
filter protocol ip pref 1 u32 fh 800: ht divisor 1 
filter protocol ip pref 1 u32 fh 800::800 order 2048 key ht 800 bkt 0 flowid 1:10 
  match c9ebfd5b/ffffffff at 12
filter protocol ip pref 1 u32 fh 800::801 order 2049 key ht 800 bkt 0 flowid 1:11 
  match c9ebfd5d/ffffffff at 12
filter protocol ip pref 1 u32 fh 800::802 order 2050 key ht 800 bkt 0 flowid 1:12 
  match c9ebfd5f/ffffffff at 12
filter protocol ip pref 1 u32 fh 800::803 order 2051 key ht 800 bkt 0 flowid 1:13 
  match c9ebfd60/ffffffff at 12
filter protocol ip pref 1 u32 fh 800::804 order 2052 key ht 800 bkt 0 flowid 1:14 
  match c9ebfd61/ffffffff at 12
filter protocol ip pref 1 u32 fh 800::805 order 2053 key ht 800 bkt 0 flowid 1:15 
  match c9ebfd62/ffffffff at 12
filter protocol ip pref 1 u32 fh 800::806 order 2054 key ht 800 bkt 0 flowid 1:16 
  match c9ebfd63/ffffffff at 12


Am I doing something wrong?
Re: How to limit traffic per vps? [message #31580 is a reply to message #31572] Thu, 03 July 2008 22:20 Go to previous messageGo to next message
swindmill is currently offline  swindmill
Messages: 57
Registered: April 2007
Member
Did you note the changes I made after the original post?
Re: How to limit traffic per vps? [message #31651 is a reply to message #31580] Fri, 04 July 2008 14:01 Go to previous messageGo to next message
silentninja is currently offline  silentninja
Messages: 37
Registered: September 2007
Member
This is my script.

- Modification: I've added an if so if the vps has 768 ram, It gets a 256kbit limit and if the vps has a higher limit 512kbit limit.
- Modification: The SHAPEIPS gets filled with the IP's on the vz conf files automatically.
- Modification: I've changed the total ammount of the webserver to 4mbit since I cannot afford 100mbit like your example and even if all 8 vps has 512kbit limit, it should never go further the 4mbits.

This is my script:

#!/bin/bash

IFSPEED="4mbit"
SHAPEIPS=`grep IP_ADDRESS /etc/vz/conf/* | cut -d\" -f2 | sed 's/ /\n/'`
STARTSHAPECLASS=10

addtc()
{
        tc qdisc del dev $DEV root
        tc qdisc add dev $DEV root handle 1: htb
        tc class add dev $DEV parent 1: classid 1:1 htb rate "$IFSPEED" burst 15k quantum 60000
        SHAPECLASS=$STARTSHAPECLASS
        for IP in `echo "$SHAPEIPS"`
        do
                if [ `grep "IP_ADDR" /etc/vz/conf/* | grep "$IP" | cut -d: -f1 | xargs -iFILE grep VMGUAR FILE | grep -c 768` -eq 1 ]; then SPEEDLIMIT="256kbit"; else SPEEDLIMIT="512kbit"; fi;
                tc class add dev $DEV parent 1:1 classid 1:"$SHAPECLASS" htb rate "$SPEEDLIMIT" quantum 3000
                SHAPECLASS=$(($SHAPECLASS+1))
        done
        SHAPECLASS=$STARTSHAPECLASS
        for IP in `echo "$SHAPEIPS"`
        do
                tc filter add dev $DEV protocol ip parent 1:1 prio 1 u32 match ip $1 "$IP" flowid 1:"$SHAPECLASS"
                SHAPECLASS=$(($SHAPECLASS+1))
        done
}

# Trafico saliente
DEV="eth0"
addtc src
# Trafico entrante
DEV="venet0"
addtc dst

[Updated on: Fri, 04 July 2008 14:02]

Report message to a moderator

Re: How to limit traffic per vps? [message #31653 is a reply to message #31651] Fri, 04 July 2008 14:41 Go to previous messageGo to next message
silentninja is currently offline  silentninja
Messages: 37
Registered: September 2007
Member
I've noticed that the HTB example on the wiki creates one qdisc for each class.. and this script doesn't, may that be something ?

EDIT: YEAP, it was that, and the quantum amounts Smile, this is the fixed script for anyone

#!/bin/bash

# Server Total Bandwidth
IFSPEED="4mbit"
MAXBURST="256kbit"

# Get VE IPs 
SHAPEIPS=`grep IP_ADDRESS /etc/vz/conf/* | cut -d\" -f2 | sed 's/ /\n/'`

# Each VE speed
SPEEDLIMIT="512kbit"
VEBURST="64kbit"

# Shape Number constant
STARTSHAPECLASS=10

addtc()
{
        tc qdisc del dev $DEV root
        tc qdisc add dev $DEV root handle 1: htb default 10
        tc class add dev $DEV parent 1: classid 1:1 htb rate "$IFSPEED" burst "$MAXBURST"
        SHAPECLASS=$STARTSHAPECLASS
        for IP in `echo "$SHAPEIPS"`
        do
                tc class add dev $DEV parent 1:1 classid 1:"$SHAPECLASS" htb rate "$SPEEDLIMIT" ceil "$SPEEDLIMIT" burst "$VEBURST"
		tc qdisc add dev $DEV parent 1:"$SHAPECLASS" handle "$SHAPECLASS": sfq perturb 10
                SHAPECLASS=$(($SHAPECLASS+1))
        done
        SHAPECLASS=$STARTSHAPECLASS
        for IP in `echo "$SHAPEIPS"`
        do
                tc filter add dev $DEV protocol ip parent 1:0 prio 1 u32 match ip $1 "$IP" flowid 1:"$SHAPECLASS"
                SHAPECLASS=$(($SHAPECLASS+1))
        done
}

# Trafico saliente
DEV="eth0"
addtc src
# Trafico entrante
DEV="venet0"
addtc dst


It would be GREAT to update the example on the wiki, so anyone could edit it faster.

[Updated on: Fri, 04 July 2008 15:00]

Report message to a moderator

Re: How to limit traffic per vps? [message #31659 is a reply to message #31653] Fri, 04 July 2008 18:30 Go to previous message
swindmill is currently offline  swindmill
Messages: 57
Registered: April 2007
Member
Your addition of the sfq qdisc didn't fix the script if it wasn't working before. That just adds a leaf qdisc granting fairness to the packets going through the parent class.

I'm curious as to why my original script after my slight modification didn't work, but regardless I'm glad you got it working Smile
Previous Topic: [solved] 3ware 9690SA and 2.6.18-53.1.19.el5.028stab053.14
Next Topic: Xfce Openvz
Goto Forum:
  


Current Time: Sat Nov 16 12:27:51 GMT 2024

Total time taken to generate the page: 0.03105 seconds