2 * RDMA protocol and interfaces
4 * Copyright IBM, Corp. 2010-2013
7 * Michael R. Hines <mrhines@us.ibm.com>
8 * Jiuxing Liu <jl@us.ibm.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or
11 * later. See the COPYING file in the top-level directory.
14 #include "qemu-common.h"
15 #include "migration/migration.h"
16 #include "migration/qemu-file.h"
17 #include "exec/cpu-common.h"
18 #include "qemu/error-report.h"
19 #include "qemu/main-loop.h"
20 #include "qemu/sockets.h"
21 #include "qemu/bitmap.h"
22 #include "block/coroutine.h"
24 #include <sys/types.h>
25 #include <sys/socket.h>
27 #include <arpa/inet.h>
29 #include <rdma/rdma_cma.h>
33 * Print and error on both the Monitor and the Log file.
35 #define ERROR(errp, fmt, ...) \
37 fprintf(stderr, "RDMA ERROR: " fmt "\n", ## __VA_ARGS__); \
38 if (errp && (*(errp) == NULL)) { \
39 error_setg(errp, "RDMA ERROR: " fmt, ## __VA_ARGS__); \
43 #define RDMA_RESOLVE_TIMEOUT_MS 10000
45 /* Do not merge data if larger than this. */
46 #define RDMA_MERGE_MAX (2 * 1024 * 1024)
47 #define RDMA_SIGNALED_SEND_MAX (RDMA_MERGE_MAX / 4096)
49 #define RDMA_REG_CHUNK_SHIFT 20 /* 1 MB */
52 * This is only for non-live state being migrated.
53 * Instead of RDMA_WRITE messages, we use RDMA_SEND
54 * messages for that state, which requires a different
55 * delivery design than main memory.
57 #define RDMA_SEND_INCREMENT 32768
60 * Maximum size infiniband SEND message
62 #define RDMA_CONTROL_MAX_BUFFER (512 * 1024)
63 #define RDMA_CONTROL_MAX_COMMANDS_PER_MESSAGE 4096
65 #define RDMA_CONTROL_VERSION_CURRENT 1
67 * Capabilities for negotiation.
69 #define RDMA_CAPABILITY_PIN_ALL 0x01
72 * Add the other flags above to this list of known capabilities
73 * as they are introduced.
75 static uint32_t known_capabilities
= RDMA_CAPABILITY_PIN_ALL
;
77 #define CHECK_ERROR_STATE() \
79 if (rdma->error_state) { \
80 if (!rdma->error_reported) { \
81 error_report("RDMA is in an error state waiting migration" \
83 rdma->error_reported = 1; \
85 return rdma->error_state; \
90 * A work request ID is 64-bits and we split up these bits
93 * bits 0-15 : type of control message, 2^16
94 * bits 16-29: ram block index, 2^14
95 * bits 30-63: ram block chunk number, 2^34
97 * The last two bit ranges are only used for RDMA writes,
98 * in order to track their completion and potentially
99 * also track unregistration status of the message.
101 #define RDMA_WRID_TYPE_SHIFT 0UL
102 #define RDMA_WRID_BLOCK_SHIFT 16UL
103 #define RDMA_WRID_CHUNK_SHIFT 30UL
105 #define RDMA_WRID_TYPE_MASK \
106 ((1UL << RDMA_WRID_BLOCK_SHIFT) - 1UL)
108 #define RDMA_WRID_BLOCK_MASK \
109 (~RDMA_WRID_TYPE_MASK & ((1UL << RDMA_WRID_CHUNK_SHIFT) - 1UL))
111 #define RDMA_WRID_CHUNK_MASK (~RDMA_WRID_BLOCK_MASK & ~RDMA_WRID_TYPE_MASK)
114 * RDMA migration protocol:
115 * 1. RDMA Writes (data messages, i.e. RAM)
116 * 2. IB Send/Recv (control channel messages)
120 RDMA_WRID_RDMA_WRITE
= 1,
121 RDMA_WRID_SEND_CONTROL
= 2000,
122 RDMA_WRID_RECV_CONTROL
= 4000,
125 static const char *wrid_desc
[] = {
126 [RDMA_WRID_NONE
] = "NONE",
127 [RDMA_WRID_RDMA_WRITE
] = "WRITE RDMA",
128 [RDMA_WRID_SEND_CONTROL
] = "CONTROL SEND",
129 [RDMA_WRID_RECV_CONTROL
] = "CONTROL RECV",
133 * Work request IDs for IB SEND messages only (not RDMA writes).
134 * This is used by the migration protocol to transmit
135 * control messages (such as device state and registration commands)
137 * We could use more WRs, but we have enough for now.
147 * SEND/RECV IB Control Messages.
150 RDMA_CONTROL_NONE
= 0,
152 RDMA_CONTROL_READY
, /* ready to receive */
153 RDMA_CONTROL_QEMU_FILE
, /* QEMUFile-transmitted bytes */
154 RDMA_CONTROL_RAM_BLOCKS_REQUEST
, /* RAMBlock synchronization */
155 RDMA_CONTROL_RAM_BLOCKS_RESULT
, /* RAMBlock synchronization */
156 RDMA_CONTROL_COMPRESS
, /* page contains repeat values */
157 RDMA_CONTROL_REGISTER_REQUEST
, /* dynamic page registration */
158 RDMA_CONTROL_REGISTER_RESULT
, /* key to use after registration */
159 RDMA_CONTROL_REGISTER_FINISHED
, /* current iteration finished */
160 RDMA_CONTROL_UNREGISTER_REQUEST
, /* dynamic UN-registration */
161 RDMA_CONTROL_UNREGISTER_FINISHED
, /* unpinning finished */
164 static const char *control_desc
[] = {
165 [RDMA_CONTROL_NONE
] = "NONE",
166 [RDMA_CONTROL_ERROR
] = "ERROR",
167 [RDMA_CONTROL_READY
] = "READY",
168 [RDMA_CONTROL_QEMU_FILE
] = "QEMU FILE",
169 [RDMA_CONTROL_RAM_BLOCKS_REQUEST
] = "RAM BLOCKS REQUEST",
170 [RDMA_CONTROL_RAM_BLOCKS_RESULT
] = "RAM BLOCKS RESULT",
171 [RDMA_CONTROL_COMPRESS
] = "COMPRESS",
172 [RDMA_CONTROL_REGISTER_REQUEST
] = "REGISTER REQUEST",
173 [RDMA_CONTROL_REGISTER_RESULT
] = "REGISTER RESULT",
174 [RDMA_CONTROL_REGISTER_FINISHED
] = "REGISTER FINISHED",
175 [RDMA_CONTROL_UNREGISTER_REQUEST
] = "UNREGISTER REQUEST",
176 [RDMA_CONTROL_UNREGISTER_FINISHED
] = "UNREGISTER FINISHED",
180 * Memory and MR structures used to represent an IB Send/Recv work request.
181 * This is *not* used for RDMA writes, only IB Send/Recv.
184 uint8_t control
[RDMA_CONTROL_MAX_BUFFER
]; /* actual buffer to register */
185 struct ibv_mr
*control_mr
; /* registration metadata */
186 size_t control_len
; /* length of the message */
187 uint8_t *control_curr
; /* start of unconsumed bytes */
188 } RDMAWorkRequestData
;
191 * Negotiate RDMA capabilities during connection-setup time.
198 static void caps_to_network(RDMACapabilities
*cap
)
200 cap
->version
= htonl(cap
->version
);
201 cap
->flags
= htonl(cap
->flags
);
204 static void network_to_caps(RDMACapabilities
*cap
)
206 cap
->version
= ntohl(cap
->version
);
207 cap
->flags
= ntohl(cap
->flags
);
211 * Representation of a RAMBlock from an RDMA perspective.
212 * This is not transmitted, only local.
213 * This and subsequent structures cannot be linked lists
214 * because we're using a single IB message to transmit
215 * the information. It's small anyway, so a list is overkill.
217 typedef struct RDMALocalBlock
{
219 uint8_t *local_host_addr
; /* local virtual address */
220 uint64_t remote_host_addr
; /* remote virtual address */
223 struct ibv_mr
**pmr
; /* MRs for chunk-level registration */
224 struct ibv_mr
*mr
; /* MR for non-chunk-level registration */
225 uint32_t *remote_keys
; /* rkeys for chunk-level registration */
226 uint32_t remote_rkey
; /* rkeys for non-chunk-level registration */
227 int index
; /* which block are we */
230 unsigned long *transit_bitmap
;
231 unsigned long *unregister_bitmap
;
235 * Also represents a RAMblock, but only on the dest.
236 * This gets transmitted by the dest during connection-time
237 * to the source VM and then is used to populate the
238 * corresponding RDMALocalBlock with
239 * the information needed to perform the actual RDMA.
241 typedef struct QEMU_PACKED RDMADestBlock
{
242 uint64_t remote_host_addr
;
245 uint32_t remote_rkey
;
249 static uint64_t htonll(uint64_t v
)
251 union { uint32_t lv
[2]; uint64_t llv
; } u
;
252 u
.lv
[0] = htonl(v
>> 32);
253 u
.lv
[1] = htonl(v
& 0xFFFFFFFFULL
);
257 static uint64_t ntohll(uint64_t v
) {
258 union { uint32_t lv
[2]; uint64_t llv
; } u
;
260 return ((uint64_t)ntohl(u
.lv
[0]) << 32) | (uint64_t) ntohl(u
.lv
[1]);
263 static void dest_block_to_network(RDMADestBlock
*db
)
265 db
->remote_host_addr
= htonll(db
->remote_host_addr
);
266 db
->offset
= htonll(db
->offset
);
267 db
->length
= htonll(db
->length
);
268 db
->remote_rkey
= htonl(db
->remote_rkey
);
271 static void network_to_dest_block(RDMADestBlock
*db
)
273 db
->remote_host_addr
= ntohll(db
->remote_host_addr
);
274 db
->offset
= ntohll(db
->offset
);
275 db
->length
= ntohll(db
->length
);
276 db
->remote_rkey
= ntohl(db
->remote_rkey
);
280 * Virtual address of the above structures used for transmitting
281 * the RAMBlock descriptions at connection-time.
282 * This structure is *not* transmitted.
284 typedef struct RDMALocalBlocks
{
286 bool init
; /* main memory init complete */
287 RDMALocalBlock
*block
;
291 * Main data structure for RDMA state.
292 * While there is only one copy of this structure being allocated right now,
293 * this is the place where one would start if you wanted to consider
294 * having more than one RDMA connection open at the same time.
296 typedef struct RDMAContext
{
300 RDMAWorkRequestData wr_data
[RDMA_WRID_MAX
];
303 * This is used by *_exchange_send() to figure out whether or not
304 * the initial "READY" message has already been received or not.
305 * This is because other functions may potentially poll() and detect
306 * the READY message before send() does, in which case we need to
307 * know if it completed.
309 int control_ready_expected
;
311 /* number of outstanding writes */
314 /* store info about current buffer so that we can
315 merge it with future sends */
316 uint64_t current_addr
;
317 uint64_t current_length
;
318 /* index of ram block the current buffer belongs to */
320 /* index of the chunk in the current ram block */
326 * infiniband-specific variables for opening the device
327 * and maintaining connection state and so forth.
329 * cm_id also has ibv_context, rdma_event_channel, and ibv_qp in
330 * cm_id->verbs, cm_id->channel, and cm_id->qp.
332 struct rdma_cm_id
*cm_id
; /* connection manager ID */
333 struct rdma_cm_id
*listen_id
;
336 struct ibv_context
*verbs
;
337 struct rdma_event_channel
*channel
;
338 struct ibv_qp
*qp
; /* queue pair */
339 struct ibv_comp_channel
*comp_channel
; /* completion channel */
340 struct ibv_pd
*pd
; /* protection domain */
341 struct ibv_cq
*cq
; /* completion queue */
344 * If a previous write failed (perhaps because of a failed
345 * memory registration, then do not attempt any future work
346 * and remember the error state.
352 * Description of ram blocks used throughout the code.
354 RDMALocalBlocks local_ram_blocks
;
355 RDMADestBlock
*dest_blocks
;
358 * Migration on *destination* started.
359 * Then use coroutine yield function.
360 * Source runs in a thread, so we don't care.
362 int migration_started_on_destination
;
364 int total_registrations
;
367 int unregister_current
, unregister_next
;
368 uint64_t unregistrations
[RDMA_SIGNALED_SEND_MAX
];
370 GHashTable
*blockmap
;
374 * Interface to the rest of the migration call stack.
376 typedef struct QEMUFileRDMA
{
383 * Main structure for IB Send/Recv control messages.
384 * This gets prepended at the beginning of every Send/Recv.
386 typedef struct QEMU_PACKED
{
387 uint32_t len
; /* Total length of data portion */
388 uint32_t type
; /* which control command to perform */
389 uint32_t repeat
; /* number of commands in data portion of same type */
393 static void control_to_network(RDMAControlHeader
*control
)
395 control
->type
= htonl(control
->type
);
396 control
->len
= htonl(control
->len
);
397 control
->repeat
= htonl(control
->repeat
);
400 static void network_to_control(RDMAControlHeader
*control
)
402 control
->type
= ntohl(control
->type
);
403 control
->len
= ntohl(control
->len
);
404 control
->repeat
= ntohl(control
->repeat
);
408 * Register a single Chunk.
409 * Information sent by the source VM to inform the dest
410 * to register an single chunk of memory before we can perform
411 * the actual RDMA operation.
413 typedef struct QEMU_PACKED
{
415 uint64_t current_addr
; /* offset into the ram_addr_t space */
416 uint64_t chunk
; /* chunk to lookup if unregistering */
418 uint32_t current_index
; /* which ramblock the chunk belongs to */
420 uint64_t chunks
; /* how many sequential chunks to register */
423 static void register_to_network(RDMAContext
*rdma
, RDMARegister
*reg
)
425 RDMALocalBlock
*local_block
;
426 local_block
= &rdma
->local_ram_blocks
.block
[reg
->current_index
];
428 if (local_block
->is_ram_block
) {
430 * current_addr as passed in is an address in the local ram_addr_t
431 * space, we need to translate this for the destination
433 reg
->key
.current_addr
-= local_block
->offset
;
434 reg
->key
.current_addr
+= rdma
->dest_blocks
[reg
->current_index
].offset
;
436 reg
->key
.current_addr
= htonll(reg
->key
.current_addr
);
437 reg
->current_index
= htonl(reg
->current_index
);
438 reg
->chunks
= htonll(reg
->chunks
);
441 static void network_to_register(RDMARegister
*reg
)
443 reg
->key
.current_addr
= ntohll(reg
->key
.current_addr
);
444 reg
->current_index
= ntohl(reg
->current_index
);
445 reg
->chunks
= ntohll(reg
->chunks
);
448 typedef struct QEMU_PACKED
{
449 uint32_t value
; /* if zero, we will madvise() */
450 uint32_t block_idx
; /* which ram block index */
451 uint64_t offset
; /* Address in remote ram_addr_t space */
452 uint64_t length
; /* length of the chunk */
455 static void compress_to_network(RDMAContext
*rdma
, RDMACompress
*comp
)
457 comp
->value
= htonl(comp
->value
);
459 * comp->offset as passed in is an address in the local ram_addr_t
460 * space, we need to translate this for the destination
462 comp
->offset
-= rdma
->local_ram_blocks
.block
[comp
->block_idx
].offset
;
463 comp
->offset
+= rdma
->dest_blocks
[comp
->block_idx
].offset
;
464 comp
->block_idx
= htonl(comp
->block_idx
);
465 comp
->offset
= htonll(comp
->offset
);
466 comp
->length
= htonll(comp
->length
);
469 static void network_to_compress(RDMACompress
*comp
)
471 comp
->value
= ntohl(comp
->value
);
472 comp
->block_idx
= ntohl(comp
->block_idx
);
473 comp
->offset
= ntohll(comp
->offset
);
474 comp
->length
= ntohll(comp
->length
);
478 * The result of the dest's memory registration produces an "rkey"
479 * which the source VM must reference in order to perform
480 * the RDMA operation.
482 typedef struct QEMU_PACKED
{
486 } RDMARegisterResult
;
488 static void result_to_network(RDMARegisterResult
*result
)
490 result
->rkey
= htonl(result
->rkey
);
491 result
->host_addr
= htonll(result
->host_addr
);
494 static void network_to_result(RDMARegisterResult
*result
)
496 result
->rkey
= ntohl(result
->rkey
);
497 result
->host_addr
= ntohll(result
->host_addr
);
500 const char *print_wrid(int wrid
);
501 static int qemu_rdma_exchange_send(RDMAContext
*rdma
, RDMAControlHeader
*head
,
502 uint8_t *data
, RDMAControlHeader
*resp
,
504 int (*callback
)(RDMAContext
*rdma
));
506 static inline uint64_t ram_chunk_index(const uint8_t *start
,
509 return ((uintptr_t) host
- (uintptr_t) start
) >> RDMA_REG_CHUNK_SHIFT
;
512 static inline uint8_t *ram_chunk_start(const RDMALocalBlock
*rdma_ram_block
,
515 return (uint8_t *)(uintptr_t)(rdma_ram_block
->local_host_addr
+
516 (i
<< RDMA_REG_CHUNK_SHIFT
));
519 static inline uint8_t *ram_chunk_end(const RDMALocalBlock
*rdma_ram_block
,
522 uint8_t *result
= ram_chunk_start(rdma_ram_block
, i
) +
523 (1UL << RDMA_REG_CHUNK_SHIFT
);
525 if (result
> (rdma_ram_block
->local_host_addr
+ rdma_ram_block
->length
)) {
526 result
= rdma_ram_block
->local_host_addr
+ rdma_ram_block
->length
;
532 static int rdma_add_block(RDMAContext
*rdma
, const char *block_name
,
534 ram_addr_t block_offset
, uint64_t length
)
536 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
537 RDMALocalBlock
*block
= g_hash_table_lookup(rdma
->blockmap
,
538 (void *)(uintptr_t)block_offset
);
539 RDMALocalBlock
*old
= local
->block
;
541 assert(block
== NULL
);
543 local
->block
= g_malloc0(sizeof(RDMALocalBlock
) * (local
->nb_blocks
+ 1));
545 if (local
->nb_blocks
) {
548 for (x
= 0; x
< local
->nb_blocks
; x
++) {
549 g_hash_table_remove(rdma
->blockmap
,
550 (void *)(uintptr_t)old
[x
].offset
);
551 g_hash_table_insert(rdma
->blockmap
,
552 (void *)(uintptr_t)old
[x
].offset
,
555 memcpy(local
->block
, old
, sizeof(RDMALocalBlock
) * local
->nb_blocks
);
559 block
= &local
->block
[local
->nb_blocks
];
561 block
->block_name
= g_strdup(block_name
);
562 block
->local_host_addr
= host_addr
;
563 block
->offset
= block_offset
;
564 block
->length
= length
;
565 block
->index
= local
->nb_blocks
;
566 block
->nb_chunks
= ram_chunk_index(host_addr
, host_addr
+ length
) + 1UL;
567 block
->transit_bitmap
= bitmap_new(block
->nb_chunks
);
568 bitmap_clear(block
->transit_bitmap
, 0, block
->nb_chunks
);
569 block
->unregister_bitmap
= bitmap_new(block
->nb_chunks
);
570 bitmap_clear(block
->unregister_bitmap
, 0, block
->nb_chunks
);
571 block
->remote_keys
= g_malloc0(block
->nb_chunks
* sizeof(uint32_t));
573 block
->is_ram_block
= local
->init
? false : true;
575 g_hash_table_insert(rdma
->blockmap
, (void *) block_offset
, block
);
577 trace_rdma_add_block(block_name
, local
->nb_blocks
,
578 (uintptr_t) block
->local_host_addr
,
579 block
->offset
, block
->length
,
580 (uintptr_t) (block
->local_host_addr
+ block
->length
),
581 BITS_TO_LONGS(block
->nb_chunks
) *
582 sizeof(unsigned long) * 8,
591 * Memory regions need to be registered with the device and queue pairs setup
592 * in advanced before the migration starts. This tells us where the RAM blocks
593 * are so that we can register them individually.
595 static int qemu_rdma_init_one_block(const char *block_name
, void *host_addr
,
596 ram_addr_t block_offset
, ram_addr_t length
, void *opaque
)
598 return rdma_add_block(opaque
, block_name
, host_addr
, block_offset
, length
);
602 * Identify the RAMBlocks and their quantity. They will be references to
603 * identify chunk boundaries inside each RAMBlock and also be referenced
604 * during dynamic page registration.
606 static int qemu_rdma_init_ram_blocks(RDMAContext
*rdma
)
608 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
610 assert(rdma
->blockmap
== NULL
);
611 rdma
->blockmap
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
612 memset(local
, 0, sizeof *local
);
613 qemu_ram_foreach_block(qemu_rdma_init_one_block
, rdma
);
614 trace_qemu_rdma_init_ram_blocks(local
->nb_blocks
);
615 rdma
->dest_blocks
= (RDMADestBlock
*) g_malloc0(sizeof(RDMADestBlock
) *
616 rdma
->local_ram_blocks
.nb_blocks
);
621 static int rdma_delete_block(RDMAContext
*rdma
, ram_addr_t block_offset
)
623 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
624 RDMALocalBlock
*block
= g_hash_table_lookup(rdma
->blockmap
,
625 (void *) block_offset
);
626 RDMALocalBlock
*old
= local
->block
;
634 for (j
= 0; j
< block
->nb_chunks
; j
++) {
635 if (!block
->pmr
[j
]) {
638 ibv_dereg_mr(block
->pmr
[j
]);
639 rdma
->total_registrations
--;
646 ibv_dereg_mr(block
->mr
);
647 rdma
->total_registrations
--;
651 g_free(block
->transit_bitmap
);
652 block
->transit_bitmap
= NULL
;
654 g_free(block
->unregister_bitmap
);
655 block
->unregister_bitmap
= NULL
;
657 g_free(block
->remote_keys
);
658 block
->remote_keys
= NULL
;
660 g_free(block
->block_name
);
661 block
->block_name
= NULL
;
663 for (x
= 0; x
< local
->nb_blocks
; x
++) {
664 g_hash_table_remove(rdma
->blockmap
, (void *)(uintptr_t)old
[x
].offset
);
667 if (local
->nb_blocks
> 1) {
669 local
->block
= g_malloc0(sizeof(RDMALocalBlock
) *
670 (local
->nb_blocks
- 1));
673 memcpy(local
->block
, old
, sizeof(RDMALocalBlock
) * block
->index
);
676 if (block
->index
< (local
->nb_blocks
- 1)) {
677 memcpy(local
->block
+ block
->index
, old
+ (block
->index
+ 1),
678 sizeof(RDMALocalBlock
) *
679 (local
->nb_blocks
- (block
->index
+ 1)));
682 assert(block
== local
->block
);
686 trace_rdma_delete_block(local
->nb_blocks
,
687 (uintptr_t)block
->local_host_addr
,
688 block
->offset
, block
->length
,
689 (uintptr_t)(block
->local_host_addr
+ block
->length
),
690 BITS_TO_LONGS(block
->nb_chunks
) *
691 sizeof(unsigned long) * 8, block
->nb_chunks
);
697 if (local
->nb_blocks
) {
698 for (x
= 0; x
< local
->nb_blocks
; x
++) {
699 g_hash_table_insert(rdma
->blockmap
,
700 (void *)(uintptr_t)local
->block
[x
].offset
,
709 * Put in the log file which RDMA device was opened and the details
710 * associated with that device.
712 static void qemu_rdma_dump_id(const char *who
, struct ibv_context
*verbs
)
714 struct ibv_port_attr port
;
716 if (ibv_query_port(verbs
, 1, &port
)) {
717 error_report("Failed to query port information");
721 printf("%s RDMA Device opened: kernel name %s "
722 "uverbs device name %s, "
723 "infiniband_verbs class device path %s, "
724 "infiniband class device path %s, "
725 "transport: (%d) %s\n",
728 verbs
->device
->dev_name
,
729 verbs
->device
->dev_path
,
730 verbs
->device
->ibdev_path
,
732 (port
.link_layer
== IBV_LINK_LAYER_INFINIBAND
) ? "Infiniband" :
733 ((port
.link_layer
== IBV_LINK_LAYER_ETHERNET
)
734 ? "Ethernet" : "Unknown"));
738 * Put in the log file the RDMA gid addressing information,
739 * useful for folks who have trouble understanding the
740 * RDMA device hierarchy in the kernel.
742 static void qemu_rdma_dump_gid(const char *who
, struct rdma_cm_id
*id
)
746 inet_ntop(AF_INET6
, &id
->route
.addr
.addr
.ibaddr
.sgid
, sgid
, sizeof sgid
);
747 inet_ntop(AF_INET6
, &id
->route
.addr
.addr
.ibaddr
.dgid
, dgid
, sizeof dgid
);
748 trace_qemu_rdma_dump_gid(who
, sgid
, dgid
);
752 * As of now, IPv6 over RoCE / iWARP is not supported by linux.
753 * We will try the next addrinfo struct, and fail if there are
754 * no other valid addresses to bind against.
756 * If user is listening on '[::]', then we will not have a opened a device
757 * yet and have no way of verifying if the device is RoCE or not.
759 * In this case, the source VM will throw an error for ALL types of
760 * connections (both IPv4 and IPv6) if the destination machine does not have
761 * a regular infiniband network available for use.
763 * The only way to guarantee that an error is thrown for broken kernels is
764 * for the management software to choose a *specific* interface at bind time
765 * and validate what time of hardware it is.
767 * Unfortunately, this puts the user in a fix:
769 * If the source VM connects with an IPv4 address without knowing that the
770 * destination has bound to '[::]' the migration will unconditionally fail
771 * unless the management software is explicitly listening on the the IPv4
772 * address while using a RoCE-based device.
774 * If the source VM connects with an IPv6 address, then we're OK because we can
775 * throw an error on the source (and similarly on the destination).
777 * But in mixed environments, this will be broken for a while until it is fixed
780 * We do provide a *tiny* bit of help in this function: We can list all of the
781 * devices in the system and check to see if all the devices are RoCE or
784 * If we detect that we have a *pure* RoCE environment, then we can safely
785 * thrown an error even if the management software has specified '[::]' as the
788 * However, if there is are multiple hetergeneous devices, then we cannot make
789 * this assumption and the user just has to be sure they know what they are
792 * Patches are being reviewed on linux-rdma.
794 static int qemu_rdma_broken_ipv6_kernel(Error
**errp
, struct ibv_context
*verbs
)
796 struct ibv_port_attr port_attr
;
798 /* This bug only exists in linux, to our knowledge. */
802 * Verbs are only NULL if management has bound to '[::]'.
804 * Let's iterate through all the devices and see if there any pure IB
805 * devices (non-ethernet).
807 * If not, then we can safely proceed with the migration.
808 * Otherwise, there are no guarantees until the bug is fixed in linux.
812 struct ibv_device
** dev_list
= ibv_get_device_list(&num_devices
);
813 bool roce_found
= false;
814 bool ib_found
= false;
816 for (x
= 0; x
< num_devices
; x
++) {
817 verbs
= ibv_open_device(dev_list
[x
]);
819 if (errno
== EPERM
) {
826 if (ibv_query_port(verbs
, 1, &port_attr
)) {
827 ibv_close_device(verbs
);
828 ERROR(errp
, "Could not query initial IB port");
832 if (port_attr
.link_layer
== IBV_LINK_LAYER_INFINIBAND
) {
834 } else if (port_attr
.link_layer
== IBV_LINK_LAYER_ETHERNET
) {
838 ibv_close_device(verbs
);
844 fprintf(stderr
, "WARN: migrations may fail:"
845 " IPv6 over RoCE / iWARP in linux"
846 " is broken. But since you appear to have a"
847 " mixed RoCE / IB environment, be sure to only"
848 " migrate over the IB fabric until the kernel "
849 " fixes the bug.\n");
851 ERROR(errp
, "You only have RoCE / iWARP devices in your systems"
852 " and your management software has specified '[::]'"
853 ", but IPv6 over RoCE / iWARP is not supported in Linux.");
862 * If we have a verbs context, that means that some other than '[::]' was
863 * used by the management software for binding. In which case we can
864 * actually warn the user about a potentially broken kernel.
867 /* IB ports start with 1, not 0 */
868 if (ibv_query_port(verbs
, 1, &port_attr
)) {
869 ERROR(errp
, "Could not query initial IB port");
873 if (port_attr
.link_layer
== IBV_LINK_LAYER_ETHERNET
) {
874 ERROR(errp
, "Linux kernel's RoCE / iWARP does not support IPv6 "
875 "(but patches on linux-rdma in progress)");
885 * Figure out which RDMA device corresponds to the requested IP hostname
886 * Also create the initial connection manager identifiers for opening
889 static int qemu_rdma_resolve_host(RDMAContext
*rdma
, Error
**errp
)
892 struct rdma_addrinfo
*res
;
894 struct rdma_cm_event
*cm_event
;
895 char ip
[40] = "unknown";
896 struct rdma_addrinfo
*e
;
898 if (rdma
->host
== NULL
|| !strcmp(rdma
->host
, "")) {
899 ERROR(errp
, "RDMA hostname has not been set");
903 /* create CM channel */
904 rdma
->channel
= rdma_create_event_channel();
905 if (!rdma
->channel
) {
906 ERROR(errp
, "could not create CM channel");
911 ret
= rdma_create_id(rdma
->channel
, &rdma
->cm_id
, NULL
, RDMA_PS_TCP
);
913 ERROR(errp
, "could not create channel id");
914 goto err_resolve_create_id
;
917 snprintf(port_str
, 16, "%d", rdma
->port
);
920 ret
= rdma_getaddrinfo(rdma
->host
, port_str
, NULL
, &res
);
922 ERROR(errp
, "could not rdma_getaddrinfo address %s", rdma
->host
);
923 goto err_resolve_get_addr
;
926 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
927 inet_ntop(e
->ai_family
,
928 &((struct sockaddr_in
*) e
->ai_dst_addr
)->sin_addr
, ip
, sizeof ip
);
929 trace_qemu_rdma_resolve_host_trying(rdma
->host
, ip
);
931 ret
= rdma_resolve_addr(rdma
->cm_id
, NULL
, e
->ai_dst_addr
,
932 RDMA_RESOLVE_TIMEOUT_MS
);
934 if (e
->ai_family
== AF_INET6
) {
935 ret
= qemu_rdma_broken_ipv6_kernel(errp
, rdma
->cm_id
->verbs
);
944 ERROR(errp
, "could not resolve address %s", rdma
->host
);
945 goto err_resolve_get_addr
;
948 qemu_rdma_dump_gid("source_resolve_addr", rdma
->cm_id
);
950 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
952 ERROR(errp
, "could not perform event_addr_resolved");
953 goto err_resolve_get_addr
;
956 if (cm_event
->event
!= RDMA_CM_EVENT_ADDR_RESOLVED
) {
957 ERROR(errp
, "result not equal to event_addr_resolved %s",
958 rdma_event_str(cm_event
->event
));
959 perror("rdma_resolve_addr");
960 rdma_ack_cm_event(cm_event
);
962 goto err_resolve_get_addr
;
964 rdma_ack_cm_event(cm_event
);
967 ret
= rdma_resolve_route(rdma
->cm_id
, RDMA_RESOLVE_TIMEOUT_MS
);
969 ERROR(errp
, "could not resolve rdma route");
970 goto err_resolve_get_addr
;
973 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
975 ERROR(errp
, "could not perform event_route_resolved");
976 goto err_resolve_get_addr
;
978 if (cm_event
->event
!= RDMA_CM_EVENT_ROUTE_RESOLVED
) {
979 ERROR(errp
, "result not equal to event_route_resolved: %s",
980 rdma_event_str(cm_event
->event
));
981 rdma_ack_cm_event(cm_event
);
983 goto err_resolve_get_addr
;
985 rdma_ack_cm_event(cm_event
);
986 rdma
->verbs
= rdma
->cm_id
->verbs
;
987 qemu_rdma_dump_id("source_resolve_host", rdma
->cm_id
->verbs
);
988 qemu_rdma_dump_gid("source_resolve_host", rdma
->cm_id
);
991 err_resolve_get_addr
:
992 rdma_destroy_id(rdma
->cm_id
);
994 err_resolve_create_id
:
995 rdma_destroy_event_channel(rdma
->channel
);
996 rdma
->channel
= NULL
;
1001 * Create protection domain and completion queues
1003 static int qemu_rdma_alloc_pd_cq(RDMAContext
*rdma
)
1006 rdma
->pd
= ibv_alloc_pd(rdma
->verbs
);
1008 error_report("failed to allocate protection domain");
1012 /* create completion channel */
1013 rdma
->comp_channel
= ibv_create_comp_channel(rdma
->verbs
);
1014 if (!rdma
->comp_channel
) {
1015 error_report("failed to allocate completion channel");
1016 goto err_alloc_pd_cq
;
1020 * Completion queue can be filled by both read and write work requests,
1021 * so must reflect the sum of both possible queue sizes.
1023 rdma
->cq
= ibv_create_cq(rdma
->verbs
, (RDMA_SIGNALED_SEND_MAX
* 3),
1024 NULL
, rdma
->comp_channel
, 0);
1026 error_report("failed to allocate completion queue");
1027 goto err_alloc_pd_cq
;
1034 ibv_dealloc_pd(rdma
->pd
);
1036 if (rdma
->comp_channel
) {
1037 ibv_destroy_comp_channel(rdma
->comp_channel
);
1040 rdma
->comp_channel
= NULL
;
1046 * Create queue pairs.
1048 static int qemu_rdma_alloc_qp(RDMAContext
*rdma
)
1050 struct ibv_qp_init_attr attr
= { 0 };
1053 attr
.cap
.max_send_wr
= RDMA_SIGNALED_SEND_MAX
;
1054 attr
.cap
.max_recv_wr
= 3;
1055 attr
.cap
.max_send_sge
= 1;
1056 attr
.cap
.max_recv_sge
= 1;
1057 attr
.send_cq
= rdma
->cq
;
1058 attr
.recv_cq
= rdma
->cq
;
1059 attr
.qp_type
= IBV_QPT_RC
;
1061 ret
= rdma_create_qp(rdma
->cm_id
, rdma
->pd
, &attr
);
1066 rdma
->qp
= rdma
->cm_id
->qp
;
1070 static int qemu_rdma_reg_whole_ram_blocks(RDMAContext
*rdma
)
1073 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
1075 for (i
= 0; i
< local
->nb_blocks
; i
++) {
1076 local
->block
[i
].mr
=
1077 ibv_reg_mr(rdma
->pd
,
1078 local
->block
[i
].local_host_addr
,
1079 local
->block
[i
].length
,
1080 IBV_ACCESS_LOCAL_WRITE
|
1081 IBV_ACCESS_REMOTE_WRITE
1083 if (!local
->block
[i
].mr
) {
1084 perror("Failed to register local dest ram block!\n");
1087 rdma
->total_registrations
++;
1090 if (i
>= local
->nb_blocks
) {
1094 for (i
--; i
>= 0; i
--) {
1095 ibv_dereg_mr(local
->block
[i
].mr
);
1096 rdma
->total_registrations
--;
1104 * Find the ram block that corresponds to the page requested to be
1105 * transmitted by QEMU.
1107 * Once the block is found, also identify which 'chunk' within that
1108 * block that the page belongs to.
1110 * This search cannot fail or the migration will fail.
1112 static int qemu_rdma_search_ram_block(RDMAContext
*rdma
,
1113 uintptr_t block_offset
,
1116 uint64_t *block_index
,
1117 uint64_t *chunk_index
)
1119 uint64_t current_addr
= block_offset
+ offset
;
1120 RDMALocalBlock
*block
= g_hash_table_lookup(rdma
->blockmap
,
1121 (void *) block_offset
);
1123 assert(current_addr
>= block
->offset
);
1124 assert((current_addr
+ length
) <= (block
->offset
+ block
->length
));
1126 *block_index
= block
->index
;
1127 *chunk_index
= ram_chunk_index(block
->local_host_addr
,
1128 block
->local_host_addr
+ (current_addr
- block
->offset
));
1134 * Register a chunk with IB. If the chunk was already registered
1135 * previously, then skip.
1137 * Also return the keys associated with the registration needed
1138 * to perform the actual RDMA operation.
1140 static int qemu_rdma_register_and_get_keys(RDMAContext
*rdma
,
1141 RDMALocalBlock
*block
, uintptr_t host_addr
,
1142 uint32_t *lkey
, uint32_t *rkey
, int chunk
,
1143 uint8_t *chunk_start
, uint8_t *chunk_end
)
1147 *lkey
= block
->mr
->lkey
;
1150 *rkey
= block
->mr
->rkey
;
1155 /* allocate memory to store chunk MRs */
1157 block
->pmr
= g_malloc0(block
->nb_chunks
* sizeof(struct ibv_mr
*));
1161 * If 'rkey', then we're the destination, so grant access to the source.
1163 * If 'lkey', then we're the source VM, so grant access only to ourselves.
1165 if (!block
->pmr
[chunk
]) {
1166 uint64_t len
= chunk_end
- chunk_start
;
1168 trace_qemu_rdma_register_and_get_keys(len
, chunk_start
);
1170 block
->pmr
[chunk
] = ibv_reg_mr(rdma
->pd
,
1172 (rkey
? (IBV_ACCESS_LOCAL_WRITE
|
1173 IBV_ACCESS_REMOTE_WRITE
) : 0));
1175 if (!block
->pmr
[chunk
]) {
1176 perror("Failed to register chunk!");
1177 fprintf(stderr
, "Chunk details: block: %d chunk index %d"
1178 " start %" PRIuPTR
" end %" PRIuPTR
1180 " local %" PRIuPTR
" registrations: %d\n",
1181 block
->index
, chunk
, (uintptr_t)chunk_start
,
1182 (uintptr_t)chunk_end
, host_addr
,
1183 (uintptr_t)block
->local_host_addr
,
1184 rdma
->total_registrations
);
1187 rdma
->total_registrations
++;
1191 *lkey
= block
->pmr
[chunk
]->lkey
;
1194 *rkey
= block
->pmr
[chunk
]->rkey
;
1200 * Register (at connection time) the memory used for control
1203 static int qemu_rdma_reg_control(RDMAContext
*rdma
, int idx
)
1205 rdma
->wr_data
[idx
].control_mr
= ibv_reg_mr(rdma
->pd
,
1206 rdma
->wr_data
[idx
].control
, RDMA_CONTROL_MAX_BUFFER
,
1207 IBV_ACCESS_LOCAL_WRITE
| IBV_ACCESS_REMOTE_WRITE
);
1208 if (rdma
->wr_data
[idx
].control_mr
) {
1209 rdma
->total_registrations
++;
1212 error_report("qemu_rdma_reg_control failed");
1216 const char *print_wrid(int wrid
)
1218 if (wrid
>= RDMA_WRID_RECV_CONTROL
) {
1219 return wrid_desc
[RDMA_WRID_RECV_CONTROL
];
1221 return wrid_desc
[wrid
];
1225 * RDMA requires memory registration (mlock/pinning), but this is not good for
1228 * In preparation for the future where LRU information or workload-specific
1229 * writable writable working set memory access behavior is available to QEMU
1230 * it would be nice to have in place the ability to UN-register/UN-pin
1231 * particular memory regions from the RDMA hardware when it is determine that
1232 * those regions of memory will likely not be accessed again in the near future.
1234 * While we do not yet have such information right now, the following
1235 * compile-time option allows us to perform a non-optimized version of this
1238 * By uncommenting this option, you will cause *all* RDMA transfers to be
1239 * unregistered immediately after the transfer completes on both sides of the
1240 * connection. This has no effect in 'rdma-pin-all' mode, only regular mode.
1242 * This will have a terrible impact on migration performance, so until future
1243 * workload information or LRU information is available, do not attempt to use
1244 * this feature except for basic testing.
1246 //#define RDMA_UNREGISTRATION_EXAMPLE
1249 * Perform a non-optimized memory unregistration after every transfer
1250 * for demonstration purposes, only if pin-all is not requested.
1252 * Potential optimizations:
1253 * 1. Start a new thread to run this function continuously
1255 - and for receipt of unregister messages
1257 * 3. Use workload hints.
1259 static int qemu_rdma_unregister_waiting(RDMAContext
*rdma
)
1261 while (rdma
->unregistrations
[rdma
->unregister_current
]) {
1263 uint64_t wr_id
= rdma
->unregistrations
[rdma
->unregister_current
];
1265 (wr_id
& RDMA_WRID_CHUNK_MASK
) >> RDMA_WRID_CHUNK_SHIFT
;
1267 (wr_id
& RDMA_WRID_BLOCK_MASK
) >> RDMA_WRID_BLOCK_SHIFT
;
1268 RDMALocalBlock
*block
=
1269 &(rdma
->local_ram_blocks
.block
[index
]);
1270 RDMARegister reg
= { .current_index
= index
};
1271 RDMAControlHeader resp
= { .type
= RDMA_CONTROL_UNREGISTER_FINISHED
,
1273 RDMAControlHeader head
= { .len
= sizeof(RDMARegister
),
1274 .type
= RDMA_CONTROL_UNREGISTER_REQUEST
,
1278 trace_qemu_rdma_unregister_waiting_proc(chunk
,
1279 rdma
->unregister_current
);
1281 rdma
->unregistrations
[rdma
->unregister_current
] = 0;
1282 rdma
->unregister_current
++;
1284 if (rdma
->unregister_current
== RDMA_SIGNALED_SEND_MAX
) {
1285 rdma
->unregister_current
= 0;
1290 * Unregistration is speculative (because migration is single-threaded
1291 * and we cannot break the protocol's inifinband message ordering).
1292 * Thus, if the memory is currently being used for transmission,
1293 * then abort the attempt to unregister and try again
1294 * later the next time a completion is received for this memory.
1296 clear_bit(chunk
, block
->unregister_bitmap
);
1298 if (test_bit(chunk
, block
->transit_bitmap
)) {
1299 trace_qemu_rdma_unregister_waiting_inflight(chunk
);
1303 trace_qemu_rdma_unregister_waiting_send(chunk
);
1305 ret
= ibv_dereg_mr(block
->pmr
[chunk
]);
1306 block
->pmr
[chunk
] = NULL
;
1307 block
->remote_keys
[chunk
] = 0;
1310 perror("unregistration chunk failed");
1313 rdma
->total_registrations
--;
1315 reg
.key
.chunk
= chunk
;
1316 register_to_network(rdma
, ®
);
1317 ret
= qemu_rdma_exchange_send(rdma
, &head
, (uint8_t *) ®
,
1323 trace_qemu_rdma_unregister_waiting_complete(chunk
);
1329 static uint64_t qemu_rdma_make_wrid(uint64_t wr_id
, uint64_t index
,
1332 uint64_t result
= wr_id
& RDMA_WRID_TYPE_MASK
;
1334 result
|= (index
<< RDMA_WRID_BLOCK_SHIFT
);
1335 result
|= (chunk
<< RDMA_WRID_CHUNK_SHIFT
);
1341 * Set bit for unregistration in the next iteration.
1342 * We cannot transmit right here, but will unpin later.
1344 static void qemu_rdma_signal_unregister(RDMAContext
*rdma
, uint64_t index
,
1345 uint64_t chunk
, uint64_t wr_id
)
1347 if (rdma
->unregistrations
[rdma
->unregister_next
] != 0) {
1348 error_report("rdma migration: queue is full");
1350 RDMALocalBlock
*block
= &(rdma
->local_ram_blocks
.block
[index
]);
1352 if (!test_and_set_bit(chunk
, block
->unregister_bitmap
)) {
1353 trace_qemu_rdma_signal_unregister_append(chunk
,
1354 rdma
->unregister_next
);
1356 rdma
->unregistrations
[rdma
->unregister_next
++] =
1357 qemu_rdma_make_wrid(wr_id
, index
, chunk
);
1359 if (rdma
->unregister_next
== RDMA_SIGNALED_SEND_MAX
) {
1360 rdma
->unregister_next
= 0;
1363 trace_qemu_rdma_signal_unregister_already(chunk
);
1369 * Consult the connection manager to see a work request
1370 * (of any kind) has completed.
1371 * Return the work request ID that completed.
1373 static uint64_t qemu_rdma_poll(RDMAContext
*rdma
, uint64_t *wr_id_out
,
1380 ret
= ibv_poll_cq(rdma
->cq
, 1, &wc
);
1383 *wr_id_out
= RDMA_WRID_NONE
;
1388 error_report("ibv_poll_cq return %d", ret
);
1392 wr_id
= wc
.wr_id
& RDMA_WRID_TYPE_MASK
;
1394 if (wc
.status
!= IBV_WC_SUCCESS
) {
1395 fprintf(stderr
, "ibv_poll_cq wc.status=%d %s!\n",
1396 wc
.status
, ibv_wc_status_str(wc
.status
));
1397 fprintf(stderr
, "ibv_poll_cq wrid=%s!\n", wrid_desc
[wr_id
]);
1402 if (rdma
->control_ready_expected
&&
1403 (wr_id
>= RDMA_WRID_RECV_CONTROL
)) {
1404 trace_qemu_rdma_poll_recv(wrid_desc
[RDMA_WRID_RECV_CONTROL
],
1405 wr_id
- RDMA_WRID_RECV_CONTROL
, wr_id
, rdma
->nb_sent
);
1406 rdma
->control_ready_expected
= 0;
1409 if (wr_id
== RDMA_WRID_RDMA_WRITE
) {
1411 (wc
.wr_id
& RDMA_WRID_CHUNK_MASK
) >> RDMA_WRID_CHUNK_SHIFT
;
1413 (wc
.wr_id
& RDMA_WRID_BLOCK_MASK
) >> RDMA_WRID_BLOCK_SHIFT
;
1414 RDMALocalBlock
*block
= &(rdma
->local_ram_blocks
.block
[index
]);
1416 trace_qemu_rdma_poll_write(print_wrid(wr_id
), wr_id
, rdma
->nb_sent
,
1417 index
, chunk
, block
->local_host_addr
,
1418 (void *)(uintptr_t)block
->remote_host_addr
);
1420 clear_bit(chunk
, block
->transit_bitmap
);
1422 if (rdma
->nb_sent
> 0) {
1426 if (!rdma
->pin_all
) {
1428 * FYI: If one wanted to signal a specific chunk to be unregistered
1429 * using LRU or workload-specific information, this is the function
1430 * you would call to do so. That chunk would then get asynchronously
1431 * unregistered later.
1433 #ifdef RDMA_UNREGISTRATION_EXAMPLE
1434 qemu_rdma_signal_unregister(rdma
, index
, chunk
, wc
.wr_id
);
1438 trace_qemu_rdma_poll_other(print_wrid(wr_id
), wr_id
, rdma
->nb_sent
);
1441 *wr_id_out
= wc
.wr_id
;
1443 *byte_len
= wc
.byte_len
;
1450 * Block until the next work request has completed.
1452 * First poll to see if a work request has already completed,
1455 * If we encounter completed work requests for IDs other than
1456 * the one we're interested in, then that's generally an error.
1458 * The only exception is actual RDMA Write completions. These
1459 * completions only need to be recorded, but do not actually
1460 * need further processing.
1462 static int qemu_rdma_block_for_wrid(RDMAContext
*rdma
, int wrid_requested
,
1465 int num_cq_events
= 0, ret
= 0;
1468 uint64_t wr_id
= RDMA_WRID_NONE
, wr_id_in
;
1470 if (ibv_req_notify_cq(rdma
->cq
, 0)) {
1474 while (wr_id
!= wrid_requested
) {
1475 ret
= qemu_rdma_poll(rdma
, &wr_id_in
, byte_len
);
1480 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
1482 if (wr_id
== RDMA_WRID_NONE
) {
1485 if (wr_id
!= wrid_requested
) {
1486 trace_qemu_rdma_block_for_wrid_miss(print_wrid(wrid_requested
),
1487 wrid_requested
, print_wrid(wr_id
), wr_id
);
1491 if (wr_id
== wrid_requested
) {
1497 * Coroutine doesn't start until process_incoming_migration()
1498 * so don't yield unless we know we're running inside of a coroutine.
1500 if (rdma
->migration_started_on_destination
) {
1501 yield_until_fd_readable(rdma
->comp_channel
->fd
);
1504 if (ibv_get_cq_event(rdma
->comp_channel
, &cq
, &cq_ctx
)) {
1505 perror("ibv_get_cq_event");
1506 goto err_block_for_wrid
;
1511 if (ibv_req_notify_cq(cq
, 0)) {
1512 goto err_block_for_wrid
;
1515 while (wr_id
!= wrid_requested
) {
1516 ret
= qemu_rdma_poll(rdma
, &wr_id_in
, byte_len
);
1518 goto err_block_for_wrid
;
1521 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
1523 if (wr_id
== RDMA_WRID_NONE
) {
1526 if (wr_id
!= wrid_requested
) {
1527 trace_qemu_rdma_block_for_wrid_miss(print_wrid(wrid_requested
),
1528 wrid_requested
, print_wrid(wr_id
), wr_id
);
1532 if (wr_id
== wrid_requested
) {
1533 goto success_block_for_wrid
;
1537 success_block_for_wrid
:
1538 if (num_cq_events
) {
1539 ibv_ack_cq_events(cq
, num_cq_events
);
1544 if (num_cq_events
) {
1545 ibv_ack_cq_events(cq
, num_cq_events
);
1551 * Post a SEND message work request for the control channel
1552 * containing some data and block until the post completes.
1554 static int qemu_rdma_post_send_control(RDMAContext
*rdma
, uint8_t *buf
,
1555 RDMAControlHeader
*head
)
1558 RDMAWorkRequestData
*wr
= &rdma
->wr_data
[RDMA_WRID_CONTROL
];
1559 struct ibv_send_wr
*bad_wr
;
1560 struct ibv_sge sge
= {
1561 .addr
= (uintptr_t)(wr
->control
),
1562 .length
= head
->len
+ sizeof(RDMAControlHeader
),
1563 .lkey
= wr
->control_mr
->lkey
,
1565 struct ibv_send_wr send_wr
= {
1566 .wr_id
= RDMA_WRID_SEND_CONTROL
,
1567 .opcode
= IBV_WR_SEND
,
1568 .send_flags
= IBV_SEND_SIGNALED
,
1573 trace_qemu_rdma_post_send_control(control_desc
[head
->type
]);
1576 * We don't actually need to do a memcpy() in here if we used
1577 * the "sge" properly, but since we're only sending control messages
1578 * (not RAM in a performance-critical path), then its OK for now.
1580 * The copy makes the RDMAControlHeader simpler to manipulate
1581 * for the time being.
1583 assert(head
->len
<= RDMA_CONTROL_MAX_BUFFER
- sizeof(*head
));
1584 memcpy(wr
->control
, head
, sizeof(RDMAControlHeader
));
1585 control_to_network((void *) wr
->control
);
1588 memcpy(wr
->control
+ sizeof(RDMAControlHeader
), buf
, head
->len
);
1592 ret
= ibv_post_send(rdma
->qp
, &send_wr
, &bad_wr
);
1595 error_report("Failed to use post IB SEND for control");
1599 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_SEND_CONTROL
, NULL
);
1601 error_report("rdma migration: send polling control error");
1608 * Post a RECV work request in anticipation of some future receipt
1609 * of data on the control channel.
1611 static int qemu_rdma_post_recv_control(RDMAContext
*rdma
, int idx
)
1613 struct ibv_recv_wr
*bad_wr
;
1614 struct ibv_sge sge
= {
1615 .addr
= (uintptr_t)(rdma
->wr_data
[idx
].control
),
1616 .length
= RDMA_CONTROL_MAX_BUFFER
,
1617 .lkey
= rdma
->wr_data
[idx
].control_mr
->lkey
,
1620 struct ibv_recv_wr recv_wr
= {
1621 .wr_id
= RDMA_WRID_RECV_CONTROL
+ idx
,
1627 if (ibv_post_recv(rdma
->qp
, &recv_wr
, &bad_wr
)) {
1635 * Block and wait for a RECV control channel message to arrive.
1637 static int qemu_rdma_exchange_get_response(RDMAContext
*rdma
,
1638 RDMAControlHeader
*head
, int expecting
, int idx
)
1641 int ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RECV_CONTROL
+ idx
,
1645 error_report("rdma migration: recv polling control error!");
1649 network_to_control((void *) rdma
->wr_data
[idx
].control
);
1650 memcpy(head
, rdma
->wr_data
[idx
].control
, sizeof(RDMAControlHeader
));
1652 trace_qemu_rdma_exchange_get_response_start(control_desc
[expecting
]);
1654 if (expecting
== RDMA_CONTROL_NONE
) {
1655 trace_qemu_rdma_exchange_get_response_none(control_desc
[head
->type
],
1657 } else if (head
->type
!= expecting
|| head
->type
== RDMA_CONTROL_ERROR
) {
1658 error_report("Was expecting a %s (%d) control message"
1659 ", but got: %s (%d), length: %d",
1660 control_desc
[expecting
], expecting
,
1661 control_desc
[head
->type
], head
->type
, head
->len
);
1664 if (head
->len
> RDMA_CONTROL_MAX_BUFFER
- sizeof(*head
)) {
1665 error_report("too long length: %d", head
->len
);
1668 if (sizeof(*head
) + head
->len
!= byte_len
) {
1669 error_report("Malformed length: %d byte_len %d", head
->len
, byte_len
);
1677 * When a RECV work request has completed, the work request's
1678 * buffer is pointed at the header.
1680 * This will advance the pointer to the data portion
1681 * of the control message of the work request's buffer that
1682 * was populated after the work request finished.
1684 static void qemu_rdma_move_header(RDMAContext
*rdma
, int idx
,
1685 RDMAControlHeader
*head
)
1687 rdma
->wr_data
[idx
].control_len
= head
->len
;
1688 rdma
->wr_data
[idx
].control_curr
=
1689 rdma
->wr_data
[idx
].control
+ sizeof(RDMAControlHeader
);
1693 * This is an 'atomic' high-level operation to deliver a single, unified
1694 * control-channel message.
1696 * Additionally, if the user is expecting some kind of reply to this message,
1697 * they can request a 'resp' response message be filled in by posting an
1698 * additional work request on behalf of the user and waiting for an additional
1701 * The extra (optional) response is used during registration to us from having
1702 * to perform an *additional* exchange of message just to provide a response by
1703 * instead piggy-backing on the acknowledgement.
1705 static int qemu_rdma_exchange_send(RDMAContext
*rdma
, RDMAControlHeader
*head
,
1706 uint8_t *data
, RDMAControlHeader
*resp
,
1708 int (*callback
)(RDMAContext
*rdma
))
1713 * Wait until the dest is ready before attempting to deliver the message
1714 * by waiting for a READY message.
1716 if (rdma
->control_ready_expected
) {
1717 RDMAControlHeader resp
;
1718 ret
= qemu_rdma_exchange_get_response(rdma
,
1719 &resp
, RDMA_CONTROL_READY
, RDMA_WRID_READY
);
1726 * If the user is expecting a response, post a WR in anticipation of it.
1729 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_DATA
);
1731 error_report("rdma migration: error posting"
1732 " extra control recv for anticipated result!");
1738 * Post a WR to replace the one we just consumed for the READY message.
1740 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
1742 error_report("rdma migration: error posting first control recv!");
1747 * Deliver the control message that was requested.
1749 ret
= qemu_rdma_post_send_control(rdma
, data
, head
);
1752 error_report("Failed to send control buffer!");
1757 * If we're expecting a response, block and wait for it.
1761 trace_qemu_rdma_exchange_send_issue_callback();
1762 ret
= callback(rdma
);
1768 trace_qemu_rdma_exchange_send_waiting(control_desc
[resp
->type
]);
1769 ret
= qemu_rdma_exchange_get_response(rdma
, resp
,
1770 resp
->type
, RDMA_WRID_DATA
);
1776 qemu_rdma_move_header(rdma
, RDMA_WRID_DATA
, resp
);
1778 *resp_idx
= RDMA_WRID_DATA
;
1780 trace_qemu_rdma_exchange_send_received(control_desc
[resp
->type
]);
1783 rdma
->control_ready_expected
= 1;
1789 * This is an 'atomic' high-level operation to receive a single, unified
1790 * control-channel message.
1792 static int qemu_rdma_exchange_recv(RDMAContext
*rdma
, RDMAControlHeader
*head
,
1795 RDMAControlHeader ready
= {
1797 .type
= RDMA_CONTROL_READY
,
1803 * Inform the source that we're ready to receive a message.
1805 ret
= qemu_rdma_post_send_control(rdma
, NULL
, &ready
);
1808 error_report("Failed to send control buffer!");
1813 * Block and wait for the message.
1815 ret
= qemu_rdma_exchange_get_response(rdma
, head
,
1816 expecting
, RDMA_WRID_READY
);
1822 qemu_rdma_move_header(rdma
, RDMA_WRID_READY
, head
);
1825 * Post a new RECV work request to replace the one we just consumed.
1827 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
1829 error_report("rdma migration: error posting second control recv!");
1837 * Write an actual chunk of memory using RDMA.
1839 * If we're using dynamic registration on the dest-side, we have to
1840 * send a registration command first.
1842 static int qemu_rdma_write_one(QEMUFile
*f
, RDMAContext
*rdma
,
1843 int current_index
, uint64_t current_addr
,
1847 struct ibv_send_wr send_wr
= { 0 };
1848 struct ibv_send_wr
*bad_wr
;
1849 int reg_result_idx
, ret
, count
= 0;
1850 uint64_t chunk
, chunks
;
1851 uint8_t *chunk_start
, *chunk_end
;
1852 RDMALocalBlock
*block
= &(rdma
->local_ram_blocks
.block
[current_index
]);
1854 RDMARegisterResult
*reg_result
;
1855 RDMAControlHeader resp
= { .type
= RDMA_CONTROL_REGISTER_RESULT
};
1856 RDMAControlHeader head
= { .len
= sizeof(RDMARegister
),
1857 .type
= RDMA_CONTROL_REGISTER_REQUEST
,
1862 sge
.addr
= (uintptr_t)(block
->local_host_addr
+
1863 (current_addr
- block
->offset
));
1864 sge
.length
= length
;
1866 chunk
= ram_chunk_index(block
->local_host_addr
,
1867 (uint8_t *)(uintptr_t)sge
.addr
);
1868 chunk_start
= ram_chunk_start(block
, chunk
);
1870 if (block
->is_ram_block
) {
1871 chunks
= length
/ (1UL << RDMA_REG_CHUNK_SHIFT
);
1873 if (chunks
&& ((length
% (1UL << RDMA_REG_CHUNK_SHIFT
)) == 0)) {
1877 chunks
= block
->length
/ (1UL << RDMA_REG_CHUNK_SHIFT
);
1879 if (chunks
&& ((block
->length
% (1UL << RDMA_REG_CHUNK_SHIFT
)) == 0)) {
1884 trace_qemu_rdma_write_one_top(chunks
+ 1,
1886 (1UL << RDMA_REG_CHUNK_SHIFT
) / 1024 / 1024);
1888 chunk_end
= ram_chunk_end(block
, chunk
+ chunks
);
1890 if (!rdma
->pin_all
) {
1891 #ifdef RDMA_UNREGISTRATION_EXAMPLE
1892 qemu_rdma_unregister_waiting(rdma
);
1896 while (test_bit(chunk
, block
->transit_bitmap
)) {
1898 trace_qemu_rdma_write_one_block(count
++, current_index
, chunk
,
1899 sge
.addr
, length
, rdma
->nb_sent
, block
->nb_chunks
);
1901 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RDMA_WRITE
, NULL
);
1904 error_report("Failed to Wait for previous write to complete "
1905 "block %d chunk %" PRIu64
1906 " current %" PRIu64
" len %" PRIu64
" %d",
1907 current_index
, chunk
, sge
.addr
, length
, rdma
->nb_sent
);
1912 if (!rdma
->pin_all
|| !block
->is_ram_block
) {
1913 if (!block
->remote_keys
[chunk
]) {
1915 * This chunk has not yet been registered, so first check to see
1916 * if the entire chunk is zero. If so, tell the other size to
1917 * memset() + madvise() the entire chunk without RDMA.
1920 if (can_use_buffer_find_nonzero_offset((void *)(uintptr_t)sge
.addr
,
1922 && buffer_find_nonzero_offset((void *)(uintptr_t)sge
.addr
,
1923 length
) == length
) {
1924 RDMACompress comp
= {
1925 .offset
= current_addr
,
1927 .block_idx
= current_index
,
1931 head
.len
= sizeof(comp
);
1932 head
.type
= RDMA_CONTROL_COMPRESS
;
1934 trace_qemu_rdma_write_one_zero(chunk
, sge
.length
,
1935 current_index
, current_addr
);
1937 compress_to_network(rdma
, &comp
);
1938 ret
= qemu_rdma_exchange_send(rdma
, &head
,
1939 (uint8_t *) &comp
, NULL
, NULL
, NULL
);
1945 acct_update_position(f
, sge
.length
, true);
1951 * Otherwise, tell other side to register.
1953 reg
.current_index
= current_index
;
1954 if (block
->is_ram_block
) {
1955 reg
.key
.current_addr
= current_addr
;
1957 reg
.key
.chunk
= chunk
;
1959 reg
.chunks
= chunks
;
1961 trace_qemu_rdma_write_one_sendreg(chunk
, sge
.length
, current_index
,
1964 register_to_network(rdma
, ®
);
1965 ret
= qemu_rdma_exchange_send(rdma
, &head
, (uint8_t *) ®
,
1966 &resp
, ®_result_idx
, NULL
);
1971 /* try to overlap this single registration with the one we sent. */
1972 if (qemu_rdma_register_and_get_keys(rdma
, block
, sge
.addr
,
1973 &sge
.lkey
, NULL
, chunk
,
1974 chunk_start
, chunk_end
)) {
1975 error_report("cannot get lkey");
1979 reg_result
= (RDMARegisterResult
*)
1980 rdma
->wr_data
[reg_result_idx
].control_curr
;
1982 network_to_result(reg_result
);
1984 trace_qemu_rdma_write_one_recvregres(block
->remote_keys
[chunk
],
1985 reg_result
->rkey
, chunk
);
1987 block
->remote_keys
[chunk
] = reg_result
->rkey
;
1988 block
->remote_host_addr
= reg_result
->host_addr
;
1990 /* already registered before */
1991 if (qemu_rdma_register_and_get_keys(rdma
, block
, sge
.addr
,
1992 &sge
.lkey
, NULL
, chunk
,
1993 chunk_start
, chunk_end
)) {
1994 error_report("cannot get lkey!");
1999 send_wr
.wr
.rdma
.rkey
= block
->remote_keys
[chunk
];
2001 send_wr
.wr
.rdma
.rkey
= block
->remote_rkey
;
2003 if (qemu_rdma_register_and_get_keys(rdma
, block
, sge
.addr
,
2004 &sge
.lkey
, NULL
, chunk
,
2005 chunk_start
, chunk_end
)) {
2006 error_report("cannot get lkey!");
2012 * Encode the ram block index and chunk within this wrid.
2013 * We will use this information at the time of completion
2014 * to figure out which bitmap to check against and then which
2015 * chunk in the bitmap to look for.
2017 send_wr
.wr_id
= qemu_rdma_make_wrid(RDMA_WRID_RDMA_WRITE
,
2018 current_index
, chunk
);
2020 send_wr
.opcode
= IBV_WR_RDMA_WRITE
;
2021 send_wr
.send_flags
= IBV_SEND_SIGNALED
;
2022 send_wr
.sg_list
= &sge
;
2023 send_wr
.num_sge
= 1;
2024 send_wr
.wr
.rdma
.remote_addr
= block
->remote_host_addr
+
2025 (current_addr
- block
->offset
);
2027 trace_qemu_rdma_write_one_post(chunk
, sge
.addr
, send_wr
.wr
.rdma
.remote_addr
,
2031 * ibv_post_send() does not return negative error numbers,
2032 * per the specification they are positive - no idea why.
2034 ret
= ibv_post_send(rdma
->qp
, &send_wr
, &bad_wr
);
2036 if (ret
== ENOMEM
) {
2037 trace_qemu_rdma_write_one_queue_full();
2038 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RDMA_WRITE
, NULL
);
2040 error_report("rdma migration: failed to make "
2041 "room in full send queue! %d", ret
);
2047 } else if (ret
> 0) {
2048 perror("rdma migration: post rdma write failed");
2052 set_bit(chunk
, block
->transit_bitmap
);
2053 acct_update_position(f
, sge
.length
, false);
2054 rdma
->total_writes
++;
2060 * Push out any unwritten RDMA operations.
2062 * We support sending out multiple chunks at the same time.
2063 * Not all of them need to get signaled in the completion queue.
2065 static int qemu_rdma_write_flush(QEMUFile
*f
, RDMAContext
*rdma
)
2069 if (!rdma
->current_length
) {
2073 ret
= qemu_rdma_write_one(f
, rdma
,
2074 rdma
->current_index
, rdma
->current_addr
, rdma
->current_length
);
2082 trace_qemu_rdma_write_flush(rdma
->nb_sent
);
2085 rdma
->current_length
= 0;
2086 rdma
->current_addr
= 0;
2091 static inline int qemu_rdma_buffer_mergable(RDMAContext
*rdma
,
2092 uint64_t offset
, uint64_t len
)
2094 RDMALocalBlock
*block
;
2098 if (rdma
->current_index
< 0) {
2102 if (rdma
->current_chunk
< 0) {
2106 block
= &(rdma
->local_ram_blocks
.block
[rdma
->current_index
]);
2107 host_addr
= block
->local_host_addr
+ (offset
- block
->offset
);
2108 chunk_end
= ram_chunk_end(block
, rdma
->current_chunk
);
2110 if (rdma
->current_length
== 0) {
2115 * Only merge into chunk sequentially.
2117 if (offset
!= (rdma
->current_addr
+ rdma
->current_length
)) {
2121 if (offset
< block
->offset
) {
2125 if ((offset
+ len
) > (block
->offset
+ block
->length
)) {
2129 if ((host_addr
+ len
) > chunk_end
) {
2137 * We're not actually writing here, but doing three things:
2139 * 1. Identify the chunk the buffer belongs to.
2140 * 2. If the chunk is full or the buffer doesn't belong to the current
2141 * chunk, then start a new chunk and flush() the old chunk.
2142 * 3. To keep the hardware busy, we also group chunks into batches
2143 * and only require that a batch gets acknowledged in the completion
2144 * qeueue instead of each individual chunk.
2146 static int qemu_rdma_write(QEMUFile
*f
, RDMAContext
*rdma
,
2147 uint64_t block_offset
, uint64_t offset
,
2150 uint64_t current_addr
= block_offset
+ offset
;
2151 uint64_t index
= rdma
->current_index
;
2152 uint64_t chunk
= rdma
->current_chunk
;
2155 /* If we cannot merge it, we flush the current buffer first. */
2156 if (!qemu_rdma_buffer_mergable(rdma
, current_addr
, len
)) {
2157 ret
= qemu_rdma_write_flush(f
, rdma
);
2161 rdma
->current_length
= 0;
2162 rdma
->current_addr
= current_addr
;
2164 ret
= qemu_rdma_search_ram_block(rdma
, block_offset
,
2165 offset
, len
, &index
, &chunk
);
2167 error_report("ram block search failed");
2170 rdma
->current_index
= index
;
2171 rdma
->current_chunk
= chunk
;
2175 rdma
->current_length
+= len
;
2177 /* flush it if buffer is too large */
2178 if (rdma
->current_length
>= RDMA_MERGE_MAX
) {
2179 return qemu_rdma_write_flush(f
, rdma
);
2185 static void qemu_rdma_cleanup(RDMAContext
*rdma
)
2187 struct rdma_cm_event
*cm_event
;
2190 if (rdma
->cm_id
&& rdma
->connected
) {
2191 if (rdma
->error_state
) {
2192 RDMAControlHeader head
= { .len
= 0,
2193 .type
= RDMA_CONTROL_ERROR
,
2196 error_report("Early error. Sending error.");
2197 qemu_rdma_post_send_control(rdma
, NULL
, &head
);
2200 ret
= rdma_disconnect(rdma
->cm_id
);
2202 trace_qemu_rdma_cleanup_waiting_for_disconnect();
2203 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
2205 rdma_ack_cm_event(cm_event
);
2208 trace_qemu_rdma_cleanup_disconnect();
2209 rdma
->connected
= false;
2212 g_free(rdma
->dest_blocks
);
2213 rdma
->dest_blocks
= NULL
;
2215 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2216 if (rdma
->wr_data
[idx
].control_mr
) {
2217 rdma
->total_registrations
--;
2218 ibv_dereg_mr(rdma
->wr_data
[idx
].control_mr
);
2220 rdma
->wr_data
[idx
].control_mr
= NULL
;
2223 if (rdma
->local_ram_blocks
.block
) {
2224 while (rdma
->local_ram_blocks
.nb_blocks
) {
2225 rdma_delete_block(rdma
, rdma
->local_ram_blocks
.block
->offset
);
2230 rdma_destroy_qp(rdma
->cm_id
);
2234 ibv_destroy_cq(rdma
->cq
);
2237 if (rdma
->comp_channel
) {
2238 ibv_destroy_comp_channel(rdma
->comp_channel
);
2239 rdma
->comp_channel
= NULL
;
2242 ibv_dealloc_pd(rdma
->pd
);
2246 rdma_destroy_id(rdma
->cm_id
);
2249 if (rdma
->listen_id
) {
2250 rdma_destroy_id(rdma
->listen_id
);
2251 rdma
->listen_id
= NULL
;
2253 if (rdma
->channel
) {
2254 rdma_destroy_event_channel(rdma
->channel
);
2255 rdma
->channel
= NULL
;
2262 static int qemu_rdma_source_init(RDMAContext
*rdma
, Error
**errp
, bool pin_all
)
2265 Error
*local_err
= NULL
, **temp
= &local_err
;
2268 * Will be validated against destination's actual capabilities
2269 * after the connect() completes.
2271 rdma
->pin_all
= pin_all
;
2273 ret
= qemu_rdma_resolve_host(rdma
, temp
);
2275 goto err_rdma_source_init
;
2278 ret
= qemu_rdma_alloc_pd_cq(rdma
);
2280 ERROR(temp
, "rdma migration: error allocating pd and cq! Your mlock()"
2281 " limits may be too low. Please check $ ulimit -a # and "
2282 "search for 'ulimit -l' in the output");
2283 goto err_rdma_source_init
;
2286 ret
= qemu_rdma_alloc_qp(rdma
);
2288 ERROR(temp
, "rdma migration: error allocating qp!");
2289 goto err_rdma_source_init
;
2292 ret
= qemu_rdma_init_ram_blocks(rdma
);
2294 ERROR(temp
, "rdma migration: error initializing ram blocks!");
2295 goto err_rdma_source_init
;
2298 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2299 ret
= qemu_rdma_reg_control(rdma
, idx
);
2301 ERROR(temp
, "rdma migration: error registering %d control!",
2303 goto err_rdma_source_init
;
2309 err_rdma_source_init
:
2310 error_propagate(errp
, local_err
);
2311 qemu_rdma_cleanup(rdma
);
2315 static int qemu_rdma_connect(RDMAContext
*rdma
, Error
**errp
)
2317 RDMACapabilities cap
= {
2318 .version
= RDMA_CONTROL_VERSION_CURRENT
,
2321 struct rdma_conn_param conn_param
= { .initiator_depth
= 2,
2323 .private_data
= &cap
,
2324 .private_data_len
= sizeof(cap
),
2326 struct rdma_cm_event
*cm_event
;
2330 * Only negotiate the capability with destination if the user
2331 * on the source first requested the capability.
2333 if (rdma
->pin_all
) {
2334 trace_qemu_rdma_connect_pin_all_requested();
2335 cap
.flags
|= RDMA_CAPABILITY_PIN_ALL
;
2338 caps_to_network(&cap
);
2340 ret
= rdma_connect(rdma
->cm_id
, &conn_param
);
2342 perror("rdma_connect");
2343 ERROR(errp
, "connecting to destination!");
2344 goto err_rdma_source_connect
;
2347 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
2349 perror("rdma_get_cm_event after rdma_connect");
2350 ERROR(errp
, "connecting to destination!");
2351 rdma_ack_cm_event(cm_event
);
2352 goto err_rdma_source_connect
;
2355 if (cm_event
->event
!= RDMA_CM_EVENT_ESTABLISHED
) {
2356 perror("rdma_get_cm_event != EVENT_ESTABLISHED after rdma_connect");
2357 ERROR(errp
, "connecting to destination!");
2358 rdma_ack_cm_event(cm_event
);
2359 goto err_rdma_source_connect
;
2361 rdma
->connected
= true;
2363 memcpy(&cap
, cm_event
->param
.conn
.private_data
, sizeof(cap
));
2364 network_to_caps(&cap
);
2367 * Verify that the *requested* capabilities are supported by the destination
2368 * and disable them otherwise.
2370 if (rdma
->pin_all
&& !(cap
.flags
& RDMA_CAPABILITY_PIN_ALL
)) {
2371 ERROR(errp
, "Server cannot support pinning all memory. "
2372 "Will register memory dynamically.");
2373 rdma
->pin_all
= false;
2376 trace_qemu_rdma_connect_pin_all_outcome(rdma
->pin_all
);
2378 rdma_ack_cm_event(cm_event
);
2380 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
2382 ERROR(errp
, "posting second control recv!");
2383 goto err_rdma_source_connect
;
2386 rdma
->control_ready_expected
= 1;
2390 err_rdma_source_connect
:
2391 qemu_rdma_cleanup(rdma
);
2395 static int qemu_rdma_dest_init(RDMAContext
*rdma
, Error
**errp
)
2398 struct rdma_cm_id
*listen_id
;
2399 char ip
[40] = "unknown";
2400 struct rdma_addrinfo
*res
, *e
;
2403 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2404 rdma
->wr_data
[idx
].control_len
= 0;
2405 rdma
->wr_data
[idx
].control_curr
= NULL
;
2408 if (!rdma
->host
|| !rdma
->host
[0]) {
2409 ERROR(errp
, "RDMA host is not set!");
2410 rdma
->error_state
= -EINVAL
;
2413 /* create CM channel */
2414 rdma
->channel
= rdma_create_event_channel();
2415 if (!rdma
->channel
) {
2416 ERROR(errp
, "could not create rdma event channel");
2417 rdma
->error_state
= -EINVAL
;
2422 ret
= rdma_create_id(rdma
->channel
, &listen_id
, NULL
, RDMA_PS_TCP
);
2424 ERROR(errp
, "could not create cm_id!");
2425 goto err_dest_init_create_listen_id
;
2428 snprintf(port_str
, 16, "%d", rdma
->port
);
2429 port_str
[15] = '\0';
2431 ret
= rdma_getaddrinfo(rdma
->host
, port_str
, NULL
, &res
);
2433 ERROR(errp
, "could not rdma_getaddrinfo address %s", rdma
->host
);
2434 goto err_dest_init_bind_addr
;
2437 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
2438 inet_ntop(e
->ai_family
,
2439 &((struct sockaddr_in
*) e
->ai_dst_addr
)->sin_addr
, ip
, sizeof ip
);
2440 trace_qemu_rdma_dest_init_trying(rdma
->host
, ip
);
2441 ret
= rdma_bind_addr(listen_id
, e
->ai_dst_addr
);
2445 if (e
->ai_family
== AF_INET6
) {
2446 ret
= qemu_rdma_broken_ipv6_kernel(errp
, listen_id
->verbs
);
2455 ERROR(errp
, "Error: could not rdma_bind_addr!");
2456 goto err_dest_init_bind_addr
;
2459 rdma
->listen_id
= listen_id
;
2460 qemu_rdma_dump_gid("dest_init", listen_id
);
2463 err_dest_init_bind_addr
:
2464 rdma_destroy_id(listen_id
);
2465 err_dest_init_create_listen_id
:
2466 rdma_destroy_event_channel(rdma
->channel
);
2467 rdma
->channel
= NULL
;
2468 rdma
->error_state
= ret
;
2473 static void *qemu_rdma_data_init(const char *host_port
, Error
**errp
)
2475 RDMAContext
*rdma
= NULL
;
2476 InetSocketAddress
*addr
;
2479 rdma
= g_malloc0(sizeof(RDMAContext
));
2480 rdma
->current_index
= -1;
2481 rdma
->current_chunk
= -1;
2483 addr
= inet_parse(host_port
, NULL
);
2485 rdma
->port
= atoi(addr
->port
);
2486 rdma
->host
= g_strdup(addr
->host
);
2488 ERROR(errp
, "bad RDMA migration address '%s'", host_port
);
2493 qapi_free_InetSocketAddress(addr
);
2500 * QEMUFile interface to the control channel.
2501 * SEND messages for control only.
2502 * VM's ram is handled with regular RDMA messages.
2504 static int qemu_rdma_put_buffer(void *opaque
, const uint8_t *buf
,
2505 int64_t pos
, int size
)
2507 QEMUFileRDMA
*r
= opaque
;
2508 QEMUFile
*f
= r
->file
;
2509 RDMAContext
*rdma
= r
->rdma
;
2510 size_t remaining
= size
;
2511 uint8_t * data
= (void *) buf
;
2514 CHECK_ERROR_STATE();
2517 * Push out any writes that
2518 * we're queued up for VM's ram.
2520 ret
= qemu_rdma_write_flush(f
, rdma
);
2522 rdma
->error_state
= ret
;
2527 RDMAControlHeader head
;
2529 r
->len
= MIN(remaining
, RDMA_SEND_INCREMENT
);
2530 remaining
-= r
->len
;
2533 head
.type
= RDMA_CONTROL_QEMU_FILE
;
2535 ret
= qemu_rdma_exchange_send(rdma
, &head
, data
, NULL
, NULL
, NULL
);
2538 rdma
->error_state
= ret
;
2548 static size_t qemu_rdma_fill(RDMAContext
*rdma
, uint8_t *buf
,
2553 if (rdma
->wr_data
[idx
].control_len
) {
2554 trace_qemu_rdma_fill(rdma
->wr_data
[idx
].control_len
, size
);
2556 len
= MIN(size
, rdma
->wr_data
[idx
].control_len
);
2557 memcpy(buf
, rdma
->wr_data
[idx
].control_curr
, len
);
2558 rdma
->wr_data
[idx
].control_curr
+= len
;
2559 rdma
->wr_data
[idx
].control_len
-= len
;
2566 * QEMUFile interface to the control channel.
2567 * RDMA links don't use bytestreams, so we have to
2568 * return bytes to QEMUFile opportunistically.
2570 static int qemu_rdma_get_buffer(void *opaque
, uint8_t *buf
,
2571 int64_t pos
, int size
)
2573 QEMUFileRDMA
*r
= opaque
;
2574 RDMAContext
*rdma
= r
->rdma
;
2575 RDMAControlHeader head
;
2578 CHECK_ERROR_STATE();
2581 * First, we hold on to the last SEND message we
2582 * were given and dish out the bytes until we run
2585 r
->len
= qemu_rdma_fill(r
->rdma
, buf
, size
, 0);
2591 * Once we run out, we block and wait for another
2592 * SEND message to arrive.
2594 ret
= qemu_rdma_exchange_recv(rdma
, &head
, RDMA_CONTROL_QEMU_FILE
);
2597 rdma
->error_state
= ret
;
2602 * SEND was received with new bytes, now try again.
2604 return qemu_rdma_fill(r
->rdma
, buf
, size
, 0);
2608 * Block until all the outstanding chunks have been delivered by the hardware.
2610 static int qemu_rdma_drain_cq(QEMUFile
*f
, RDMAContext
*rdma
)
2614 if (qemu_rdma_write_flush(f
, rdma
) < 0) {
2618 while (rdma
->nb_sent
) {
2619 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RDMA_WRITE
, NULL
);
2621 error_report("rdma migration: complete polling error!");
2626 qemu_rdma_unregister_waiting(rdma
);
2631 static int qemu_rdma_close(void *opaque
)
2633 trace_qemu_rdma_close();
2634 QEMUFileRDMA
*r
= opaque
;
2636 qemu_rdma_cleanup(r
->rdma
);
2646 * This means that 'block_offset' is a full virtual address that does not
2647 * belong to a RAMBlock of the virtual machine and instead
2648 * represents a private malloc'd memory area that the caller wishes to
2652 * Offset is an offset to be added to block_offset and used
2653 * to also lookup the corresponding RAMBlock.
2656 * Initiate an transfer this size.
2659 * A 'hint' or 'advice' that means that we wish to speculatively
2660 * and asynchronously unregister this memory. In this case, there is no
2661 * guarantee that the unregister will actually happen, for example,
2662 * if the memory is being actively transmitted. Additionally, the memory
2663 * may be re-registered at any future time if a write within the same
2664 * chunk was requested again, even if you attempted to unregister it
2667 * @size < 0 : TODO, not yet supported
2668 * Unregister the memory NOW. This means that the caller does not
2669 * expect there to be any future RDMA transfers and we just want to clean
2670 * things up. This is used in case the upper layer owns the memory and
2671 * cannot wait for qemu_fclose() to occur.
2673 * @bytes_sent : User-specificed pointer to indicate how many bytes were
2674 * sent. Usually, this will not be more than a few bytes of
2675 * the protocol because most transfers are sent asynchronously.
2677 static size_t qemu_rdma_save_page(QEMUFile
*f
, void *opaque
,
2678 ram_addr_t block_offset
, ram_addr_t offset
,
2679 size_t size
, uint64_t *bytes_sent
)
2681 QEMUFileRDMA
*rfile
= opaque
;
2682 RDMAContext
*rdma
= rfile
->rdma
;
2685 CHECK_ERROR_STATE();
2691 * Add this page to the current 'chunk'. If the chunk
2692 * is full, or the page doen't belong to the current chunk,
2693 * an actual RDMA write will occur and a new chunk will be formed.
2695 ret
= qemu_rdma_write(f
, rdma
, block_offset
, offset
, size
);
2697 error_report("rdma migration: write error! %d", ret
);
2702 * We always return 1 bytes because the RDMA
2703 * protocol is completely asynchronous. We do not yet know
2704 * whether an identified chunk is zero or not because we're
2705 * waiting for other pages to potentially be merged with
2706 * the current chunk. So, we have to call qemu_update_position()
2707 * later on when the actual write occurs.
2713 uint64_t index
, chunk
;
2715 /* TODO: Change QEMUFileOps prototype to be signed: size_t => long
2717 ret = qemu_rdma_drain_cq(f, rdma);
2719 fprintf(stderr, "rdma: failed to synchronously drain"
2720 " completion queue before unregistration.\n");
2726 ret
= qemu_rdma_search_ram_block(rdma
, block_offset
,
2727 offset
, size
, &index
, &chunk
);
2730 error_report("ram block search failed");
2734 qemu_rdma_signal_unregister(rdma
, index
, chunk
, 0);
2737 * TODO: Synchronous, guaranteed unregistration (should not occur during
2738 * fast-path). Otherwise, unregisters will process on the next call to
2739 * qemu_rdma_drain_cq()
2741 qemu_rdma_unregister_waiting(rdma);
2747 * Drain the Completion Queue if possible, but do not block,
2750 * If nothing to poll, the end of the iteration will do this
2751 * again to make sure we don't overflow the request queue.
2754 uint64_t wr_id
, wr_id_in
;
2755 int ret
= qemu_rdma_poll(rdma
, &wr_id_in
, NULL
);
2757 error_report("rdma migration: polling error! %d", ret
);
2761 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
2763 if (wr_id
== RDMA_WRID_NONE
) {
2768 return RAM_SAVE_CONTROL_DELAYED
;
2770 rdma
->error_state
= ret
;
2774 static int qemu_rdma_accept(RDMAContext
*rdma
)
2776 RDMACapabilities cap
;
2777 struct rdma_conn_param conn_param
= {
2778 .responder_resources
= 2,
2779 .private_data
= &cap
,
2780 .private_data_len
= sizeof(cap
),
2782 struct rdma_cm_event
*cm_event
;
2783 struct ibv_context
*verbs
;
2787 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
2789 goto err_rdma_dest_wait
;
2792 if (cm_event
->event
!= RDMA_CM_EVENT_CONNECT_REQUEST
) {
2793 rdma_ack_cm_event(cm_event
);
2794 goto err_rdma_dest_wait
;
2797 memcpy(&cap
, cm_event
->param
.conn
.private_data
, sizeof(cap
));
2799 network_to_caps(&cap
);
2801 if (cap
.version
< 1 || cap
.version
> RDMA_CONTROL_VERSION_CURRENT
) {
2802 error_report("Unknown source RDMA version: %d, bailing...",
2804 rdma_ack_cm_event(cm_event
);
2805 goto err_rdma_dest_wait
;
2809 * Respond with only the capabilities this version of QEMU knows about.
2811 cap
.flags
&= known_capabilities
;
2814 * Enable the ones that we do know about.
2815 * Add other checks here as new ones are introduced.
2817 if (cap
.flags
& RDMA_CAPABILITY_PIN_ALL
) {
2818 rdma
->pin_all
= true;
2821 rdma
->cm_id
= cm_event
->id
;
2822 verbs
= cm_event
->id
->verbs
;
2824 rdma_ack_cm_event(cm_event
);
2826 trace_qemu_rdma_accept_pin_state(rdma
->pin_all
);
2828 caps_to_network(&cap
);
2830 trace_qemu_rdma_accept_pin_verbsc(verbs
);
2833 rdma
->verbs
= verbs
;
2834 } else if (rdma
->verbs
!= verbs
) {
2835 error_report("ibv context not matching %p, %p!", rdma
->verbs
,
2837 goto err_rdma_dest_wait
;
2840 qemu_rdma_dump_id("dest_init", verbs
);
2842 ret
= qemu_rdma_alloc_pd_cq(rdma
);
2844 error_report("rdma migration: error allocating pd and cq!");
2845 goto err_rdma_dest_wait
;
2848 ret
= qemu_rdma_alloc_qp(rdma
);
2850 error_report("rdma migration: error allocating qp!");
2851 goto err_rdma_dest_wait
;
2854 ret
= qemu_rdma_init_ram_blocks(rdma
);
2856 error_report("rdma migration: error initializing ram blocks!");
2857 goto err_rdma_dest_wait
;
2860 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2861 ret
= qemu_rdma_reg_control(rdma
, idx
);
2863 error_report("rdma: error registering %d control", idx
);
2864 goto err_rdma_dest_wait
;
2868 qemu_set_fd_handler(rdma
->channel
->fd
, NULL
, NULL
, NULL
);
2870 ret
= rdma_accept(rdma
->cm_id
, &conn_param
);
2872 error_report("rdma_accept returns %d", ret
);
2873 goto err_rdma_dest_wait
;
2876 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
2878 error_report("rdma_accept get_cm_event failed %d", ret
);
2879 goto err_rdma_dest_wait
;
2882 if (cm_event
->event
!= RDMA_CM_EVENT_ESTABLISHED
) {
2883 error_report("rdma_accept not event established");
2884 rdma_ack_cm_event(cm_event
);
2885 goto err_rdma_dest_wait
;
2888 rdma_ack_cm_event(cm_event
);
2889 rdma
->connected
= true;
2891 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
2893 error_report("rdma migration: error posting second control recv");
2894 goto err_rdma_dest_wait
;
2897 qemu_rdma_dump_gid("dest_connect", rdma
->cm_id
);
2902 rdma
->error_state
= ret
;
2903 qemu_rdma_cleanup(rdma
);
2908 * During each iteration of the migration, we listen for instructions
2909 * by the source VM to perform dynamic page registrations before they
2910 * can perform RDMA operations.
2912 * We respond with the 'rkey'.
2914 * Keep doing this until the source tells us to stop.
2916 static int qemu_rdma_registration_handle(QEMUFile
*f
, void *opaque
,
2919 RDMAControlHeader reg_resp
= { .len
= sizeof(RDMARegisterResult
),
2920 .type
= RDMA_CONTROL_REGISTER_RESULT
,
2923 RDMAControlHeader unreg_resp
= { .len
= 0,
2924 .type
= RDMA_CONTROL_UNREGISTER_FINISHED
,
2927 RDMAControlHeader blocks
= { .type
= RDMA_CONTROL_RAM_BLOCKS_RESULT
,
2929 QEMUFileRDMA
*rfile
= opaque
;
2930 RDMAContext
*rdma
= rfile
->rdma
;
2931 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
2932 RDMAControlHeader head
;
2933 RDMARegister
*reg
, *registers
;
2935 RDMARegisterResult
*reg_result
;
2936 static RDMARegisterResult results
[RDMA_CONTROL_MAX_COMMANDS_PER_MESSAGE
];
2937 RDMALocalBlock
*block
;
2944 CHECK_ERROR_STATE();
2947 trace_qemu_rdma_registration_handle_wait(flags
);
2949 ret
= qemu_rdma_exchange_recv(rdma
, &head
, RDMA_CONTROL_NONE
);
2955 if (head
.repeat
> RDMA_CONTROL_MAX_COMMANDS_PER_MESSAGE
) {
2956 error_report("rdma: Too many requests in this message (%d)."
2957 "Bailing.", head
.repeat
);
2962 switch (head
.type
) {
2963 case RDMA_CONTROL_COMPRESS
:
2964 comp
= (RDMACompress
*) rdma
->wr_data
[idx
].control_curr
;
2965 network_to_compress(comp
);
2967 trace_qemu_rdma_registration_handle_compress(comp
->length
,
2970 block
= &(rdma
->local_ram_blocks
.block
[comp
->block_idx
]);
2972 host_addr
= block
->local_host_addr
+
2973 (comp
->offset
- block
->offset
);
2975 ram_handle_compressed(host_addr
, comp
->value
, comp
->length
);
2978 case RDMA_CONTROL_REGISTER_FINISHED
:
2979 trace_qemu_rdma_registration_handle_finished();
2982 case RDMA_CONTROL_RAM_BLOCKS_REQUEST
:
2983 trace_qemu_rdma_registration_handle_ram_blocks();
2985 if (rdma
->pin_all
) {
2986 ret
= qemu_rdma_reg_whole_ram_blocks(rdma
);
2988 error_report("rdma migration: error dest "
2989 "registering ram blocks");
2995 * Dest uses this to prepare to transmit the RAMBlock descriptions
2996 * to the source VM after connection setup.
2997 * Both sides use the "remote" structure to communicate and update
2998 * their "local" descriptions with what was sent.
3000 for (i
= 0; i
< local
->nb_blocks
; i
++) {
3001 rdma
->dest_blocks
[i
].remote_host_addr
=
3002 (uintptr_t)(local
->block
[i
].local_host_addr
);
3004 if (rdma
->pin_all
) {
3005 rdma
->dest_blocks
[i
].remote_rkey
= local
->block
[i
].mr
->rkey
;
3008 rdma
->dest_blocks
[i
].offset
= local
->block
[i
].offset
;
3009 rdma
->dest_blocks
[i
].length
= local
->block
[i
].length
;
3011 dest_block_to_network(&rdma
->dest_blocks
[i
]);
3014 blocks
.len
= rdma
->local_ram_blocks
.nb_blocks
3015 * sizeof(RDMADestBlock
);
3018 ret
= qemu_rdma_post_send_control(rdma
,
3019 (uint8_t *) rdma
->dest_blocks
, &blocks
);
3022 error_report("rdma migration: error sending remote info");
3027 case RDMA_CONTROL_REGISTER_REQUEST
:
3028 trace_qemu_rdma_registration_handle_register(head
.repeat
);
3030 reg_resp
.repeat
= head
.repeat
;
3031 registers
= (RDMARegister
*) rdma
->wr_data
[idx
].control_curr
;
3033 for (count
= 0; count
< head
.repeat
; count
++) {
3035 uint8_t *chunk_start
, *chunk_end
;
3037 reg
= ®isters
[count
];
3038 network_to_register(reg
);
3040 reg_result
= &results
[count
];
3042 trace_qemu_rdma_registration_handle_register_loop(count
,
3043 reg
->current_index
, reg
->key
.current_addr
, reg
->chunks
);
3045 block
= &(rdma
->local_ram_blocks
.block
[reg
->current_index
]);
3046 if (block
->is_ram_block
) {
3047 host_addr
= (block
->local_host_addr
+
3048 (reg
->key
.current_addr
- block
->offset
));
3049 chunk
= ram_chunk_index(block
->local_host_addr
,
3050 (uint8_t *) host_addr
);
3052 chunk
= reg
->key
.chunk
;
3053 host_addr
= block
->local_host_addr
+
3054 (reg
->key
.chunk
* (1UL << RDMA_REG_CHUNK_SHIFT
));
3056 chunk_start
= ram_chunk_start(block
, chunk
);
3057 chunk_end
= ram_chunk_end(block
, chunk
+ reg
->chunks
);
3058 if (qemu_rdma_register_and_get_keys(rdma
, block
,
3059 (uintptr_t)host_addr
, NULL
, ®_result
->rkey
,
3060 chunk
, chunk_start
, chunk_end
)) {
3061 error_report("cannot get rkey");
3066 reg_result
->host_addr
= (uintptr_t)block
->local_host_addr
;
3068 trace_qemu_rdma_registration_handle_register_rkey(
3071 result_to_network(reg_result
);
3074 ret
= qemu_rdma_post_send_control(rdma
,
3075 (uint8_t *) results
, ®_resp
);
3078 error_report("Failed to send control buffer");
3082 case RDMA_CONTROL_UNREGISTER_REQUEST
:
3083 trace_qemu_rdma_registration_handle_unregister(head
.repeat
);
3084 unreg_resp
.repeat
= head
.repeat
;
3085 registers
= (RDMARegister
*) rdma
->wr_data
[idx
].control_curr
;
3087 for (count
= 0; count
< head
.repeat
; count
++) {
3088 reg
= ®isters
[count
];
3089 network_to_register(reg
);
3091 trace_qemu_rdma_registration_handle_unregister_loop(count
,
3092 reg
->current_index
, reg
->key
.chunk
);
3094 block
= &(rdma
->local_ram_blocks
.block
[reg
->current_index
]);
3096 ret
= ibv_dereg_mr(block
->pmr
[reg
->key
.chunk
]);
3097 block
->pmr
[reg
->key
.chunk
] = NULL
;
3100 perror("rdma unregistration chunk failed");
3105 rdma
->total_registrations
--;
3107 trace_qemu_rdma_registration_handle_unregister_success(
3111 ret
= qemu_rdma_post_send_control(rdma
, NULL
, &unreg_resp
);
3114 error_report("Failed to send control buffer");
3118 case RDMA_CONTROL_REGISTER_RESULT
:
3119 error_report("Invalid RESULT message at dest.");
3123 error_report("Unknown control message %s", control_desc
[head
.type
]);
3130 rdma
->error_state
= ret
;
3135 static int qemu_rdma_registration_start(QEMUFile
*f
, void *opaque
,
3138 QEMUFileRDMA
*rfile
= opaque
;
3139 RDMAContext
*rdma
= rfile
->rdma
;
3141 CHECK_ERROR_STATE();
3143 trace_qemu_rdma_registration_start(flags
);
3144 qemu_put_be64(f
, RAM_SAVE_FLAG_HOOK
);
3151 * Inform dest that dynamic registrations are done for now.
3152 * First, flush writes, if any.
3154 static int qemu_rdma_registration_stop(QEMUFile
*f
, void *opaque
,
3157 Error
*local_err
= NULL
, **errp
= &local_err
;
3158 QEMUFileRDMA
*rfile
= opaque
;
3159 RDMAContext
*rdma
= rfile
->rdma
;
3160 RDMAControlHeader head
= { .len
= 0, .repeat
= 1 };
3163 CHECK_ERROR_STATE();
3166 ret
= qemu_rdma_drain_cq(f
, rdma
);
3172 if (flags
== RAM_CONTROL_SETUP
) {
3173 RDMAControlHeader resp
= {.type
= RDMA_CONTROL_RAM_BLOCKS_RESULT
};
3174 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
3175 int reg_result_idx
, i
, j
, nb_dest_blocks
;
3177 head
.type
= RDMA_CONTROL_RAM_BLOCKS_REQUEST
;
3178 trace_qemu_rdma_registration_stop_ram();
3181 * Make sure that we parallelize the pinning on both sides.
3182 * For very large guests, doing this serially takes a really
3183 * long time, so we have to 'interleave' the pinning locally
3184 * with the control messages by performing the pinning on this
3185 * side before we receive the control response from the other
3186 * side that the pinning has completed.
3188 ret
= qemu_rdma_exchange_send(rdma
, &head
, NULL
, &resp
,
3189 ®_result_idx
, rdma
->pin_all
?
3190 qemu_rdma_reg_whole_ram_blocks
: NULL
);
3192 ERROR(errp
, "receiving remote info!");
3196 nb_dest_blocks
= resp
.len
/ sizeof(RDMADestBlock
);
3199 * The protocol uses two different sets of rkeys (mutually exclusive):
3200 * 1. One key to represent the virtual address of the entire ram block.
3201 * (dynamic chunk registration disabled - pin everything with one rkey.)
3202 * 2. One to represent individual chunks within a ram block.
3203 * (dynamic chunk registration enabled - pin individual chunks.)
3205 * Once the capability is successfully negotiated, the destination transmits
3206 * the keys to use (or sends them later) including the virtual addresses
3207 * and then propagates the remote ram block descriptions to his local copy.
3210 if (local
->nb_blocks
!= nb_dest_blocks
) {
3211 ERROR(errp
, "ram blocks mismatch #1! "
3212 "Your QEMU command line parameters are probably "
3213 "not identical on both the source and destination.");
3217 qemu_rdma_move_header(rdma
, reg_result_idx
, &resp
);
3218 memcpy(rdma
->dest_blocks
,
3219 rdma
->wr_data
[reg_result_idx
].control_curr
, resp
.len
);
3220 for (i
= 0; i
< nb_dest_blocks
; i
++) {
3221 network_to_dest_block(&rdma
->dest_blocks
[i
]);
3223 /* search local ram blocks */
3224 for (j
= 0; j
< local
->nb_blocks
; j
++) {
3225 if (rdma
->dest_blocks
[i
].offset
!= local
->block
[j
].offset
) {
3229 if (rdma
->dest_blocks
[i
].length
!= local
->block
[j
].length
) {
3230 ERROR(errp
, "ram blocks mismatch #2! "
3231 "Your QEMU command line parameters are probably "
3232 "not identical on both the source and destination.");
3235 local
->block
[j
].remote_host_addr
=
3236 rdma
->dest_blocks
[i
].remote_host_addr
;
3237 local
->block
[j
].remote_rkey
= rdma
->dest_blocks
[i
].remote_rkey
;
3241 if (j
>= local
->nb_blocks
) {
3242 ERROR(errp
, "ram blocks mismatch #3! "
3243 "Your QEMU command line parameters are probably "
3244 "not identical on both the source and destination.");
3250 trace_qemu_rdma_registration_stop(flags
);
3252 head
.type
= RDMA_CONTROL_REGISTER_FINISHED
;
3253 ret
= qemu_rdma_exchange_send(rdma
, &head
, NULL
, NULL
, NULL
, NULL
);
3261 rdma
->error_state
= ret
;
3265 static int qemu_rdma_get_fd(void *opaque
)
3267 QEMUFileRDMA
*rfile
= opaque
;
3268 RDMAContext
*rdma
= rfile
->rdma
;
3270 return rdma
->comp_channel
->fd
;
3273 static const QEMUFileOps rdma_read_ops
= {
3274 .get_buffer
= qemu_rdma_get_buffer
,
3275 .get_fd
= qemu_rdma_get_fd
,
3276 .close
= qemu_rdma_close
,
3277 .hook_ram_load
= qemu_rdma_registration_handle
,
3280 static const QEMUFileOps rdma_write_ops
= {
3281 .put_buffer
= qemu_rdma_put_buffer
,
3282 .close
= qemu_rdma_close
,
3283 .before_ram_iterate
= qemu_rdma_registration_start
,
3284 .after_ram_iterate
= qemu_rdma_registration_stop
,
3285 .save_page
= qemu_rdma_save_page
,
3288 static void *qemu_fopen_rdma(RDMAContext
*rdma
, const char *mode
)
3292 if (qemu_file_mode_is_not_valid(mode
)) {
3296 r
= g_malloc0(sizeof(QEMUFileRDMA
));
3299 if (mode
[0] == 'w') {
3300 r
->file
= qemu_fopen_ops(r
, &rdma_write_ops
);
3302 r
->file
= qemu_fopen_ops(r
, &rdma_read_ops
);
3308 static void rdma_accept_incoming_migration(void *opaque
)
3310 RDMAContext
*rdma
= opaque
;
3313 Error
*local_err
= NULL
, **errp
= &local_err
;
3315 trace_qemu_rdma_accept_incoming_migration();
3316 ret
= qemu_rdma_accept(rdma
);
3319 ERROR(errp
, "RDMA Migration initialization failed!");
3323 trace_qemu_rdma_accept_incoming_migration_accepted();
3325 f
= qemu_fopen_rdma(rdma
, "rb");
3327 ERROR(errp
, "could not qemu_fopen_rdma!");
3328 qemu_rdma_cleanup(rdma
);
3332 rdma
->migration_started_on_destination
= 1;
3333 process_incoming_migration(f
);
3336 void rdma_start_incoming_migration(const char *host_port
, Error
**errp
)
3340 Error
*local_err
= NULL
;
3342 trace_rdma_start_incoming_migration();
3343 rdma
= qemu_rdma_data_init(host_port
, &local_err
);
3349 ret
= qemu_rdma_dest_init(rdma
, &local_err
);
3355 trace_rdma_start_incoming_migration_after_dest_init();
3357 ret
= rdma_listen(rdma
->listen_id
, 5);
3360 ERROR(errp
, "listening on socket!");
3364 trace_rdma_start_incoming_migration_after_rdma_listen();
3366 qemu_set_fd_handler(rdma
->channel
->fd
, rdma_accept_incoming_migration
,
3367 NULL
, (void *)(intptr_t)rdma
);
3370 error_propagate(errp
, local_err
);
3374 void rdma_start_outgoing_migration(void *opaque
,
3375 const char *host_port
, Error
**errp
)
3377 MigrationState
*s
= opaque
;
3378 Error
*local_err
= NULL
, **temp
= &local_err
;
3379 RDMAContext
*rdma
= qemu_rdma_data_init(host_port
, &local_err
);
3383 ERROR(temp
, "Failed to initialize RDMA data structures! %d", ret
);
3387 ret
= qemu_rdma_source_init(rdma
, &local_err
,
3388 s
->enabled_capabilities
[MIGRATION_CAPABILITY_RDMA_PIN_ALL
]);
3394 trace_rdma_start_outgoing_migration_after_rdma_source_init();
3395 ret
= qemu_rdma_connect(rdma
, &local_err
);
3401 trace_rdma_start_outgoing_migration_after_rdma_connect();
3403 s
->file
= qemu_fopen_rdma(rdma
, "wb");
3404 migrate_fd_connect(s
);
3407 error_propagate(errp
, local_err
);
3409 migrate_fd_error(s
);