You are on page 1of 4

30/9/2014

Mikrotik 4 WAN Load Balancing using PCC method. Complete Script ! by zaiB | Syed Jahanzaib Personnel Blog to Share Knowledge !

Mikrotik 4 WAN Load Balancing using PCC method. Complete


Script ! by zaiB
Filed under: Mikrotik Related Tags: mikrotik 4 wan load balance, mikrotik bandwidth, mikrotik howto combine multiple
wan links, mikrotik pcc,multi wan, pcc, rate limit Syed Jahanzaib / Pinochio~:) @ 3:32 PM

57 Votes

Updated 4th December, 2013


Following is a complete script for Mikrotik to Load Balance 4 WAN links OR DSL links , the script idea is taken
from
http://wiki.mikrotik.com/wiki/Manual:PCC#Example
In this example I have used MikrotikT RB750 5 ports router. 4 ports were connected with Four DSL Routers,
and 5thport was connected with User LAN. In this particual screenshots example, all DSL lines have un-equal
speed. Also dont forget to rename the interface names accordingly.
In my personnel experience , If users request are directly hitting Mikrotik configured with PCC , then you will get
good load balancing. Use src-address as classifier, this way you will get rid of problems like https/broken link,
streaming issues etc. Load balancing using this PCCtechnique (src-address) will be effective and balanced
approach when more and more connections (from clients) that occurred.
I also recommend to use SQUID proxy server along with mikrotik , for better response time and it will also
increase good browsing experience to users.
If somehow you are not satisfied with thesrc-address approach,play with the PCC Classifier, Try both addresses
and ports as the classifier. While this will randomize things the most and in theory give you the most fair
allocation of bandwidth, BUT there is also a good chance that it will break certain things like banking web sites
and some forums. This is because often times a HTTP requests will generate several connections, so there is a
chance that some requests may go out a different route than the initial one, and that will break secure web sites.
For that reason I usually like to go with src-address PCC load balancing approach (but only if users are hitting
the pcc box)
Result of this script is as follows on screenshots.
[Thanks to sam @ fastmesh for the screenshots

http://aacable.wordpress.com/2011/06/04/mikrotik-4-wan-load-balance-pcc-complete-script-by-zaib/

1/4

30/9/2014

Mikrotik 4 WAN Load Balancing using PCC method. Complete Script ! by zaiB | Syed Jahanzaib Personnel Blog to Share Knowledge !

Syed Jahanzaib

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

/ip
add
add
add
add
add

address
address=192.168.0.1/24
address=192.168.1.2/24
address=192.168.2.2/24
address=192.168.3.2/24
address=192.168.4.2/24

/ip
add
add
add
add

firewall mangle
chain=input in-interface=WAN1
chain=input in-interface=WAN2
chain=input in-interface=WAN3
chain=input in-interface=WAN4

add
add
add
add

chain=output
chain=output
chain=output
chain=output

add
add
add
add

chain=prerouting
chain=prerouting
chain=prerouting
chain=prerouting

dst-address=192.168.1.0/24
dst-address=192.168.2.0/24
dst-address=192.168.3.0/24
dst-address=192.168.4.0/24

add
add
add
add
add

chain=prerouting
chain=prerouting
chain=prerouting
chain=prerouting
chain=prerouting

dst-address-type=!local in-interface=Local per-conn


dst-address-type=!local in-interface=Local per-conn
dst-address-type=!local in-interface=Local per-conn
dst-address-type=!local in-interface=Local per-conn
connection-mark=WAN1_conn in-interface=Local action

network=192.168.0.0
network=192.168.1.0
network=192.168.2.0
network=192.168.3.0
network=192.168.4.0

broadcast=192.168.0.255
broadcast=192.168.1.255
broadcast=192.168.2.255
broadcast=192.168.3.255
broadcast=192.168.4.255

action=mark-connection
action=mark-connection
action=mark-connection
action=mark-connection

connection-mark=WAN1_conn
connection-mark=WAN2_conn
connection-mark=WAN3_conn
connection-mark=WAN4_conn

http://aacable.wordpress.com/2011/06/04/mikrotik-4-wan-load-balance-pcc-complete-script-by-zaib/

i
i
i
i
i

new-connectionnew-connectionnew-connectionnew-connection-

action=mark-routing
action=mark-routing
action=mark-routing
action=mark-routing
action=accept
action=accept
action=accept
action=accept

new-routi
new-routi
new-routi
new-routi

in-interfa
in-interfa
in-interfa
in-interfa

2/4

30/9/2014

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

Mikrotik 4 WAN Load Balancing using PCC method. Complete Script ! by zaiB | Syed Jahanzaib Personnel Blog to Share Knowledge !

add chain=prerouting connection-mark=WAN2_conn in-interface=Local action


add chain=prerouting connection-mark=WAN3_conn in-interface=Local action
add chain=prerouting connection-mark=WAN4_conn in-interface=Local action
/ip
add
add
add
add

route
dst-address=0.0.0.0/0
dst-address=0.0.0.0/0
dst-address=0.0.0.0/0
dst-address=0.0.0.0/0

gateway=192.168.1.1
gateway=192.168.2.1
gateway=192.168.3.1
gateway=192.168.4.1

routing-mark=to_WAN1
routing-mark=to_WAN2
routing-mark=to_WAN3
routing-mark=to_WAN4

add
add
add
add

dst-address=0.0.0.0/0
dst-address=0.0.0.0/0
dst-address=0.0.0.0/0
dst-address=0.0.0.0/0

gateway=192.168.1.1
gateway=192.168.2.1
gateway=192.168.3.1
gateway=192.168.4.1

distance=1
distance=2
distance=3
distance=4

/ip
add
add
add
add

firewall nat
chain=srcnat
chain=srcnat
chain=srcnat
chain=srcnat

out-interface=WAN1
out-interface=WAN2
out-interface=WAN3
out-interface=WAN4

check
check
check
check

check-gateway=p
check-gateway=p
check-gateway=p
check-gateway=p

action=masquerade
action=masquerade
action=masquerade
action=masquerade

Now Configure DNS server so users can resolve hostnames,

/ip dns set allow-remote-requests=yes cache-max-ttl=1w cache-size=5000KiB

All Done ! Now Test the link by putting user load, the more multiple users load you put on it, the better Load
Balance result you will get

PCC WITH UN-EQUAL WAN LINKS


If you have Un-Equal WAN Links, for example WAN,1,2,3 are of 4MB and WAN,4 is of 8 Mb, and you want to force
MT to use WAN4 link more then other because of its capacity, Then you have to Add more PCC rules assigning
the same two marks to a specific link i.e WAN4 , something like
Code:

1
2
3
4
5

add
add
add
add
add

chain=prerouting
chain=prerouting
chain=prerouting
chain=prerouting
chain=prerouting

dst-address-type=!local
dst-address-type=!local
dst-address-type=!local
dst-address-type=!local
dst-address-type=!local

in-interface=Local
in-interface=Local
in-interface=Local
in-interface=Local
in-interface=Local

per-conne
per-conne
per-conne
per-conne
per-conne

PCC WITH SAME GATEWAY

Try defining the outbound interface.


For example replace this.

http://aacable.wordpress.com/2011/06/04/mikrotik-4-wan-load-balance-pcc-complete-script-by-zaib/

3/4

30/9/2014

1
2
3
4
5
6
7
8
9

Mikrotik 4 WAN Load Balancing using PCC method. Complete Script ! by zaiB | Syed Jahanzaib Personnel Blog to Share Knowledge !

/ip route
add dst-address=0.0.0.0/0 gateway=1.2.3.4 routing-mark=to_wan-ISP1 check
add dst-address=0.0.0.0/0 gateway=1.2.3.4 routing-mark=to_wan-ISP2 check
With following

/ip route
add dst-address=0.0.0.0/0 gateway=1.2.3.4%eth1-ISP routing-mark=to_wan-I
add dst-address=0.0.0.0/0 gateway=1.2.3.4%eth2-ISP routing-mark=to_wan-I

Replace the eth1-ISP with your own wan interface name , use the notation % for sure.
If it works , let me know

http://aacable.wordpress.com/2011/06/04/mikrotik-4-wan-load-balance-pcc-complete-script-by-zaib/

4/4

You might also like