Limit all Openvz VPS traffic

Limit all Openvz VPS traffic

Postby lik » Sat Oct 03, 2009 12:58 am

There is a limiting function in hypervm for uplink, however the ability to limit downlink speed is not included.

For those who want to offer a truly unmetered VPS (with no bandwidth limit but a capped speed), this feature is inadequate as a customer could use more bandwidth than they are capped at by downloading more.

When HyperVM limits the uplink speed, it is inserting code into this file:

/usr/local/lxlabs/hypervm/etc/openvz_tc.sh


If you have used traffic limiting on a vps, skip this step and go on to the next. If you haven't used traffic limiting, this file will not exist, create it:

touch /usr/local/lxlabs/hypervm/etc/openvz_tc.sh


Now open it with vi or an editor you are comfortable with:

vi /usr/local/lxlabs/hypervm/etc/openvz_tc.sh


If you have used or currently are using traffic limiting on any VPS, you should see something similar to this at the top of the file:

#!/bin/sh
export PATH=$PATH:/sbin
tc qdisc del dev eth0 root
tc qdisc add dev eth0 root handle 1: cbq avpkt 1000 bandwidth 100mbit


If it isn't there, go ahead and paste it there.

After the above mentioned lines of code, HyperVM inserts code to limit the uplink on each VPS. For instance on VPS 110:

#vpsid 110
tc class add dev eth0 parent 1: classid 1:1 cbq rate 500kbps allot 1500 prio 5 bounded isolated
tc filter add dev eth0 parent 1: protocol ip prio 16 u32 match ip src xxx.xxx.xxx.xxx flowid 1:1
tc qdisc add dev eth0 parent 1:1 sfq perturb 1


Any line with a '#' before it is considered a comment and is ignored, hence the '#vpsid 110' is just letting you know that this is what VPS the following code is for. Take note of 'eth0', 'classid 1:1', 'rate 500kbps', 'flowid 1:1', 'ip src xxx.xxx.xxx.xxx' and 'parent 1:1' as these will be changing. I'm not going to go into a lengthy explanation of what everything is, you can check http://wiki.openvz.org for that.

Next we want to set the ability to limit the downlink speed. Firstly, under the following lines:

tc qdisc del dev eth0 root
tc qdisc add dev eth0 root handle 1: cbq avpkt 1000 bandwidth 100mbit


We add this:

tc qdisc del dev venet0 root
tc qdisc add dev venet0 root handle 1: cbq avpkt 1000 bandwidth 100mbit


Again, this will allow us to limit the downlink speed in each VPS with further commands.

Now we'll limit the downlink of the VPS itself. Under the following lines:

#vpsid 110
tc class add dev eth0 parent 1: classid 1:1 cbq rate 500kbps allot 1500 prio 5 bounded isolated
tc filter add dev eth0 parent 1: protocol ip prio 16 u32 match ip src xxx.xxx.xxx.xxx flowid 1:1
tc qdisc add dev eth0 parent 1:1 sfq perturb 1


We need to add this:

tc class add dev venet0 parent 1: classid 1:2 cbq rate 256kbps allot 1500 prio 5 bounded isolated
tc filter add dev venet0 parent 1: protocol ip prio 16 u32 match ip dst xxx.xxx.xxx.xxx flowid 1:2
tc qdisc add dev venet0 parent 1:2 sfq perturb 10



Notice we changed 'eth0' to 'venet0', as openvz uses venet0 to limit downlink traffic, whereas it uses eth0 to limit uplink.
We also changed 'classid 1:1' to 'classid 1:2'. This is because after every rule (whether it is limiting downlink or uplink), you need to increment classid by 1 (1:1 -> 1:2, 1:2 -> 1:3, etc).
We changed 'rate 500kbps' to 'rate 256kbps' just to show you can have different limits for up and downlinks.
We changed 'ip src xxx.xxx.xxx.xxx' to 'ip dst xxx.xxx.xxx.xxx'. What we did here was not change the IP address, but we changed 'dst' to 'src'.
We changed 'flowid 1:1' to 'flowid 1:2' for similar reasons to why we changed 'classid'.

After this, we save the file and run the following command:

/usr/local/lxlabs/hypervm/etc/openvz_tc.sh


This seems to go into effect immediately for my VPS.

Note, for 'classid' and 'flowid', these will always increment by one, no matter if you are adding more rules for one VPS or another VPS.

For instance, if I were to set both up and downlink limiting on VPS 120, you would see this:

#vpsid 120
tc class add dev eth0 parent 1: classid 1:3 cbq rate 500kbps allot 1500 prio 5 bounded isolated
tc filter add dev eth0 parent 1: protocol ip prio 16 u32 match ip src xxx.xxx.xxx.xxx flowid 1:3
tc qdisc add dev eth0 parent 1:3 sfq perturb 1
tc class add dev venet0 parent 1: classid 1:4 cbq rate 256kbps allot 1500 prio 5 bounded isolated
tc filter add dev venet0 parent 1: protocol ip prio 16 u32 match ip dst xxx.xxx.xxx.xxx flowid 1:4
tc qdisc add dev venet0 parent 1:4 sfq perturb 10



Another Note: you will want to keep a backup of this file somewhere because if you set uplink throttling in HyperVM and then add in downlink limiting manually, and then remove the uplink throttling you set in HyperVM, it will remove both uplink and downlink throttling for that VPS.

And a third note: You will know if you entered something incorrectly if you get this response:

RTNETLINK answers: No such file or directory


after running this:

/usr/local/lxlabs/hypervm/etc/openvz_tc.sh
lik
Founder
Founder
 
Posts: 497
Joined: Wed Dec 15, 2010 3:21 am

Re: Limit all Openvz VPS traffic

Postby lik » Sat Oct 03, 2009 12:58 am

lik
Founder
Founder
 
Posts: 497
Joined: Wed Dec 15, 2010 3:21 am


Return to Interesting links

 


  • Related topics
    Replies
    Views
    Last post
cron