vrf.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 L1
  17. (Layer 1 separation), VLANs on the interfaces within a namespace provide
  18. L2 separation 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. using an rx_handler which gives the impression that packets flow through
  36. the VRF device. Similarly on egress routing rules are used to send packets
  37. to the VRF device driver before getting sent out the actual interface. This
  38. allows tcpdump on a VRF device to capture all packets into and out of the
  39. VRF as a whole.[1] Similiarly, netfilter [2] and tc rules can be applied
  40. using the VRF device to specify rules that apply to the VRF domain as a whole.
  41. [1] Packets in the forwarded state do not flow through the device, so those
  42. packets are not seen by tcpdump. Will revisit this limitation in a
  43. future release.
  44. [2] Iptables on ingress is limited to NF_INET_PRE_ROUTING only with skb->dev
  45. set to real ingress device and egress is limited to NF_INET_POST_ROUTING.
  46. Will revisit this limitation in a future release.
  47. Setup
  48. -----
  49. 1. VRF device is created with an association to a FIB table.
  50. e.g, ip link add vrf-blue type vrf table 10
  51. ip link set dev vrf-blue up
  52. 2. Rules are added that send lookups to the associated FIB table when the
  53. iif or oif is the VRF device. e.g.,
  54. ip ru add oif vrf-blue table 10
  55. ip ru add iif vrf-blue table 10
  56. Set the default route for the table (and hence default route for the VRF).
  57. e.g, ip route add table 10 prohibit default
  58. 3. Enslave L3 interfaces to a VRF device.
  59. e.g, ip link set dev eth1 master vrf-blue
  60. Local and connected routes for enslaved devices are automatically moved to
  61. the table associated with VRF device. Any additional routes depending on
  62. the enslaved device will need to be reinserted following the enslavement.
  63. 4. Additional VRF routes are added to associated table.
  64. e.g., ip route add table 10 ...
  65. Applications
  66. ------------
  67. Applications that are to work within a VRF need to bind their socket to the
  68. VRF device:
  69. setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, dev, strlen(dev)+1);
  70. or to specify the output device using cmsg and IP_PKTINFO.
  71. Limitations
  72. -----------
  73. Index of original ingress interface is not available via cmsg. Will address
  74. soon.
  75. ################################################################################
  76. Using iproute2 for VRFs
  77. =======================
  78. VRF devices do *not* have to start with 'vrf-'. That is a convention used here
  79. for emphasis of the device type, similar to use of 'br' in bridge names.
  80. 1. Create a VRF
  81. To instantiate a VRF device and associate it with a table:
  82. $ ip link add dev NAME type vrf table ID
  83. Remember to add the ip rules as well:
  84. $ ip ru add oif NAME table 10
  85. $ ip ru add iif NAME table 10
  86. $ ip -6 ru add oif NAME table 10
  87. $ ip -6 ru add iif NAME table 10
  88. Without the rules route lookups are not directed to the table.
  89. For example:
  90. $ ip link add dev vrf-blue type vrf table 10
  91. $ ip ru add pref 200 oif vrf-blue table 10
  92. $ ip ru add pref 200 iif vrf-blue table 10
  93. $ ip -6 ru add pref 200 oif vrf-blue table 10
  94. $ ip -6 ru add pref 200 iif vrf-blue table 10
  95. 2. List VRFs
  96. To list VRFs that have been created:
  97. $ ip [-d] link show type vrf
  98. NOTE: The -d option is needed to show the table id
  99. For example:
  100. $ ip -d link show type vrf
  101. 11: vrf-mgmt: <NOARP,MASTER,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
  102. link/ether 72:b3:ba:91:e2:24 brd ff:ff:ff:ff:ff:ff promiscuity 0
  103. vrf table 1 addrgenmode eui64
  104. 12: vrf-red: <NOARP,MASTER,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
  105. link/ether b6:6f:6e:f6:da:73 brd ff:ff:ff:ff:ff:ff promiscuity 0
  106. vrf table 10 addrgenmode eui64
  107. 13: vrf-blue: <NOARP,MASTER,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
  108. link/ether 36:62:e8:7d:bb:8c brd ff:ff:ff:ff:ff:ff promiscuity 0
  109. vrf table 66 addrgenmode eui64
  110. 14: vrf-green: <NOARP,MASTER,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
  111. link/ether e6:28:b8:63:70:bb brd ff:ff:ff:ff:ff:ff promiscuity 0
  112. vrf table 81 addrgenmode eui64
  113. Or in brief output:
  114. $ ip -br link show type vrf
  115. vrf-mgmt UP 72:b3:ba:91:e2:24 <NOARP,MASTER,UP,LOWER_UP>
  116. vrf-red UP b6:6f:6e:f6:da:73 <NOARP,MASTER,UP,LOWER_UP>
  117. vrf-blue UP 36:62:e8:7d:bb:8c <NOARP,MASTER,UP,LOWER_UP>
  118. vrf-green UP e6:28:b8:63:70:bb <NOARP,MASTER,UP,LOWER_UP>
  119. 3. Assign a Network Interface to a VRF
  120. Network interfaces are assigned to a VRF by enslaving the netdevice to a
  121. VRF device:
  122. $ ip link set dev NAME master VRF-NAME
  123. On enslavement connected and local routes are automatically moved to the
  124. table associated with the VRF device.
  125. For example:
  126. $ ip link set dev eth0 master vrf-mgmt
  127. 4. Show Devices Assigned to a VRF
  128. To show devices that have been assigned to a specific VRF add the master
  129. option to the ip command:
  130. $ ip link show master VRF-NAME
  131. For example:
  132. $ ip link show master vrf-red
  133. 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master vrf-red state UP mode DEFAULT group default qlen 1000
  134. link/ether 02:00:00:00:02:02 brd ff:ff:ff:ff:ff:ff
  135. 4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master vrf-red state UP mode DEFAULT group default qlen 1000
  136. link/ether 02:00:00:00:02:03 brd ff:ff:ff:ff:ff:ff
  137. 7: eth5: <BROADCAST,MULTICAST> mtu 1500 qdisc noop master vrf-red state DOWN mode DEFAULT group default qlen 1000
  138. link/ether 02:00:00:00:02:06 brd ff:ff:ff:ff:ff:ff
  139. Or using the brief output:
  140. $ ip -br link show master vrf-red
  141. eth1 UP 02:00:00:00:02:02 <BROADCAST,MULTICAST,UP,LOWER_UP>
  142. eth2 UP 02:00:00:00:02:03 <BROADCAST,MULTICAST,UP,LOWER_UP>
  143. eth5 DOWN 02:00:00:00:02:06 <BROADCAST,MULTICAST>
  144. 5. Show Neighbor Entries for a VRF
  145. To list neighbor entries associated with devices enslaved to a VRF device
  146. add the master option to the ip command:
  147. $ ip [-6] neigh show master VRF-NAME
  148. For example:
  149. $ ip neigh show master vrf-red
  150. 10.2.1.254 dev eth1 lladdr a6:d9:c7:4f:06:23 REACHABLE
  151. 10.2.2.254 dev eth2 lladdr 5e:54:01:6a:ee:80 REACHABLE
  152. $ ip -6 neigh show master vrf-red
  153. 2002:1::64 dev eth1 lladdr a6:d9:c7:4f:06:23 REACHABLE
  154. 6. Show Addresses for a VRF
  155. To show addresses for interfaces associated with a VRF add the master
  156. option to the ip command:
  157. $ ip addr show master VRF-NAME
  158. For example:
  159. $ ip addr show master vrf-red
  160. 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master vrf-red state UP group default qlen 1000
  161. link/ether 02:00:00:00:02:02 brd ff:ff:ff:ff:ff:ff
  162. inet 10.2.1.2/24 brd 10.2.1.255 scope global eth1
  163. valid_lft forever preferred_lft forever
  164. inet6 2002:1::2/120 scope global
  165. valid_lft forever preferred_lft forever
  166. inet6 fe80::ff:fe00:202/64 scope link
  167. valid_lft forever preferred_lft forever
  168. 4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master vrf-red state UP group default qlen 1000
  169. link/ether 02:00:00:00:02:03 brd ff:ff:ff:ff:ff:ff
  170. inet 10.2.2.2/24 brd 10.2.2.255 scope global eth2
  171. valid_lft forever preferred_lft forever
  172. inet6 2002:2::2/120 scope global
  173. valid_lft forever preferred_lft forever
  174. inet6 fe80::ff:fe00:203/64 scope link
  175. valid_lft forever preferred_lft forever
  176. 7: eth5: <BROADCAST,MULTICAST> mtu 1500 qdisc noop master vrf-red state DOWN group default qlen 1000
  177. link/ether 02:00:00:00:02:06 brd ff:ff:ff:ff:ff:ff
  178. Or in brief format:
  179. $ ip -br addr show master vrf-red
  180. eth1 UP 10.2.1.2/24 2002:1::2/120 fe80::ff:fe00:202/64
  181. eth2 UP 10.2.2.2/24 2002:2::2/120 fe80::ff:fe00:203/64
  182. eth5 DOWN
  183. 7. Show Routes for a VRF
  184. To show routes for a VRF use the ip command to display the table associated
  185. with the VRF device:
  186. $ ip [-6] route show table ID
  187. For example:
  188. $ ip route show table vrf-red
  189. prohibit default
  190. broadcast 10.2.1.0 dev eth1 proto kernel scope link src 10.2.1.2
  191. 10.2.1.0/24 dev eth1 proto kernel scope link src 10.2.1.2
  192. local 10.2.1.2 dev eth1 proto kernel scope host src 10.2.1.2
  193. broadcast 10.2.1.255 dev eth1 proto kernel scope link src 10.2.1.2
  194. broadcast 10.2.2.0 dev eth2 proto kernel scope link src 10.2.2.2
  195. 10.2.2.0/24 dev eth2 proto kernel scope link src 10.2.2.2
  196. local 10.2.2.2 dev eth2 proto kernel scope host src 10.2.2.2
  197. broadcast 10.2.2.255 dev eth2 proto kernel scope link src 10.2.2.2
  198. $ ip -6 route show table vrf-red
  199. local 2002:1:: dev lo proto none metric 0 pref medium
  200. local 2002:1::2 dev lo proto none metric 0 pref medium
  201. 2002:1::/120 dev eth1 proto kernel metric 256 pref medium
  202. local 2002:2:: dev lo proto none metric 0 pref medium
  203. local 2002:2::2 dev lo proto none metric 0 pref medium
  204. 2002:2::/120 dev eth2 proto kernel metric 256 pref medium
  205. local fe80:: dev lo proto none metric 0 pref medium
  206. local fe80:: dev lo proto none metric 0 pref medium
  207. local fe80::ff:fe00:202 dev lo proto none metric 0 pref medium
  208. local fe80::ff:fe00:203 dev lo proto none metric 0 pref medium
  209. fe80::/64 dev eth1 proto kernel metric 256 pref medium
  210. fe80::/64 dev eth2 proto kernel metric 256 pref medium
  211. ff00::/8 dev vrf-red metric 256 pref medium
  212. ff00::/8 dev eth1 metric 256 pref medium
  213. ff00::/8 dev eth2 metric 256 pref medium
  214. 8. Route Lookup for a VRF
  215. A test route lookup can be done for a VRF by adding the oif option to ip:
  216. $ ip [-6] route get oif VRF-NAME ADDRESS
  217. For example:
  218. $ ip route get 10.2.1.40 oif vrf-red
  219. 10.2.1.40 dev eth1 table vrf-red src 10.2.1.2
  220. cache
  221. $ ip -6 route get 2002:1::32 oif vrf-red
  222. 2002:1::32 from :: dev eth1 table vrf-red proto kernel src 2002:1::2 metric 256 pref medium
  223. 9. Removing Network Interface from a VRF
  224. Network interfaces are removed from a VRF by breaking the enslavement to
  225. the VRF device:
  226. $ ip link set dev NAME nomaster
  227. Connected routes are moved back to the default table and local entries are
  228. moved to the local table.
  229. For example:
  230. $ ip link set dev eth0 nomaster
  231. --------------------------------------------------------------------------------
  232. Commands used in this example:
  233. cat >> /etc/iproute2/rt_tables <<EOF
  234. 1 vrf-mgmt
  235. 10 vrf-red
  236. 66 vrf-blue
  237. 81 vrf-green
  238. EOF
  239. function vrf_create
  240. {
  241. VRF=$1
  242. TBID=$2
  243. # create VRF device
  244. ip link add vrf-${VRF} type vrf table ${TBID}
  245. # add rules that direct lookups to vrf table
  246. ip ru add pref 200 oif vrf-${VRF} table ${TBID}
  247. ip ru add pref 200 iif vrf-${VRF} table ${TBID}
  248. ip -6 ru add pref 200 oif vrf-${VRF} table ${TBID}
  249. ip -6 ru add pref 200 iif vrf-${VRF} table ${TBID}
  250. if [ "${VRF}" != "mgmt" ]; then
  251. ip route add table ${TBID} prohibit default
  252. fi
  253. ip link set dev vrf-${VRF} up
  254. ip link set dev vrf-${VRF} state up
  255. }
  256. vrf_create mgmt 1
  257. ip link set dev eth0 master vrf-mgmt
  258. vrf_create red 10
  259. ip link set dev eth1 master vrf-red
  260. ip link set dev eth2 master vrf-red
  261. ip link set dev eth5 master vrf-red
  262. vrf_create blue 66
  263. ip link set dev eth3 master vrf-blue
  264. vrf_create green 81
  265. ip link set dev eth4 master vrf-green
  266. Interface addresses from /etc/network/interfaces:
  267. auto eth0
  268. iface eth0 inet static
  269. address 10.0.0.2
  270. netmask 255.255.255.0
  271. gateway 10.0.0.254
  272. iface eth0 inet6 static
  273. address 2000:1::2
  274. netmask 120
  275. auto eth1
  276. iface eth1 inet static
  277. address 10.2.1.2
  278. netmask 255.255.255.0
  279. iface eth1 inet6 static
  280. address 2002:1::2
  281. netmask 120
  282. auto eth2
  283. iface eth2 inet static
  284. address 10.2.2.2
  285. netmask 255.255.255.0
  286. iface eth2 inet6 static
  287. address 2002:2::2
  288. netmask 120
  289. auto eth3
  290. iface eth3 inet static
  291. address 10.2.3.2
  292. netmask 255.255.255.0
  293. iface eth3 inet6 static
  294. address 2002:3::2
  295. netmask 120
  296. auto eth4
  297. iface eth4 inet static
  298. address 10.2.4.2
  299. netmask 255.255.255.0
  300. iface eth4 inet6 static
  301. address 2002:4::2
  302. netmask 120