i need help for 2 wan bonding/balancing

Hi!
I am new to mikrotik and never made a single script before.I sow some youtube videos how mikrotik users successfully managed to bond 2 wans and got double bandwidth.
I have 2 routers(gateways) from 2 differens ISPs and i want to bond two wans. Also i have 2 lan network adapters in my pc case so i am asking am i able with my mikrotik RB951Ui-2nD to do the following:

  1. successfully bond two wans(if not then at least make load balancing)
  2. do that by connecting and using both of my lan network adapters
  3. send and receive all tcp traffic over first lan network adapter trough wan1 and all udp traffic(send and receive) over second lan adapter trough wan2

anyone can help me here please? Is this possible like it is on the drawing? If not bonding 2 wans at least i need only udp traffic on wan1 and tcp traffic on wan2 while rest of my family is connecting by wi fi trough gateway(wan2 adsl)?

It’s not specifically bonding you’re after, but load balancing in general. Bonding is connecting two pieces of hardware through multiple physical connections in a single logical one giving more bandwidth and/or fault tolerance, depending on what specific method you use (e.g. balance-rr or 802.3ad).

http://wiki.mikrotik.com/wiki/Load_Balancing provides a good overview of available methods.

In addition to the methods provided on this wiki page, Tomas’ presentation available http://mum.mikrotik.com/presentations/US12/tomas.pdf provides a good walkthrough on how to implement the bandwidth based one as well as how it is distinguished from other methods suchs als PCC or PBR.

If you want the TCP/UDP distribution as described above, you’re looking at PBR or Per-Traffic load balancing.

If you’re looking to more effectively use available bandwidth, you’re looking at solutions like PCC, NTH or bandwidth based load balancing script as explained in Tomas’ slides.

What you don’t mention is WHY want this TCP/UDP distribution, but I assume it’s for gaming purposes and I think there’s a better, more straight forward approach to make this work. However any more specific guidance requires more information of what you specifically want to achieve.

Also, you don’t mention you current status:

  • Is the Mikrotik already in place/working at least partially, or have you not implemented anything yet?
  • How do you currently use the WAN connections? Do you have WAN1 exclusively connected to one of the PC NICs and the other NIC connected to the wireless router to WAN2?
  • The picture suggests the PC is already splitting up the TCP/UDP traffic. How do you plan doing this? Do/can you make the application listen on a particular interface? Or do you expect the Mikrotik to take care of this?

Thanks for your prompt respond. Yes I want to separate UDP traffic from TCP traffic for gaming. Games I am playing are using UDP and UDP traffic can have bigger packet loss if tcp traffic is well synchronized( especially on my 0.5Mbit/s upload speed). I live in suburban area and wont be having fast fiber optic connection at least few years from now so i need to use all available upload bandwidth as best as i can. My download is about 10 Mbits/s and it is sufficient for my needs. Right now i am connected without mikrotik router on gateway1 with lan connection while rest of my family are connecting trough gateway2 over wi fi. Both of gateways are close to my pc so i would like to use mikrotik router(that i am not using at the moment) between my PC and two gateways and gateway one(that i intended to use for UDP traffic only) i would put in bridge mode and use as a modem as on the picture above. I sow few youtube videos how it is posible to add two wans and use bandwidht of the both at the same time like in this video:

https://www.youtube.com/watch?v=ThYBtmo_bcs

It would be nice too if that is possible by using mikrotik router :slight_smile:
btw is this type feller in those two lines from video above:

add chain=prerouting dst-address=192.168.5.0/24 action=accept in-interface=wlan1
add chain=prerouting dst-address=192.168.10.0/24 action=accept in-interface=wlan2

it should be wan1 and wan2 right?

anyway thanks again for your reply!

best regards

cmon guys i just bought mikrotik router and its scripting is something new to me,i need help here from mikrotik gurus :slight_smile:

will this script work in my case :

/interface enable WAN1,WAN2

/ interface pppoe-client
add interface=WAN1 user=“xxxxxxxxxx.com” password=“mypassword”
add-default-route=yes use-peer-dns=yes disabled=no

/ip firewall mangle
add chain=input in-interface=WAN1 action=mark-connection new-connection-

mark=WAN1_conn
add chain=input in-interface=WAN2 action=mark-connection new-connection-

mark=WAN2_conn

add chain=output connection-mark=WAN1_conn action=mark-routing new-routing-

mark=to_WAN1
add chain=output connection-mark=WAN2_conn action=mark-routing new-routing-

mark=to_WAN2

add chain=prerouting dst-address=192.168.1.1/24 action=accept in-

interface=LAN1
add chain=prerouting dst-address=192.168.1.254/24 action=accept in-

interface=LAN2

add chain=prerouting dst-address-type=!local in-interface=LAN1 action=mark-

connection new-connection-mark=WAN1_conn passthrough=yes
add chain=prerouting dst-address-type=!local in-interface=LAN2 action=mark-

connection new-connection-mark=WAN2_conn passthrough=yes

add chain=prerouting connection-mark=WAN1_conn in-interface=LAN1

action=mark-routing new-routing-mark=to_WAN1 passthrough=yes
add chain=prerouting connection-mark=WAN2_conn in-interface=LAN2

action=mark-routing new-routing-mark=to_WAN2 passthrough=yes


/ip route
add dst-address=0.0.0.0/0 gateway=192.168.1.1 routing-mark=to_WAN1 check-

gateway=ping
add dst-address=0.0.0.0/0 gateway=192.168.1.254 routing-mark=to_WAN2 check-

gateway=ping

add dst-address=0.0.0.0/0 gateway=192.168.1.1 distance=1 check-gateway=ping
add dst-address=0.0.0.0/0 gateway=192.168.1.254 distance=2 check-

gateway=ping


/ip firewall nat
add chain=srcnat out-interface=WAN1 action=masquerade
add chain=srcnat out-interface=WAN2 action=masquerade



if this will work then how to send all UDP on wan1and all TCP on WAN2

Besides those with Mikrotik below their name, none of us are getting paid for for being on this forum so you might just have to wait before you get response. Especially given your more specific requirements and ideas, this cannot be addressed with a typical 5 minute reply.
Secondly, use the code tags when posting exports: works wonders for readability.
Thirdly, while most of us will help you when you run with difficulties, it’s up to you to your configuration working. We will not do it for you.

The configuration you’ve posted should generally work, but has a few problems going with to it.

First and foremost, your configuration assumes complete UDP and TCP separation between the NICs already has taken place. It is the host (i.e. your game PC) that is responsible which NIC it uses to source the traffic from, not the router that tells the PC what is allowed on what link. Unless the game allows you to specifically configure which NIC to use, I think you’ll going to find that it’s quite hard to reliably configure a multihomed host where traffic is split across NICs and the applications used lack control.

If the game does allow you to bind to a specific NIC, the rules you have a sufficient (i.e.: all traffic on LAN1 goes to WAN1). If the game doesn’t allow you to explicitly bind the application to a specific NIC, you’ll have to opt for a ‘neutral’ link, i.e. all traffic is pushed over the link regardless and the hAP separates the traffic.

If the NIC drivers on the PC allow for this, you could try and bond the two interfaces together, e.g. using 802.3ad. This would give you 200Mbit (in case of your hAP) full duplex bandwidth to the hAP. Drawback of that bonding in RouterOS is that it is software-based, so you’re adding a layer of indirection here and tax the CPU besides routing duties. If I were you, I’d benchmark to see what different bonding options do with pingtimes and packetloss compared to just a single ethernet link. Given the eventual bandwidth available on the WAN connections though, I doubt TCP traffic on the PC-hAP-link will be the bottleneck and result in UDP packet loss, even on a single 100Mbit link.

To make the actual policy based routing happen, I don’t know the specifics of your game (or even what game it is), but I wouldn’t just blindly select on ALL UDP or ALL TCP, but specifically mark the traffic you need for your application. You could for instance mangle outgoing traffic from your game PC destined for UDP port 27000-27005 to go out WAN1 by using:

/ip firewall mangle
add chain=prerouting src-address=192.168.1.33 protocol=udp dst-port=27000-27005 action=mark-routing new-routing-mark=to_WAN1
/ip route 
add dst-address=0.0.0.0/0 gateway=192.168.1.1 routing-mark=to_WAN1 check-gateway=ping

Also, don’t forget to set the correct dst-nat rules for portforwarding for incoming connections.

/ip firewall nat
add action=dst-nat chain=dstnat in-interface=WAN2 dst-port=27000-27005 protocol=udp to-addresses=192.168.1.33
/ip firewall filter
add action=accept chain=forward connection-nat-state=dstnat

Second problem I see is that it it only marks LAN-bound traffic directed to the hAP (i.e. input and output chains), not through the hAP. In other word: this works for services runing on the hAP, but not for traffic that is being NATted to a host in the LAN. The key to understand the rules necessary is to understand the packetflow in RouterOS. Study http://wiki.mikrotik.com/wiki/Manual:Packet_Flow_v6. Say you would be able to completely separate UDP on WAN1 and TCP traffic on WAN2, any TCP traffic still destined and inadvertently NATted for WAN1, would be answered on WAN2 leaving receiving end very confused by your responses.
To prevent this, you must also mark (inbound) connections on the forward chain and mark the (outbound) traffic in those connections for routing on the prerouting chains to make sure traffic leaves on the same WAN connection as it entered.
The following example uses the new in-interface-list attribute to achieve this (which sticks to the methods used in your configuration), but you could also do this src-address(-list) based.

/interface list
add name=LAN_INTERFACES
/interface list member
add interface=LAN1 list=lan-interfaces
add interface=LAN2 list=lan-interfaces


/ip firewall mangle
add chain=forward connection-mark=no-mark in-interface=WAN2 action=mark-connection new-connection-mark=WAN2_fwd
add chain=prerouting connection-mark=WAN2->LAN in-interface-list=LAN_INTERFACES action=mark-routing new-routing-mark=to_WAN2

Third issues is that for the (unmarked) default routes, WAN1 is set as distance=1 and WAN2 as distance=2. This means WAN1 will serve as the active default route and be preferred over WAN2 for all unmarked traffic. If you’re really anxious not to have any other traffic, I’d suggest to turn this around. This means that unless WAN2 goes down OR the traffic is marked to go over WAN1, all traffic will go over WAN2. If you’re manually configuring the routes, you can change the distance in your routing table. If using PPPoE or DHCP client to automatically create default routes you can add/change them at the respective client settings.


Besides these points, personally I’d opt for a topology where the hAP sits in the middle for all routing, including that of the LAN routing. Consider the following logical traffic flow based on your diagram:

                           Outbound
                      +-----------------+
               Eth2+3 |          Gaming | Eth1      +--------+
   Game PC +----------> ++------------> +---------->+  WAN1  |
                      |  |              |           +--------+
                      |  |        Other |
                 Eth4 |  +------------> | Eth4      +--------+
WiFi hosts +----------> +-------------> +---------->+  WAN2  |
                      |                 |           +--------+
                      +-----------------+

                           Inbound
                      +-----------------+
 +--------+      Eth1 |          Gaming | Eth2+3
 |  WAN1  +-----------> +------------>  +----------> Game PC
 +--------+           |  +----------->  |
                      |  |        Other |
 +--------+      Eth4 |  |              | Eth4
 |  WAN2  +-----------> ++----------->  +----------> WiFi hosts
 +--------+           |                 |
                      +-----------------+

This does mean, both traffic to and from WiFi hosts as well as traffic to and from the gateway for WAN2 are on the same (in my example eth4) interface. For clarity and to prevent routing issues, I’d separate broadcast domains on the LAN side of the hAP from the WAN side of the hAP in the following steps:

  • give WAN2 gateway a fixed IP in a different subnet, e.g. 192.168.2.1/30.
  • disable DHCP server on on the WAN2 gateway
  • set IP in the ‘gateway subnet’ (192.168.2.2/30 following the IP from above) for ether4 on the hAP
  • also set IP for 192.168.1.2/24 for ether4 on the hAP
  • configure your hAP as DHCP server for 192.168.1.0/24 subnet with 192.168.1.2/24 as gateway (if that is the IP of your hAP)

This way you can easily distinguish between traffic coming in from LAN and traffic coming in from the WAN2 connection. This would prevent any routing problems because of ‘upstream gateways’ being in the same broadcast domain.

Two notes:

  1. I would probably also use a different subnet between the hAP and the WAN1 gateway, e.g. 192.168.2.5/30 and 192.168.2.6/30).
  2. I would recommend NOT to use the 192.168.1.0/24 for you LAN, but use something less common (e.g. 192.168.37.0/24, 192.168.213.0/24, etc.)

Another step to further simplify your routing configuration is to prevent a double NAT scenario i.e. both the hAP and WAN2-gateway are NATting and firewalling traffic. You’re already doing this with WAN1 by putting it into bridge mode, but this generally isn’t possible without disabling e.g. the wireless capabilities. However a default-server or DMZ option that’s available on most consumer devices would already be step in the right direction here.


Now finally if you have this topology, you can also start thinking about how to maintain a level of exclusivity on WAN1 when you’re gaming, but not let the bandwidth go to waste by allowing the rest of the LAN to use the WAN1 connection when it’s available. You could do so by adding a more dynamic load balancing rules, e.g. PCC (simple and useful if connection speeds are quite similar) or a bandwidth based (as explained by Tomas in his slides). You can keep things very simple here by just disabling these load balancing rules when you start gaming en re-enable them when you’re done, add a schedule to the rules (e.g. only weekdays during business hours) or more advanced QoS through and queues priorities that make sure UDP traffic gets priority when routing. For the latter, you could just adapt a QoS example used for VoIP as it will generally have the same requirements you have for your gaming packets.

thanks for your replay and suggestions!

Some time ago I have read this article:

https://www.isoc.org/inet97/proceedings/F3/F3_1.HTM

It is about how UDP packet loss is directly related to synchronized TCP traffic on a bottlenecked node. As my upload is 0.5 Mbit/s per single WAN and it takes more than several seconds on speedtest to get to that maximum available speed, (it is raising from about 200 Kbits/s to maximum of 500 Kbits/s in few steps on speedtest speedometer) so i think its even not 0.5 mbit/s in short bursts of traffic when i am gaming. As my game is using about 20 KB/sec of upload and that is close to that initial 200 Kb/sec of my upload speed i think all active TCP connections on my pc in the same time are causing UDP packet loss on my line and that is why i wanted to separate my UDP from TCP trough different wans. I know i cant do anything about eventually bottlenecked hops on my route to the server but at least if i could do it on my side i will like to do that.
So as you see my top priority is to eliminate UDP loss as much as possible and if that means wasting some good port of my bandwidth so be it.

Once again thanks for your help, your advices and all your time and effort invested in your replies!

I understand your concern to keep your WAN1 connection free from TCP traffic. My point is: if you have 100Mbit/s available between PC and router and 200 Kbit/s upload on WAN1, the bottleneck is not between PC and router and you can safely send all traffic over one ethernet link and split the traffic at the router.

Ok thanx. If there is no any benefit from two nics i will disable second and try do achieve separate tcp and udp traffic to both WANs trough single lan.