Saturday, October 10, 2009

bandwidth management MikroTik

One of the ways to avoid network traffic ‘jams’ is usage of traffic shaping in large networks. Traffic shaping and bandwidth allocation is implemented in the MikroTik RouterOS as queuing mechanism. Thus, the network administrator is able to allocate a definite portion of the total bandwidth and grant it to a particular network segment or interface. Also the bandwidth of particular nodes can be limited by using this mechanism.
Further on, several examples of using bandwidth management are given arranged according to complexity:
Example of Emulating a 128k/64k Line
Example of Emulating a 128k/64k Line

Assume we want to emulate a 128k download and 64k upload line connecting IP network 192.168.0.0/24. The network is served through the Local interface of customer's router. The basic network setup is in the following diagram:
128/64k Line
The IP addresses and routes of the MikroTik router are as follows:

[MikroTik] > ip address print
Flags: X - disabled, I - invalid, D - dynamic
# ADDRESS NETWORK BROADCAST INTERFACE
0 10.0.0.217/24 10.0.0.217 10.0.0.255 Public
1 192.168.0.254/24 192.168.0.0 192.168.0.255 Local
[MikroTik] > ip route print
Flags: X - disabled, I - invalid, D - dynamic, J - rejected,
C - connect, S - static, R - rip, O - ospf, B - bgp
# DST-ADDRESS G GATEWAY DISTANCE INTERFACE
0 S 0.0.0.0/0 r 10.0.0.1 1 Public
1 DC 192.168.0.0/24 r 0.0.0.0 0 Local
2 DC 10.0.0.0/24 r 0.0.0.0 0 Public
[MikroTik] >

Assume you want to limit the bandwidth to 128kbps on downloads and 64kbps on uploads for all hosts on the LAN. Bandwidth limitation is done by applying queues for outgoing interfaces regarding the traffic flow. It is enough to add two queues at the MikroTik router:
[MikroTik] queue simple>
add name=Down interface Local limit-at 128000
add name=UP interface Public limit-at 64000
[MikroTik] queue simple> print
Flags: X - disabled, I - invalid
0 name=Down src-address=0.0.0.0/0 dst-address=0.0.0.0/0 interface=Local
limit-at=128000 queue=default priority=8 bounded=yes
1 name=UP src-address=0.0.0.0/0 dst-address=0.0.0.0/0 interface=Public
limit-at=64000 queue=default priority=8 bounded=yes

[MikroTik] queue simple> .. tree print
Flags: X - disabled, I - invalid, D - dynamic
0 D name=Down parent=Local flow="" limit-at=128000 max-burst=20
queue=default priority=8 weight=1 allot=1514 bounded=yes bytes=60
packets=1
1 D name=UP parent=Public flow="" limit-at=64000 max-burst=20
queue=default priority=8 weight=1 allot=1514 bounded=yes bytes=4169
packets=30

[MikroTik] queue simple>

Leave all other parameters as set by default. The limit is approximately 128kbps going to the LAN and 64kbps leaving the client's LAN. Please note, that the queues have been added for the outgoing interfaces regarding the traffic flow.

To monitor the traffic flow through the interface while doing file transfer, use the '/interface monitor-traffic' command:

[MikroTik] interface> monitor-traffic Public
received-packets-per-second: 9
received-bits-per-second: 4.32kbps
sent-packets-per-second: 6
sent-bits-per-second: 65.58kbps

received-packets-per-second: 7
received-bits-per-second: 3.36kbps
sent-packets-per-second: 10
sent-bits-per-second: 65.15kbps

received-packets-per-second: 11
received-bits-per-second: 5.66kbps
sent-packets-per-second: 7
sent-bits-per-second: 52.70kbps

[MikroTik] interface>
If you want to exclude the server from being limited, add two queues for it with limit-at=0 (no limit) and move them to the top:
[MikroTik] queue simple>
add name=Serv_D interface=Local dst-address=192.168.0.17/32 limit-at=0
add name=Serv_U interface Public src-address=192.168.0.17/32 limit-at=0
[MikroTik] queue simple> print
Flags: X - disabled, I - invalid
0 name=Down src-address=0.0.0.0/0 dst-address=0.0.0.0/0 interface=Local
limit-at=128000 queue=default priority=8 bounded=yes
1 name=UP src-address=0.0.0.0/0 dst-address=0.0.0.0/0 interface=Public
limit-at=64000 queue=default priority=8 bounded=yes
2 name=Serv_D src-address=0.0.0.0/0 dst-address=192.168.0.17/32
interface=Local limit-at=0 queue=default priority=8 bounded=yes
3 name=Serv_U src-address=192.168.0.17/32 dst-address=0.0.0.0/0
interface=Public limit-at=0 queue=default priority=8 bounded=yes

[MikroTik] queue simple> move 2 0
[MikroTik] queue simple> move 3 1
[MikroTik] queue simple> print
Flags: X - disabled, I - invalid
0 name=Serv_D src-address=0.0.0.0/0 dst-address=192.168.0.17/32
interface=Local limit-at=0 queue=default priority=8 bounded=yes
1 name=Serv_U src-address=192.168.0.17/32 dst-address=0.0.0.0/0
interface=Public limit-at=0 queue=default priority=8 bounded=yes
2 name=Down src-address=0.0.0.0/0 dst-address=0.0.0.0/0 interface=Local
limit-at=128000 queue=default priority=8 bounded=yes
3 name=UP src-address=0.0.0.0/0 dst-address=0.0.0.0/0 interface=Public
limit-at=64000 queue=default priority=8 bounded=yes
[MikroTik] queue simple>