1 Virtual Routing and Forwarding (VRF)
2 ====================================
3 The VRF device combined with ip rules provides the ability to create virtual
4 routing and forwarding domains (aka VRFs, VRF-lite to be specific) in the
5 Linux network stack. One use case is the multi-tenancy problem where each
6 tenant has their own unique routing tables and in the very least need
7 different default gateways.
9 Processes can be "VRF aware" by binding a socket to the VRF device. Packets
10 through the socket then use the routing table associated with the VRF
11 device. An important feature of the VRF device implementation is that it
12 impacts only Layer 3 and above so L2 tools (e.g., LLDP) are not affected
13 (ie., they do not need to be run in each VRF). The design also allows
14 the use of higher priority ip rules (Policy Based Routing, PBR) to take
15 precedence over the VRF device rules directing specific traffic as desired.
17 In addition, VRF devices allow VRFs to be nested within namespaces. For
18 example network namespaces provide separation of network interfaces at L1
19 (Layer 1 separation), VLANs on the interfaces within a namespace provide
20 L2 separation and then VRF devices provide L3 separation.
24 A VRF device is created with an associated route table. Network interfaces
25 are then enslaved to a VRF device:
27 +-----------------------------+
28 | vrf-blue | ===> route table 10
29 +-----------------------------+
31 +------+ +------+ +-------------+
32 | eth1 | | eth2 | ... | bond1 |
33 +------+ +------+ +-------------+
39 Packets received on an enslaved device and are switched to the VRF device
40 using an rx_handler which gives the impression that packets flow through
41 the VRF device. Similarly on egress routing rules are used to send packets
42 to the VRF device driver before getting sent out the actual interface. This
43 allows tcpdump on a VRF device to capture all packets into and out of the
44 VRF as a whole.[1] Similiarly, netfilter [2] and tc rules can be applied
45 using the VRF device to specify rules that apply to the VRF domain as a whole.
47 [1] Packets in the forwarded state do not flow through the device, so those
48 packets are not seen by tcpdump. Will revisit this limitation in a
51 [2] Iptables on ingress is limited to NF_INET_PRE_ROUTING only with skb->dev
52 set to real ingress device and egress is limited to NF_INET_POST_ROUTING.
53 Will revisit this limitation in a future release.
58 1. VRF device is created with an association to a FIB table.
59 e.g, ip link add vrf-blue type vrf table 10
60 ip link set dev vrf-blue up
62 2. Rules are added that send lookups to the associated FIB table when the
63 iif or oif is the VRF device. e.g.,
64 ip ru add oif vrf-blue table 10
65 ip ru add iif vrf-blue table 10
67 Set the default route for the table (and hence default route for the VRF).
68 e.g, ip route add table 10 prohibit default
70 3. Enslave L3 interfaces to a VRF device.
71 e.g, ip link set dev eth1 master vrf-blue
73 Local and connected routes for enslaved devices are automatically moved to
74 the table associated with VRF device. Any additional routes depending on
75 the enslaved device will need to be reinserted following the enslavement.
77 4. Additional VRF routes are added to associated table.
78 e.g., ip route add table 10 ...
83 Applications that are to work within a VRF need to bind their socket to the
86 setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, dev, strlen(dev)+1);
88 or to specify the output device using cmsg and IP_PKTINFO.
93 Index of original ingress interface is not available via cmsg. Will address
96 ################################################################################
98 Using iproute2 for VRFs
99 =======================
100 VRF devices do *not* have to start with 'vrf-'. That is a convention used here
101 for emphasis of the device type, similar to use of 'br' in bridge names.
105 To instantiate a VRF device and associate it with a table:
106 $ ip link add dev NAME type vrf table ID
108 Remember to add the ip rules as well:
109 $ ip ru add oif NAME table 10
110 $ ip ru add iif NAME table 10
111 $ ip -6 ru add oif NAME table 10
112 $ ip -6 ru add iif NAME table 10
114 Without the rules route lookups are not directed to the table.
117 $ ip link add dev vrf-blue type vrf table 10
118 $ ip ru add pref 200 oif vrf-blue table 10
119 $ ip ru add pref 200 iif vrf-blue table 10
120 $ ip -6 ru add pref 200 oif vrf-blue table 10
121 $ ip -6 ru add pref 200 iif vrf-blue table 10
126 To list VRFs that have been created:
127 $ ip [-d] link show type vrf
128 NOTE: The -d option is needed to show the table id
131 $ ip -d link show type vrf
132 11: vrf-mgmt: <NOARP,MASTER,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
133 link/ether 72:b3:ba:91:e2:24 brd ff:ff:ff:ff:ff:ff promiscuity 0
134 vrf table 1 addrgenmode eui64
135 12: vrf-red: <NOARP,MASTER,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
136 link/ether b6:6f:6e:f6:da:73 brd ff:ff:ff:ff:ff:ff promiscuity 0
137 vrf table 10 addrgenmode eui64
138 13: vrf-blue: <NOARP,MASTER,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
139 link/ether 36:62:e8:7d:bb:8c brd ff:ff:ff:ff:ff:ff promiscuity 0
140 vrf table 66 addrgenmode eui64
141 14: vrf-green: <NOARP,MASTER,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
142 link/ether e6:28:b8:63:70:bb brd ff:ff:ff:ff:ff:ff promiscuity 0
143 vrf table 81 addrgenmode eui64
148 $ ip -br link show type vrf
149 vrf-mgmt UP 72:b3:ba:91:e2:24 <NOARP,MASTER,UP,LOWER_UP>
150 vrf-red UP b6:6f:6e:f6:da:73 <NOARP,MASTER,UP,LOWER_UP>
151 vrf-blue UP 36:62:e8:7d:bb:8c <NOARP,MASTER,UP,LOWER_UP>
152 vrf-green UP e6:28:b8:63:70:bb <NOARP,MASTER,UP,LOWER_UP>
155 3. Assign a Network Interface to a VRF
157 Network interfaces are assigned to a VRF by enslaving the netdevice to a
159 $ ip link set dev NAME master VRF-NAME
161 On enslavement connected and local routes are automatically moved to the
162 table associated with the VRF device.
165 $ ip link set dev eth0 master vrf-mgmt
168 4. Show Devices Assigned to a VRF
170 To show devices that have been assigned to a specific VRF add the master
171 option to the ip command:
172 $ ip link show master VRF-NAME
175 $ ip link show master vrf-red
176 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master vrf-red state UP mode DEFAULT group default qlen 1000
177 link/ether 02:00:00:00:02:02 brd ff:ff:ff:ff:ff:ff
178 4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master vrf-red state UP mode DEFAULT group default qlen 1000
179 link/ether 02:00:00:00:02:03 brd ff:ff:ff:ff:ff:ff
180 7: eth5: <BROADCAST,MULTICAST> mtu 1500 qdisc noop master vrf-red state DOWN mode DEFAULT group default qlen 1000
181 link/ether 02:00:00:00:02:06 brd ff:ff:ff:ff:ff:ff
184 Or using the brief output:
185 $ ip -br link show master vrf-red
186 eth1 UP 02:00:00:00:02:02 <BROADCAST,MULTICAST,UP,LOWER_UP>
187 eth2 UP 02:00:00:00:02:03 <BROADCAST,MULTICAST,UP,LOWER_UP>
188 eth5 DOWN 02:00:00:00:02:06 <BROADCAST,MULTICAST>
191 5. Show Neighbor Entries for a VRF
193 To list neighbor entries associated with devices enslaved to a VRF device
194 add the master option to the ip command:
195 $ ip [-6] neigh show master VRF-NAME
198 $ ip neigh show master vrf-red
199 10.2.1.254 dev eth1 lladdr a6:d9:c7:4f:06:23 REACHABLE
200 10.2.2.254 dev eth2 lladdr 5e:54:01:6a:ee:80 REACHABLE
202 $ ip -6 neigh show master vrf-red
203 2002:1::64 dev eth1 lladdr a6:d9:c7:4f:06:23 REACHABLE
206 6. Show Addresses for a VRF
208 To show addresses for interfaces associated with a VRF add the master
209 option to the ip command:
210 $ ip addr show master VRF-NAME
213 $ ip addr show master vrf-red
214 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master vrf-red state UP group default qlen 1000
215 link/ether 02:00:00:00:02:02 brd ff:ff:ff:ff:ff:ff
216 inet 10.2.1.2/24 brd 10.2.1.255 scope global eth1
217 valid_lft forever preferred_lft forever
218 inet6 2002:1::2/120 scope global
219 valid_lft forever preferred_lft forever
220 inet6 fe80::ff:fe00:202/64 scope link
221 valid_lft forever preferred_lft forever
222 4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master vrf-red state UP group default qlen 1000
223 link/ether 02:00:00:00:02:03 brd ff:ff:ff:ff:ff:ff
224 inet 10.2.2.2/24 brd 10.2.2.255 scope global eth2
225 valid_lft forever preferred_lft forever
226 inet6 2002:2::2/120 scope global
227 valid_lft forever preferred_lft forever
228 inet6 fe80::ff:fe00:203/64 scope link
229 valid_lft forever preferred_lft forever
230 7: eth5: <BROADCAST,MULTICAST> mtu 1500 qdisc noop master vrf-red state DOWN group default qlen 1000
231 link/ether 02:00:00:00:02:06 brd ff:ff:ff:ff:ff:ff
234 $ ip -br addr show master vrf-red
235 eth1 UP 10.2.1.2/24 2002:1::2/120 fe80::ff:fe00:202/64
236 eth2 UP 10.2.2.2/24 2002:2::2/120 fe80::ff:fe00:203/64
240 7. Show Routes for a VRF
242 To show routes for a VRF use the ip command to display the table associated
244 $ ip [-6] route show table ID
247 $ ip route show table vrf-red
249 broadcast 10.2.1.0 dev eth1 proto kernel scope link src 10.2.1.2
250 10.2.1.0/24 dev eth1 proto kernel scope link src 10.2.1.2
251 local 10.2.1.2 dev eth1 proto kernel scope host src 10.2.1.2
252 broadcast 10.2.1.255 dev eth1 proto kernel scope link src 10.2.1.2
253 broadcast 10.2.2.0 dev eth2 proto kernel scope link src 10.2.2.2
254 10.2.2.0/24 dev eth2 proto kernel scope link src 10.2.2.2
255 local 10.2.2.2 dev eth2 proto kernel scope host src 10.2.2.2
256 broadcast 10.2.2.255 dev eth2 proto kernel scope link src 10.2.2.2
258 $ ip -6 route show table vrf-red
259 local 2002:1:: dev lo proto none metric 0 pref medium
260 local 2002:1::2 dev lo proto none metric 0 pref medium
261 2002:1::/120 dev eth1 proto kernel metric 256 pref medium
262 local 2002:2:: dev lo proto none metric 0 pref medium
263 local 2002:2::2 dev lo proto none metric 0 pref medium
264 2002:2::/120 dev eth2 proto kernel metric 256 pref medium
265 local fe80:: dev lo proto none metric 0 pref medium
266 local fe80:: dev lo proto none metric 0 pref medium
267 local fe80::ff:fe00:202 dev lo proto none metric 0 pref medium
268 local fe80::ff:fe00:203 dev lo proto none metric 0 pref medium
269 fe80::/64 dev eth1 proto kernel metric 256 pref medium
270 fe80::/64 dev eth2 proto kernel metric 256 pref medium
271 ff00::/8 dev vrf-red metric 256 pref medium
272 ff00::/8 dev eth1 metric 256 pref medium
273 ff00::/8 dev eth2 metric 256 pref medium
276 8. Route Lookup for a VRF
278 A test route lookup can be done for a VRF by adding the oif option to ip:
279 $ ip [-6] route get oif VRF-NAME ADDRESS
282 $ ip route get 10.2.1.40 oif vrf-red
283 10.2.1.40 dev eth1 table vrf-red src 10.2.1.2
286 $ ip -6 route get 2002:1::32 oif vrf-red
287 2002:1::32 from :: dev eth1 table vrf-red proto kernel src 2002:1::2 metric 256 pref medium
290 9. Removing Network Interface from a VRF
292 Network interfaces are removed from a VRF by breaking the enslavement to
294 $ ip link set dev NAME nomaster
296 Connected routes are moved back to the default table and local entries are
297 moved to the local table.
300 $ ip link set dev eth0 nomaster
302 --------------------------------------------------------------------------------
304 Commands used in this example:
306 cat >> /etc/iproute2/rt_tables <<EOF
318 ip link add vrf-${VRF} type vrf table ${TBID}
320 # add rules that direct lookups to vrf table
321 ip ru add pref 200 oif vrf-${VRF} table ${TBID}
322 ip ru add pref 200 iif vrf-${VRF} table ${TBID}
323 ip -6 ru add pref 200 oif vrf-${VRF} table ${TBID}
324 ip -6 ru add pref 200 iif vrf-${VRF} table ${TBID}
326 if [ "${VRF}" != "mgmt" ]; then
327 ip route add table ${TBID} prohibit default
329 ip link set dev vrf-${VRF} up
330 ip link set dev vrf-${VRF} state up
334 ip link set dev eth0 master vrf-mgmt
337 ip link set dev eth1 master vrf-red
338 ip link set dev eth2 master vrf-red
339 ip link set dev eth5 master vrf-red
342 ip link set dev eth3 master vrf-blue
345 ip link set dev eth4 master vrf-green
348 Interface addresses from /etc/network/interfaces:
350 iface eth0 inet static
352 netmask 255.255.255.0
355 iface eth0 inet6 static
360 iface eth1 inet static
362 netmask 255.255.255.0
364 iface eth1 inet6 static
369 iface eth2 inet static
371 netmask 255.255.255.0
373 iface eth2 inet6 static
378 iface eth3 inet static
380 netmask 255.255.255.0
382 iface eth3 inet6 static
387 iface eth4 inet static
389 netmask 255.255.255.0
391 iface eth4 inet6 static