rdma.txt 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. (RDMA: Remote Direct Memory Access)
  2. RDMA Live Migration Specification, Version # 1
  3. ==============================================
  4. Wiki: https://wiki.qemu.org/Features/RDMALiveMigration
  5. Github: git@github.com:hinesmr/qemu.git, 'rdma' branch
  6. Copyright (C) 2013 Michael R. Hines <mrhines@us.ibm.com>
  7. An *exhaustive* paper (2010) shows additional performance details
  8. linked on the QEMU wiki above.
  9. Contents:
  10. =========
  11. * Introduction
  12. * Before running
  13. * Running
  14. * Performance
  15. * RDMA Migration Protocol Description
  16. * Versioning and Capabilities
  17. * QEMUFileRDMA Interface
  18. * Migration of VM's ram
  19. * Error handling
  20. * TODO
  21. Introduction:
  22. =============
  23. RDMA helps make your migration more deterministic under heavy load because
  24. of the significantly lower latency and higher throughput over TCP/IP. This is
  25. because the RDMA I/O architecture reduces the number of interrupts and
  26. data copies by bypassing the host networking stack. In particular, a TCP-based
  27. migration, under certain types of memory-bound workloads, may take a more
  28. unpredictable amount of time to complete the migration if the amount of
  29. memory tracked during each live migration iteration round cannot keep pace
  30. with the rate of dirty memory produced by the workload.
  31. RDMA currently comes in two flavors: both Ethernet based (RoCE, or RDMA
  32. over Converged Ethernet) as well as Infiniband-based. This implementation of
  33. migration using RDMA is capable of using both technologies because of
  34. the use of the OpenFabrics OFED software stack that abstracts out the
  35. programming model irrespective of the underlying hardware.
  36. Refer to openfabrics.org or your respective RDMA hardware vendor for
  37. an understanding on how to verify that you have the OFED software stack
  38. installed in your environment. You should be able to successfully link
  39. against the "librdmacm" and "libibverbs" libraries and development headers
  40. for a working build of QEMU to run successfully using RDMA Migration.
  41. BEFORE RUNNING:
  42. ===============
  43. Use of RDMA during migration requires pinning and registering memory
  44. with the hardware. This means that memory must be physically resident
  45. before the hardware can transmit that memory to another machine.
  46. If this is not acceptable for your application or product, then the use
  47. of RDMA migration may in fact be harmful to co-located VMs or other
  48. software on the machine if there is not sufficient memory available to
  49. relocate the entire footprint of the virtual machine. If so, then the
  50. use of RDMA is discouraged and it is recommended to use standard TCP migration.
  51. Experimental: Next, decide if you want dynamic page registration.
  52. For example, if you have an 8GB RAM virtual machine, but only 1GB
  53. is in active use, then enabling this feature will cause all 8GB to
  54. be pinned and resident in memory. This feature mostly affects the
  55. bulk-phase round of the migration and can be enabled for extremely
  56. high-performance RDMA hardware using the following command:
  57. QEMU Monitor Command:
  58. $ migrate_set_capability rdma-pin-all on # disabled by default
  59. Performing this action will cause all 8GB to be pinned, so if that's
  60. not what you want, then please ignore this step altogether.
  61. On the other hand, this will also significantly speed up the bulk round
  62. of the migration, which can greatly reduce the "total" time of your migration.
  63. Example performance of this using an idle VM in the previous example
  64. can be found in the "Performance" section.
  65. Note: for very large virtual machines (hundreds of GBs), pinning all
  66. *all* of the memory of your virtual machine in the kernel is very expensive
  67. may extend the initial bulk iteration time by many seconds,
  68. and thus extending the total migration time. However, this will not
  69. affect the determinism or predictability of your migration you will
  70. still gain from the benefits of advanced pinning with RDMA.
  71. RUNNING:
  72. ========
  73. First, set the migration speed to match your hardware's capabilities:
  74. QEMU Monitor Command:
  75. $ migrate_set_parameter max_bandwidth 40g # or whatever is the MAX of your RDMA device
  76. Next, on the destination machine, add the following to the QEMU command line:
  77. qemu ..... -incoming rdma:host:port
  78. Finally, perform the actual migration on the source machine:
  79. QEMU Monitor Command:
  80. $ migrate -d rdma:host:port
  81. PERFORMANCE
  82. ===========
  83. Here is a brief summary of total migration time and downtime using RDMA:
  84. Using a 40gbps infiniband link performing a worst-case stress test,
  85. using an 8GB RAM virtual machine:
  86. Using the following command:
  87. $ apt-get install stress
  88. $ stress --vm-bytes 7500M --vm 1 --vm-keep
  89. 1. Migration throughput: 26 gigabits/second.
  90. 2. Downtime (stop time) varies between 15 and 100 milliseconds.
  91. EFFECTS of memory registration on bulk phase round:
  92. For example, in the same 8GB RAM example with all 8GB of memory in
  93. active use and the VM itself is completely idle using the same 40 gbps
  94. infiniband link:
  95. 1. rdma-pin-all disabled total time: approximately 7.5 seconds @ 9.5 Gbps
  96. 2. rdma-pin-all enabled total time: approximately 4 seconds @ 26 Gbps
  97. These numbers would of course scale up to whatever size virtual machine
  98. you have to migrate using RDMA.
  99. Enabling this feature does *not* have any measurable affect on
  100. migration *downtime*. This is because, without this feature, all of the
  101. memory will have already been registered already in advance during
  102. the bulk round and does not need to be re-registered during the successive
  103. iteration rounds.
  104. RDMA Protocol Description:
  105. ==========================
  106. Migration with RDMA is separated into two parts:
  107. 1. The transmission of the pages using RDMA
  108. 2. Everything else (a control channel is introduced)
  109. "Everything else" is transmitted using a formal
  110. protocol now, consisting of infiniband SEND messages.
  111. An infiniband SEND message is the standard ibverbs
  112. message used by applications of infiniband hardware.
  113. The only difference between a SEND message and an RDMA
  114. message is that SEND messages cause notifications
  115. to be posted to the completion queue (CQ) on the
  116. infiniband receiver side, whereas RDMA messages (used
  117. for VM's ram) do not (to behave like an actual DMA).
  118. Messages in infiniband require two things:
  119. 1. registration of the memory that will be transmitted
  120. 2. (SEND only) work requests to be posted on both
  121. sides of the network before the actual transmission
  122. can occur.
  123. RDMA messages are much easier to deal with. Once the memory
  124. on the receiver side is registered and pinned, we're
  125. basically done. All that is required is for the sender
  126. side to start dumping bytes onto the link.
  127. (Memory is not released from pinning until the migration
  128. completes, given that RDMA migrations are very fast.)
  129. SEND messages require more coordination because the
  130. receiver must have reserved space (using a receive
  131. work request) on the receive queue (RQ) before QEMUFileRDMA
  132. can start using them to carry all the bytes as
  133. a control transport for migration of device state.
  134. To begin the migration, the initial connection setup is
  135. as follows (migration-rdma.c):
  136. 1. Receiver and Sender are started (command line or libvirt):
  137. 2. Both sides post two RQ work requests
  138. 3. Receiver does listen()
  139. 4. Sender does connect()
  140. 5. Receiver accept()
  141. 6. Check versioning and capabilities (described later)
  142. At this point, we define a control channel on top of SEND messages
  143. which is described by a formal protocol. Each SEND message has a
  144. header portion and a data portion (but together are transmitted
  145. as a single SEND message).
  146. Header:
  147. * Length (of the data portion, uint32, network byte order)
  148. * Type (what command to perform, uint32, network byte order)
  149. * Repeat (Number of commands in data portion, same type only)
  150. The 'Repeat' field is here to support future multiple page registrations
  151. in a single message without any need to change the protocol itself
  152. so that the protocol is compatible against multiple versions of QEMU.
  153. Version #1 requires that all server implementations of the protocol must
  154. check this field and register all requests found in the array of commands located
  155. in the data portion and return an equal number of results in the response.
  156. The maximum number of repeats is hard-coded to 4096. This is a conservative
  157. limit based on the maximum size of a SEND message along with empirical
  158. observations on the maximum future benefit of simultaneous page registrations.
  159. The 'type' field has 12 different command values:
  160. 1. Unused
  161. 2. Error (sent to the source during bad things)
  162. 3. Ready (control-channel is available)
  163. 4. QEMU File (for sending non-live device state)
  164. 5. RAM Blocks request (used right after connection setup)
  165. 6. RAM Blocks result (used right after connection setup)
  166. 7. Compress page (zap zero page and skip registration)
  167. 8. Register request (dynamic chunk registration)
  168. 9. Register result ('rkey' to be used by sender)
  169. 10. Register finished (registration for current iteration finished)
  170. 11. Unregister request (unpin previously registered memory)
  171. 12. Unregister finished (confirmation that unpin completed)
  172. A single control message, as hinted above, can contain within the data
  173. portion an array of many commands of the same type. If there is more than
  174. one command, then the 'repeat' field will be greater than 1.
  175. After connection setup, message 5 & 6 are used to exchange ram block
  176. information and optionally pin all the memory if requested by the user.
  177. After ram block exchange is completed, we have two protocol-level
  178. functions, responsible for communicating control-channel commands
  179. using the above list of values:
  180. Logically:
  181. qemu_rdma_exchange_recv(header, expected command type)
  182. 1. We transmit a READY command to let the sender know that
  183. we are *ready* to receive some data bytes on the control channel.
  184. 2. Before attempting to receive the expected command, we post another
  185. RQ work request to replace the one we just used up.
  186. 3. Block on a CQ event channel and wait for the SEND to arrive.
  187. 4. When the send arrives, librdmacm will unblock us.
  188. 5. Verify that the command-type and version received matches the one we expected.
  189. qemu_rdma_exchange_send(header, data, optional response header & data):
  190. 1. Block on the CQ event channel waiting for a READY command
  191. from the receiver to tell us that the receiver
  192. is *ready* for us to transmit some new bytes.
  193. 2. Optionally: if we are expecting a response from the command
  194. (that we have not yet transmitted), let's post an RQ
  195. work request to receive that data a few moments later.
  196. 3. When the READY arrives, librdmacm will
  197. unblock us and we immediately post a RQ work request
  198. to replace the one we just used up.
  199. 4. Now, we can actually post the work request to SEND
  200. the requested command type of the header we were asked for.
  201. 5. Optionally, if we are expecting a response (as before),
  202. we block again and wait for that response using the additional
  203. work request we previously posted. (This is used to carry
  204. 'Register result' commands #6 back to the sender which
  205. hold the rkey need to perform RDMA. Note that the virtual address
  206. corresponding to this rkey was already exchanged at the beginning
  207. of the connection (described below).
  208. All of the remaining command types (not including 'ready')
  209. described above all use the aforementioned two functions to do the hard work:
  210. 1. After connection setup, RAMBlock information is exchanged using
  211. this protocol before the actual migration begins. This information includes
  212. a description of each RAMBlock on the server side as well as the virtual addresses
  213. and lengths of each RAMBlock. This is used by the client to determine the
  214. start and stop locations of chunks and how to register them dynamically
  215. before performing the RDMA operations.
  216. 2. During runtime, once a 'chunk' becomes full of pages ready to
  217. be sent with RDMA, the registration commands are used to ask the
  218. other side to register the memory for this chunk and respond
  219. with the result (rkey) of the registration.
  220. 3. Also, the QEMUFile interfaces also call these functions (described below)
  221. when transmitting non-live state, such as devices or to send
  222. its own protocol information during the migration process.
  223. 4. Finally, zero pages are only checked if a page has not yet been registered
  224. using chunk registration (or not checked at all and unconditionally
  225. written if chunk registration is disabled. This is accomplished using
  226. the "Compress" command listed above. If the page *has* been registered
  227. then we check the entire chunk for zero. Only if the entire chunk is
  228. zero, then we send a compress command to zap the page on the other side.
  229. Versioning and Capabilities
  230. ===========================
  231. Current version of the protocol is version #1.
  232. The same version applies to both for protocol traffic and capabilities
  233. negotiation. (i.e. There is only one version number that is referred to
  234. by all communication).
  235. librdmacm provides the user with a 'private data' area to be exchanged
  236. at connection-setup time before any infiniband traffic is generated.
  237. Header:
  238. * Version (protocol version validated before send/recv occurs),
  239. uint32, network byte order
  240. * Flags (bitwise OR of each capability),
  241. uint32, network byte order
  242. There is no data portion of this header right now, so there is
  243. no length field. The maximum size of the 'private data' section
  244. is only 192 bytes per the Infiniband specification, so it's not
  245. very useful for data anyway. This structure needs to remain small.
  246. This private data area is a convenient place to check for protocol
  247. versioning because the user does not need to register memory to
  248. transmit a few bytes of version information.
  249. This is also a convenient place to negotiate capabilities
  250. (like dynamic page registration).
  251. If the version is invalid, we throw an error.
  252. If the version is new, we only negotiate the capabilities that the
  253. requested version is able to perform and ignore the rest.
  254. Currently there is only one capability in Version #1: dynamic page registration
  255. Finally: Negotiation happens with the Flags field: If the primary-VM
  256. sets a flag, but the destination does not support this capability, it
  257. will return a zero-bit for that flag and the primary-VM will understand
  258. that as not being an available capability and will thus disable that
  259. capability on the primary-VM side.
  260. QEMUFileRDMA Interface:
  261. =======================
  262. QEMUFileRDMA introduces a couple of new functions:
  263. 1. qemu_rdma_get_buffer() (QEMUFileOps rdma_read_ops)
  264. 2. qemu_rdma_put_buffer() (QEMUFileOps rdma_write_ops)
  265. These two functions are very short and simply use the protocol
  266. describe above to deliver bytes without changing the upper-level
  267. users of QEMUFile that depend on a bytestream abstraction.
  268. Finally, how do we handoff the actual bytes to get_buffer()?
  269. Again, because we're trying to "fake" a bytestream abstraction
  270. using an analogy not unlike individual UDP frames, we have
  271. to hold on to the bytes received from control-channel's SEND
  272. messages in memory.
  273. Each time we receive a complete "QEMU File" control-channel
  274. message, the bytes from SEND are copied into a small local holding area.
  275. Then, we return the number of bytes requested by get_buffer()
  276. and leave the remaining bytes in the holding area until get_buffer()
  277. comes around for another pass.
  278. If the buffer is empty, then we follow the same steps
  279. listed above and issue another "QEMU File" protocol command,
  280. asking for a new SEND message to re-fill the buffer.
  281. Migration of VM's ram:
  282. ====================
  283. At the beginning of the migration, (migration-rdma.c),
  284. the sender and the receiver populate the list of RAMBlocks
  285. to be registered with each other into a structure.
  286. Then, using the aforementioned protocol, they exchange a
  287. description of these blocks with each other, to be used later
  288. during the iteration of main memory. This description includes
  289. a list of all the RAMBlocks, their offsets and lengths, virtual
  290. addresses and possibly includes pre-registered RDMA keys in case dynamic
  291. page registration was disabled on the server-side, otherwise not.
  292. Main memory is not migrated with the aforementioned protocol,
  293. but is instead migrated with normal RDMA Write operations.
  294. Pages are migrated in "chunks" (hard-coded to 1 Megabyte right now).
  295. Chunk size is not dynamic, but it could be in a future implementation.
  296. There's nothing to indicate that this is useful right now.
  297. When a chunk is full (or a flush() occurs), the memory backed by
  298. the chunk is registered with librdmacm is pinned in memory on
  299. both sides using the aforementioned protocol.
  300. After pinning, an RDMA Write is generated and transmitted
  301. for the entire chunk.
  302. Chunks are also transmitted in batches: This means that we
  303. do not request that the hardware signal the completion queue
  304. for the completion of *every* chunk. The current batch size
  305. is about 64 chunks (corresponding to 64 MB of memory).
  306. Only the last chunk in a batch must be signaled.
  307. This helps keep everything as asynchronous as possible
  308. and helps keep the hardware busy performing RDMA operations.
  309. Error-handling:
  310. ===============
  311. Infiniband has what is called a "Reliable, Connected"
  312. link (one of 4 choices). This is the mode in which
  313. we use for RDMA migration.
  314. If a *single* message fails,
  315. the decision is to abort the migration entirely and
  316. cleanup all the RDMA descriptors and unregister all
  317. the memory.
  318. After cleanup, the Virtual Machine is returned to normal
  319. operation the same way that would happen if the TCP
  320. socket is broken during a non-RDMA based migration.
  321. TODO:
  322. =====
  323. 1. Currently, 'ulimit -l' mlock() limits as well as cgroups swap limits
  324. are not compatible with infiniband memory pinning and will result in
  325. an aborted migration (but with the source VM left unaffected).
  326. 2. Use of the recent /proc/<pid>/pagemap would likely speed up
  327. the use of KSM and ballooning while using RDMA.
  328. 3. Also, some form of balloon-device usage tracking would also
  329. help alleviate some issues.
  330. 4. Use LRU to provide more fine-grained direction of UNREGISTER
  331. requests for unpinning memory in an overcommitted environment.
  332. 5. Expose UNREGISTER support to the user by way of workload-specific
  333. hints about application behavior.