Graphic Motion Video Art, Website, IT Network, Culture

Loading...
Limiting Clients Bandwidth by Quota Volume Bytes
Bandwidth Management is most depending on the kind of your internet network clients services. For those of you that may not want to limit bandwidth by the type of connection packets, such as download, streaming, online games and others. Rather give freedom to use all the connection packets to your clients, and just want to limit the bandwidth clients by the quota volume bytes usage of the amount purchased by the clients. In this case the clients doesn't want to be limited by the kind of connection in use the internet connection. In other words, give me as fast as possible I will pay that.


The case is the rationale this idea, how to limit bandwidth by quota volume bytes usage. It allows you to give a different bandwidth limitation to your clients by volume quota bytes usage, of course it depends on the amount of total bytes that that represent the quota volume bytes that they bought and should they get. For that you must setup the total bytes that represent quota volume usage. Ok, let's get started!

1. Here I use Simple Queue that have most powerful to limit bandwidth every client/Ip address based on upload and download rates. We are going to limit bandwidth by Total Bytes that as accumulation of total upload and download. I have the script to create the simple queue of each clients. the script as follows:

/queue simple
add name="All Bandwidth" target-addresses=192.168.1.0/24 interface=all max-limit=2M/2M disabled=no parent="" priority=1 queue=default-small/default-small total-queue=default
add name="Billing" target-addresses=192.168.1.11 interface=all limit-at=256k/256k max-limit=256k/256k burst-limit=720k/720k burst-threshold=192k/192k burst-time=2s/2s disabled=no parent="All Bandwidth" priority=1 queue=default-small/default-small total-queue=default
add name="Client1" target-addresses=192.168.1.17 interface=all limit-at=256k/256k max-limit=256k/256k burst-limit=720k/720k burst-threshold=192k/192k burst-time=2s/2s disabled=no parent="All Bandwidth" priority=1 queue=default-small/default-small total-queue=default
add name="Client2" target-addresses=192.168.1.16 interface=all limit-at=256k/256k max-limit=256k/256k burst-limit=720k/720k burst-threshold=192k/192k burst-time=2s/2s disabled=no parent="All Bandwidth" priority=1 queue=default-small/default-small total-queue=default
add name="Client3" target-addresses=192.168.1.15 interface=all limit-at=256k/256k max-limit=256k/256k burst-limit=720k/720k burst-threshold=192k/192k burst-time=2s/2s disabled=no parent="All Bandwidth" priority=1 queue=default-small/default-small total-queue=default
add name="Client4" target-addresses=192.168.1.14 interface=all limit-at=256k/256k max-limit=256k/256k burst-limit=720k/720k burst-threshold=192k/192k burst-time=2s/2s disabled=no parent="All Bandwidth" priority=1 queue=default-small/default-small total-queue=default
add name="Client5" target-addresses=192.168.1.20 interface=all limit-at=256k/256k max-limit=256k/256k burst-limit=720k/720k burst-threshold=192k/192k burst-time=2s/2s disabled=no parent="All Bandwidth" priority=1 queue=default-small/default-small total-queue=default
add name="Client6" target-addresses=192.168.1.21 interface=all limit-at=256k/256k max-limit=256k/256k burst-limit=720k/720k burst-threshold=192k/192k burst-time=2s/2s disabled=no parent="All Bandwidth" priority=1 queue=default-small/default-small total-queue=default
add name="Master" target-addresses=192.168.1.8 interface=all limit-at=256k/256k max-limit=256k/256k burst-limit=720k/720k burst-threshold=192k/192k burst-time=2s/2s disabled=no parent="All Bandwidth" priority=1 queue=default-small/default-small total-queue=default

Script above after I was inserted to the new terminal window of the mikrotik winbox, the result being as shown below!

Make sure the Total Bytes on simple queue in an active state, the value is the sum of total rx bytes (upload) and total tx bytes (download). The Next we are going to limit the bandwidth per client based on the total bytes that achieved by a certain client.

2. Making the System Scripts for each client. This script contains the logic that determines the bandwidth limit based on the total bytes that have been achieved. Here you are free to determine the value of total bytes that used to limit the bandwidth of each client. For more quickly I give the same value to all the clients, you can change it yourself by total bytes are inappropriate given to your client. This script to create the system scripts being limit-client. In order to make the system scripts I divide it into two parts.

The first is the scripts that determines the bandwidth limit based on total bytes per client. You can give a different value according as you wish! Here the scripts of the clients system scripts :

/system script
add name=Limit-Billing source={
:local traf;
/queue simple
:set traf [get [find name="Billing"] total-bytes]
:if ($traf < 100000000) do={set [find name="Billing"] limit-at="256k/256k" max-limit="256k/256k" burst-limit="720k/720k" burst-threshold="192k/192k" burst-time="2s/2s" priority="1"}
:if ($traf > 100000000) do={set [find name="Billing"] limit-at="220k/220k" max-limit="220k/220k" burst-limit="720k/720k" burst-threshold="165k/165k" burst-time="2s/2s" priority="2"}
:if ($traf > 250000000) do={set [find name="Billing"] limit-at="200k/200k" max-limit="200k/200k" burst-limit="512k/512k" burst-threshold="150k/150k" burst-time="2s/2s" priority="3"}
:if ($traf > 500000000) do={set [find name="Billing"] limit-at="180k/180k" max-limit="180k/180k" burst-limit="512k/512k" burst-threshold="135k/135k" burst-time="2s/2s" priority="4"}
:if ($traf < 700000000) do={/ip firewall filter disable [find comment="Drop-Billing"]}
:if ($traf > 700000000) do={/ip firewall filter enable [find comment="Drop-Billing"]
/sys sched disa [find name="Sched-Billing"]}}

/system script
add name=Limit-Client1 source={
:local traf;
/queue simple
:set traf [get [find name="Client1"] total-bytes]
:if ($traf < 10000000) do={set [find name="Client1"] limit-at="256k/256k" max-limit="256k/256k" burst-limit="720k/720k" burst-threshold="192k/192k" burst-time="2s/2s" priority="1"}
:if ($traf > 10000000) do={set [find name="Client1"] limit-at="220k/220k" max-limit="220k/220k" burst-limit="720k/720k" burst-threshold="165k/165k" burst-time="2s/2s" priority="2"}
:if ($traf > 250000000) do={set [find name="Client1"] limit-at="200k/200k" max-limit="200k/200k" burst-limit="512k/512k" burst-threshold="150k/150k" burst-time="2s/2s" priority="3"}
:if ($traf > 500000000) do={set [find name="Client1"] limit-at="180k/180k" max-limit="180k/180k" burst-limit="512k/512k" burst-threshold="135k/135k" burst-time="2s/2s" priority="4"}
:if ($traf < 700000000) do={/ip firewall filter disable [find comment="Drop-Client1"]}
:if ($traf > 700000000) do={/ip firewall filter enable [find comment="Drop-Client1"]
/sys sched disa [find name="Sched-Client1"]}}

/system script
add name=Limit-Client2 source={
:local traf;
/queue simple
:set traf [get [find name="Client2"] total-bytes]
:if ($traf < 10000000) do={set [find name="Client2"] limit-at="256k/256k" max-limit="256k/256k" burst-limit="720k/720k" burst-threshold="192k/192k" burst-time="2s/2s" priority="1"}
:if ($traf > 10000000) do={set [find name="Client2"] limit-at="220k/220k" max-limit="220k/220k" burst-limit="720k/720k" burst-threshold="165k/165k" burst-time="2s/2s" priority="2"}
:if ($traf > 250000000) do={set [find name="Client2"] limit-at="200k/200k" max-limit="200k/200k" burst-limit="512k/512k" burst-threshold="150k/150k" burst-time="2s/2s" priority="3"}
:if ($traf > 500000000) do={set [find name="Client2"] limit-at="180k/180k" max-limit="180k/180k" burst-limit="512k/512k" burst-threshold="135k/135k" burst-time="2s/2s" priority="4"}
:if ($traf < 700000000) do={/ip firewall filter disable [find comment="Drop-Client2"]}
:if ($traf > 700000000) do={/ip firewall filter enable [find comment="Drop-Client2"]
/sys sched disa [find name="Sched-Client2"]}}

/system script
add name=Limit-Client3 source={
:local traf;
/queue simple
:set traf [get [find name="Client3"] total-bytes]
:if ($traf < 10000000) do={set [find name="Client3"] limit-at="256k/256k" max-limit="256k/256k" burst-limit="720k/720k" burst-threshold="192k/192k" burst-time="2s/2s" priority="1"}
:if ($traf > 10000000) do={set [find name="Client3"] limit-at="220k/220k" max-limit="220k/220k" burst-limit="720k/720k" burst-threshold="165k/165k" burst-time="2s/2s" priority="2"}
:if ($traf > 250000000) do={set [find name="Client3"] limit-at="200k/200k" max-limit="200k/200k" burst-limit="512k/512k" burst-threshold="150k/150k" burst-time="2s/2s" priority="3"}
:if ($traf > 500000000) do={set [find name="Client3"] limit-at="180k/180k" max-limit="180k/180k" burst-limit="512k/512k" burst-threshold="135k/135k" burst-time="2s/2s" priority="4"}
:if ($traf < 700000000) do={/ip firewall filter disable [find comment="Drop-Client3"]}
:if ($traf > 700000000) do={/ip firewall filter enable [find comment="Drop-Client3"]
/sys sched disa [find name="Sched-Client3"]}}


/system script
add name=Limit-Client4 source={
:local traf;
/queue simple
:set traf [get [find name="Client4"] total-bytes]
:if ($traf < 10000000) do={set [find name="Client4"] limit-at="256k/256k" max-limit="256k/256k" burst-limit="720k/720k" burst-threshold="192k/192k" burst-time="2s/2s" priority="1"}
:if ($traf > 10000000) do={set [find name="Client4"] limit-at="220k/220k" max-limit="220k/220k" burst-limit="720k/720k" burst-threshold="165k/165k" burst-time="2s/2s" priority="2"}
:if ($traf > 250000000) do={set [find name="Client4"] limit-at="200k/200k" max-limit="200k/200k" burst-limit="512k/512k" burst-threshold="150k/150k" burst-time="2s/2s" priority="3"}
:if ($traf > 500000000) do={set [find name="Client4"] limit-at="180k/180k" max-limit="180k/180k" burst-limit="512k/512k" burst-threshold="135k/135k" burst-time="2s/2s" priority="4"}
:if ($traf < 700000000) do={/ip firewall filter disable [find comment="Drop-Client4"]}
:if ($traf > 700000000) do={/ip firewall filter enable [find comment="Drop-Client4"]
/sys sched disa [find name="Sched-Client4"]}}

/system script
add name=Limit-Client5 source={
:local traf;
/queue simple
:set traf [get [find name="Client5"] total-bytes]
:if ($traf < 10000000) do={set [find name="Client5"] limit-at="256k/256k" max-limit="256k/256k" burst-limit="720k/720k" burst-threshold="192k/192k" burst-time="2s/2s" priority="1"}
:if ($traf > 10000000) do={set [find name="Client5"] limit-at="220k/220k" max-limit="220k/220k" burst-limit="720k/720k" burst-threshold="165k/165k" burst-time="2s/2s" priority="2"}
:if ($traf > 250000000) do={set [find name="Client5"] limit-at="200k/200k" max-limit="200k/200k" burst-limit="512k/512k" burst-threshold="150k/150k" burst-time="2s/2s" priority="3"}
:if ($traf > 500000000) do={set [find name="Client5"] limit-at="180k/180k" max-limit="180k/180k" burst-limit="512k/512k" burst-threshold="135k/135k" burst-time="2s/2s" priority="4"}
:if ($traf < 700000000) do={/ip firewall filter disable [find comment="Drop-Client5"]}
:if ($traf > 700000000) do={/ip firewall filter enable [find comment="Drop-Client5"]
/sys sched disa [find name="Sched-Client5"]}}

/system script
add name=Limit-Client6 source={
:local traf;
/queue simple
:set traf [get [find name="Client6"] total-bytes]
:if ($traf < 10000000) do={set [find name="Client6"] limit-at="256k/256k" max-limit="256k/256k" burst-limit="720k/720k" burst-threshold="192k/192k" burst-time="2s/2s" priority="1"}
:if ($traf > 10000000) do={set [find name="Client6"] limit-at="220k/220k" max-limit="220k/220k" burst-limit="720k/720k" burst-threshold="165k/165k" burst-time="2s/2s" priority="2"}
:if ($traf > 250000000) do={set [find name="Client6"] limit-at="200k/200k" max-limit="200k/200k" burst-limit="512k/512k" burst-threshold="150k/150k" burst-time="2s/2s" priority="3"}
:if ($traf > 500000000) do={set [find name="Client6"] limit-at="180k/180k" max-limit="180k/180k" burst-limit="512k/512k" burst-threshold="135k/135k" burst-time="2s/2s" priority="4"}
:if ($traf < 700000000) do={/ip firewall filter disable [find comment="Drop-Client6"]}
:if ($traf > 700000000) do={/ip firewall filter enable [find comment="Drop-Client6"]
/sys sched disa [find name="Sched-Client6"]}}

/system script
add name=Limit-Master source={
:local traf;
/queue simple
:set traf [get [find name="Master"] total-bytes]
:if ($traf < 10000000) do={set [find name="Master"] limit-at="256k/256k" max-limit="256k/256k" burst-limit="720k/720k" burst-threshold="192k/192k" burst-time="2s/2s" priority="1"}
:if ($traf > 10000000) do={set [find name="Master"] limit-at="220k/220k" max-limit="220k/220k" burst-limit="720k/720k" burst-threshold="165k/165k" burst-time="2s/2s" priority="2"}
:if ($traf > 250000000) do={set [find name="Master"] limit-at="200k/200k" max-limit="200k/200k" burst-limit="512k/512k" burst-threshold="150k/150k" burst-time="2s/2s" priority="3"}
:if ($traf > 500000000) do={set [find name="Master"] limit-at="180k/180k" max-limit="180k/180k" burst-limit="512k/512k" burst-threshold="135k/135k" burst-time="2s/2s" priority="4"}
:if ($traf < 700000000) do={/ip firewall filter disable [find comment="Drop-Master"]}
:if ($traf > 700000000) do={/ip firewall filter enable [find comment="Drop-Master"]
/sys sched disa [find name="Sched-Master"]}}

The value of total bytes is in five levels that represent a certain quota volumes bytes that is given different bandwidth limitation. If the value of the total bytes is greater the limit speed of the bandwidth is decreasing. then when it reaches a certain threshold of total bytes you can cut the bandwidth by using filter firewall drop so that a certain client didn't get the internet connection at all. If the clients is in a drop state the client schedulers does not work anymore or in disable state until the condition will reset it back.

The Second, making the script of system script to reset all the state of the conditions system of our clients. The script as follows :
/system script
add name="Reset-All" source={
/ip fire filt reset-counters-all
/que tree reset-counters-all
/que sim reset-counters-all
/sys sched ena [find name="Sched-Billing"]
/sys sched ena [find name="Sched-Client1"]
/sys sched ena [find name="Sched-Client2"]
/sys sched ena [find name="Sched-Client3"]
/sys sched ena [find name="Sched-Client4"]
/sys sched ena [find name="Sched-Client5"]
/sys sched ena [find name="Sched-Client6"]
/sys sched ena [find name="Sched-Master"]}

From the script above, you can see, this script will reset the firewall filter, queue tree, simple queue counters. And reset to enable system scheduler of all the clients. If all the scripts of system scripts you have inserted to the new terminal, then you can see the result in system>scripts on your Winbox. As shown like the picture below!


3. The scripts of system script above using firewall filters drop, we should create the firewall filter drop rules for all the clients that was given the comment name that associated with the above client's system script. The Script for firewall filters rules are as follows:

/ip firewall filter
add chain=forward comment="Drop-Billing" src-address=("192.168.1.11") dst-address=0.0.0.0/0 action=drop disable=yes
add chain=forward comment="Drop-Client1" src-address=("192.168.1.17") dst-address=0.0.0.0/0 action=drop disable=yes
add chain=forward comment="Drop-Client2" src-address=("192.168.1.16") dst-address=0.0.0.0/0 action=drop disable=yes
add chain=forward comment="Drop-Client3" src-address=("192.168.1.15") dst-address=0.0.0.0/0 action=drop disable=yes
add chain=forward comment="Drop-Client4" src-address=("192.168.1.14") dst-address=0.0.0.0/0 action=drop disable=yes
add chain=forward comment="Drop-Client5" src-address=("192.168.1.20") dst-address=0.0.0.0/0 action=drop disable=yes
add chain=forward comment="Drop-Client6" src-address=("192.168.1.21") dst-address=0.0.0.0/0 action=drop disable=yes
add chain=forward comment="Drop-Master" src-address=("192.168.1.8") dst-address=0.0.0.0/0 action=drop disable=yes

This script will create a firewall filter for each client in the default state are disable. Insert the above script to the new terminal Winbox, then the result as shown below!


4. Making the System Scheduler to run the all system script above it all automatically, you can set the start-time, and the interval time that make the router will check and execute the clients system scripts as mentioned in the on-event. The client's system scripts are Limit-Billing, Limit-Client1,etc.
/system scheduler
add name="Sched-Billing" on-event=Limit-Billing start-date=Jan/02/1970 start-time=06:00:00 interval=10m comment="Sched Billing Watcher" disabled=no
add name="Sched-Client1" on-event=Limit-Client1 start-date=Jan/02/1970 start-time=06:00:00 interval=10m comment="Sched Client1 Watcher" disabled=no
add name="Sched-Client2" on-event=Limit-Client2 start-date=Jan/02/1970 start-time=06:00:00 interval=10m comment="Sched Client2 Watcher" disabled=no
add name="Sched-Client3" on-event=Limit-Client3 start-date=Jan/02/1970 start-time=06:00:00 interval=10m comment="Sched Client3 Watcher" disabled=no
add name="Sched-Client4" on-event=Limit-Client4 start-date=Jan/02/1970 start-time=06:00:00 interval=10m comment="Sched Client4 Watcher" disabled=no
add name="Sched-Client5" on-event=Limit-Client5 start-date=Jan/02/1970 start-time=06:00:00 interval=10m comment="Sched Client5 Watcher" disabled=no
add name="Sched-Client6" on-event=Limit-Client6 start-date=Jan/02/1970 start-time=06:00:00 interval=10m comment="Sched Client6 Watcher" disabled=no
add name="Sched-Master" on-event=Limit-Master start-date=Jan/02/1970 start-time=06:00:00 interval=10m comment="Sched Master Watcher" disabled=no
add name="Sched-ResetAll" on-event=Reset-All start-date=Jan/02/1970 start-time=00:00:00 interval=24h comment="Sched AutoClear All" disabled=no

Once you adjust the system scheduler, as usual insert this script to the new terminal windows, it will looks like the picture below!

How does it works :
The system clients scheduler will run the clients system script, the client system script will run the script logic inside and make change the simple queue of the clients, if it reaches a threshold in certain quota volume bytes then the client system script will enabling firewall filter drop client and disabling the client scheduler itself. And then scheduler of reset all will reset all the counter included total bytes usage of the clients at a certain period and will enable the client scheduler to check and execute again the client system script. The client system will run the logic itself and find the value of the total bytes that has been reset by system reset all.

In some other case, I do not want to change the bandwidth limit speed, but I just want to change the priority of the clients using simple queue based on quota volume bytes. It is up to you, for more details, let's see the article video!

Share This Article :
Related Articles

14 comments :

  1. That is very useful. Thank you so much. I've been looking for such a script and I only find it here. It works fine. :)
    I've just some questions to implement this method on this case and I'd like to know your suggestion:

    There are 3 types of users and I don't want to have hotspot or any login based authentication systems.
    1-Free limited wireless Users (at most 30 concurrent devices) and they might change everyday.
    2-Staff (15-20 people), They are not changing everyday but they are still have some limitations in bandwidths and quota.
    3-Master Users (5-6 people) who are not limited so strictly, but we prefer to keep their usage data at leas for one week.

    Do you think that for each people I should add a bunch of rules and commands in all 3 or 4 parts of your scripts and then change MAC address of some of them into static?!
    By using your loop scripts in " Simple Way Limiting Bandwidth on Mikrotik Winbox" it will become more efficient but I'm afraid it becomes very complex and confusing and uses too much resources.
    Is there a way to have groups or something to keep it smaller and easier?!

    I also need to have different sets of rules based on weekdays. Maybe 2 or 3 would be enough, So how can I define something like a profile to activate them using different schedulers?

    I would greatly appreciate it if you kindly help me on finding a proper solution.

    Good Luck ;)

    ReplyDelete
    Replies
    1. I should update the topic that I mentioned in my comment about creating rules using loops. It's the correct one:
      http://arieccnewbie.blogspot.ca/2010/12/script-to-make-auto-simple-queue.html

      Delete
    2. hello my friend iwant reset Tx/Rx bytes(Reset countres) in my interfaces every 1min
      please help me

      Delete
  2. i wanna simple way limiting qouta bandwith? how does it works ?

    ReplyDelete
  3. simple way

    http://agratitudesign.blogspot.com/2013/05/separating-and-giving-priority-browsing.html#prettyPhoto

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. total bytes are lost every time router power off, is there a way to save this data

    ReplyDelete
  6. hi sir, how to implement this on hotspot?

    ReplyDelete
  7. local traf;
    for i from=1 to=254 do{
    :if ([/queue simple find target=("192.168.12." . $i)] != "") do={
    :set traf [get [/queue simple find name=("traf")] bytes]
    :if ($traf >$traf/0) do = {
    :set [ find target=("192.168.12." . $i)] max-limit= 64k/64k
    }
    }
    }
    hello every one this the script i do but have problem in configuration i use the mikrotik v6.3 but work what i get to do ?
    can you have help me

    ReplyDelete
  8. Dear Ketut Agus Suardika!

    It's an interesting article and I would like to try it. Can you help me some more information? What's the different between Client, Master and Billing? It's necessary to create Master and Billing and if it is, then how should I choose IP for them and what service or PC should I setup on the other side?
    Thank you for your time and help full answer.

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. If you are getting a message saying "Hp Driver update" then you are required to upadte your driver to keep using it. you can get the latest driver from 123.hp.com.

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. Hi, Why my total-bytes not recording. I have 6.43.4 (mipsbe) of Mikrotik.
    Please Help me.

    ReplyDelete

Back to Top