vrf.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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.
  8. Processes can be "VRF aware" by binding a socket to the VRF device. Packets
  9. through the socket then use the routing table associated with the VRF
  10. device. An important feature of the VRF device implementation is that it
  11. impacts only Layer 3 and above so L2 tools (e.g., LLDP) are not affected
  12. (ie., they do not need to be run in each VRF). The design also allows
  13. the use of higher priority ip rules (Policy Based Routing, PBR) to take
  14. precedence over the VRF device rules directing specific traffic as desired.
  15. In addition, VRF devices allow VRFs to be nested within namespaces. For
  16. example network namespaces provide separation of network interfaces at the
  17. device layer, VLANs on the interfaces within a namespace provide L2 separation
  18. and then VRF devices provide L3 separation.
  19. Design
  20. ------
  21. A VRF device is created with an associated route table. Network interfaces
  22. are then enslaved to a VRF device:
  23. +-----------------------------+
  24. | vrf-blue | ===> route table 10
  25. +-----------------------------+
  26. | | |
  27. +------+ +------+ +-------------+
  28. | eth1 | | eth2 | ... | bond1 |
  29. +------+ +------+ +-------------+
  30. | |
  31. +------+ +------+
  32. | eth8 | | eth9 |
  33. +------+ +------+
  34. Packets received on an enslaved device and are switched to the VRF device
  35. in the IPv4 and IPv6 processing stacks giving the impression that packets
  36. flow through the VRF device. Similarly on egress routing rules are used to
  37. send packets to the VRF device driver before getting sent out the actual
  38. interface. This allows tcpdump on a VRF device to capture all packets into
  39. and out of the VRF as a whole.[1] Similarly, netfilter[2] and tc rules can be
  40. applied using the VRF device to specify rules that apply to the VRF domain
  41. as a whole.
  42. [1] Packets in the forwarded state do not flow through the device, so those
  43. packets are not seen by tcpdump. Will revisit this limitation in a
  44. future release.
  45. [2] Iptables on ingress supports PREROUTING with skb->dev set to the real
  46. ingress device and both INPUT and PREROUTING rules with skb->dev set to
  47. the VRF device. For egress POSTROUTING and OUTPUT rules can be written
  48. using either the VRF device or real egress device.
  49. Setup
  50. -----
  51. 1. VRF device is created with an association to a FIB table.
  52. e.g, ip link add vrf-blue type vrf table 10
  53. ip link set dev vrf-blue up
  54. 2. An l3mdev FIB rule directs lookups to the table associated with the device.
  55. A single l3mdev rule is sufficient for all VRFs. The VRF device adds the
  56. l3mdev rule for IPv4 and IPv6 when the first device is created with a
  57. default preference of 1000. Users may delete the rule if desired and add
  58. with a different priority or install per-VRF rules.
  59. Prior to the v4.8 kernel iif and oif rules are needed for each VRF device:
  60. ip ru add oif vrf-blue table 10
  61. ip ru add iif vrf-blue table 10
  62. 3. Set the default route for the table (and hence default route for the VRF).
  63. ip route add table 10 unreachable default metric 4278198272
  64. This high metric value ensures that the default unreachable route can
  65. be overridden by a routing protocol suite. FRRouting interprets
  66. kernel metrics as a combined admin distance (upper byte) and priority
  67. (lower 3 bytes). Thus the above metric translates to [255/8192].
  68. 4. Enslave L3 interfaces to a VRF device.
  69. ip link set dev eth1 master vrf-blue
  70. Local and connected routes for enslaved devices are automatically moved to
  71. the table associated with VRF device. Any additional routes depending on
  72. the enslaved device are dropped and will need to be reinserted to the VRF
  73. FIB table following the enslavement.
  74. The IPv6 sysctl option keep_addr_on_down can be enabled to keep IPv6 global
  75. addresses as VRF enslavement changes.
  76. sysctl -w net.ipv6.conf.all.keep_addr_on_down=1
  77. 5. Additional VRF routes are added to associated table.
  78. ip route add table 10 ...
  79. Applications
  80. ------------
  81. Applications that are to work within a VRF need to bind their socket to the
  82. VRF device:
  83. setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, dev, strlen(dev)+1);
  84. or to specify the output device using cmsg and IP_PKTINFO.
  85. TCP & UDP services running in the default VRF context (ie., not bound
  86. to any VRF device) can work across all VRF domains by enabling the
  87. tcp_l3mdev_accept and udp_l3mdev_accept sysctl options:
  88. sysctl -w net.ipv4.tcp_l3mdev_accept=1
  89. sysctl -w net.ipv4.udp_l3mdev_accept=1
  90. netfilter rules on the VRF device can be used to limit access to services
  91. running in the default VRF context as well.
  92. The default VRF does not have limited scope with respect to port bindings.
  93. That is, if a process does a wildcard bind to a port in the default VRF it
  94. owns the port across all VRF domains within the network namespace.
  95. ################################################################################
  96. Using iproute2 for VRFs
  97. =======================
  98. iproute2 supports the vrf keyword as of v4.7. For backwards compatibility this
  99. section lists both commands where appropriate -- with the vrf keyword and the
  100. older form without it.
  101. 1. Create a VRF
  102. To instantiate a VRF device and associate it with a table:
  103. $ ip link add dev NAME type vrf table ID
  104. As of v4.8 the kernel supports the l3mdev FIB rule where a single rule
  105. covers all VRFs. The l3mdev rule is created for IPv4 and IPv6 on first
  106. device create.
  107. 2. List VRFs
  108. To list VRFs that have been created:
  109. $ ip [-d] link show type vrf
  110. NOTE: The -d option is needed to show the table id
  111. For example:
  112. $ ip -d link show type vrf
  113. 11: mgmt: <NOARP,MASTER,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
  114. link/ether 72:b3:ba:91:e2:24 brd ff:ff:ff:ff:ff:ff promiscuity 0
  115. vrf table 1 addrgenmode eui64
  116. 12: red: <NOARP,MASTER,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
  117. link/ether b6:6f:6e:f6:da:73 brd ff:ff:ff:ff:ff:ff promiscuity 0
  118. vrf table 10 addrgenmode eui64
  119. 13: blue: <NOARP,MASTER,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
  120. link/ether 36:62:e8:7d:bb:8c brd ff:ff:ff:ff:ff:ff promiscuity 0
  121. vrf table 66 addrgenmode eui64
  122. 14: green: <NOARP,MASTER,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
  123. link/ether e6:28:b8:63:70:bb brd ff:ff:ff:ff:ff:ff promiscuity 0
  124. vrf table 81 addrgenmode eui64
  125. Or in brief output:
  126. $ ip -br link show type vrf
  127. mgmt UP 72:b3:ba:91:e2:24 <NOARP,MASTER,UP,LOWER_UP>
  128. red UP b6:6f:6e:f6:da:73 <NOARP,MASTER,UP,LOWER_UP>
  129. blue UP 36:62:e8:7d:bb:8c <NOARP,MASTER,UP,LOWER_UP>
  130. green UP e6:28:b8:63:70:bb <NOARP,MASTER,UP,LOWER_UP>
  131. 3. Assign a Network Interface to a VRF
  132. Network interfaces are assigned to a VRF by enslaving the netdevice to a
  133. VRF device:
  134. $ ip link set dev NAME master NAME
  135. On enslavement connected and local routes are automatically moved to the
  136. table associated with the VRF device.
  137. For example:
  138. $ ip link set dev eth0 master mgmt
  139. 4. Show Devices Assigned to a VRF
  140. To show devices that have been assigned to a specific VRF add the master
  141. option to the ip command:
  142. $ ip link show vrf NAME
  143. $ ip link show master NAME
  144. For example:
  145. $ ip link show vrf red
  146. 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master red state UP mode DEFAULT group default qlen 1000
  147. link/ether 02:00:00:00:02:02 brd ff:ff:ff:ff:ff:ff
  148. 4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master red state UP mode DEFAULT group default qlen 1000
  149. link/ether 02:00:00:00:02:03 brd ff:ff:ff:ff:ff:ff
  150. 7: eth5: <BROADCAST,MULTICAST> mtu 1500 qdisc noop master red state DOWN mode DEFAULT group default qlen 1000
  151. link/ether 02:00:00:00:02:06 brd ff:ff:ff:ff:ff:ff
  152. Or using the brief output:
  153. $ ip -br link show vrf red
  154. eth1 UP 02:00:00:00:02:02 <BROADCAST,MULTICAST,UP,LOWER_UP>
  155. eth2 UP 02:00:00:00:02:03 <BROADCAST,MULTICAST,UP,LOWER_UP>
  156. eth5 DOWN 02:00:00:00:02:06 <BROADCAST,MULTICAST>
  157. 5. Show Neighbor Entries for a VRF
  158. To list neighbor entries associated with devices enslaved to a VRF device
  159. add the master option to the ip command:
  160. $ ip [-6] neigh show vrf NAME
  161. $ ip [-6] neigh show master NAME
  162. For example:
  163. $ ip neigh show vrf red
  164. 10.2.1.254 dev eth1 lladdr a6:d9:c7:4f:06:23 REACHABLE
  165. 10.2.2.254 dev eth2 lladdr 5e:54:01:6a:ee:80 REACHABLE
  166. $ ip -6 neigh show vrf red
  167. 2002:1::64 dev eth1 lladdr a6:d9:c7:4f:06:23 REACHABLE
  168. 6. Show Addresses for a VRF
  169. To show addresses for interfaces associated with a VRF add the master
  170. option to the ip command:
  171. $ ip addr show vrf NAME
  172. $ ip addr show master NAME
  173. For example:
  174. $ ip addr show vrf red
  175. 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master red state UP group default qlen 1000
  176. link/ether 02:00:00:00:02:02 brd ff:ff:ff:ff:ff:ff
  177. inet 10.2.1.2/24 brd 10.2.1.255 scope global eth1
  178. valid_lft forever preferred_lft forever
  179. inet6 2002:1::2/120 scope global
  180. valid_lft forever preferred_lft forever
  181. inet6 fe80::ff:fe00:202/64 scope link
  182. valid_lft forever preferred_lft forever
  183. 4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master red state UP group default qlen 1000
  184. link/ether 02:00:00:00:02:03 brd ff:ff:ff:ff:ff:ff
  185. inet 10.2.2.2/24 brd 10.2.2.255 scope global eth2
  186. valid_lft forever preferred_lft forever
  187. inet6 2002:2::2/120 scope global
  188. valid_lft forever preferred_lft forever
  189. inet6 fe80::ff:fe00:203/64 scope link
  190. valid_lft forever preferred_lft forever
  191. 7: eth5: <BROADCAST,MULTICAST> mtu 1500 qdisc noop master red state DOWN group default qlen 1000
  192. link/ether 02:00:00:00:02:06 brd ff:ff:ff:ff:ff:ff
  193. Or in brief format:
  194. $ ip -br addr show vrf red
  195. eth1 UP 10.2.1.2/24 2002:1::2/120 fe80::ff:fe00:202/64
  196. eth2 UP 10.2.2.2/24 2002:2::2/120 fe80::ff:fe00:203/64
  197. eth5 DOWN
  198. 7. Show Routes for a VRF
  199. To show routes for a VRF use the ip command to display the table associated
  200. with the VRF device:
  201. $ ip [-6] route show vrf NAME
  202. $ ip [-6] route show table ID
  203. For example:
  204. $ ip route show vrf red
  205. unreachable default metric 4278198272
  206. broadcast 10.2.1.0 dev eth1 proto kernel scope link src 10.2.1.2
  207. 10.2.1.0/24 dev eth1 proto kernel scope link src 10.2.1.2
  208. local 10.2.1.2 dev eth1 proto kernel scope host src 10.2.1.2
  209. broadcast 10.2.1.255 dev eth1 proto kernel scope link src 10.2.1.2
  210. broadcast 10.2.2.0 dev eth2 proto kernel scope link src 10.2.2.2
  211. 10.2.2.0/24 dev eth2 proto kernel scope link src 10.2.2.2
  212. local 10.2.2.2 dev eth2 proto kernel scope host src 10.2.2.2
  213. broadcast 10.2.2.255 dev eth2 proto kernel scope link src 10.2.2.2
  214. $ ip -6 route show vrf red
  215. local 2002:1:: dev lo proto none metric 0 pref medium
  216. local 2002:1::2 dev lo proto none metric 0 pref medium
  217. 2002:1::/120 dev eth1 proto kernel metric 256 pref medium
  218. local 2002:2:: dev lo proto none metric 0 pref medium
  219. local 2002:2::2 dev lo proto none metric 0 pref medium
  220. 2002:2::/120 dev eth2 proto kernel metric 256 pref medium
  221. local fe80:: dev lo proto none metric 0 pref medium
  222. local fe80:: dev lo proto none metric 0 pref medium
  223. local fe80::ff:fe00:202 dev lo proto none metric 0 pref medium
  224. local fe80::ff:fe00:203 dev lo proto none metric 0 pref medium
  225. fe80::/64 dev eth1 proto kernel metric 256 pref medium
  226. fe80::/64 dev eth2 proto kernel metric 256 pref medium
  227. ff00::/8 dev red metric 256 pref medium
  228. ff00::/8 dev eth1 metric 256 pref medium
  229. ff00::/8 dev eth2 metric 256 pref medium
  230. unreachable default dev lo metric 4278198272 error -101 pref medium
  231. 8. Route Lookup for a VRF
  232. A test route lookup can be done for a VRF:
  233. $ ip [-6] route get vrf NAME ADDRESS
  234. $ ip [-6] route get oif NAME ADDRESS
  235. For example:
  236. $ ip route get 10.2.1.40 vrf red
  237. 10.2.1.40 dev eth1 table red src 10.2.1.2
  238. cache
  239. $ ip -6 route get 2002:1::32 vrf red
  240. 2002:1::32 from :: dev eth1 table red proto kernel src 2002:1::2 metric 256 pref medium
  241. 9. Removing Network Interface from a VRF
  242. Network interfaces are removed from a VRF by breaking the enslavement to
  243. the VRF device:
  244. $ ip link set dev NAME nomaster
  245. Connected routes are moved back to the default table and local entries are
  246. moved to the local table.
  247. For example:
  248. $ ip link set dev eth0 nomaster
  249. --------------------------------------------------------------------------------
  250. Commands used in this example:
  251. cat >> /etc/iproute2/rt_tables.d/vrf.conf <<EOF
  252. 1 mgmt
  253. 10 red
  254. 66 blue
  255. 81 green
  256. EOF
  257. function vrf_create
  258. {
  259. VRF=$1
  260. TBID=$2
  261. # create VRF device
  262. ip link add ${VRF} type vrf table ${TBID}
  263. if [ "${VRF}" != "mgmt" ]; then
  264. ip route add table ${TBID} unreachable default metric 4278198272
  265. fi
  266. ip link set dev ${VRF} up
  267. }
  268. vrf_create mgmt 1
  269. ip link set dev eth0 master mgmt
  270. vrf_create red 10
  271. ip link set dev eth1 master red
  272. ip link set dev eth2 master red
  273. ip link set dev eth5 master red
  274. vrf_create blue 66
  275. ip link set dev eth3 master blue
  276. vrf_create green 81
  277. ip link set dev eth4 master green
  278. Interface addresses from /etc/network/interfaces:
  279. auto eth0
  280. iface eth0 inet static
  281. address 10.0.0.2
  282. netmask 255.255.255.0
  283. gateway 10.0.0.254
  284. iface eth0 inet6 static
  285. address 2000:1::2
  286. netmask 120
  287. auto eth1
  288. iface eth1 inet static
  289. address 10.2.1.2
  290. netmask 255.255.255.0
  291. iface eth1 inet6 static
  292. address 2002:1::2
  293. netmask 120
  294. auto eth2
  295. iface eth2 inet static
  296. address 10.2.2.2
  297. netmask 255.255.255.0
  298. iface eth2 inet6 static
  299. address 2002:2::2
  300. netmask 120
  301. auto eth3
  302. iface eth3 inet static
  303. address 10.2.3.2
  304. netmask 255.255.255.0
  305. iface eth3 inet6 static
  306. address 2002:3::2
  307. netmask 120
  308. auto eth4
  309. iface eth4 inet static
  310. address 10.2.4.2
  311. netmask 255.255.255.0
  312. iface eth4 inet6 static
  313. address 2002:4::2
  314. netmask 120