Ticker

6/recent/ticker-posts

Limiting Clients Bandwidth by Quota Volume Bytes

Artikel dan Gambar dari : https://agratitudesign.blogspot.com/2013/11/limiting-clients-bandwidth-by-quota.html
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:

1
2
3
4
5
6
7
8
9
10
/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!
simple+queue+total+bytes

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 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/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 :
1
2
3
4
5
6
7
8
9
10
11
12
13
/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!
insert+system+scriptScript+List

script+limit-billing

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:

1
2
3
4
5
6
7
8
9
/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!

firewall+filter+drop

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.
1
2
3
4
5
6
7
8
9
10
/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!

system+scheduller
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!

Posting Komentar

0 Komentar