Skip to main content
Version: 7.26

Routing Decision

Routing is the process of selecting paths across networks to move packets from one host to another. The path selection, also called the routing decision, relies on a routing information table. That table contains route entries, each of which specifies a destination and the next hop used to reach it. A hop occurs when a packet passes from one network segment to another.

Host A sends a packet to host B through Router1 and Router2: hop 1 from Router1 to Router2, hop 2 from Router2 to host B

Typically, routing information has two parts and RouterOS is no exception:

  • FIB – the Forwarding Information Base used to make packet‑forwarding decisions. It contains a subset of the routing information needed for forwarding.
  • RIB – the Routing Information Base, which contains all prefixes learned from routing sources such as connected networks, static routes, BGP, RIP, and OSPF.

Connected, static, OSPF, RIP and BGP routes go into the RIB, and the RIB fills the FIB Connected, static, OSPF, RIP and BGP routes go into the RIB, and the RIB fills the FIB

Routing Information Base​

The RIB contains complete routing information, including static routes, connected networks, and routes learned from dynamic protocols and MPLS label‑binding protocols such as OSPF, BGP, and LDP.
The RIB does more than store routes: it filters routing information to calculate the best route for each destination prefix, builds and updates the FIB, and distributes routes between routing protocols.

By default, all routes are organized in a single main routing table.

Connected networks​

A connected route represents a network that hosts can reach directly (a direct attachment to a Layer 2 broadcast domain). RouterOS automatically creates a connected route for each IP network with at least one enabled interface configured under /ip/address or /ipv6/address. The RIB tracks connected‑route status but does not modify these routes. For each connected route there is a corresponding IP address item such that:

  • address part of the dst-address of the connected route is equal to a network of IP address item.
  • netmask part of dst-address of the connected route is equal to the netmask part of the address of the IP address item.
  • gateway of the connected route is equal to the actual-interface of the IP address item (same as an interface, except for bridge interface ports) and represents an interface where directly connected hosts from the particular Layer3 network can be reached.

Example of connected networks:

[admin@MikroTik] > /ip/route/print where connect
Flags: D - DYNAMIC; A - ACTIVE; c - CONNECT
Columns: DST-ADDRESS, GATEWAY, ROUTING-TABLE, DISTANCE
DST-ADDRESS GATEWAY ROUTING-TABLE DISTANCE
DAc 172.16.1.0/24 ether2 main 0
DAc 172.16.2.0/24 ether3 main 0
DAc 198.51.100.0/24 ether1 main 0
DAc 192.168.1.0/24 ether4 main 0

Default route​

A default route is used when no other route in the table matches the destination. In RouterOS the default route has a dst-address of 0.0.0.0/0 for IPv4 or ::/0 for IPv6. If the routing table contains an active default route, lookups in that table always succeed.

The routing table of a typical home router contains only connected networks and one default route that sends all other traffic to the gateway of the internet provider. A static default route is added with /ip/route/add dst-address=0.0.0.0/0 gateway=172.16.1.1. In the following example, the DHCP client on ether1 added the default route:

[admin@MikroTik] > /ip/route/print where dst-address=0.0.0.0/0
Flags: D - DYNAMIC; A - ACTIVE; d - DHCP
Columns: DST-ADDRESS, GATEWAY, ROUTING-TABLE, DISTANCE
DST-ADDRESS GATEWAY ROUTING-TABLE DISTANCE
DAd 0.0.0.0/0 198.51.100.1 main 1

Hardware offloaded route​

Devices with Layer 3 Hardware Offloading (L3HW, also called IP switching or HW routing) can offload packet forwarding to the switch chip. When L3HW is enabled, eligible routes display the H flag:

[admin@MikroTik] > /ip/route print where static
Flags: A - ACTIVE; s - STATIC, y - COPY; H - HW-OFFLOADED
Columns: DST-ADDRESS, GATEWAY, DISTANCE
# DST-ADDRESS GATEWAY D
0 AsH 0.0.0.0/0 172.16.2.1 1
1 AsH 10.0.0.0/8 10.155.121.254 1
2 AsH 192.168.3.0/24 172.16.2.1 1

By default, all routes are candidates for hardware offload. You can fine‑tune offloading by enabling or disabling the suppress-hw-offload option on individual IPv4 or IPv6 static routes. For example, if most traffic goes to a server network, enable offloading only for that destination:

/ip/route/set [find where static && dst-address!="192.168.3.0/24"] \
suppress-hw-offload=yes

Now only the route to 192.168.3.0/24 has an H-flag, indicating that it will be the only one eligible to be selected for HW offloading:

[admin@MikroTik] > /ip/route print where static
Flags: A - ACTIVE; s - STATIC, y - COPY; H - HW-OFFLOADED
Columns: DST-ADDRESS, GATEWAY, DISTANCE
# DST-ADDRESS GATEWAY D
0 As 0.0.0.0/0 172.16.2.1 1
1 As 10.0.0.0/8 10.155.121.254 1
2 AsH 192.168.3.0/24 172.16.2.1 1
warning

H-flag does not indicate that the route is actually HW offloaded, it indicates only that the route can be selected to be HW offloaded.

Multipath (ECMP) routes​

Some setups, such as load balancing, require more than one path to a given destination.

flowchart TB
  R[Router] -- ECMP --> GW1["Gateway 1<br/>172.16.1.1"]
  R -- ECMP --> GW2["Gateway 2<br/>172.16.2.1"]

ECMP (Equal Cost Multi-Path) routes have multiple gateway (next‑hop) values. All reachable next hops are copied to the FIB for packet forwarding.

You can create these routes manually or they can be learned dynamically by protocols such as OSPF, BGP, or RIP. RouterOS groups equally preferred routes to the same destination into one ECMP route and marks them with the + flag. For example, two static routes with the same destination and distance:

/ip/route
add dst-address=192.168.2.0/24 gateway=172.16.1.1
add dst-address=192.168.2.0/24 gateway=172.16.2.1
[admin@MikroTik] > /ip/route/print where dst-address=192.168.2.0/24
Flags: A - ACTIVE; s - STATIC; + - ECMP
Columns: DST-ADDRESS, GATEWAY, ROUTING-TABLE, DISTANCE
# DST-ADDRESS GATEWAY ROUTING-TABLE DISTANCE
0 As+ 192.168.2.0/24 172.16.1.1 main 1
1 As+ 192.168.2.0/24 172.16.2.1 main 1

By default, ECMP uses the Layer3 hash policy, which hashes source and destination IP addresses (IPv4) or source/destination IP, flow label, and IP protocol (IPv6).
You can change the hash policy with ipv4-multipath-hash-policy in /ip/settings and multipath-hash-policy in /ipv6/settings to Layer4 or inner Layer3 hashing.
Supported hashing methods include:

IPv4IPv6
L3srcIPv4, dstIPv4srcIPv6, dstIPv6, flow label, IP proto
L4srcIPv4, dstIPv4, srcPort, dstPort, IP protosrcIPv6, dstIPv6, srcPort, dstPort, IP Proto
L3-Inner
  • srcIPv4, dstIPv4 (if inner IPv4)
  • srcIPv6, dstIPv6, flow label, IP proto (if inner IPv6)
  • Same as L3 if inner is not present.
  • srcIPv4, dstIPv4 (if inner IPv4)
  • srcIPv6, dstIPv6, flow label, IP proto (if inner IPv6)
  • Same as L3 if inner is not present.

When ECMP is offloaded by L3 hardware offloading, the switch chip uses its own hash mask, which includes L4 ports for TCP/UDP; the configured hash policy is ignored for hardware-forwarded ECMP. FastPath caches routes by destination address, so the Layer4 and inner Layer3 policies do not provide per-flow distribution for FastPath-cached traffic.

Route selection​

Multiple routes with the same destination can be learned from different protocols or static configurations, but only one best route is used for forwarding. The RIB runs a route‑selection algorithm to choose the best route among the candidates for each destination.

Only routes that meet these criteria participate in route selection:

  • Route is not disabled.
  • If the type of route is unicast, it must have at least one reachable next hop. A gateway in a connected network with an active connected route is reachable.
  • Route should not be synthetic.

The candidate route with the lowest distance becomes active. Candidates with the same lowest distance become one ECMP route, also when they come from different protocols.

In the following example, a static route and an OSPF route point to the same destination. OSPF installs its route with the default distance 110, the static route has distance 1, so the static route is active:

[admin@MikroTik] > /ip/route/print where dst-address=203.0.113.1/32
Flags: D - DYNAMIC; A - ACTIVE; s - STATIC, o - OSPF
Columns: DST-ADDRESS, GATEWAY, ROUTING-TABLE, DISTANCE
# DST-ADDRESS GATEWAY ROUTING-TABLE DISTANCE
D o 203.0.113.1/32 172.16.1.1%ether2 main 110
0 As 203.0.113.1/32 172.16.1.1 main 1

With distance=110 on the static route, both routes have the same distance and become one ECMP route:

/ip/route/set [find dst-address=203.0.113.1/32 && static] distance=110
[admin@MikroTik] > /ip/route/print where dst-address=203.0.113.1/32
Flags: D - DYNAMIC; A - ACTIVE; s - STATIC, o - OSPF; + - ECMP
Columns: DST-ADDRESS, GATEWAY, ROUTING-TABLE, DISTANCE
# DST-ADDRESS GATEWAY ROUTING-TABLE DISTANCE
DAo+ 203.0.113.1/32 172.16.1.1%ether2 main 110
0 As+ 203.0.113.1/32 172.16.1.1 main 110

Nexthop lookup​

Nexthop lookup is a part of the route selection process. Its main purpose is to find a directly reachable gateway address (next-hop). Only after a valid next-hop is selected router knows which interface to use for packet forwarding.

Nexthop lookup becomes more complicated if routes have a gateway address that is several hops away from this router (for example iBGP or multihop eBGP). Such routes are installed in the FIB after the next-hop selection algorithm determines the address of the directly reachable/immediate gateway (shown by immediate-gw parameter).

It is necessary to restrict the set of routes that can be used to look up immediate next-hops. Nexthop values of RIP or OSPF routes, for example, are supposed to be directly reachable and should be looked up only using connected routes. This is achieved using scope and target-scope properties.

Routes with a scope greater than the maximum accepted value are not used for next-hop lookup. Each route specifies the maximum accepted scope value for its nexthop in the target-scope property. The default value of this property allows nexthop lookup only through connected routes, with the exception of iBGP and multihop eBGP routes, which have a larger default value (30) and can look up the nexthop also through IGP and static routes.

ScopeRoute typeTarget scope
0
10Connected5
20IGP (OSPF, RIP)10
30Static10
40eBGP10
40iBGP, multihop eBGP30

Matrix shows which route type (line) can resolve gateways of route types (columns) based on default scope and target-scope values.

IGPStaticeBGPiBGP, multihop eBGP
Connected++++
IGP+
Static+
eBGP
iBGP

A multihop eBGP session needs no change for this: with multihop=yes, its routes get target scope 30 and are resolved through IGP and static routes, see Nexthop selection. To change the default behaviour, adjust the scope and target-scope values with a routing filter. For example, eBGP routes can be resolved over iBGP routes in this way.

Routes are processed in scope order, and updates to routes with a larger scope cannot affect the state of nexthop lookup for routes with a smaller scope.

RIB keeps multiple nexthop objects per address, one for each combination of scope and gateway check.

When you change a route's target-scope or gateway check, it does not affect other routes, because these properties attach to the nexthop object rather than the route.

RouterOS fixes an invalid scope value automatically: if the scope of a route is not greater than its target-scope, RouterOS sets the scope of the route's nexthop in /routing/nexthop to target-scope + 1.

You can view the actual scope and target-scope values in the /routing/nexthop menu.

You can extend gateway checking with the check-gateway parameter of a route. The router then checks the gateway with ARP requests (arp), ICMP echo requests (ping) or BFD sessions (bfd). The check-gateway-ping-interval, check-gateway-ping-timeout and check-gateway-ping-count settings in /routing/settings set how often the router checks (default 10 seconds), how long it waits for a reply (1 second), and after how many missed replies the gateway is unreachable (2). A reply makes the gateway reachable again.

warning

gateway set as non point-to-point interface cannot be used to forward packets with destination multiple hops away.

You cannot use the check-gateway parameter on such gateways because no destination IP is known.

Route storage​

Routing information is stored to minimize memory usage in common cases. These optimizations can have non‑obvious worst‑case scenarios and affect performance.

All routes and gateways are kept in a single hierarchy by the prefix/address.

Dst [4]/0 1/0+4 18 <-- number of prefixes
^ ^ ^ ^ ^
| | | | |
| | | | \- bytes taken by Route distinguisher or Interface Id
| | | \--- vrf/routing table
| | \----- AFI
| \------- netmask length of prefix
\---------- bytes taken by prefix value

[subject to change without notice]

Each 'Dst' entry corresponds to a unique 'dst-address' (for a route) or gateway address. Each 'Dst' entry requires one or more 'T2Node' objects.

All routes with the same 'dst-address' are kept in Dst in a list sorted by route preference.

info

WORST CASE: having many routes with the same 'dst-address' is very slow, even if they are inactive. Updating a sorted list with tens of thousands of elements degrades performance.

Route order changes only when route attributes change. If the route becomes active/inactive, the order does not change.

Each Route has three copies of route attributes:

  • private -- what is received from the peer, before passing in-filters.
  • updated -- what is the result of applying in-filters.
  • current -- the attributes the route uses.

Periodically (when needed), update attributes are calculated from private attributes. This happens when route update is received, or when in-filter is updated.

When the routing table is recalculated, current attributes are set to the value from updated attributes.

---
config:
  layout: elk
---
graph LR
    subgraph **RIB**
        direction LR
        P[**Private**] --> F{"in-filter"}
        F --> U[**Update**]
        U --> calc{ Calculation }
        calc --> C[**Current**]
    end

    A@{ shape: f-circ } -->|prefix in| P
    C -->|BGP peer /<br /> protocol output| O@{ shape: f-circ }

This means, that usually if there is no in-filter that changes route attributes, private, updated, and current share the same value.

Route attributes are kept in several groups:

  • L1 Data - all flags, list of extra properties, as-path;
  • L2 Data – nexthops, RIP or OSPF metrics, BGP metrics, route tags, originators, and similar attributes.
  • L3 Data – distance, scope, kernel type, and MPLS information.
  • extra properties - communities, originator, aggregator-id, cluster-list, unknown

Having, for example, many different combinations of distance and scope route attributes will use more memory!

Matching communities or as-path using regexp will cache the result, to speed up filtering. Each as-path or community value has a cache for all regexp, which is filled on-demand with match results.

info

WORST CASE: changing attributes in in-filter will make the route program use more memory! Because private and updated attributes will be different! Having a lot of different regexps will make matching slow and use a lot of memory! Because each value will have a cache with thousands of entries!

The /routing/stats/memory menu shows the memory that the routing protocols use.

Forwarding Information Base​

FIB (Forwarding Information Base) contains a copy of the information that is necessary for the packet forwarding:

  • vrf tables
  • active routes
  • policy routing rules
  • other routing decision rules

Each route has a dst-address property, that specifies all destination addresses this route can be used for. If several routes apply to a particular IP address, the most specific one (with the largest netmask) is used. In cases where the routing table contains several routes with the same dst-address and netmask, all equally best routes are combined into one ECMP route, installed into FIB and marked as ''active''

Only one best route can be used for packet forwarding.
Finding the most specific route that matches a given address is called routing table lookup.

When forwarding decision uses additional information, such as the source address of the packet, it is called policy routing. Policy routing is implemented as a list of policy routing rules, that select different routing tables based on the destination address, source address, source interface, and routing mark (which can be changed by firewall mangle rules) of the packet.

Routing table lookup​

FIB uses the following information from the packet to determine its destination:

  • source address
  • destination address
  • source interface
  • routing mark

Possible routing decisions are:

  • receive packet locally
  • discard the packet (either silently or by sending an ICMP message to the sender of the packet)
  • send the packet to a specific IP address on a specific interface

Routing decision actions are determined by the policy-rules parameter in /routing/settings. By default:

[admin@MikroTik] > /routing/settings/print
single-process: no
check-gateway-ping-interval: 10s
check-gateway-ping-timeout: 1s
check-gateway-ping-count: 2
policy-rules: mangle
vrf-lookup
vrf-unreach
local
user
main
  • Look up the destination in the routing table marked by mangle rule.
  • If interface belongs to a VRF or packet was steered to a VRF, then try to lookup in the VRF table.
  • If VRF lookup fails reject the packet.
  • Check that the packet has to be locally delivered (the destination address is the address of the router).
  • Process the user-defined routing rules (the "user" chain).
  • Look up the destination in the main routing table.

Depending on the complexity of required tasks, you can change the order of default rules or fine tune the setup by adding your custom rules.

For example, to resolve the destination 1.2.3.4 only in other_table while you also use mangle, add the routing rule and move mangle after user. The new order applies to all traffic that mangle marks, not only to 1.2.3.4:

/routing/table/add name=other_table fib
/routing/settings/set policy-rules=vrf-lookup,vrf-unreach,local,user,mangle,main
/routing/rule/add dst-address=1.2.3.4 action=lookup-only-in-table \
table=other_table

If a rule has action:

  • drop or unreachable, then it is returned as a result of the routing decision process.
  • lookup then the destination address of the packet is looked up in the routing table that is specified in the rule. If the lookup fails (no route matches the destination address of the packet), then FIB proceeds to the next rule.
  • lookup-only-in-table is similar to lookup, except that the lookup ends there: if no route in the table matches the packet, the destination is unreachable.

Otherwise:

  • If the type of the route is blackhole, then return this action as the routing decision result.
  • If this is a connected route or route with an interface as the gateway value, then return this interface and the destination address of the packet as the routing decision result.
  • If this route has an IP address as the value of the gateway, then return this address and associated interface as the routing decision result.
  • If this route has multiple values of nexthop, then pick one of them with the configured hash policy.

An interface as the gateway of a static route is suitable only in two cases:

  • on point-to-point type interfaces;
  • on interfaces where the destination address is directly connected.

If the routing decision returns an interface and the destination address of the packet as the result on a broadcast network, the router will try to resolve the packet's destination address over the interface by sending ARP probes. If none of the hosts in the same broadcast domain has the IP address, forwarding will fail, so such gateways cannot be used to route packets whose destination is multiple hops away.