Skip to main content
Version: 7.26

VRF

A Virtual Routing and Forwarding instance (VRF) is a separate routing table with its own set of interfaces. Traffic that arrives on an interface of a VRF is routed by the routes of that VRF (unless a firewall mangle rule sets another routing mark), so several customers or networks can use the same router, even with overlapping IP prefixes, and stay apart.

RouterOS uses VRFs in two ways:

  • VRF-lite: VRFs on one router, with static routes or routing protocols that run inside each VRF.
  • BGP/MPLS IP VPNs (L3VPN, RFC 4364): PE routers exchange the routes of their VRFs as VPNv4 routes over multi-protocol BGP, and forward the traffic over an MPLS network. Unlike BGP VPLS, which carries Layer 2 frames, an L3VPN exchanges IP prefixes.

BGP VPN also copies routes between VRFs on the same router, without a BGP session.

Create a VRF​

A VRF is created in the /ip/vrf menu with a name and a list of interfaces. Each VRF gets a dynamic routing table with the same name and a virtual interface with the same name. The example creates two customer VRFs on a router whose ether2 connects to the internet provider:

/ip/vrf
add name=cust-a interfaces=ether3
add name=cust-b interfaces=ether4
/ip/address
add address=172.16.1.2/24 interface=ether2
add address=192.168.1.1/24 interface=ether3
add address=192.168.2.1/24 interface=ether4
[admin@MikroTik] > /ip/vrf/print
Flags: * - BUILTIN
0 name="cust-b" interfaces=ether4

1 name="cust-a" interfaces=ether3

2 * name="main" interfaces=all
[admin@MikroTik] > /routing/table/print
Flags: D - DYNAMIC
0 D name="main" fib

1 D name="cust-a" fib

2 D name="cust-b" fib
[admin@MikroTik] > /interface/print where type=vrf
Flags: R - RUNNING
Columns: NAME, TYPE, ACTUAL-MTU, MAC-ADDRESS
# NAME TYPE ACTUAL-MTU MAC-ADDRESS
0 R cust-a vrf 65536 32:C5:67:C6:DA:17
1 R cust-b vrf 65536 D2:A3:59:94:81:02

The connected routes of the interfaces in a VRF are installed in the VRF's routing table. The gateway shows the VRF after @:

[admin@MikroTik] > /ip/route/print where routing-table=cust-a
Flags: D - DYNAMIC; A - ACTIVE; c - CONNECT
Columns: DST-ADDRESS, GATEWAY, ROUTING-TABLE, DISTANCE
DST-ADDRESS GATEWAY ROUTING-TABLE DISTANCE
DAc 192.168.1.0/24 ether3@cust-a cust-a 0

A bridge port stays in main even when a VRF lists it: its address and connected route stay in the main table. On a router with the default configuration, first remove the port from the bridge (in /interface/bridge/port), then add it to the VRF.

warning

The router's services stay in main (see the services section later on this page). A WinBox, SSH or web session through an interface you move into a VRF is cut, and you cannot connect through that interface again. Connect through another interface, or use Safe Mode, which undoes the change when the session is lost.

Order of the VRF entries​

An interface belongs to the first VRF entry, from the top, that lists it. The built-in main VRF has interfaces=all, is always the last entry and cannot be moved. A new VRF is added at the top of the list. When two VRFs list the same interface, the upper one gets it. When that VRF is disabled, its routing table is removed and the interface moves to the next entry that lists it.

info

Each VRF has its own routing table, which limits a router to 1024 VRFs.

Add routes to a VRF​

Add a route to a VRF with the routing-table parameter of /ip/route. The VRF's routing table exists only after the VRF is created: add the routes in a separate step, not in the same script as the VRF. In the same script, the route fails with input does not match any value of routing-table.

A gateway without @ is resolved in the main table, also for a route in a VRF table. To resolve the gateway in a VRF, append @ and the VRF name. In this example, both customer VRFs get a default route through the provider's gateway, which is in the main table:

/ip/route
add gateway=172.16.1.1@main routing-table=cust-a
add gateway=172.16.1.1@main routing-table=cust-b
[admin@MikroTik] > /ip/route/print where routing-table~"cust"
Flags: D - DYNAMIC; A - ACTIVE; c - CONNECT, s - STATIC
Columns: DST-ADDRESS, GATEWAY, ROUTING-TABLE, DISTANCE
# DST-ADDRESS GATEWAY ROUTING-TABLE DISTANCE
0 As 0.0.0.0/0 172.16.1.1 cust-a 1
DAc 192.168.1.0/24 ether3@cust-a cust-a 0
1 As 0.0.0.0/0 172.16.1.1 cust-b 1
DAc 192.168.2.0/24 ether4@cust-b cust-b 0

The gateway forms work for routes in any table, also in main:

GatewayResolved
172.16.1.1In the main table.
172.16.1.1@mainIn the main table.
192.168.2.2@cust-bIn the cust-b VRF.
192.168.2.2%ether4@cust-bOn ether4 in the cust-b VRF.
ether4@cust-bOn ether4 in the cust-b VRF, for a destination on that link.
cust-b@cust-bThe VRF interface: the route leads into the cust-b VRF, including the router's own addresses there.

An interface of a VRF without @vrf (gateway=ether4 or gateway=192.168.2.2%ether4) leaves the route inactive in another table. This applies to point-to-point interfaces such as tunnels too: a tunnel interface in cust-b works as a gateway in another table only as tunnel-name@cust-b. A route whose gateway is in another table forwards traffic between VRFs (route leaking): see the route leaking examples later on this page.

When the provider's gateway comes from a DHCP client, the client can add its default route to the VRF tables instead of main: set default-route-tables=cust-a,cust-b in /ip/dhcp-client. The default routes then use the gateway that the client receives.

Run services in a VRF​

An interface in a VRF gets its connected routes in that VRF, but the router's services do not follow the IP address: by default they listen and connect in the main table. A service works in a VRF only when it supports VRFs and is set to the VRF, with its vrf parameter or with @vrf after the address it connects to. For example, to accept WinBox connections only on the interfaces of cust-a:

/ip/service
set winbox vrf=cust-a
[admin@MikroTik] > /ip/service/print where name=winbox
Columns: NAME, PORT, PROTO, VRF, MAX-SESSIONS
# NAME PORT PROTO VRF MAX-SESSIONS
0 winbox 8291 tcp cust-a 20
warning

A service set to a VRF listens only in that VRF. Do not move the service you are connected with to a VRF unless you can reach the router through that VRF.

The services with VRF support and the parameter they use:

ServiceConfiguration
BGP/routing/bgp/template/add name=bgp-template1 vrf=vrf1
/routing/bgp/vpls/add name=bgp-vpls1 site-id=10 vrf=vrf1
/routing/bgp/vpn/add instance=bgp1 vrf=vrf1 ...
DHCP relay/ip/dhcp-relay/add dhcp-server-vrf=vrf1 ...
Not needed when the DHCP server is in the same VRF as the client.
DNS/ip/dns/set vrf=vrf1 sets the VRF where the router listens for DNS queries.
/ip/dns/set servers=8.8.8.8@vrf1 sets the VRF used to reach an upstream server.
Email/tool/e-mail/set server=192.168.88.1 vrf=vrf1
EoIP/interface/eoip/add remote-address=192.168.1.1@vrf1 tunnel-id=1
Fetch/tool/fetch address=192.168.1.2@vrf1 mode=ftp src-path=file.txt
GRE/interface/gre/add remote-address=192.168.1.1@vrf1
IP services/ip/service/set ftp vrf=vrf1
Each service in /ip/service has its own vrf: ftp, ssh, telnet, www, www-ssl, winbox, api, api-ssl and reverse-proxy.
IPIP/interface/ipip/add remote-address=192.168.1.1@vrf1
L2TP client/interface/l2tp-client/add connect-to=192.168.88.1@vrf1 name=l2tp-out1 user=l2tp-client
L2TP-ether/interface/l2tp-ether/add connect-to=192.168.2.2@vrf1
MPLS LDP/mpls/ldp/add afi=ip vrf=vrf1 lsr-id=192.168.1.1
Netwatch/tool/netwatch/add host=192.168.88.1@vrf1
The probe target accepts @vrf. The dns probe's dns-server field does not. Use a VRF-qualified upstream server in /ip/dns.
NTP/system/ntp/client/set vrf=vrf1
/system/ntp/server/set vrf=vrf1
OpenVPN server and client/interface/ovpn-client/add connect-to=192.168.1.1@vrf1
The OpenVPN server has a vrf parameter in /interface/ovpn-server/server.
OSPF/routing/ospf/instance/add name=ospf-instance-1 vrf=vrf1
Ping/ping 192.168.88.1 vrf=vrf1
RADIUS/radius/add address=192.168.88.1@vrf1
/radius/incoming/set vrf=vrf1
Remote logging/system/logging/action/add name=remote1 target=remote remote=192.168.1.1 vrf=vrf1
RIP/routing/rip/instance/add name=rip-instance-1 vrf=vrf1
RPKI/routing/rpki/add group=rpki1 address=192.168.1.2 port=323 vrf=vrf1
SNMP/snmp/set vrf=vrf1
SSTP client/interface/sstp-client/add connect-to=192.168.1.1@vrf1
VXLAN/interface/vxlan/add vni=10 vtep-vrf=vrf1 port=8472
VXLAN interfaces with different vtep-vrf values need different UDP ports.

Match VRF traffic in the firewall​

When an interface is in a VRF, firewall filter rules match the VRF interface, not the member port. Use the VRF name in in-interface and out-interface:

/ip/firewall/filter
add chain=input action=accept protocol=icmp in-interface=cust-a

A filter rule with a member port of a VRF in in-interface or out-interface is marked invalid and does not match:

[admin@MikroTik] > /ip/firewall/filter/print
Flags: I - INVALID
0 chain=input action=accept protocol=icmp in-interface=cust-a

1 I ;;; in/out-interface matcher not possible when interface (ether3) belongs
to vrf - use 'cust-a' interface instead
chain=input action=accept in-interface=ether3

Interface lists match the member port: a rule with in-interface-list=LAN matches the traffic of a VRF port that is a member of LAN. On a router with the default configuration, LAN lists the bridge. A port that you remove from the bridge and add to a VRF is no longer in LAN: add it to the list (/interface/list/member/add list=LAN interface=ether3), or the default input rule that drops traffic not from LAN drops its traffic to the router.

To match only one of several interfaces of a VRF, mark the connections in the mangle prerouting chain, where in-interface accepts the member port, and match the connection mark in the filter:

/ip/firewall/mangle
add chain=prerouting action=mark-connection connection-state=new \
in-interface=ether3 new-connection-mark=from-ether3 passthrough=yes
/ip/firewall/filter
add chain=input action=accept connection-mark=from-ether3

After a ping from a host on ether3, the rule counts the packets:

[admin@MikroTik] > /ip/firewall/filter/print stats
Columns: CHAIN, ACTION, BYTES, PACKETS
# CHAIN ACTION BYTES PACKETS
0 input accept 168 3

VRF-lite: internet access for two customers​

The router of the previous sections connects two customers, cust-a on ether3 and cust-b on ether4, to the internet provider on ether2. Each VRF has a default route through 172.16.1.1 in main. Add source NAT for the customers and send the replies back into the right VRF:

/ip/firewall/nat
add chain=srcnat action=masquerade out-interface=ether2
/ip/firewall/mangle
add chain=prerouting action=mark-connection connection-state=new \
in-interface=cust-a new-connection-mark=cust-a-conn passthrough=no
add chain=prerouting action=mark-connection connection-state=new \
in-interface=cust-b new-connection-mark=cust-b-conn passthrough=no
add chain=prerouting action=mark-routing in-interface=ether2 \
connection-mark=cust-a-conn new-routing-mark=cust-a passthrough=no
add chain=prerouting action=mark-routing in-interface=ether2 \
connection-mark=cust-b-conn new-routing-mark=cust-b passthrough=no

The replies arrive on ether2, which is in main. Without the routing marks, the router routes them by the main table, which has no route to the customer networks, and the customers get no reply. The connection mark records the VRF of each connection, and the routing mark sends its replies to the VRF's table.

Check from a host in each customer network, for example 192.168.1.2 and 192.168.2.2, that an internet address answers (the hosts in these outputs are RouterOS routers):

[admin@MikroTik] > /ping 198.51.100.1 count=3
SEQ HOST SIZE TTL TIME STATUS
0 198.51.100.1 56 63 2ms458us
1 198.51.100.1 56 63 1ms38us
2 198.51.100.1 56 63 1ms74us
sent=3 received=3 packet-loss=0% min-rtt=1ms38us avg-rtt=1ms523us
max-rtt=2ms458us
[admin@MikroTik] > /ping 198.51.100.1 count=3
SEQ HOST SIZE TTL TIME STATUS
0 198.51.100.1 56 63 1ms479us
1 198.51.100.1 56 63 791us
2 198.51.100.1 56 63 778us
sent=3 received=3 packet-loss=0% min-rtt=778us avg-rtt=1ms16us
max-rtt=1ms479us

The default route through the provider's gateway in main does not connect the customers: the router looks up their traffic only in their own VRF table, and only the gateway is resolved in main. Check that the customers stay apart. From the host 192.168.2.2 in cust-b, the host 192.168.1.2 in cust-a does not answer:

[admin@MikroTik] > /ping 192.168.1.2 count=3
SEQ HOST SIZE TTL TIME STATUS
0 192.168.1.2 timeout
1 192.168.1.2 timeout
2 192.168.1.2 timeout
sent=3 received=0 packet-loss=100%

On the router itself, a ping uses the main table unless you name the VRF with vrf:

[admin@MikroTik] > /ping 192.168.1.2 count=3
SEQ HOST SIZE TTL TIME STATUS
0 192.168.1.2 timeout
1 192.168.1.2 timeout
2 192.168.1.2 timeout
sent=3 received=0 packet-loss=100%
[admin@MikroTik] > /ping 192.168.1.2 vrf=cust-a count=3
SEQ HOST SIZE TTL TIME STATUS
0 192.168.1.2 56 64 521us
1 192.168.1.2 56 64 524us
2 192.168.1.2 56 64 516us
sent=3 received=3 packet-loss=0% min-rtt=516us avg-rtt=520us
max-rtt=524us

Leak routes between VRFs​

VRFs keep their customers apart. When two VRFs must reach each other, copy routes between them (route leaking). For more than a few routes, use BGP VPN on the router: it copies the routes by route target, without a BGP session. For single networks, static routes are enough.

Static leaking of connected networks​

In this example, the hosts in cust-a (192.168.1.0/24) and cust-b (192.168.2.0/24) reach each other. Each VRF gets a route to the other VRF's connected network, with the interface and VRF of that network as the gateway:

/ip/route
add dst-address=192.168.2.0/24 gateway=ether4@cust-b \
routing-table=cust-a
add dst-address=192.168.1.0/24 gateway=ether3@cust-a \
routing-table=cust-b
[admin@MikroTik] > /ip/route/print where dst-address in 192.168.0.0/16
Flags: D - DYNAMIC; A - ACTIVE; c - CONNECT, s - STATIC
Columns: DST-ADDRESS, GATEWAY, ROUTING-TABLE, DISTANCE
# DST-ADDRESS GATEWAY ROUTING-TABLE DISTANCE
DAc 192.168.1.0/24 ether3@cust-a cust-a 0
2 As 192.168.2.0/24 ether4@cust-b cust-a 1
3 As 192.168.1.0/24 ether3@cust-a cust-b 1
DAc 192.168.2.0/24 ether4@cust-b cust-b 0

From a host in cust-b, the host 192.168.1.2 in cust-a answers, but the router's own address 192.168.1.1 in cust-a does not:

[admin@MikroTik] > /ping 192.168.1.2 count=3
SEQ HOST SIZE TTL TIME STATUS
0 192.168.1.2 56 63 755us
1 192.168.1.2 56 63 1ms243us
2 192.168.1.2 56 63 711us
sent=3 received=3 packet-loss=0% min-rtt=711us avg-rtt=903us
max-rtt=1ms243us
[admin@MikroTik] > /ping 192.168.1.1 count=3
SEQ HOST SIZE TTL TIME STATUS
0 192.168.1.1 timeout
1 192.168.1.1 timeout
2 192.168.1.1 timeout
sent=3 received=0 packet-loss=100%

An interface@vrf gateway works only for forwarded traffic. To reach the router's addresses in the other VRF too, use the VRF interface as the gateway:

/ip/route
set [find dst-address=192.168.2.0/24 routing-table=cust-a] \
gateway=cust-b@cust-b
set [find dst-address=192.168.1.0/24 routing-table=cust-b] \
gateway=cust-a@cust-a
[admin@MikroTik] > /ip/route/print where dst-address in 192.168.0.0/16
Flags: D - DYNAMIC; A - ACTIVE; c - CONNECT, s - STATIC
Columns: DST-ADDRESS, GATEWAY, ROUTING-TABLE, DISTANCE
# DST-ADDRESS GATEWAY ROUTING-TABLE DISTANCE
DAc 192.168.1.0/24 ether3@cust-a cust-a 0
2 As 192.168.2.0/24 cust-b@cust-b cust-a 1
3 As 192.168.1.0/24 cust-a@cust-a cust-b 1
DAc 192.168.2.0/24 ether4@cust-b cust-b 0
[admin@MikroTik] > /ping 192.168.1.1 count=3
SEQ HOST SIZE TTL TIME STATUS
0 192.168.1.1 56 64 393us
1 192.168.1.1 56 64 307us
2 192.168.1.1 56 64 308us
sent=3 received=3 packet-loss=0% min-rtt=307us avg-rtt=336us
max-rtt=393us
warning

Leaking overlapping networks between VRFs does not work.

Leak one server to and from main​

A single host can be leaked in the same way between a VRF and main. In this example, a monitoring server at 172.16.1.1 on the main side (the network of ether2) reaches the host 192.168.1.2 in cust-a. The main table gets a route to the host, and cust-a gets a route back to the server's network:

/ip/route
add dst-address=192.168.1.2/32 gateway=192.168.1.2@cust-a
add dst-address=172.16.1.0/24 gateway=ether2@main \
routing-table=cust-a
[admin@MikroTik] > /ip/route/print where dst-address=192.168.1.2/32 || (dst-address=172.16.1.0/24 && routing-table=cust-a)
Flags: A - ACTIVE; s - STATIC
Columns: DST-ADDRESS, GATEWAY, ROUTING-TABLE, DISTANCE
# DST-ADDRESS GATEWAY ROUTING-TABLE DISTANCE
8 As 192.168.1.2/32 192.168.1.2@cust-a main 1
9 As 172.16.1.0/24 ether2 cust-a 1

The server needs a route to 192.168.1.2 through the router (172.16.1.2), and the host a route to 172.16.1.0/24 through 192.168.1.1. The server reaches the host, but not the router's address 192.168.1.1 in cust-a, which is not leaked:

[admin@MikroTik] > /ping 192.168.1.2 count=3
SEQ HOST SIZE TTL TIME STATUS
0 192.168.1.2 56 63 923us
1 192.168.1.2 56 63 980us
2 192.168.1.2 56 63 1ms21us
sent=3 received=3 packet-loss=0% min-rtt=923us avg-rtt=974us
max-rtt=1ms21us
[admin@MikroTik] > /ping 192.168.1.1 count=3
SEQ HOST SIZE TTL TIME STATUS
0 192.168.1.1 timeout
1 192.168.1.1 timeout
2 192.168.1.1 timeout
sent=3 received=0 packet-loss=100%

A leaked route passes all traffic between the two networks. To allow only some of it, add firewall filter rules in the forward chain, with the VRF interface (cust-a) in in-interface or out-interface.

Dynamic leaking with BGP VPN​

/routing/bgp/vpn exports the routes of a VRF with an export route target and imports the routes whose route target is in its import list. On one router this copies routes between VRFs without a BGP session. A BGP VPN needs a BGP instance. This example replaces the static routes of the previous example and leaks the connected IPv4 and IPv6 networks of the two VRFs:

/ip/route
remove [find dst-address=192.168.2.0/24 routing-table=cust-a]
remove [find dst-address=192.168.1.0/24 routing-table=cust-b]
/ipv6/address
add address=2001:db8:1::1/64 interface=ether3 advertise=no
add address=2001:db8:2::1/64 interface=ether4 advertise=no
/routing/bgp/instance
add name=bgp1 as=65000 router-id=172.16.1.2
/routing/bgp/vpn
add name=cust-a instance=bgp1 vrf=cust-a \
route-distinguisher=65000:1 label-allocation-policy=per-vrf \
export.redistribute=connected export.route-targets=65000:1 \
import.route-targets=65000:2
add name=cust-b instance=bgp1 vrf=cust-b \
route-distinguisher=65000:2 label-allocation-policy=per-vrf \
export.redistribute=connected export.route-targets=65000:2 \
import.route-targets=65000:1

Each VRF now has the other VRF's networks, with the VRF interface as the gateway (flag y):

[admin@MikroTik] > /ip/route/print where dst-address in 192.168.0.0/16
Flags: D - DYNAMIC; A - ACTIVE; c - CONNECT, y - BGP-MPLS-VPN
Columns: DST-ADDRESS, GATEWAY, ROUTING-TABLE, DISTANCE
DST-ADDRESS GATEWAY ROUTING-TABLE DISTANCE
DAc 192.168.1.0/24 ether3@cust-a cust-a 0
DAy 192.168.2.0/24 cust-b@cust-b cust-a 200
DAy 192.168.1.0/24 cust-a@cust-a cust-b 200
DAc 192.168.2.0/24 ether4@cust-b cust-b 0
[admin@MikroTik] > /ipv6/route/print where dst-address in 2001:db8::/32
Flags: D - DYNAMIC; A - ACTIVE; c - CONNECT, y - BGP-MPLS-VPN
Columns: DST-ADDRESS, GATEWAY, ROUTING-TABLE, DISTANCE
DST-ADDRESS GATEWAY ROUTING-TABLE DISTANCE
DAc 2001:db8:1::/64 ether3@cust-a cust-a 0
DAy 2001:db8:2::/64 cust-b@cust-b cust-a 200
DAy 2001:db8:1::/64 cust-a@cust-a cust-b 200
DAc 2001:db8:2::/64 ether4@cust-b cust-b 0

The imported route carries the route target and the route distinguisher of the VPN it came from:

[admin@MikroTik] > /routing/route/print detail where routing-table=cust-a && dst-address=192.168.2.0/24
Flags: A - ACTIVE; y - BGP-MPLS-VPN
Ay afi=ip contribution=active dst-address=192.168.2.0/24 routing-table=cust-a
gateway=cust-b@cust-b immediate-gw=cust-b distance=200 scope=20
target-scope=10 belongs-to="cust-a-cust-b-connected-export-import"
bgp.ext-communities=rt:65000:2 .origin=incomplete
route-distinguisher="65000:2" debug.fwp-ptr=0x20282660

Only the route types in export.redistribute are exported: the static default routes of the VRFs stay in their own VRF. The host in cust-b reaches both the host and the router's address in cust-a:

[admin@MikroTik] > /ping 192.168.1.1 count=3
SEQ HOST SIZE TTL TIME STATUS
0 192.168.1.1 56 64 250us
1 192.168.1.1 56 64 334us
2 192.168.1.1 56 64 330us
sent=3 received=3 packet-loss=0% min-rtt=250us avg-rtt=304us
max-rtt=334us

MPLS VPN between two PE routers​

In this example, an MPLS backbone of two provider edge routers (PE1 and PE2) connects the two sites of the customer cust-one, CE1 and CE2. The links from the PEs to the customer routers are in the cust-one VRF. The PEs run LDP on the backbone link and exchange the VRF routes as VPNv4 routes over iBGP between their loopback addresses.

graph LR
    ce1["<b>CE1</b><br />10.1.1.1"] ---|"cust-one<br />10.1.1.0/24"| pe1["<b>PE1</b><br />lo 10.5.5.2"]
    pe1 ---|"MPLS backbone<br />10.2.2.0/24"| pe2["<b>PE2</b><br />lo 10.5.5.3"]
    pe2 ---|"cust-one<br />10.3.3.0/24"| ce2["<b>CE2</b><br />10.3.3.4"]

CE1

/ip/address
add address=10.1.1.1/24 interface=ether2
/ip/route
add dst-address=10.3.3.0/24 gateway=10.1.1.2

CE2

/ip/address
add address=10.3.3.4/24 interface=ether2
/ip/route
add dst-address=10.1.1.0/24 gateway=10.3.3.3

PE1 (ether2 to PE2, ether3 to CE1)

/ip/address
add address=10.5.5.2/32 interface=lo
add address=10.2.2.2/24 interface=ether2
add address=10.1.1.2/24 interface=ether3
/ip/route
add dst-address=10.5.5.3/32 gateway=10.2.2.3
/mpls/ldp
add afi=ip lsr-id=10.5.5.2 transport-addresses=10.5.5.2
/mpls/ldp/interface
add interface=ether2
/ip/vrf
add name=cust-one interfaces=ether3
/routing/bgp/instance
add name=bgp1 as=65000 router-id=10.5.5.2
/routing/bgp/connection
add name=to-pe2 instance=bgp1 remote.address=10.5.5.3 \
local.address=10.5.5.2 local.role=ibgp afi=vpnv4
/routing/bgp/vpn
add name=cust-one instance=bgp1 vrf=cust-one \
route-distinguisher=1.1.1.1:111 label-allocation-policy=per-vrf \
import.route-targets=1.1.1.1:111 export.route-targets=1.1.1.1:111 \
export.redistribute=connected

PE2 (ether2 to PE1, ether3 to CE2)

/ip/address
add address=10.5.5.3/32 interface=lo
add address=10.2.2.3/24 interface=ether2
add address=10.3.3.3/24 interface=ether3
/ip/route
add dst-address=10.5.5.2/32 gateway=10.2.2.2
/mpls/ldp
add afi=ip lsr-id=10.5.5.3 transport-addresses=10.5.5.3
/mpls/ldp/interface
add interface=ether2
/ip/vrf
add name=cust-one interfaces=ether3
/routing/bgp/instance
add name=bgp1 as=65000 router-id=10.5.5.3
/routing/bgp/connection
add name=to-pe1 instance=bgp1 remote.address=10.5.5.2 \
local.address=10.5.5.3 local.role=ibgp afi=vpnv4
/routing/bgp/vpn
add name=cust-one instance=bgp1 vrf=cust-one \
route-distinguisher=1.1.1.2:111 label-allocation-policy=per-vrf \
import.route-targets=1.1.1.1:111 export.route-targets=1.1.1.1:111 \
export.redistribute=connected

In this example, a static route to the other PE's loopback address replaces an IGP. In a larger backbone, run OSPF or another IGP between the PEs and the P routers, as on the LDP page. Each PE uses its own route distinguisher (one per site, explained later on this page), and both use the same route target 1.1.1.1:111, so each PE imports the other PE's cust-one routes.

Check the MPLS VPN​

The BGP session carries the vpnv4 address family:

[admin@MikroTik] > /routing/bgp/session/print
Flags: E - ESTABLISHED
0 E name="to-pe2-1" instance=bgp1 remote.address=10.5.5.3 .as=65000
.id=10.5.5.3 .capabilities=mp,rr,enhe,gr,as4 .afi=vpnv4 .messages=2
.bytes=105 .eor="" local.role=ibgp .address=10.5.5.2 .as=65000
.id=10.5.5.2 .cluster-id=10.5.5.2 .capabilities=mp,rr,enhe,gr,as4
.afi=vpnv4 .messages=2 .bytes=105 .eor="" output.procid=20 input.procid=20
ibgp multihop=yes hold-time=3m keepalive-time=1m uptime=29s130ms
last-started=2026-12-02 10:56:11 prefix-count=1

The cust-one table on PE1 has the connected network of CE1 and the network of CE2, learned from PE2 (flag y):

[admin@MikroTik] > /ip/route/print where routing-table=cust-one
Flags: D - DYNAMIC; A - ACTIVE; c - CONNECT, y - BGP-MPLS-VPN
Columns: DST-ADDRESS, GATEWAY, ROUTING-TABLE, DISTANCE
DST-ADDRESS GATEWAY ROUTING-TABLE DISTANCE
DAc 10.1.1.0/24 ether3@cust-one cust-one 0
DAy 10.3.3.0/24 10.5.5.3 cust-one 200

The VPNv4 routes are in the main table: the local network exported from the VRF, and the remote one received from PE2. The destination carries the route distinguisher after &, and label is the VPN label:

[admin@MikroTik] > /routing/route/print detail where afi=vpnv4
Flags: A - ACTIVE; b - BGP, y - BGP-MPLS-VPN
Ay afi=vpnv4 contribution=active dst-address=10.1.1.0/24&1.1.1.1:111
routing-table=main label=16 gateway=ether3@cust-one immediate-gw=ether3
distance=200 scope=40 target-scope=30 belongs-to="cust-one-connected-export"
.ext-communities=rt:1.1.1.1:111 .origin=incomplete debug.fwp-ptr=0x20282540

Ab afi=vpnv4 contribution=active dst-address=10.3.3.0/24&1.1.1.2:111
routing-table=main label=16 gateway=10.5.5.3 immediate-gw=10.2.2.3%ether2
distance=200 scope=40 target-scope=30 belongs-to="bgp-VPNv4-10.5.5.3"
bgp.session=to-pe2-1 .ext-communities=rt:1.1.1.1:111 .local-pref=100
.origin=incomplete debug.fwp-ptr=0x202825A0

The 10.3.3.0/24 route is imported into cust-one because the route target of its extended communities (rt:1.1.1.1:111) is in the import.route-targets of the VPN. CE1 reaches CE2, and the traceroute shows the label on the backbone hop:

[admin@MikroTik] > /ping 10.3.3.4 count=3
SEQ HOST SIZE TTL TIME STATUS
0 10.3.3.4 56 62 1ms983us
1 10.3.3.4 56 62 3ms547us
2 10.3.3.4 56 62 4ms97us
sent=3 received=3 packet-loss=0% min-rtt=1ms983us avg-rtt=3ms209us
max-rtt=4ms97us
[admin@MikroTik] > /tool/traceroute 10.3.3.4 count=1
Columns: ADDRESS, LOSS, SENT, LAST, AVG, BEST, WORST, STD-DEV, STATUS
# ADDRESS LOSS SENT LAST AVG BEST WORST STD-DEV STATUS
0 10.1.1.2 0% 1 1.3ms 1.3 1.3 1.3 0
1 10.2.2.3 0% 1 0.7ms 0.7 0.7 0.7 0 <MPLS:L=16,E=0>
2 10.3.3.4 0% 1 1.3ms 1.3 1.3 1.3 0

The label in the traceroute is the VPN label 16 of the route. PE2 advertises an implicit-null LDP label for its loopback address (the BGP next hop), so PE1 sends the packets with the VPN label only. The LDP label mapping for the other PE's loopback address is required: when LDP is disabled on PE1, the 10.3.3.0/24 route stays in cust-one, but CE1 no longer reaches CE2.

Route distinguisher per site or per customer​

A customer network can be connected to more than one PE, for example a site with two uplinks. In this example, the network 10.12.0.0/24 is connected to PE1 (ether5, 10.12.0.1) and to PE2 (10.12.0.2), both in cust-one. The route distinguisher can be assigned in two ways:

  • Per customer: the VPN has the same route distinguisher on every PE.
  • Per site: each PE uses its own route distinguisher for the VPN.

With the same route distinguisher, both PEs export the same VPNv4 route, 10.12.0.0/24&1.1.1.1:111. BGP selects one best route for it, and each PE prefers its own export. The route from the other PE stays a candidate in the VPNv4 table and is not imported into the VRF. When the local network fails, the PE withdraws its route, selects the other PE's route and imports it into the VRF. Only then can traffic use the other site. A similar selection happens when the exported route is a BGP route learned from the CE, where the BGP best-path selection picks one of them.

With a route distinguisher per site, the two VPNv4 routes have different destinations (10.12.0.0/24&1.1.1.1:111 and 10.12.0.0/24&1.1.1.2:111). Both are kept and both are imported into the VRF, where the route from the other site waits as a candidate next to the connected route. Per-site route distinguishers are the reason PE1 and PE2 use 1.1.1.1:111 and 1.1.1.2:111 in the example. Add the shared network to cust-one on both PEs:

PE1

/ip/vrf
set [find name=cust-one] interfaces=ether3,ether5
/ip/address
add address=10.12.0.1/24 interface=ether5

PE2

/ip/vrf
set [find name=cust-one] interfaces=ether3,ether5
/ip/address
add address=10.12.0.2/24 interface=ether5

On PE1, the connected route is active, and the route from PE2 waits in cust-one as a candidate (flag y without A):

[admin@MikroTik] > /ip/route/print where dst-address=10.12.0.0/24
Flags: D - DYNAMIC; A - ACTIVE; c - CONNECT, y - BGP-MPLS-VPN
Columns: DST-ADDRESS, GATEWAY, ROUTING-TABLE, DISTANCE
DST-ADDRESS GATEWAY ROUTING-TABLE DISTANCE
DAc 10.12.0.0/24 ether5@cust-one cust-one 0
D y 10.12.0.0/24 10.5.5.3 cust-one 200
[admin@MikroTik] > /routing/route/print detail where dst-address=10.12.0.0/24
Flags: A - ACTIVE; c - CONNECT, b - BGP, y - BGP-MPLS-VPN
Ac afi=ip contribution=active dst-address=10.12.0.0/24 routing-table=cust-one
gateway=ether5@cust-one immediate-gw=ether5 distance=0 scope=10
target-scope=5 belongs-to="connected"
local-address=10.12.0.1%ether5@cust-one debug.fwp-ptr=0x20282660

y afi=ip contribution=best-candidate dst-address=10.12.0.0/24
routing-table=cust-one label=16 gateway=10.5.5.3
immediate-gw=10.2.2.3%ether2 distance=200 scope=40 target-scope=30
belongs-to="cust-one-bgp-VPNv4-10.5.5.3-import" bgp.session=to-pe2-1
.ext-communities=rt:1.1.1.1:111 .local-pref=100 .origin=incomplete
route-distinguisher="1.1.1.2:111" debug.fwp-ptr=0x20282600

Ay afi=vpnv4 contribution=active dst-address=10.12.0.0/24&1.1.1.1:111
routing-table=main label=16 gateway=ether5@cust-one immediate-gw=ether5
distance=200 scope=40 target-scope=30 belongs-to="cust-one-connected-export"
.ext-communities=rt:1.1.1.1:111 .origin=incomplete debug.fwp-ptr=0x20282360

Ab afi=vpnv4 contribution=active dst-address=10.12.0.0/24&1.1.1.2:111
routing-table=main label=16 gateway=10.5.5.3 immediate-gw=10.2.2.3%ether2
distance=200 scope=40 target-scope=30 belongs-to="bgp-VPNv4-10.5.5.3"
bgp.session=to-pe2-1 .ext-communities=rt:1.1.1.1:111 .local-pref=100
.origin=incomplete debug.fwp-ptr=0x202825A0

To influence which of the routes is used, see route selection and filtering.

The next section adds a second customer whose VPN imports cust-one. Before you continue, remove the shared network from cust-one on both PEs:

/ip/address
remove [find interface=ether5]
/ip/vrf
set [find name=cust-one] interfaces=ether3

Exchange routes between two customers​

By default, a site of one VPN has no access to another VPN: separate routing keeps customers private and allows overlapping prefixes. Exchanging routes between VPNs breaks both, but it is sometimes needed, for example during the migration of two customers to one network. This example continues from the two-PE example, after the shared network of the previous section is removed. PE2 gets a second customer, cust-two on ether4 (10.4.4.0/24, CE3 at 10.4.4.5), and the two VPNs import each other's route targets.

graph LR
    ce1["<b>CE1, cust-one</b><br />10.1.1.1"] ---|"cust-one<br />10.1.1.0/24"| pe1["<b>PE1</b><br />lo 10.5.5.2"]
    pe1 ---|"MPLS backbone<br />10.2.2.0/24"| pe2["<b>PE2</b><br />lo 10.5.5.3"]
    pe2 ---|"cust-one<br />10.3.3.0/24"| ce2["<b>CE2, cust-one</b><br />10.3.3.4"]
    pe2 ---|"cust-two<br />10.4.4.0/24"| ce3["<b>CE3, cust-two</b><br />10.4.4.5"]

PE1

/routing/bgp/vpn
set [find name=cust-one] import.route-targets=1.1.1.1:111,2.2.2.2:222

PE2

/ip/address
add address=10.4.4.3/24 interface=ether4
/ip/vrf
add name=cust-two interfaces=ether4
/routing/bgp/vpn
set [find name=cust-one] import.route-targets=1.1.1.1:111,2.2.2.2:222
add name=cust-two instance=bgp1 vrf=cust-two \
route-distinguisher=2.2.2.2:222 label-allocation-policy=per-vrf \
import.route-targets=1.1.1.1:111,2.2.2.2:222 \
export.route-targets=2.2.2.2:222 export.redistribute=connected

CE1

/ip/route
add dst-address=10.4.4.0/24 gateway=10.1.1.2

CE2

/ip/route
add dst-address=10.4.4.0/24 gateway=10.3.3.3

CE3

/ip/address
add address=10.4.4.5/24 interface=ether2
/ip/route
add dst-address=10.1.1.0/24 gateway=10.4.4.3
add dst-address=10.3.3.0/24 gateway=10.4.4.3

On PE2, both VRF tables now hold the networks of both customers:

[admin@MikroTik] > /ip/route/print where routing-table~"cust"
Flags: D - DYNAMIC; A - ACTIVE; c - CONNECT, y - BGP-MPLS-VPN
Columns: DST-ADDRESS, GATEWAY, ROUTING-TABLE, DISTANCE
DST-ADDRESS GATEWAY ROUTING-TABLE DISTANCE
DAy 10.1.1.0/24 10.5.5.2 cust-one 200
DAc 10.3.3.0/24 ether3@cust-one cust-one 0
DAy 10.4.4.0/24 cust-two@cust-two cust-one 200
DAy 10.1.1.0/24 10.5.5.2 cust-two 200
DAy 10.3.3.0/24 cust-one@cust-one cust-two 200
DAc 10.4.4.0/24 ether4@cust-two cust-two 0

10.1.1.0/24 comes from PE1 and is imported into both tables. 10.3.3.0/24 and 10.4.4.0/24 are each connected in one table and imported into the other: PE2 exports them to its local VPNv4 table and imports them again by route target. Such a route has the VRF interface as the gateway and no BGP session attributes:

[admin@MikroTik] > /routing/route/print detail where routing-table=cust-one && dst-address=10.4.4.0/24
Flags: A - ACTIVE; y - BGP-MPLS-VPN
Ay afi=ip contribution=active dst-address=10.4.4.0/24 routing-table=cust-one
gateway=cust-two@cust-two immediate-gw=cust-two distance=200 scope=20
target-scope=10 belongs-to="cust-one-cust-two-connected-export-import"
bgp.ext-communities=rt:2.2.2.2:222 .origin=incomplete
route-distinguisher="2.2.2.2:222" debug.fwp-ptr=0x202C27E0

CE1 of cust-one reaches CE3 of cust-two:

[admin@MikroTik] > /ping 10.4.4.5 count=3
SEQ HOST SIZE TTL TIME STATUS
0 10.4.4.5 56 62 1ms355us
1 10.4.4.5 56 62 2ms490us
2 10.4.4.5 56 62 1ms18us
sent=3 received=3 packet-loss=0% min-rtt=1ms18us avg-rtt=1ms621us
max-rtt=2ms490us

Interoperate with a Cisco PE​

PE2 can be a Cisco router. The following Cisco configuration matches the cust-one VPN of PE1. It uses the route distinguisher 1.1.1.1:111 and the route target 1.1.1.1:111, and announces the connected network 10.3.3.0/24 of FastEthernet1/0. The route distinguisher does not have to match PE1's: as with the RouterOS PE2, a separate route distinguisher per site works. The route targets have to match.

ip vrf cust-one
rd 1.1.1.1:111
route-target export 1.1.1.1:111
route-target import 1.1.1.1:111
exit

interface Loopback0
ip address 10.5.5.3 255.255.255.255

mpls ldp router-id Loopback0 force
mpls label protocol ldp

interface FastEthernet0/0
ip address 10.2.2.3 255.255.255.0
mpls ip

interface FastEthernet1/0
ip vrf forwarding cust-one
ip address 10.3.3.3 255.255.255.0

router bgp 65000
neighbor 10.5.5.2 remote-as 65000
neighbor 10.5.5.2 update-source Loopback0
address-family vpnv4
neighbor 10.5.5.2 activate
neighbor 10.5.5.2 send-community both
exit-address-family
address-family ipv4 vrf cust-one
redistribute connected
exit-address-family

ip route 10.5.5.2 255.255.255.255 10.2.2.2

The VPNv4 and VRF tables on the Cisco PE:

PE2#show ip bgp vpnv4 all
BGP table version is 5, local router ID is 10.5.5.3
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
r RIB-failure, S Stale
Origin codes: i - IGP, e - EGP, ? - incomplete
Network Next Hop Metric LocPrf Weight Path
Route Distinguisher: 1.1.1.1:111 (default for vrf cust-one)
*>i10.1.1.0/24 10.5.5.2 100 0 ?
*> 10.3.3.0/24 0.0.0.0 0 32768 ?

PE2#show ip route vrf cust-one
Routing Table: cust-one
Codes: C - connected, S - static, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2
i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
ia - IS-IS inter area, * - candidate default, U - per-user static route
o - ODR, P - periodic downloaded static route

Gateway of last resort is not set
10.0.0.0/24 is subnetted, 1 subnets
B 10.1.1.0 [200/0] via 10.5.5.2, 00:05:33
10.0.0.0/24 is subnetted, 1 subnets
C 10.3.3.0 is directly connected, FastEthernet1/0

For the second customer, the Cisco configuration adds the cust-two VRF and the cross-customer route targets:

ip vrf cust-one
rd 1.1.1.1:111
route-target export 1.1.1.1:111
route-target import 1.1.1.1:111
route-target import 2.2.2.2:222
exit

ip vrf cust-two
rd 2.2.2.2:222
route-target export 2.2.2.2:222
route-target import 1.1.1.1:111
route-target import 2.2.2.2:222
exit

interface FastEthernet2/0
ip vrf forwarding cust-two
ip address 10.4.4.3 255.255.255.0

router bgp 65000
address-family ipv4 vrf cust-two
redistribute connected
exit-address-family

Technical details​

VRF interface​

Each VRF adds an interface of type vrf with the VRF's name. It is the gateway of routes that lead into the VRF (cust-a@cust-a), the interface that firewall filter rules match in in-interface and out-interface for traffic of the VRF's ports, and the gateway of routes that BGP VPN imports from another VRF on the same router.

Hardware offloading​

On devices with L3 hardware offloading, VRF routes can be offloaded: see Hardware-offloaded VRF.

References​

For all properties, see the CLI reference of /ip/vrf and /routing/bgp/vpn.