vrf.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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
  64. 4. Enslave L3 interfaces to a VRF device.
  65. ip link set dev eth1 master vrf-blue
  66. Local and connected routes for enslaved devices are automatically moved to
  67. the table associated with VRF device. Any additional routes depending on
  68. the enslaved device are dropped and will need to be reinserted to the VRF
  69. FIB table following the enslavement.
  70. The IPv6 sysctl option keep_addr_on_down can be enabled to keep IPv6 global
  71. addresses as VRF enslavement changes.
  72. sysctl -w net.ipv6.conf.all.keep_addr_on_down=1
  73. 5. Additional VRF routes are added to associated table.
  74. ip route add table 10 ...
  75. Applications
  76. ------------
  77. Applications that are to work within a VRF need to bind their socket to the
  78. VRF device:
  79. setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, dev, strlen(dev)+1);
  80. or to specify the output device using cmsg and IP_PKTINFO.
  81. TCP services running in the default VRF context (ie., not bound to any VRF
  82. device) can work across all VRF domains by enabling the tcp_l3mdev_accept
  83. sysctl option:
  84. sysctl -w net.ipv4.tcp_l3mdev_accept=1
  85. netfilter rules on the VRF device can be used to limit access to services
  86. running in the default VRF context as well.
  87. The default VRF does not have limited scope with respect to port bindings.
  88. That is, if a process does a wildcard bind to a port in the default VRF it
  89. owns the port across all VRF domains within the network namespace.
  90. ################################################################################
  91. Using iproute2 for VRFs
  92. =======================
  93. iproute2 supports the vrf keyword as of v4.7. For backwards compatibility this
  94. section lists both commands where appropriate -- with the vrf keyword and the
  95. older form without it.
  96. 1. Create a VRF
  97. To instantiate a VRF device and associate it with a table:
  98. $ ip link add dev NAME type vrf table ID
  99. As of v4.8 the kernel supports the l3mdev FIB rule where a single rule
  100. covers all VRFs. The l3mdev rule is created for IPv4 and IPv6 on first
  101. device create.
  102. 2. List VRFs
  103. To list VRFs that have been created:
  104. $ ip [-d] link show type vrf
  105. NOTE: The -d option is needed to show the table id
  106. For example:
  107. $ ip -d link show type vrf
  108. 11: mgmt: <NOARP,MASTER,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
  109. link/ether 72:b3:ba:91:e2:24 brd ff:ff:ff:ff:ff:ff promiscuity 0
  110. vrf table 1 addrgenmode eui64
  111. 12: red: <NOARP,MASTER,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
  112. link/ether b6:6f:6e:f6:da:73 brd ff:ff:ff:ff:ff:ff promiscuity 0
  113. vrf table 10 addrgenmode eui64
  114. 13: blue: <NOARP,MASTER,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
  115. link/ether 36:62:e8:7d:bb:8c brd ff:ff:ff:ff:ff:ff promiscuity 0
  116. vrf table 66 addrgenmode eui64
  117. 14: green: <NOARP,MASTER,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
  118. link/ether e6:28:b8:63:70:bb brd ff:ff:ff:ff:ff:ff promiscuity 0
  119. vrf table 81 addrgenmode eui64
  120. Or in brief output:
  121. $ ip -br link show type vrf
  122. mgmt UP 72:b3:ba:91:e2:24 <NOARP,MASTER,UP,LOWER_UP>
  123. red UP b6:6f:6e:f6:da:73 <NOARP,MASTER,UP,LOWER_UP>
  124. blue UP 36:62:e8:7d:bb:8c <NOARP,MASTER,UP,LOWER_UP>
  125. green UP e6:28:b8:63:70:bb <NOARP,MASTER,UP,LOWER_UP>
  126. 3. Assign a Network Interface to a VRF
  127. Network interfaces are assigned to a VRF by enslaving the netdevice to a
  128. VRF device:
  129. $ ip link set dev NAME master NAME
  130. On enslavement connected and local routes are automatically moved to the
  131. table associated with the VRF device.
  132. For example:
  133. $ ip link set dev eth0 master mgmt
  134. 4. Show Devices Assigned to a VRF
  135. To show devices that have been assigned to a specific VRF add the master
  136. option to the ip command:
  137. $ ip link show vrf NAME
  138. $ ip link show master NAME
  139. For example:
  140. $ ip link show vrf red
  141. 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master red state UP mode DEFAULT group default qlen 1000
  142. link/ether 02:00:00:00:02:02 brd ff:ff:ff:ff:ff:ff
  143. 4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master red state UP mode DEFAULT group default qlen 1000
  144. link/ether 02:00:00:00:02:03 brd ff:ff:ff:ff:ff:ff
  145. 7: eth5: <BROADCAST,MULTICAST> mtu 1500 qdisc noop master red state DOWN mode DEFAULT group default qlen 1000
  146. link/ether 02:00:00:00:02:06 brd ff:ff:ff:ff:ff:ff
  147. Or using the brief output:
  148. $ ip -br link show vrf red
  149. eth1 UP 02:00:00:00:02:02 <BROADCAST,MULTICAST,UP,LOWER_UP>
  150. eth2 UP 02:00:00:00:02:03 <BROADCAST,MULTICAST,UP,LOWER_UP>
  151. eth5 DOWN 02:00:00:00:02:06 <BROADCAST,MULTICAST>
  152. 5. Show Neighbor Entries for a VRF
  153. To list neighbor entries associated with devices enslaved to a VRF device
  154. add the master option to the ip command:
  155. $ ip [-6] neigh show vrf NAME
  156. $ ip [-6] neigh show master NAME
  157. For example:
  158. $ ip neigh show vrf red
  159. 10.2.1.254 dev eth1 lladdr a6:d9:c7:4f:06:23 REACHABLE
  160. 10.2.2.254 dev eth2 lladdr 5e:54:01:6a:ee:80 REACHABLE
  161. $ ip -6 neigh show vrf red
  162. 2002:1::64 dev eth1 lladdr a6:d9:c7:4f:06:23 REACHABLE
  163. 6. Show Addresses for a VRF
  164. To show addresses for interfaces associated with a VRF add the master
  165. option to the ip command:
  166. $ ip addr show vrf NAME
  167. $ ip addr show master NAME
  168. For example:
  169. $ ip addr show vrf red
  170. 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master red state UP group default qlen 1000
  171. link/ether 02:00:00:00:02:02 brd ff:ff:ff:ff:ff:ff
  172. inet 10.2.1.2/24 brd 10.2.1.255 scope global eth1
  173. valid_lft forever preferred_lft forever
  174. inet6 2002:1::2/120 scope global
  175. valid_lft forever preferred_lft forever
  176. inet6 fe80::ff:fe00:202/64 scope link
  177. valid_lft forever preferred_lft forever
  178. 4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master red state UP group default qlen 1000
  179. link/ether 02:00:00:00:02:03 brd ff:ff:ff:ff:ff:ff
  180. inet 10.2.2.2/24 brd 10.2.2.255 scope global eth2
  181. valid_lft forever preferred_lft forever
  182. inet6 2002:2::2/120 scope global
  183. valid_lft forever preferred_lft forever
  184. inet6 fe80::ff:fe00:203/64 scope link
  185. valid_lft forever preferred_lft forever
  186. 7: eth5: <BROADCAST,MULTICAST> mtu 1500 qdisc noop master red state DOWN group default qlen 1000
  187. link/ether 02:00:00:00:02:06 brd ff:ff:ff:ff:ff:ff
  188. Or in brief format:
  189. $ ip -br addr show vrf red
  190. eth1 UP 10.2.1.2/24 2002:1::2/120 fe80::ff:fe00:202/64
  191. eth2 UP 10.2.2.2/24 2002:2::2/120 fe80::ff:fe00:203/64
  192. eth5 DOWN
  193. 7. Show Routes for a VRF
  194. To show routes for a VRF use the ip command to display the table associated
  195. with the VRF device:
  196. $ ip [-6] route show vrf NAME
  197. $ ip [-6] route show table ID
  198. For example:
  199. $ ip route show vrf red
  200. prohibit default
  201. broadcast 10.2.1.0 dev eth1 proto kernel scope link src 10.2.1.2
  202. 10.2.1.0/24 dev eth1 proto kernel scope link src 10.2.1.2
  203. local 10.2.1.2 dev eth1 proto kernel scope host src 10.2.1.2
  204. broadcast 10.2.1.255 dev eth1 proto kernel scope link src 10.2.1.2
  205. broadcast 10.2.2.0 dev eth2 proto kernel scope link src 10.2.2.2
  206. 10.2.2.0/24 dev eth2 proto kernel scope link src 10.2.2.2
  207. local 10.2.2.2 dev eth2 proto kernel scope host src 10.2.2.2
  208. broadcast 10.2.2.255 dev eth2 proto kernel scope link src 10.2.2.2
  209. $ ip -6 route show vrf red
  210. local 2002:1:: dev lo proto none metric 0 pref medium
  211. local 2002:1::2 dev lo proto none metric 0 pref medium
  212. 2002:1::/120 dev eth1 proto kernel metric 256 pref medium
  213. local 2002:2:: dev lo proto none metric 0 pref medium
  214. local 2002:2::2 dev lo proto none metric 0 pref medium
  215. 2002:2::/120 dev eth2 proto kernel metric 256 pref medium
  216. local fe80:: dev lo proto none metric 0 pref medium
  217. local fe80:: dev lo proto none metric 0 pref medium
  218. local fe80::ff:fe00:202 dev lo proto none metric 0 pref medium
  219. local fe80::ff:fe00:203 dev lo proto none metric 0 pref medium
  220. fe80::/64 dev eth1 proto kernel metric 256 pref medium
  221. fe80::/64 dev eth2 proto kernel metric 256 pref medium
  222. ff00::/8 dev red metric 256 pref medium
  223. ff00::/8 dev eth1 metric 256 pref medium
  224. ff00::/8 dev eth2 metric 256 pref medium
  225. 8. Route Lookup for a VRF
  226. A test route lookup can be done for a VRF:
  227. $ ip [-6] route get vrf NAME ADDRESS
  228. $ ip [-6] route get oif NAME ADDRESS
  229. For example:
  230. $ ip route get 10.2.1.40 vrf red
  231. 10.2.1.40 dev eth1 table red src 10.2.1.2
  232. cache
  233. $ ip -6 route get 2002:1::32 vrf red
  234. 2002:1::32 from :: dev eth1 table red proto kernel src 2002:1::2 metric 256 pref medium
  235. 9. Removing Network Interface from a VRF
  236. Network interfaces are removed from a VRF by breaking the enslavement to
  237. the VRF device:
  238. $ ip link set dev NAME nomaster
  239. Connected routes are moved back to the default table and local entries are
  240. moved to the local table.
  241. For example:
  242. $ ip link set dev eth0 nomaster
  243. --------------------------------------------------------------------------------
  244. Commands used in this example:
  245. cat >> /etc/iproute2/rt_tables.d/vrf.conf <<EOF
  246. 1 mgmt
  247. 10 red
  248. 66 blue
  249. 81 green
  250. EOF
  251. function vrf_create
  252. {
  253. VRF=$1
  254. TBID=$2
  255. # create VRF device
  256. ip link add ${VRF} type vrf table ${TBID}
  257. if [ "${VRF}" != "mgmt" ]; then
  258. ip route add table ${TBID} unreachable default
  259. fi
  260. ip link set dev ${VRF} up
  261. }
  262. vrf_create mgmt 1
  263. ip link set dev eth0 master mgmt
  264. vrf_create red 10
  265. ip link set dev eth1 master red
  266. ip link set dev eth2 master red
  267. ip link set dev eth5 master red
  268. vrf_create blue 66
  269. ip link set dev eth3 master blue
  270. vrf_create green 81
  271. ip link set dev eth4 master green
  272. Interface addresses from /etc/network/interfaces:
  273. auto eth0
  274. iface eth0 inet static
  275. address 10.0.0.2
  276. netmask 255.255.255.0
  277. gateway 10.0.0.254
  278. iface eth0 inet6 static
  279. address 2000:1::2
  280. netmask 120
  281. auto eth1
  282. iface eth1 inet static
  283. address 10.2.1.2
  284. netmask 255.255.255.0
  285. iface eth1 inet6 static
  286. address 2002:1::2
  287. netmask 120
  288. auto eth2
  289. iface eth2 inet static
  290. address 10.2.2.2
  291. netmask 255.255.255.0
  292. iface eth2 inet6 static
  293. address 2002:2::2
  294. netmask 120
  295. auto eth3
  296. iface eth3 inet static
  297. address 10.2.3.2
  298. netmask 255.255.255.0
  299. iface eth3 inet6 static
  300. address 2002:3::2
  301. netmask 120
  302. auto eth4
  303. iface eth4 inet static
  304. address 10.2.4.2
  305. netmask 255.255.255.0
  306. iface eth4 inet6 static
  307. address 2002:4::2
  308. netmask 120