2 * RDMA protocol and interfaces
4 * Copyright IBM, Corp. 2010-2013
5 * Copyright Red Hat, Inc. 2015-2016
8 * Michael R. Hines <mrhines@us.ibm.com>
9 * Jiuxing Liu <jl@us.ibm.com>
10 * Daniel P. Berrange <berrange@redhat.com>
12 * This work is licensed under the terms of the GNU GPL, version 2 or
13 * later. See the COPYING file in the top-level directory.
17 #include "qemu/osdep.h"
18 #include "qapi/error.h"
19 #include "qemu/cutils.h"
20 #include "exec/target_page.h"
22 #include "migration.h"
23 #include "migration-stats.h"
24 #include "qemu-file.h"
26 #include "qemu/error-report.h"
27 #include "qemu/main-loop.h"
28 #include "qemu/module.h"
30 #include "qemu/sockets.h"
31 #include "qemu/bitmap.h"
32 #include "qemu/coroutine.h"
33 #include "exec/memory.h"
34 #include <sys/socket.h>
36 #include <arpa/inet.h>
37 #include <rdma/rdma_cma.h>
39 #include "qom/object.h"
44 * Print and error on both the Monitor and the Log file.
46 #define ERROR(errp, fmt, ...) \
48 fprintf(stderr, "RDMA ERROR: " fmt "\n", ## __VA_ARGS__); \
49 if (errp && (*(errp) == NULL)) { \
50 error_setg(errp, "RDMA ERROR: " fmt, ## __VA_ARGS__); \
54 #define RDMA_RESOLVE_TIMEOUT_MS 10000
56 /* Do not merge data if larger than this. */
57 #define RDMA_MERGE_MAX (2 * 1024 * 1024)
58 #define RDMA_SIGNALED_SEND_MAX (RDMA_MERGE_MAX / 4096)
60 #define RDMA_REG_CHUNK_SHIFT 20 /* 1 MB */
63 * This is only for non-live state being migrated.
64 * Instead of RDMA_WRITE messages, we use RDMA_SEND
65 * messages for that state, which requires a different
66 * delivery design than main memory.
68 #define RDMA_SEND_INCREMENT 32768
71 * Maximum size infiniband SEND message
73 #define RDMA_CONTROL_MAX_BUFFER (512 * 1024)
74 #define RDMA_CONTROL_MAX_COMMANDS_PER_MESSAGE 4096
76 #define RDMA_CONTROL_VERSION_CURRENT 1
78 * Capabilities for negotiation.
80 #define RDMA_CAPABILITY_PIN_ALL 0x01
83 * Add the other flags above to this list of known capabilities
84 * as they are introduced.
86 static uint32_t known_capabilities
= RDMA_CAPABILITY_PIN_ALL
;
88 #define CHECK_ERROR_STATE() \
90 if (rdma->error_state) { \
91 if (!rdma->error_reported) { \
92 error_report("RDMA is in an error state waiting migration" \
94 rdma->error_reported = 1; \
96 return rdma->error_state; \
101 * A work request ID is 64-bits and we split up these bits
104 * bits 0-15 : type of control message, 2^16
105 * bits 16-29: ram block index, 2^14
106 * bits 30-63: ram block chunk number, 2^34
108 * The last two bit ranges are only used for RDMA writes,
109 * in order to track their completion and potentially
110 * also track unregistration status of the message.
112 #define RDMA_WRID_TYPE_SHIFT 0UL
113 #define RDMA_WRID_BLOCK_SHIFT 16UL
114 #define RDMA_WRID_CHUNK_SHIFT 30UL
116 #define RDMA_WRID_TYPE_MASK \
117 ((1UL << RDMA_WRID_BLOCK_SHIFT) - 1UL)
119 #define RDMA_WRID_BLOCK_MASK \
120 (~RDMA_WRID_TYPE_MASK & ((1UL << RDMA_WRID_CHUNK_SHIFT) - 1UL))
122 #define RDMA_WRID_CHUNK_MASK (~RDMA_WRID_BLOCK_MASK & ~RDMA_WRID_TYPE_MASK)
125 * RDMA migration protocol:
126 * 1. RDMA Writes (data messages, i.e. RAM)
127 * 2. IB Send/Recv (control channel messages)
131 RDMA_WRID_RDMA_WRITE
= 1,
132 RDMA_WRID_SEND_CONTROL
= 2000,
133 RDMA_WRID_RECV_CONTROL
= 4000,
137 * Work request IDs for IB SEND messages only (not RDMA writes).
138 * This is used by the migration protocol to transmit
139 * control messages (such as device state and registration commands)
141 * We could use more WRs, but we have enough for now.
151 * SEND/RECV IB Control Messages.
154 RDMA_CONTROL_NONE
= 0,
156 RDMA_CONTROL_READY
, /* ready to receive */
157 RDMA_CONTROL_QEMU_FILE
, /* QEMUFile-transmitted bytes */
158 RDMA_CONTROL_RAM_BLOCKS_REQUEST
, /* RAMBlock synchronization */
159 RDMA_CONTROL_RAM_BLOCKS_RESULT
, /* RAMBlock synchronization */
160 RDMA_CONTROL_COMPRESS
, /* page contains repeat values */
161 RDMA_CONTROL_REGISTER_REQUEST
, /* dynamic page registration */
162 RDMA_CONTROL_REGISTER_RESULT
, /* key to use after registration */
163 RDMA_CONTROL_REGISTER_FINISHED
, /* current iteration finished */
164 RDMA_CONTROL_UNREGISTER_REQUEST
, /* dynamic UN-registration */
165 RDMA_CONTROL_UNREGISTER_FINISHED
, /* unpinning finished */
170 * Memory and MR structures used to represent an IB Send/Recv work request.
171 * This is *not* used for RDMA writes, only IB Send/Recv.
174 uint8_t control
[RDMA_CONTROL_MAX_BUFFER
]; /* actual buffer to register */
175 struct ibv_mr
*control_mr
; /* registration metadata */
176 size_t control_len
; /* length of the message */
177 uint8_t *control_curr
; /* start of unconsumed bytes */
178 } RDMAWorkRequestData
;
181 * Negotiate RDMA capabilities during connection-setup time.
188 static void caps_to_network(RDMACapabilities
*cap
)
190 cap
->version
= htonl(cap
->version
);
191 cap
->flags
= htonl(cap
->flags
);
194 static void network_to_caps(RDMACapabilities
*cap
)
196 cap
->version
= ntohl(cap
->version
);
197 cap
->flags
= ntohl(cap
->flags
);
201 * Representation of a RAMBlock from an RDMA perspective.
202 * This is not transmitted, only local.
203 * This and subsequent structures cannot be linked lists
204 * because we're using a single IB message to transmit
205 * the information. It's small anyway, so a list is overkill.
207 typedef struct RDMALocalBlock
{
209 uint8_t *local_host_addr
; /* local virtual address */
210 uint64_t remote_host_addr
; /* remote virtual address */
213 struct ibv_mr
**pmr
; /* MRs for chunk-level registration */
214 struct ibv_mr
*mr
; /* MR for non-chunk-level registration */
215 uint32_t *remote_keys
; /* rkeys for chunk-level registration */
216 uint32_t remote_rkey
; /* rkeys for non-chunk-level registration */
217 int index
; /* which block are we */
218 unsigned int src_index
; /* (Only used on dest) */
221 unsigned long *transit_bitmap
;
222 unsigned long *unregister_bitmap
;
226 * Also represents a RAMblock, but only on the dest.
227 * This gets transmitted by the dest during connection-time
228 * to the source VM and then is used to populate the
229 * corresponding RDMALocalBlock with
230 * the information needed to perform the actual RDMA.
232 typedef struct QEMU_PACKED RDMADestBlock
{
233 uint64_t remote_host_addr
;
236 uint32_t remote_rkey
;
240 static const char *control_desc(unsigned int rdma_control
)
242 static const char *strs
[] = {
243 [RDMA_CONTROL_NONE
] = "NONE",
244 [RDMA_CONTROL_ERROR
] = "ERROR",
245 [RDMA_CONTROL_READY
] = "READY",
246 [RDMA_CONTROL_QEMU_FILE
] = "QEMU FILE",
247 [RDMA_CONTROL_RAM_BLOCKS_REQUEST
] = "RAM BLOCKS REQUEST",
248 [RDMA_CONTROL_RAM_BLOCKS_RESULT
] = "RAM BLOCKS RESULT",
249 [RDMA_CONTROL_COMPRESS
] = "COMPRESS",
250 [RDMA_CONTROL_REGISTER_REQUEST
] = "REGISTER REQUEST",
251 [RDMA_CONTROL_REGISTER_RESULT
] = "REGISTER RESULT",
252 [RDMA_CONTROL_REGISTER_FINISHED
] = "REGISTER FINISHED",
253 [RDMA_CONTROL_UNREGISTER_REQUEST
] = "UNREGISTER REQUEST",
254 [RDMA_CONTROL_UNREGISTER_FINISHED
] = "UNREGISTER FINISHED",
257 if (rdma_control
> RDMA_CONTROL_UNREGISTER_FINISHED
) {
258 return "??BAD CONTROL VALUE??";
261 return strs
[rdma_control
];
264 static uint64_t htonll(uint64_t v
)
266 union { uint32_t lv
[2]; uint64_t llv
; } u
;
267 u
.lv
[0] = htonl(v
>> 32);
268 u
.lv
[1] = htonl(v
& 0xFFFFFFFFULL
);
272 static uint64_t ntohll(uint64_t v
)
274 union { uint32_t lv
[2]; uint64_t llv
; } u
;
276 return ((uint64_t)ntohl(u
.lv
[0]) << 32) | (uint64_t) ntohl(u
.lv
[1]);
279 static void dest_block_to_network(RDMADestBlock
*db
)
281 db
->remote_host_addr
= htonll(db
->remote_host_addr
);
282 db
->offset
= htonll(db
->offset
);
283 db
->length
= htonll(db
->length
);
284 db
->remote_rkey
= htonl(db
->remote_rkey
);
287 static void network_to_dest_block(RDMADestBlock
*db
)
289 db
->remote_host_addr
= ntohll(db
->remote_host_addr
);
290 db
->offset
= ntohll(db
->offset
);
291 db
->length
= ntohll(db
->length
);
292 db
->remote_rkey
= ntohl(db
->remote_rkey
);
296 * Virtual address of the above structures used for transmitting
297 * the RAMBlock descriptions at connection-time.
298 * This structure is *not* transmitted.
300 typedef struct RDMALocalBlocks
{
302 bool init
; /* main memory init complete */
303 RDMALocalBlock
*block
;
307 * Main data structure for RDMA state.
308 * While there is only one copy of this structure being allocated right now,
309 * this is the place where one would start if you wanted to consider
310 * having more than one RDMA connection open at the same time.
312 typedef struct RDMAContext
{
317 RDMAWorkRequestData wr_data
[RDMA_WRID_MAX
];
320 * This is used by *_exchange_send() to figure out whether or not
321 * the initial "READY" message has already been received or not.
322 * This is because other functions may potentially poll() and detect
323 * the READY message before send() does, in which case we need to
324 * know if it completed.
326 int control_ready_expected
;
328 /* number of outstanding writes */
331 /* store info about current buffer so that we can
332 merge it with future sends */
333 uint64_t current_addr
;
334 uint64_t current_length
;
335 /* index of ram block the current buffer belongs to */
337 /* index of the chunk in the current ram block */
343 * infiniband-specific variables for opening the device
344 * and maintaining connection state and so forth.
346 * cm_id also has ibv_context, rdma_event_channel, and ibv_qp in
347 * cm_id->verbs, cm_id->channel, and cm_id->qp.
349 struct rdma_cm_id
*cm_id
; /* connection manager ID */
350 struct rdma_cm_id
*listen_id
;
353 struct ibv_context
*verbs
;
354 struct rdma_event_channel
*channel
;
355 struct ibv_qp
*qp
; /* queue pair */
356 struct ibv_comp_channel
*recv_comp_channel
; /* recv completion channel */
357 struct ibv_comp_channel
*send_comp_channel
; /* send completion channel */
358 struct ibv_pd
*pd
; /* protection domain */
359 struct ibv_cq
*recv_cq
; /* recvieve completion queue */
360 struct ibv_cq
*send_cq
; /* send completion queue */
363 * If a previous write failed (perhaps because of a failed
364 * memory registration, then do not attempt any future work
365 * and remember the error state.
372 * Description of ram blocks used throughout the code.
374 RDMALocalBlocks local_ram_blocks
;
375 RDMADestBlock
*dest_blocks
;
377 /* Index of the next RAMBlock received during block registration */
378 unsigned int next_src_index
;
381 * Migration on *destination* started.
382 * Then use coroutine yield function.
383 * Source runs in a thread, so we don't care.
385 int migration_started_on_destination
;
387 int total_registrations
;
390 int unregister_current
, unregister_next
;
391 uint64_t unregistrations
[RDMA_SIGNALED_SEND_MAX
];
393 GHashTable
*blockmap
;
395 /* the RDMAContext for return path */
396 struct RDMAContext
*return_path
;
400 #define TYPE_QIO_CHANNEL_RDMA "qio-channel-rdma"
401 OBJECT_DECLARE_SIMPLE_TYPE(QIOChannelRDMA
, QIO_CHANNEL_RDMA
)
405 struct QIOChannelRDMA
{
408 RDMAContext
*rdmaout
;
410 bool blocking
; /* XXX we don't actually honour this yet */
414 * Main structure for IB Send/Recv control messages.
415 * This gets prepended at the beginning of every Send/Recv.
417 typedef struct QEMU_PACKED
{
418 uint32_t len
; /* Total length of data portion */
419 uint32_t type
; /* which control command to perform */
420 uint32_t repeat
; /* number of commands in data portion of same type */
424 static void control_to_network(RDMAControlHeader
*control
)
426 control
->type
= htonl(control
->type
);
427 control
->len
= htonl(control
->len
);
428 control
->repeat
= htonl(control
->repeat
);
431 static void network_to_control(RDMAControlHeader
*control
)
433 control
->type
= ntohl(control
->type
);
434 control
->len
= ntohl(control
->len
);
435 control
->repeat
= ntohl(control
->repeat
);
439 * Register a single Chunk.
440 * Information sent by the source VM to inform the dest
441 * to register an single chunk of memory before we can perform
442 * the actual RDMA operation.
444 typedef struct QEMU_PACKED
{
446 uint64_t current_addr
; /* offset into the ram_addr_t space */
447 uint64_t chunk
; /* chunk to lookup if unregistering */
449 uint32_t current_index
; /* which ramblock the chunk belongs to */
451 uint64_t chunks
; /* how many sequential chunks to register */
454 static void register_to_network(RDMAContext
*rdma
, RDMARegister
*reg
)
456 RDMALocalBlock
*local_block
;
457 local_block
= &rdma
->local_ram_blocks
.block
[reg
->current_index
];
459 if (local_block
->is_ram_block
) {
461 * current_addr as passed in is an address in the local ram_addr_t
462 * space, we need to translate this for the destination
464 reg
->key
.current_addr
-= local_block
->offset
;
465 reg
->key
.current_addr
+= rdma
->dest_blocks
[reg
->current_index
].offset
;
467 reg
->key
.current_addr
= htonll(reg
->key
.current_addr
);
468 reg
->current_index
= htonl(reg
->current_index
);
469 reg
->chunks
= htonll(reg
->chunks
);
472 static void network_to_register(RDMARegister
*reg
)
474 reg
->key
.current_addr
= ntohll(reg
->key
.current_addr
);
475 reg
->current_index
= ntohl(reg
->current_index
);
476 reg
->chunks
= ntohll(reg
->chunks
);
479 typedef struct QEMU_PACKED
{
480 uint32_t value
; /* if zero, we will madvise() */
481 uint32_t block_idx
; /* which ram block index */
482 uint64_t offset
; /* Address in remote ram_addr_t space */
483 uint64_t length
; /* length of the chunk */
486 static void compress_to_network(RDMAContext
*rdma
, RDMACompress
*comp
)
488 comp
->value
= htonl(comp
->value
);
490 * comp->offset as passed in is an address in the local ram_addr_t
491 * space, we need to translate this for the destination
493 comp
->offset
-= rdma
->local_ram_blocks
.block
[comp
->block_idx
].offset
;
494 comp
->offset
+= rdma
->dest_blocks
[comp
->block_idx
].offset
;
495 comp
->block_idx
= htonl(comp
->block_idx
);
496 comp
->offset
= htonll(comp
->offset
);
497 comp
->length
= htonll(comp
->length
);
500 static void network_to_compress(RDMACompress
*comp
)
502 comp
->value
= ntohl(comp
->value
);
503 comp
->block_idx
= ntohl(comp
->block_idx
);
504 comp
->offset
= ntohll(comp
->offset
);
505 comp
->length
= ntohll(comp
->length
);
509 * The result of the dest's memory registration produces an "rkey"
510 * which the source VM must reference in order to perform
511 * the RDMA operation.
513 typedef struct QEMU_PACKED
{
517 } RDMARegisterResult
;
519 static void result_to_network(RDMARegisterResult
*result
)
521 result
->rkey
= htonl(result
->rkey
);
522 result
->host_addr
= htonll(result
->host_addr
);
525 static void network_to_result(RDMARegisterResult
*result
)
527 result
->rkey
= ntohl(result
->rkey
);
528 result
->host_addr
= ntohll(result
->host_addr
);
531 static int qemu_rdma_exchange_send(RDMAContext
*rdma
, RDMAControlHeader
*head
,
532 uint8_t *data
, RDMAControlHeader
*resp
,
534 int (*callback
)(RDMAContext
*rdma
));
536 static inline uint64_t ram_chunk_index(const uint8_t *start
,
539 return ((uintptr_t) host
- (uintptr_t) start
) >> RDMA_REG_CHUNK_SHIFT
;
542 static inline uint8_t *ram_chunk_start(const RDMALocalBlock
*rdma_ram_block
,
545 return (uint8_t *)(uintptr_t)(rdma_ram_block
->local_host_addr
+
546 (i
<< RDMA_REG_CHUNK_SHIFT
));
549 static inline uint8_t *ram_chunk_end(const RDMALocalBlock
*rdma_ram_block
,
552 uint8_t *result
= ram_chunk_start(rdma_ram_block
, i
) +
553 (1UL << RDMA_REG_CHUNK_SHIFT
);
555 if (result
> (rdma_ram_block
->local_host_addr
+ rdma_ram_block
->length
)) {
556 result
= rdma_ram_block
->local_host_addr
+ rdma_ram_block
->length
;
562 static void rdma_add_block(RDMAContext
*rdma
, const char *block_name
,
564 ram_addr_t block_offset
, uint64_t length
)
566 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
567 RDMALocalBlock
*block
;
568 RDMALocalBlock
*old
= local
->block
;
570 local
->block
= g_new0(RDMALocalBlock
, local
->nb_blocks
+ 1);
572 if (local
->nb_blocks
) {
575 if (rdma
->blockmap
) {
576 for (x
= 0; x
< local
->nb_blocks
; x
++) {
577 g_hash_table_remove(rdma
->blockmap
,
578 (void *)(uintptr_t)old
[x
].offset
);
579 g_hash_table_insert(rdma
->blockmap
,
580 (void *)(uintptr_t)old
[x
].offset
,
584 memcpy(local
->block
, old
, sizeof(RDMALocalBlock
) * local
->nb_blocks
);
588 block
= &local
->block
[local
->nb_blocks
];
590 block
->block_name
= g_strdup(block_name
);
591 block
->local_host_addr
= host_addr
;
592 block
->offset
= block_offset
;
593 block
->length
= length
;
594 block
->index
= local
->nb_blocks
;
595 block
->src_index
= ~0U; /* Filled in by the receipt of the block list */
596 block
->nb_chunks
= ram_chunk_index(host_addr
, host_addr
+ length
) + 1UL;
597 block
->transit_bitmap
= bitmap_new(block
->nb_chunks
);
598 bitmap_clear(block
->transit_bitmap
, 0, block
->nb_chunks
);
599 block
->unregister_bitmap
= bitmap_new(block
->nb_chunks
);
600 bitmap_clear(block
->unregister_bitmap
, 0, block
->nb_chunks
);
601 block
->remote_keys
= g_new0(uint32_t, block
->nb_chunks
);
603 block
->is_ram_block
= local
->init
? false : true;
605 if (rdma
->blockmap
) {
606 g_hash_table_insert(rdma
->blockmap
, (void *)(uintptr_t)block_offset
, block
);
609 trace_rdma_add_block(block_name
, local
->nb_blocks
,
610 (uintptr_t) block
->local_host_addr
,
611 block
->offset
, block
->length
,
612 (uintptr_t) (block
->local_host_addr
+ block
->length
),
613 BITS_TO_LONGS(block
->nb_chunks
) *
614 sizeof(unsigned long) * 8,
621 * Memory regions need to be registered with the device and queue pairs setup
622 * in advanced before the migration starts. This tells us where the RAM blocks
623 * are so that we can register them individually.
625 static int qemu_rdma_init_one_block(RAMBlock
*rb
, void *opaque
)
627 const char *block_name
= qemu_ram_get_idstr(rb
);
628 void *host_addr
= qemu_ram_get_host_addr(rb
);
629 ram_addr_t block_offset
= qemu_ram_get_offset(rb
);
630 ram_addr_t length
= qemu_ram_get_used_length(rb
);
631 rdma_add_block(opaque
, block_name
, host_addr
, block_offset
, length
);
636 * Identify the RAMBlocks and their quantity. They will be references to
637 * identify chunk boundaries inside each RAMBlock and also be referenced
638 * during dynamic page registration.
640 static void qemu_rdma_init_ram_blocks(RDMAContext
*rdma
)
642 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
645 assert(rdma
->blockmap
== NULL
);
646 memset(local
, 0, sizeof *local
);
647 ret
= foreach_not_ignored_block(qemu_rdma_init_one_block
, rdma
);
649 trace_qemu_rdma_init_ram_blocks(local
->nb_blocks
);
650 rdma
->dest_blocks
= g_new0(RDMADestBlock
,
651 rdma
->local_ram_blocks
.nb_blocks
);
656 * Note: If used outside of cleanup, the caller must ensure that the destination
657 * block structures are also updated
659 static void rdma_delete_block(RDMAContext
*rdma
, RDMALocalBlock
*block
)
661 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
662 RDMALocalBlock
*old
= local
->block
;
665 if (rdma
->blockmap
) {
666 g_hash_table_remove(rdma
->blockmap
, (void *)(uintptr_t)block
->offset
);
671 for (j
= 0; j
< block
->nb_chunks
; j
++) {
672 if (!block
->pmr
[j
]) {
675 ibv_dereg_mr(block
->pmr
[j
]);
676 rdma
->total_registrations
--;
683 ibv_dereg_mr(block
->mr
);
684 rdma
->total_registrations
--;
688 g_free(block
->transit_bitmap
);
689 block
->transit_bitmap
= NULL
;
691 g_free(block
->unregister_bitmap
);
692 block
->unregister_bitmap
= NULL
;
694 g_free(block
->remote_keys
);
695 block
->remote_keys
= NULL
;
697 g_free(block
->block_name
);
698 block
->block_name
= NULL
;
700 if (rdma
->blockmap
) {
701 for (x
= 0; x
< local
->nb_blocks
; x
++) {
702 g_hash_table_remove(rdma
->blockmap
,
703 (void *)(uintptr_t)old
[x
].offset
);
707 if (local
->nb_blocks
> 1) {
709 local
->block
= g_new0(RDMALocalBlock
, local
->nb_blocks
- 1);
712 memcpy(local
->block
, old
, sizeof(RDMALocalBlock
) * block
->index
);
715 if (block
->index
< (local
->nb_blocks
- 1)) {
716 memcpy(local
->block
+ block
->index
, old
+ (block
->index
+ 1),
717 sizeof(RDMALocalBlock
) *
718 (local
->nb_blocks
- (block
->index
+ 1)));
719 for (x
= block
->index
; x
< local
->nb_blocks
- 1; x
++) {
720 local
->block
[x
].index
--;
724 assert(block
== local
->block
);
728 trace_rdma_delete_block(block
, (uintptr_t)block
->local_host_addr
,
729 block
->offset
, block
->length
,
730 (uintptr_t)(block
->local_host_addr
+ block
->length
),
731 BITS_TO_LONGS(block
->nb_chunks
) *
732 sizeof(unsigned long) * 8, block
->nb_chunks
);
738 if (local
->nb_blocks
&& rdma
->blockmap
) {
739 for (x
= 0; x
< local
->nb_blocks
; x
++) {
740 g_hash_table_insert(rdma
->blockmap
,
741 (void *)(uintptr_t)local
->block
[x
].offset
,
748 * Put in the log file which RDMA device was opened and the details
749 * associated with that device.
751 static void qemu_rdma_dump_id(const char *who
, struct ibv_context
*verbs
)
753 struct ibv_port_attr port
;
755 if (ibv_query_port(verbs
, 1, &port
)) {
756 error_report("Failed to query port information");
760 printf("%s RDMA Device opened: kernel name %s "
761 "uverbs device name %s, "
762 "infiniband_verbs class device path %s, "
763 "infiniband class device path %s, "
764 "transport: (%d) %s\n",
767 verbs
->device
->dev_name
,
768 verbs
->device
->dev_path
,
769 verbs
->device
->ibdev_path
,
771 (port
.link_layer
== IBV_LINK_LAYER_INFINIBAND
) ? "Infiniband" :
772 ((port
.link_layer
== IBV_LINK_LAYER_ETHERNET
)
773 ? "Ethernet" : "Unknown"));
777 * Put in the log file the RDMA gid addressing information,
778 * useful for folks who have trouble understanding the
779 * RDMA device hierarchy in the kernel.
781 static void qemu_rdma_dump_gid(const char *who
, struct rdma_cm_id
*id
)
785 inet_ntop(AF_INET6
, &id
->route
.addr
.addr
.ibaddr
.sgid
, sgid
, sizeof sgid
);
786 inet_ntop(AF_INET6
, &id
->route
.addr
.addr
.ibaddr
.dgid
, dgid
, sizeof dgid
);
787 trace_qemu_rdma_dump_gid(who
, sgid
, dgid
);
791 * As of now, IPv6 over RoCE / iWARP is not supported by linux.
792 * We will try the next addrinfo struct, and fail if there are
793 * no other valid addresses to bind against.
795 * If user is listening on '[::]', then we will not have a opened a device
796 * yet and have no way of verifying if the device is RoCE or not.
798 * In this case, the source VM will throw an error for ALL types of
799 * connections (both IPv4 and IPv6) if the destination machine does not have
800 * a regular infiniband network available for use.
802 * The only way to guarantee that an error is thrown for broken kernels is
803 * for the management software to choose a *specific* interface at bind time
804 * and validate what time of hardware it is.
806 * Unfortunately, this puts the user in a fix:
808 * If the source VM connects with an IPv4 address without knowing that the
809 * destination has bound to '[::]' the migration will unconditionally fail
810 * unless the management software is explicitly listening on the IPv4
811 * address while using a RoCE-based device.
813 * If the source VM connects with an IPv6 address, then we're OK because we can
814 * throw an error on the source (and similarly on the destination).
816 * But in mixed environments, this will be broken for a while until it is fixed
819 * We do provide a *tiny* bit of help in this function: We can list all of the
820 * devices in the system and check to see if all the devices are RoCE or
823 * If we detect that we have a *pure* RoCE environment, then we can safely
824 * thrown an error even if the management software has specified '[::]' as the
827 * However, if there is are multiple hetergeneous devices, then we cannot make
828 * this assumption and the user just has to be sure they know what they are
831 * Patches are being reviewed on linux-rdma.
833 static int qemu_rdma_broken_ipv6_kernel(struct ibv_context
*verbs
, Error
**errp
)
835 /* This bug only exists in linux, to our knowledge. */
837 struct ibv_port_attr port_attr
;
840 * Verbs are only NULL if management has bound to '[::]'.
842 * Let's iterate through all the devices and see if there any pure IB
843 * devices (non-ethernet).
845 * If not, then we can safely proceed with the migration.
846 * Otherwise, there are no guarantees until the bug is fixed in linux.
850 struct ibv_device
**dev_list
= ibv_get_device_list(&num_devices
);
851 bool roce_found
= false;
852 bool ib_found
= false;
854 for (x
= 0; x
< num_devices
; x
++) {
855 verbs
= ibv_open_device(dev_list
[x
]);
857 if (errno
== EPERM
) {
864 if (ibv_query_port(verbs
, 1, &port_attr
)) {
865 ibv_close_device(verbs
);
866 ERROR(errp
, "Could not query initial IB port");
870 if (port_attr
.link_layer
== IBV_LINK_LAYER_INFINIBAND
) {
872 } else if (port_attr
.link_layer
== IBV_LINK_LAYER_ETHERNET
) {
876 ibv_close_device(verbs
);
882 fprintf(stderr
, "WARN: migrations may fail:"
883 " IPv6 over RoCE / iWARP in linux"
884 " is broken. But since you appear to have a"
885 " mixed RoCE / IB environment, be sure to only"
886 " migrate over the IB fabric until the kernel "
887 " fixes the bug.\n");
889 ERROR(errp
, "You only have RoCE / iWARP devices in your systems"
890 " and your management software has specified '[::]'"
891 ", but IPv6 over RoCE / iWARP is not supported in Linux.");
900 * If we have a verbs context, that means that some other than '[::]' was
901 * used by the management software for binding. In which case we can
902 * actually warn the user about a potentially broken kernel.
905 /* IB ports start with 1, not 0 */
906 if (ibv_query_port(verbs
, 1, &port_attr
)) {
907 ERROR(errp
, "Could not query initial IB port");
911 if (port_attr
.link_layer
== IBV_LINK_LAYER_ETHERNET
) {
912 ERROR(errp
, "Linux kernel's RoCE / iWARP does not support IPv6 "
913 "(but patches on linux-rdma in progress)");
923 * Figure out which RDMA device corresponds to the requested IP hostname
924 * Also create the initial connection manager identifiers for opening
927 static int qemu_rdma_resolve_host(RDMAContext
*rdma
, Error
**errp
)
930 struct rdma_addrinfo
*res
;
932 struct rdma_cm_event
*cm_event
;
933 char ip
[40] = "unknown";
934 struct rdma_addrinfo
*e
;
936 if (rdma
->host
== NULL
|| !strcmp(rdma
->host
, "")) {
937 ERROR(errp
, "RDMA hostname has not been set");
941 /* create CM channel */
942 rdma
->channel
= rdma_create_event_channel();
943 if (!rdma
->channel
) {
944 ERROR(errp
, "could not create CM channel");
949 ret
= rdma_create_id(rdma
->channel
, &rdma
->cm_id
, NULL
, RDMA_PS_TCP
);
951 ERROR(errp
, "could not create channel id");
952 goto err_resolve_create_id
;
955 snprintf(port_str
, 16, "%d", rdma
->port
);
958 ret
= rdma_getaddrinfo(rdma
->host
, port_str
, NULL
, &res
);
960 ERROR(errp
, "could not rdma_getaddrinfo address %s", rdma
->host
);
961 goto err_resolve_get_addr
;
964 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
965 inet_ntop(e
->ai_family
,
966 &((struct sockaddr_in
*) e
->ai_dst_addr
)->sin_addr
, ip
, sizeof ip
);
967 trace_qemu_rdma_resolve_host_trying(rdma
->host
, ip
);
969 ret
= rdma_resolve_addr(rdma
->cm_id
, NULL
, e
->ai_dst_addr
,
970 RDMA_RESOLVE_TIMEOUT_MS
);
972 if (e
->ai_family
== AF_INET6
) {
973 ret
= qemu_rdma_broken_ipv6_kernel(rdma
->cm_id
->verbs
, errp
);
982 rdma_freeaddrinfo(res
);
983 ERROR(errp
, "could not resolve address %s", rdma
->host
);
984 goto err_resolve_get_addr
;
987 rdma_freeaddrinfo(res
);
988 qemu_rdma_dump_gid("source_resolve_addr", rdma
->cm_id
);
990 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
992 ERROR(errp
, "could not perform event_addr_resolved");
993 goto err_resolve_get_addr
;
996 if (cm_event
->event
!= RDMA_CM_EVENT_ADDR_RESOLVED
) {
997 ERROR(errp
, "result not equal to event_addr_resolved %s",
998 rdma_event_str(cm_event
->event
));
999 error_report("rdma_resolve_addr");
1000 rdma_ack_cm_event(cm_event
);
1002 goto err_resolve_get_addr
;
1004 rdma_ack_cm_event(cm_event
);
1007 ret
= rdma_resolve_route(rdma
->cm_id
, RDMA_RESOLVE_TIMEOUT_MS
);
1009 ERROR(errp
, "could not resolve rdma route");
1010 goto err_resolve_get_addr
;
1013 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
1015 ERROR(errp
, "could not perform event_route_resolved");
1016 goto err_resolve_get_addr
;
1018 if (cm_event
->event
!= RDMA_CM_EVENT_ROUTE_RESOLVED
) {
1019 ERROR(errp
, "result not equal to event_route_resolved: %s",
1020 rdma_event_str(cm_event
->event
));
1021 rdma_ack_cm_event(cm_event
);
1023 goto err_resolve_get_addr
;
1025 rdma_ack_cm_event(cm_event
);
1026 rdma
->verbs
= rdma
->cm_id
->verbs
;
1027 qemu_rdma_dump_id("source_resolve_host", rdma
->cm_id
->verbs
);
1028 qemu_rdma_dump_gid("source_resolve_host", rdma
->cm_id
);
1031 err_resolve_get_addr
:
1032 rdma_destroy_id(rdma
->cm_id
);
1034 err_resolve_create_id
:
1035 rdma_destroy_event_channel(rdma
->channel
);
1036 rdma
->channel
= NULL
;
1041 * Create protection domain and completion queues
1043 static int qemu_rdma_alloc_pd_cq(RDMAContext
*rdma
)
1046 rdma
->pd
= ibv_alloc_pd(rdma
->verbs
);
1048 error_report("failed to allocate protection domain");
1052 /* create receive completion channel */
1053 rdma
->recv_comp_channel
= ibv_create_comp_channel(rdma
->verbs
);
1054 if (!rdma
->recv_comp_channel
) {
1055 error_report("failed to allocate receive completion channel");
1056 goto err_alloc_pd_cq
;
1060 * Completion queue can be filled by read work requests.
1062 rdma
->recv_cq
= ibv_create_cq(rdma
->verbs
, (RDMA_SIGNALED_SEND_MAX
* 3),
1063 NULL
, rdma
->recv_comp_channel
, 0);
1064 if (!rdma
->recv_cq
) {
1065 error_report("failed to allocate receive completion queue");
1066 goto err_alloc_pd_cq
;
1069 /* create send completion channel */
1070 rdma
->send_comp_channel
= ibv_create_comp_channel(rdma
->verbs
);
1071 if (!rdma
->send_comp_channel
) {
1072 error_report("failed to allocate send completion channel");
1073 goto err_alloc_pd_cq
;
1076 rdma
->send_cq
= ibv_create_cq(rdma
->verbs
, (RDMA_SIGNALED_SEND_MAX
* 3),
1077 NULL
, rdma
->send_comp_channel
, 0);
1078 if (!rdma
->send_cq
) {
1079 error_report("failed to allocate send completion queue");
1080 goto err_alloc_pd_cq
;
1087 ibv_dealloc_pd(rdma
->pd
);
1089 if (rdma
->recv_comp_channel
) {
1090 ibv_destroy_comp_channel(rdma
->recv_comp_channel
);
1092 if (rdma
->send_comp_channel
) {
1093 ibv_destroy_comp_channel(rdma
->send_comp_channel
);
1095 if (rdma
->recv_cq
) {
1096 ibv_destroy_cq(rdma
->recv_cq
);
1097 rdma
->recv_cq
= NULL
;
1100 rdma
->recv_comp_channel
= NULL
;
1101 rdma
->send_comp_channel
= NULL
;
1107 * Create queue pairs.
1109 static int qemu_rdma_alloc_qp(RDMAContext
*rdma
)
1111 struct ibv_qp_init_attr attr
= { 0 };
1114 attr
.cap
.max_send_wr
= RDMA_SIGNALED_SEND_MAX
;
1115 attr
.cap
.max_recv_wr
= 3;
1116 attr
.cap
.max_send_sge
= 1;
1117 attr
.cap
.max_recv_sge
= 1;
1118 attr
.send_cq
= rdma
->send_cq
;
1119 attr
.recv_cq
= rdma
->recv_cq
;
1120 attr
.qp_type
= IBV_QPT_RC
;
1122 ret
= rdma_create_qp(rdma
->cm_id
, rdma
->pd
, &attr
);
1127 rdma
->qp
= rdma
->cm_id
->qp
;
1131 /* Check whether On-Demand Paging is supported by RDAM device */
1132 static bool rdma_support_odp(struct ibv_context
*dev
)
1134 struct ibv_device_attr_ex attr
= {0};
1135 int ret
= ibv_query_device_ex(dev
, NULL
, &attr
);
1140 if (attr
.odp_caps
.general_caps
& IBV_ODP_SUPPORT
) {
1148 * ibv_advise_mr to avoid RNR NAK error as far as possible.
1149 * The responder mr registering with ODP will sent RNR NAK back to
1150 * the requester in the face of the page fault.
1152 static void qemu_rdma_advise_prefetch_mr(struct ibv_pd
*pd
, uint64_t addr
,
1153 uint32_t len
, uint32_t lkey
,
1154 const char *name
, bool wr
)
1156 #ifdef HAVE_IBV_ADVISE_MR
1158 int advice
= wr
? IBV_ADVISE_MR_ADVICE_PREFETCH_WRITE
:
1159 IBV_ADVISE_MR_ADVICE_PREFETCH
;
1160 struct ibv_sge sg_list
= {.lkey
= lkey
, .addr
= addr
, .length
= len
};
1162 ret
= ibv_advise_mr(pd
, advice
,
1163 IBV_ADVISE_MR_FLAG_FLUSH
, &sg_list
, 1);
1164 /* ignore the error */
1166 trace_qemu_rdma_advise_mr(name
, len
, addr
, strerror(errno
));
1168 trace_qemu_rdma_advise_mr(name
, len
, addr
, "successed");
1173 static int qemu_rdma_reg_whole_ram_blocks(RDMAContext
*rdma
)
1176 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
1178 for (i
= 0; i
< local
->nb_blocks
; i
++) {
1179 int access
= IBV_ACCESS_LOCAL_WRITE
| IBV_ACCESS_REMOTE_WRITE
;
1181 local
->block
[i
].mr
=
1182 ibv_reg_mr(rdma
->pd
,
1183 local
->block
[i
].local_host_addr
,
1184 local
->block
[i
].length
, access
1187 if (!local
->block
[i
].mr
&&
1188 errno
== ENOTSUP
&& rdma_support_odp(rdma
->verbs
)) {
1189 access
|= IBV_ACCESS_ON_DEMAND
;
1190 /* register ODP mr */
1191 local
->block
[i
].mr
=
1192 ibv_reg_mr(rdma
->pd
,
1193 local
->block
[i
].local_host_addr
,
1194 local
->block
[i
].length
, access
);
1195 trace_qemu_rdma_register_odp_mr(local
->block
[i
].block_name
);
1197 if (local
->block
[i
].mr
) {
1198 qemu_rdma_advise_prefetch_mr(rdma
->pd
,
1199 (uintptr_t)local
->block
[i
].local_host_addr
,
1200 local
->block
[i
].length
,
1201 local
->block
[i
].mr
->lkey
,
1202 local
->block
[i
].block_name
,
1207 if (!local
->block
[i
].mr
) {
1208 perror("Failed to register local dest ram block!");
1211 rdma
->total_registrations
++;
1214 if (i
>= local
->nb_blocks
) {
1218 for (i
--; i
>= 0; i
--) {
1219 ibv_dereg_mr(local
->block
[i
].mr
);
1220 local
->block
[i
].mr
= NULL
;
1221 rdma
->total_registrations
--;
1229 * Find the ram block that corresponds to the page requested to be
1230 * transmitted by QEMU.
1232 * Once the block is found, also identify which 'chunk' within that
1233 * block that the page belongs to.
1235 static void qemu_rdma_search_ram_block(RDMAContext
*rdma
,
1236 uintptr_t block_offset
,
1239 uint64_t *block_index
,
1240 uint64_t *chunk_index
)
1242 uint64_t current_addr
= block_offset
+ offset
;
1243 RDMALocalBlock
*block
= g_hash_table_lookup(rdma
->blockmap
,
1244 (void *) block_offset
);
1246 assert(current_addr
>= block
->offset
);
1247 assert((current_addr
+ length
) <= (block
->offset
+ block
->length
));
1249 *block_index
= block
->index
;
1250 *chunk_index
= ram_chunk_index(block
->local_host_addr
,
1251 block
->local_host_addr
+ (current_addr
- block
->offset
));
1255 * Register a chunk with IB. If the chunk was already registered
1256 * previously, then skip.
1258 * Also return the keys associated with the registration needed
1259 * to perform the actual RDMA operation.
1261 static int qemu_rdma_register_and_get_keys(RDMAContext
*rdma
,
1262 RDMALocalBlock
*block
, uintptr_t host_addr
,
1263 uint32_t *lkey
, uint32_t *rkey
, int chunk
,
1264 uint8_t *chunk_start
, uint8_t *chunk_end
)
1268 *lkey
= block
->mr
->lkey
;
1271 *rkey
= block
->mr
->rkey
;
1276 /* allocate memory to store chunk MRs */
1278 block
->pmr
= g_new0(struct ibv_mr
*, block
->nb_chunks
);
1282 * If 'rkey', then we're the destination, so grant access to the source.
1284 * If 'lkey', then we're the source VM, so grant access only to ourselves.
1286 if (!block
->pmr
[chunk
]) {
1287 uint64_t len
= chunk_end
- chunk_start
;
1288 int access
= rkey
? IBV_ACCESS_LOCAL_WRITE
| IBV_ACCESS_REMOTE_WRITE
:
1291 trace_qemu_rdma_register_and_get_keys(len
, chunk_start
);
1293 block
->pmr
[chunk
] = ibv_reg_mr(rdma
->pd
, chunk_start
, len
, access
);
1294 if (!block
->pmr
[chunk
] &&
1295 errno
== ENOTSUP
&& rdma_support_odp(rdma
->verbs
)) {
1296 access
|= IBV_ACCESS_ON_DEMAND
;
1297 /* register ODP mr */
1298 block
->pmr
[chunk
] = ibv_reg_mr(rdma
->pd
, chunk_start
, len
, access
);
1299 trace_qemu_rdma_register_odp_mr(block
->block_name
);
1301 if (block
->pmr
[chunk
]) {
1302 qemu_rdma_advise_prefetch_mr(rdma
->pd
, (uintptr_t)chunk_start
,
1303 len
, block
->pmr
[chunk
]->lkey
,
1304 block
->block_name
, rkey
);
1309 if (!block
->pmr
[chunk
]) {
1310 perror("Failed to register chunk!");
1311 fprintf(stderr
, "Chunk details: block: %d chunk index %d"
1312 " start %" PRIuPTR
" end %" PRIuPTR
1314 " local %" PRIuPTR
" registrations: %d\n",
1315 block
->index
, chunk
, (uintptr_t)chunk_start
,
1316 (uintptr_t)chunk_end
, host_addr
,
1317 (uintptr_t)block
->local_host_addr
,
1318 rdma
->total_registrations
);
1321 rdma
->total_registrations
++;
1324 *lkey
= block
->pmr
[chunk
]->lkey
;
1327 *rkey
= block
->pmr
[chunk
]->rkey
;
1333 * Register (at connection time) the memory used for control
1336 static int qemu_rdma_reg_control(RDMAContext
*rdma
, int idx
)
1338 rdma
->wr_data
[idx
].control_mr
= ibv_reg_mr(rdma
->pd
,
1339 rdma
->wr_data
[idx
].control
, RDMA_CONTROL_MAX_BUFFER
,
1340 IBV_ACCESS_LOCAL_WRITE
| IBV_ACCESS_REMOTE_WRITE
);
1341 if (rdma
->wr_data
[idx
].control_mr
) {
1342 rdma
->total_registrations
++;
1345 error_report("qemu_rdma_reg_control failed");
1350 * Perform a non-optimized memory unregistration after every transfer
1351 * for demonstration purposes, only if pin-all is not requested.
1353 * Potential optimizations:
1354 * 1. Start a new thread to run this function continuously
1356 - and for receipt of unregister messages
1358 * 3. Use workload hints.
1360 static int qemu_rdma_unregister_waiting(RDMAContext
*rdma
)
1362 while (rdma
->unregistrations
[rdma
->unregister_current
]) {
1364 uint64_t wr_id
= rdma
->unregistrations
[rdma
->unregister_current
];
1366 (wr_id
& RDMA_WRID_CHUNK_MASK
) >> RDMA_WRID_CHUNK_SHIFT
;
1368 (wr_id
& RDMA_WRID_BLOCK_MASK
) >> RDMA_WRID_BLOCK_SHIFT
;
1369 RDMALocalBlock
*block
=
1370 &(rdma
->local_ram_blocks
.block
[index
]);
1371 RDMARegister reg
= { .current_index
= index
};
1372 RDMAControlHeader resp
= { .type
= RDMA_CONTROL_UNREGISTER_FINISHED
,
1374 RDMAControlHeader head
= { .len
= sizeof(RDMARegister
),
1375 .type
= RDMA_CONTROL_UNREGISTER_REQUEST
,
1379 trace_qemu_rdma_unregister_waiting_proc(chunk
,
1380 rdma
->unregister_current
);
1382 rdma
->unregistrations
[rdma
->unregister_current
] = 0;
1383 rdma
->unregister_current
++;
1385 if (rdma
->unregister_current
== RDMA_SIGNALED_SEND_MAX
) {
1386 rdma
->unregister_current
= 0;
1391 * Unregistration is speculative (because migration is single-threaded
1392 * and we cannot break the protocol's inifinband message ordering).
1393 * Thus, if the memory is currently being used for transmission,
1394 * then abort the attempt to unregister and try again
1395 * later the next time a completion is received for this memory.
1397 clear_bit(chunk
, block
->unregister_bitmap
);
1399 if (test_bit(chunk
, block
->transit_bitmap
)) {
1400 trace_qemu_rdma_unregister_waiting_inflight(chunk
);
1404 trace_qemu_rdma_unregister_waiting_send(chunk
);
1406 ret
= ibv_dereg_mr(block
->pmr
[chunk
]);
1407 block
->pmr
[chunk
] = NULL
;
1408 block
->remote_keys
[chunk
] = 0;
1411 perror("unregistration chunk failed");
1414 rdma
->total_registrations
--;
1416 reg
.key
.chunk
= chunk
;
1417 register_to_network(rdma
, ®
);
1418 ret
= qemu_rdma_exchange_send(rdma
, &head
, (uint8_t *) ®
,
1424 trace_qemu_rdma_unregister_waiting_complete(chunk
);
1430 static uint64_t qemu_rdma_make_wrid(uint64_t wr_id
, uint64_t index
,
1433 uint64_t result
= wr_id
& RDMA_WRID_TYPE_MASK
;
1435 result
|= (index
<< RDMA_WRID_BLOCK_SHIFT
);
1436 result
|= (chunk
<< RDMA_WRID_CHUNK_SHIFT
);
1442 * Consult the connection manager to see a work request
1443 * (of any kind) has completed.
1444 * Return the work request ID that completed.
1446 static int qemu_rdma_poll(RDMAContext
*rdma
, struct ibv_cq
*cq
,
1447 uint64_t *wr_id_out
, uint32_t *byte_len
)
1453 ret
= ibv_poll_cq(cq
, 1, &wc
);
1456 *wr_id_out
= RDMA_WRID_NONE
;
1461 error_report("ibv_poll_cq return %d", ret
);
1465 wr_id
= wc
.wr_id
& RDMA_WRID_TYPE_MASK
;
1467 if (wc
.status
!= IBV_WC_SUCCESS
) {
1468 fprintf(stderr
, "ibv_poll_cq wc.status=%d %s!\n",
1469 wc
.status
, ibv_wc_status_str(wc
.status
));
1470 fprintf(stderr
, "ibv_poll_cq wrid=%" PRIu64
"!\n", wr_id
);
1475 if (rdma
->control_ready_expected
&&
1476 (wr_id
>= RDMA_WRID_RECV_CONTROL
)) {
1477 trace_qemu_rdma_poll_recv(wr_id
- RDMA_WRID_RECV_CONTROL
, wr_id
,
1479 rdma
->control_ready_expected
= 0;
1482 if (wr_id
== RDMA_WRID_RDMA_WRITE
) {
1484 (wc
.wr_id
& RDMA_WRID_CHUNK_MASK
) >> RDMA_WRID_CHUNK_SHIFT
;
1486 (wc
.wr_id
& RDMA_WRID_BLOCK_MASK
) >> RDMA_WRID_BLOCK_SHIFT
;
1487 RDMALocalBlock
*block
= &(rdma
->local_ram_blocks
.block
[index
]);
1489 trace_qemu_rdma_poll_write(wr_id
, rdma
->nb_sent
,
1490 index
, chunk
, block
->local_host_addr
,
1491 (void *)(uintptr_t)block
->remote_host_addr
);
1493 clear_bit(chunk
, block
->transit_bitmap
);
1495 if (rdma
->nb_sent
> 0) {
1499 trace_qemu_rdma_poll_other(wr_id
, rdma
->nb_sent
);
1502 *wr_id_out
= wc
.wr_id
;
1504 *byte_len
= wc
.byte_len
;
1510 /* Wait for activity on the completion channel.
1511 * Returns 0 on success, none-0 on error.
1513 static int qemu_rdma_wait_comp_channel(RDMAContext
*rdma
,
1514 struct ibv_comp_channel
*comp_channel
)
1516 struct rdma_cm_event
*cm_event
;
1520 * Coroutine doesn't start until migration_fd_process_incoming()
1521 * so don't yield unless we know we're running inside of a coroutine.
1523 if (rdma
->migration_started_on_destination
&&
1524 migration_incoming_get_current()->state
== MIGRATION_STATUS_ACTIVE
) {
1525 yield_until_fd_readable(comp_channel
->fd
);
1527 /* This is the source side, we're in a separate thread
1528 * or destination prior to migration_fd_process_incoming()
1529 * after postcopy, the destination also in a separate thread.
1530 * we can't yield; so we have to poll the fd.
1531 * But we need to be able to handle 'cancel' or an error
1532 * without hanging forever.
1534 while (!rdma
->error_state
&& !rdma
->received_error
) {
1536 pfds
[0].fd
= comp_channel
->fd
;
1537 pfds
[0].events
= G_IO_IN
| G_IO_HUP
| G_IO_ERR
;
1538 pfds
[0].revents
= 0;
1540 pfds
[1].fd
= rdma
->channel
->fd
;
1541 pfds
[1].events
= G_IO_IN
| G_IO_HUP
| G_IO_ERR
;
1542 pfds
[1].revents
= 0;
1544 /* 0.1s timeout, should be fine for a 'cancel' */
1545 switch (qemu_poll_ns(pfds
, 2, 100 * 1000 * 1000)) {
1547 case 1: /* fd active */
1548 if (pfds
[0].revents
) {
1552 if (pfds
[1].revents
) {
1553 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
1555 error_report("failed to get cm event while wait "
1556 "completion channel");
1560 error_report("receive cm event while wait comp channel,"
1561 "cm event is %d", cm_event
->event
);
1562 if (cm_event
->event
== RDMA_CM_EVENT_DISCONNECTED
||
1563 cm_event
->event
== RDMA_CM_EVENT_DEVICE_REMOVAL
) {
1564 rdma_ack_cm_event(cm_event
);
1567 rdma_ack_cm_event(cm_event
);
1571 case 0: /* Timeout, go around again */
1574 default: /* Error of some type -
1575 * I don't trust errno from qemu_poll_ns
1577 error_report("%s: poll failed", __func__
);
1581 if (migrate_get_current()->state
== MIGRATION_STATUS_CANCELLING
) {
1582 /* Bail out and let the cancellation happen */
1588 if (rdma
->received_error
) {
1591 return rdma
->error_state
;
1594 static struct ibv_comp_channel
*to_channel(RDMAContext
*rdma
, uint64_t wrid
)
1596 return wrid
< RDMA_WRID_RECV_CONTROL
? rdma
->send_comp_channel
:
1597 rdma
->recv_comp_channel
;
1600 static struct ibv_cq
*to_cq(RDMAContext
*rdma
, uint64_t wrid
)
1602 return wrid
< RDMA_WRID_RECV_CONTROL
? rdma
->send_cq
: rdma
->recv_cq
;
1606 * Block until the next work request has completed.
1608 * First poll to see if a work request has already completed,
1611 * If we encounter completed work requests for IDs other than
1612 * the one we're interested in, then that's generally an error.
1614 * The only exception is actual RDMA Write completions. These
1615 * completions only need to be recorded, but do not actually
1616 * need further processing.
1618 static int qemu_rdma_block_for_wrid(RDMAContext
*rdma
,
1619 uint64_t wrid_requested
,
1622 int num_cq_events
= 0, ret
= 0;
1625 uint64_t wr_id
= RDMA_WRID_NONE
, wr_id_in
;
1626 struct ibv_comp_channel
*ch
= to_channel(rdma
, wrid_requested
);
1627 struct ibv_cq
*poll_cq
= to_cq(rdma
, wrid_requested
);
1629 if (ibv_req_notify_cq(poll_cq
, 0)) {
1633 while (wr_id
!= wrid_requested
) {
1634 ret
= qemu_rdma_poll(rdma
, poll_cq
, &wr_id_in
, byte_len
);
1639 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
1641 if (wr_id
== RDMA_WRID_NONE
) {
1644 if (wr_id
!= wrid_requested
) {
1645 trace_qemu_rdma_block_for_wrid_miss(wrid_requested
, wr_id
);
1649 if (wr_id
== wrid_requested
) {
1654 ret
= qemu_rdma_wait_comp_channel(rdma
, ch
);
1656 goto err_block_for_wrid
;
1659 ret
= ibv_get_cq_event(ch
, &cq
, &cq_ctx
);
1661 perror("ibv_get_cq_event");
1662 goto err_block_for_wrid
;
1667 ret
= -ibv_req_notify_cq(cq
, 0);
1669 goto err_block_for_wrid
;
1672 while (wr_id
!= wrid_requested
) {
1673 ret
= qemu_rdma_poll(rdma
, poll_cq
, &wr_id_in
, byte_len
);
1675 goto err_block_for_wrid
;
1678 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
1680 if (wr_id
== RDMA_WRID_NONE
) {
1683 if (wr_id
!= wrid_requested
) {
1684 trace_qemu_rdma_block_for_wrid_miss(wrid_requested
, wr_id
);
1688 if (wr_id
== wrid_requested
) {
1689 goto success_block_for_wrid
;
1693 success_block_for_wrid
:
1694 if (num_cq_events
) {
1695 ibv_ack_cq_events(cq
, num_cq_events
);
1700 if (num_cq_events
) {
1701 ibv_ack_cq_events(cq
, num_cq_events
);
1704 rdma
->error_state
= ret
;
1709 * Post a SEND message work request for the control channel
1710 * containing some data and block until the post completes.
1712 static int qemu_rdma_post_send_control(RDMAContext
*rdma
, uint8_t *buf
,
1713 RDMAControlHeader
*head
)
1716 RDMAWorkRequestData
*wr
= &rdma
->wr_data
[RDMA_WRID_CONTROL
];
1717 struct ibv_send_wr
*bad_wr
;
1718 struct ibv_sge sge
= {
1719 .addr
= (uintptr_t)(wr
->control
),
1720 .length
= head
->len
+ sizeof(RDMAControlHeader
),
1721 .lkey
= wr
->control_mr
->lkey
,
1723 struct ibv_send_wr send_wr
= {
1724 .wr_id
= RDMA_WRID_SEND_CONTROL
,
1725 .opcode
= IBV_WR_SEND
,
1726 .send_flags
= IBV_SEND_SIGNALED
,
1731 trace_qemu_rdma_post_send_control(control_desc(head
->type
));
1734 * We don't actually need to do a memcpy() in here if we used
1735 * the "sge" properly, but since we're only sending control messages
1736 * (not RAM in a performance-critical path), then its OK for now.
1738 * The copy makes the RDMAControlHeader simpler to manipulate
1739 * for the time being.
1741 assert(head
->len
<= RDMA_CONTROL_MAX_BUFFER
- sizeof(*head
));
1742 memcpy(wr
->control
, head
, sizeof(RDMAControlHeader
));
1743 control_to_network((void *) wr
->control
);
1746 memcpy(wr
->control
+ sizeof(RDMAControlHeader
), buf
, head
->len
);
1750 ret
= ibv_post_send(rdma
->qp
, &send_wr
, &bad_wr
);
1753 error_report("Failed to use post IB SEND for control");
1757 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_SEND_CONTROL
, NULL
);
1759 error_report("rdma migration: send polling control error");
1766 * Post a RECV work request in anticipation of some future receipt
1767 * of data on the control channel.
1769 static int qemu_rdma_post_recv_control(RDMAContext
*rdma
, int idx
)
1771 struct ibv_recv_wr
*bad_wr
;
1772 struct ibv_sge sge
= {
1773 .addr
= (uintptr_t)(rdma
->wr_data
[idx
].control
),
1774 .length
= RDMA_CONTROL_MAX_BUFFER
,
1775 .lkey
= rdma
->wr_data
[idx
].control_mr
->lkey
,
1778 struct ibv_recv_wr recv_wr
= {
1779 .wr_id
= RDMA_WRID_RECV_CONTROL
+ idx
,
1785 if (ibv_post_recv(rdma
->qp
, &recv_wr
, &bad_wr
)) {
1793 * Block and wait for a RECV control channel message to arrive.
1795 static int qemu_rdma_exchange_get_response(RDMAContext
*rdma
,
1796 RDMAControlHeader
*head
, uint32_t expecting
, int idx
)
1799 int ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RECV_CONTROL
+ idx
,
1803 error_report("rdma migration: recv polling control error!");
1807 network_to_control((void *) rdma
->wr_data
[idx
].control
);
1808 memcpy(head
, rdma
->wr_data
[idx
].control
, sizeof(RDMAControlHeader
));
1810 trace_qemu_rdma_exchange_get_response_start(control_desc(expecting
));
1812 if (expecting
== RDMA_CONTROL_NONE
) {
1813 trace_qemu_rdma_exchange_get_response_none(control_desc(head
->type
),
1815 } else if (head
->type
!= expecting
|| head
->type
== RDMA_CONTROL_ERROR
) {
1816 error_report("Was expecting a %s (%d) control message"
1817 ", but got: %s (%d), length: %d",
1818 control_desc(expecting
), expecting
,
1819 control_desc(head
->type
), head
->type
, head
->len
);
1820 if (head
->type
== RDMA_CONTROL_ERROR
) {
1821 rdma
->received_error
= true;
1825 if (head
->len
> RDMA_CONTROL_MAX_BUFFER
- sizeof(*head
)) {
1826 error_report("too long length: %d", head
->len
);
1829 if (sizeof(*head
) + head
->len
!= byte_len
) {
1830 error_report("Malformed length: %d byte_len %d", head
->len
, byte_len
);
1838 * When a RECV work request has completed, the work request's
1839 * buffer is pointed at the header.
1841 * This will advance the pointer to the data portion
1842 * of the control message of the work request's buffer that
1843 * was populated after the work request finished.
1845 static void qemu_rdma_move_header(RDMAContext
*rdma
, int idx
,
1846 RDMAControlHeader
*head
)
1848 rdma
->wr_data
[idx
].control_len
= head
->len
;
1849 rdma
->wr_data
[idx
].control_curr
=
1850 rdma
->wr_data
[idx
].control
+ sizeof(RDMAControlHeader
);
1854 * This is an 'atomic' high-level operation to deliver a single, unified
1855 * control-channel message.
1857 * Additionally, if the user is expecting some kind of reply to this message,
1858 * they can request a 'resp' response message be filled in by posting an
1859 * additional work request on behalf of the user and waiting for an additional
1862 * The extra (optional) response is used during registration to us from having
1863 * to perform an *additional* exchange of message just to provide a response by
1864 * instead piggy-backing on the acknowledgement.
1866 static int qemu_rdma_exchange_send(RDMAContext
*rdma
, RDMAControlHeader
*head
,
1867 uint8_t *data
, RDMAControlHeader
*resp
,
1869 int (*callback
)(RDMAContext
*rdma
))
1874 * Wait until the dest is ready before attempting to deliver the message
1875 * by waiting for a READY message.
1877 if (rdma
->control_ready_expected
) {
1878 RDMAControlHeader resp_ignored
;
1880 ret
= qemu_rdma_exchange_get_response(rdma
, &resp_ignored
,
1889 * If the user is expecting a response, post a WR in anticipation of it.
1892 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_DATA
);
1894 error_report("rdma migration: error posting"
1895 " extra control recv for anticipated result!");
1901 * Post a WR to replace the one we just consumed for the READY message.
1903 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
1905 error_report("rdma migration: error posting first control recv!");
1910 * Deliver the control message that was requested.
1912 ret
= qemu_rdma_post_send_control(rdma
, data
, head
);
1915 error_report("Failed to send control buffer!");
1920 * If we're expecting a response, block and wait for it.
1924 trace_qemu_rdma_exchange_send_issue_callback();
1925 ret
= callback(rdma
);
1931 trace_qemu_rdma_exchange_send_waiting(control_desc(resp
->type
));
1932 ret
= qemu_rdma_exchange_get_response(rdma
, resp
,
1933 resp
->type
, RDMA_WRID_DATA
);
1939 qemu_rdma_move_header(rdma
, RDMA_WRID_DATA
, resp
);
1941 *resp_idx
= RDMA_WRID_DATA
;
1943 trace_qemu_rdma_exchange_send_received(control_desc(resp
->type
));
1946 rdma
->control_ready_expected
= 1;
1952 * This is an 'atomic' high-level operation to receive a single, unified
1953 * control-channel message.
1955 static int qemu_rdma_exchange_recv(RDMAContext
*rdma
, RDMAControlHeader
*head
,
1958 RDMAControlHeader ready
= {
1960 .type
= RDMA_CONTROL_READY
,
1966 * Inform the source that we're ready to receive a message.
1968 ret
= qemu_rdma_post_send_control(rdma
, NULL
, &ready
);
1971 error_report("Failed to send control buffer!");
1976 * Block and wait for the message.
1978 ret
= qemu_rdma_exchange_get_response(rdma
, head
,
1979 expecting
, RDMA_WRID_READY
);
1985 qemu_rdma_move_header(rdma
, RDMA_WRID_READY
, head
);
1988 * Post a new RECV work request to replace the one we just consumed.
1990 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
1992 error_report("rdma migration: error posting second control recv!");
2000 * Write an actual chunk of memory using RDMA.
2002 * If we're using dynamic registration on the dest-side, we have to
2003 * send a registration command first.
2005 static int qemu_rdma_write_one(RDMAContext
*rdma
,
2006 int current_index
, uint64_t current_addr
,
2010 struct ibv_send_wr send_wr
= { 0 };
2011 struct ibv_send_wr
*bad_wr
;
2012 int reg_result_idx
, ret
, count
= 0;
2013 uint64_t chunk
, chunks
;
2014 uint8_t *chunk_start
, *chunk_end
;
2015 RDMALocalBlock
*block
= &(rdma
->local_ram_blocks
.block
[current_index
]);
2017 RDMARegisterResult
*reg_result
;
2018 RDMAControlHeader resp
= { .type
= RDMA_CONTROL_REGISTER_RESULT
};
2019 RDMAControlHeader head
= { .len
= sizeof(RDMARegister
),
2020 .type
= RDMA_CONTROL_REGISTER_REQUEST
,
2025 sge
.addr
= (uintptr_t)(block
->local_host_addr
+
2026 (current_addr
- block
->offset
));
2027 sge
.length
= length
;
2029 chunk
= ram_chunk_index(block
->local_host_addr
,
2030 (uint8_t *)(uintptr_t)sge
.addr
);
2031 chunk_start
= ram_chunk_start(block
, chunk
);
2033 if (block
->is_ram_block
) {
2034 chunks
= length
/ (1UL << RDMA_REG_CHUNK_SHIFT
);
2036 if (chunks
&& ((length
% (1UL << RDMA_REG_CHUNK_SHIFT
)) == 0)) {
2040 chunks
= block
->length
/ (1UL << RDMA_REG_CHUNK_SHIFT
);
2042 if (chunks
&& ((block
->length
% (1UL << RDMA_REG_CHUNK_SHIFT
)) == 0)) {
2047 trace_qemu_rdma_write_one_top(chunks
+ 1,
2049 (1UL << RDMA_REG_CHUNK_SHIFT
) / 1024 / 1024);
2051 chunk_end
= ram_chunk_end(block
, chunk
+ chunks
);
2054 while (test_bit(chunk
, block
->transit_bitmap
)) {
2056 trace_qemu_rdma_write_one_block(count
++, current_index
, chunk
,
2057 sge
.addr
, length
, rdma
->nb_sent
, block
->nb_chunks
);
2059 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RDMA_WRITE
, NULL
);
2062 error_report("Failed to Wait for previous write to complete "
2063 "block %d chunk %" PRIu64
2064 " current %" PRIu64
" len %" PRIu64
" %d",
2065 current_index
, chunk
, sge
.addr
, length
, rdma
->nb_sent
);
2070 if (!rdma
->pin_all
|| !block
->is_ram_block
) {
2071 if (!block
->remote_keys
[chunk
]) {
2073 * This chunk has not yet been registered, so first check to see
2074 * if the entire chunk is zero. If so, tell the other size to
2075 * memset() + madvise() the entire chunk without RDMA.
2078 if (buffer_is_zero((void *)(uintptr_t)sge
.addr
, length
)) {
2079 RDMACompress comp
= {
2080 .offset
= current_addr
,
2082 .block_idx
= current_index
,
2086 head
.len
= sizeof(comp
);
2087 head
.type
= RDMA_CONTROL_COMPRESS
;
2089 trace_qemu_rdma_write_one_zero(chunk
, sge
.length
,
2090 current_index
, current_addr
);
2092 compress_to_network(rdma
, &comp
);
2093 ret
= qemu_rdma_exchange_send(rdma
, &head
,
2094 (uint8_t *) &comp
, NULL
, NULL
, NULL
);
2101 * TODO: Here we are sending something, but we are not
2102 * accounting for anything transferred. The following is wrong:
2104 * stat64_add(&mig_stats.rdma_bytes, sge.length);
2106 * because we are using some kind of compression. I
2107 * would think that head.len would be the more similar
2108 * thing to a correct value.
2110 stat64_add(&mig_stats
.zero_pages
,
2111 sge
.length
/ qemu_target_page_size());
2116 * Otherwise, tell other side to register.
2118 reg
.current_index
= current_index
;
2119 if (block
->is_ram_block
) {
2120 reg
.key
.current_addr
= current_addr
;
2122 reg
.key
.chunk
= chunk
;
2124 reg
.chunks
= chunks
;
2126 trace_qemu_rdma_write_one_sendreg(chunk
, sge
.length
, current_index
,
2129 register_to_network(rdma
, ®
);
2130 ret
= qemu_rdma_exchange_send(rdma
, &head
, (uint8_t *) ®
,
2131 &resp
, ®_result_idx
, NULL
);
2136 /* try to overlap this single registration with the one we sent. */
2137 if (qemu_rdma_register_and_get_keys(rdma
, block
, sge
.addr
,
2138 &sge
.lkey
, NULL
, chunk
,
2139 chunk_start
, chunk_end
)) {
2140 error_report("cannot get lkey");
2144 reg_result
= (RDMARegisterResult
*)
2145 rdma
->wr_data
[reg_result_idx
].control_curr
;
2147 network_to_result(reg_result
);
2149 trace_qemu_rdma_write_one_recvregres(block
->remote_keys
[chunk
],
2150 reg_result
->rkey
, chunk
);
2152 block
->remote_keys
[chunk
] = reg_result
->rkey
;
2153 block
->remote_host_addr
= reg_result
->host_addr
;
2155 /* already registered before */
2156 if (qemu_rdma_register_and_get_keys(rdma
, block
, sge
.addr
,
2157 &sge
.lkey
, NULL
, chunk
,
2158 chunk_start
, chunk_end
)) {
2159 error_report("cannot get lkey!");
2164 send_wr
.wr
.rdma
.rkey
= block
->remote_keys
[chunk
];
2166 send_wr
.wr
.rdma
.rkey
= block
->remote_rkey
;
2168 if (qemu_rdma_register_and_get_keys(rdma
, block
, sge
.addr
,
2169 &sge
.lkey
, NULL
, chunk
,
2170 chunk_start
, chunk_end
)) {
2171 error_report("cannot get lkey!");
2177 * Encode the ram block index and chunk within this wrid.
2178 * We will use this information at the time of completion
2179 * to figure out which bitmap to check against and then which
2180 * chunk in the bitmap to look for.
2182 send_wr
.wr_id
= qemu_rdma_make_wrid(RDMA_WRID_RDMA_WRITE
,
2183 current_index
, chunk
);
2185 send_wr
.opcode
= IBV_WR_RDMA_WRITE
;
2186 send_wr
.send_flags
= IBV_SEND_SIGNALED
;
2187 send_wr
.sg_list
= &sge
;
2188 send_wr
.num_sge
= 1;
2189 send_wr
.wr
.rdma
.remote_addr
= block
->remote_host_addr
+
2190 (current_addr
- block
->offset
);
2192 trace_qemu_rdma_write_one_post(chunk
, sge
.addr
, send_wr
.wr
.rdma
.remote_addr
,
2196 * ibv_post_send() does not return negative error numbers,
2197 * per the specification they are positive - no idea why.
2199 ret
= ibv_post_send(rdma
->qp
, &send_wr
, &bad_wr
);
2201 if (ret
== ENOMEM
) {
2202 trace_qemu_rdma_write_one_queue_full();
2203 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RDMA_WRITE
, NULL
);
2205 error_report("rdma migration: failed to make "
2206 "room in full send queue! %d", ret
);
2212 } else if (ret
> 0) {
2213 perror("rdma migration: post rdma write failed");
2217 set_bit(chunk
, block
->transit_bitmap
);
2218 stat64_add(&mig_stats
.normal_pages
, sge
.length
/ qemu_target_page_size());
2220 * We are adding to transferred the amount of data written, but no
2221 * overhead at all. I will asume that RDMA is magicaly and don't
2222 * need to transfer (at least) the addresses where it wants to
2223 * write the pages. Here it looks like it should be something
2225 * sizeof(send_wr) + sge.length
2226 * but this being RDMA, who knows.
2228 stat64_add(&mig_stats
.rdma_bytes
, sge
.length
);
2229 ram_transferred_add(sge
.length
);
2230 rdma
->total_writes
++;
2236 * Push out any unwritten RDMA operations.
2238 * We support sending out multiple chunks at the same time.
2239 * Not all of them need to get signaled in the completion queue.
2241 static int qemu_rdma_write_flush(RDMAContext
*rdma
)
2245 if (!rdma
->current_length
) {
2249 ret
= qemu_rdma_write_one(rdma
,
2250 rdma
->current_index
, rdma
->current_addr
, rdma
->current_length
);
2258 trace_qemu_rdma_write_flush(rdma
->nb_sent
);
2261 rdma
->current_length
= 0;
2262 rdma
->current_addr
= 0;
2267 static inline bool qemu_rdma_buffer_mergeable(RDMAContext
*rdma
,
2268 uint64_t offset
, uint64_t len
)
2270 RDMALocalBlock
*block
;
2274 if (rdma
->current_index
< 0) {
2278 if (rdma
->current_chunk
< 0) {
2282 block
= &(rdma
->local_ram_blocks
.block
[rdma
->current_index
]);
2283 host_addr
= block
->local_host_addr
+ (offset
- block
->offset
);
2284 chunk_end
= ram_chunk_end(block
, rdma
->current_chunk
);
2286 if (rdma
->current_length
== 0) {
2291 * Only merge into chunk sequentially.
2293 if (offset
!= (rdma
->current_addr
+ rdma
->current_length
)) {
2297 if (offset
< block
->offset
) {
2301 if ((offset
+ len
) > (block
->offset
+ block
->length
)) {
2305 if ((host_addr
+ len
) > chunk_end
) {
2313 * We're not actually writing here, but doing three things:
2315 * 1. Identify the chunk the buffer belongs to.
2316 * 2. If the chunk is full or the buffer doesn't belong to the current
2317 * chunk, then start a new chunk and flush() the old chunk.
2318 * 3. To keep the hardware busy, we also group chunks into batches
2319 * and only require that a batch gets acknowledged in the completion
2320 * queue instead of each individual chunk.
2322 static int qemu_rdma_write(RDMAContext
*rdma
,
2323 uint64_t block_offset
, uint64_t offset
,
2326 uint64_t current_addr
= block_offset
+ offset
;
2327 uint64_t index
= rdma
->current_index
;
2328 uint64_t chunk
= rdma
->current_chunk
;
2331 /* If we cannot merge it, we flush the current buffer first. */
2332 if (!qemu_rdma_buffer_mergeable(rdma
, current_addr
, len
)) {
2333 ret
= qemu_rdma_write_flush(rdma
);
2337 rdma
->current_length
= 0;
2338 rdma
->current_addr
= current_addr
;
2340 qemu_rdma_search_ram_block(rdma
, block_offset
,
2341 offset
, len
, &index
, &chunk
);
2342 rdma
->current_index
= index
;
2343 rdma
->current_chunk
= chunk
;
2347 rdma
->current_length
+= len
;
2349 /* flush it if buffer is too large */
2350 if (rdma
->current_length
>= RDMA_MERGE_MAX
) {
2351 return qemu_rdma_write_flush(rdma
);
2357 static void qemu_rdma_cleanup(RDMAContext
*rdma
)
2361 if (rdma
->cm_id
&& rdma
->connected
) {
2362 if ((rdma
->error_state
||
2363 migrate_get_current()->state
== MIGRATION_STATUS_CANCELLING
) &&
2364 !rdma
->received_error
) {
2365 RDMAControlHeader head
= { .len
= 0,
2366 .type
= RDMA_CONTROL_ERROR
,
2369 error_report("Early error. Sending error.");
2370 qemu_rdma_post_send_control(rdma
, NULL
, &head
);
2373 rdma_disconnect(rdma
->cm_id
);
2374 trace_qemu_rdma_cleanup_disconnect();
2375 rdma
->connected
= false;
2378 if (rdma
->channel
) {
2379 qemu_set_fd_handler(rdma
->channel
->fd
, NULL
, NULL
, NULL
);
2381 g_free(rdma
->dest_blocks
);
2382 rdma
->dest_blocks
= NULL
;
2384 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2385 if (rdma
->wr_data
[idx
].control_mr
) {
2386 rdma
->total_registrations
--;
2387 ibv_dereg_mr(rdma
->wr_data
[idx
].control_mr
);
2389 rdma
->wr_data
[idx
].control_mr
= NULL
;
2392 if (rdma
->local_ram_blocks
.block
) {
2393 while (rdma
->local_ram_blocks
.nb_blocks
) {
2394 rdma_delete_block(rdma
, &rdma
->local_ram_blocks
.block
[0]);
2399 rdma_destroy_qp(rdma
->cm_id
);
2402 if (rdma
->recv_cq
) {
2403 ibv_destroy_cq(rdma
->recv_cq
);
2404 rdma
->recv_cq
= NULL
;
2406 if (rdma
->send_cq
) {
2407 ibv_destroy_cq(rdma
->send_cq
);
2408 rdma
->send_cq
= NULL
;
2410 if (rdma
->recv_comp_channel
) {
2411 ibv_destroy_comp_channel(rdma
->recv_comp_channel
);
2412 rdma
->recv_comp_channel
= NULL
;
2414 if (rdma
->send_comp_channel
) {
2415 ibv_destroy_comp_channel(rdma
->send_comp_channel
);
2416 rdma
->send_comp_channel
= NULL
;
2419 ibv_dealloc_pd(rdma
->pd
);
2423 rdma_destroy_id(rdma
->cm_id
);
2427 /* the destination side, listen_id and channel is shared */
2428 if (rdma
->listen_id
) {
2429 if (!rdma
->is_return_path
) {
2430 rdma_destroy_id(rdma
->listen_id
);
2432 rdma
->listen_id
= NULL
;
2434 if (rdma
->channel
) {
2435 if (!rdma
->is_return_path
) {
2436 rdma_destroy_event_channel(rdma
->channel
);
2438 rdma
->channel
= NULL
;
2442 if (rdma
->channel
) {
2443 rdma_destroy_event_channel(rdma
->channel
);
2444 rdma
->channel
= NULL
;
2447 g_free(rdma
->host_port
);
2449 rdma
->host_port
= NULL
;
2453 static int qemu_rdma_source_init(RDMAContext
*rdma
, bool pin_all
, Error
**errp
)
2458 * Will be validated against destination's actual capabilities
2459 * after the connect() completes.
2461 rdma
->pin_all
= pin_all
;
2463 ret
= qemu_rdma_resolve_host(rdma
, errp
);
2465 goto err_rdma_source_init
;
2468 ret
= qemu_rdma_alloc_pd_cq(rdma
);
2470 ERROR(errp
, "rdma migration: error allocating pd and cq! Your mlock()"
2471 " limits may be too low. Please check $ ulimit -a # and "
2472 "search for 'ulimit -l' in the output");
2473 goto err_rdma_source_init
;
2476 ret
= qemu_rdma_alloc_qp(rdma
);
2478 ERROR(errp
, "rdma migration: error allocating qp!");
2479 goto err_rdma_source_init
;
2482 qemu_rdma_init_ram_blocks(rdma
);
2484 /* Build the hash that maps from offset to RAMBlock */
2485 rdma
->blockmap
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
2486 for (idx
= 0; idx
< rdma
->local_ram_blocks
.nb_blocks
; idx
++) {
2487 g_hash_table_insert(rdma
->blockmap
,
2488 (void *)(uintptr_t)rdma
->local_ram_blocks
.block
[idx
].offset
,
2489 &rdma
->local_ram_blocks
.block
[idx
]);
2492 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2493 ret
= qemu_rdma_reg_control(rdma
, idx
);
2495 ERROR(errp
, "rdma migration: error registering %d control!",
2497 goto err_rdma_source_init
;
2503 err_rdma_source_init
:
2504 qemu_rdma_cleanup(rdma
);
2508 static int qemu_get_cm_event_timeout(RDMAContext
*rdma
,
2509 struct rdma_cm_event
**cm_event
,
2510 long msec
, Error
**errp
)
2513 struct pollfd poll_fd
= {
2514 .fd
= rdma
->channel
->fd
,
2520 ret
= poll(&poll_fd
, 1, msec
);
2521 } while (ret
< 0 && errno
== EINTR
);
2524 ERROR(errp
, "poll cm event timeout");
2526 } else if (ret
< 0) {
2527 ERROR(errp
, "failed to poll cm event, errno=%i", errno
);
2529 } else if (poll_fd
.revents
& POLLIN
) {
2530 return rdma_get_cm_event(rdma
->channel
, cm_event
);
2532 ERROR(errp
, "no POLLIN event, revent=%x", poll_fd
.revents
);
2537 static int qemu_rdma_connect(RDMAContext
*rdma
, bool return_path
,
2540 RDMACapabilities cap
= {
2541 .version
= RDMA_CONTROL_VERSION_CURRENT
,
2544 struct rdma_conn_param conn_param
= { .initiator_depth
= 2,
2546 .private_data
= &cap
,
2547 .private_data_len
= sizeof(cap
),
2549 struct rdma_cm_event
*cm_event
;
2553 * Only negotiate the capability with destination if the user
2554 * on the source first requested the capability.
2556 if (rdma
->pin_all
) {
2557 trace_qemu_rdma_connect_pin_all_requested();
2558 cap
.flags
|= RDMA_CAPABILITY_PIN_ALL
;
2561 caps_to_network(&cap
);
2563 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
2565 ERROR(errp
, "posting second control recv");
2566 goto err_rdma_source_connect
;
2569 ret
= rdma_connect(rdma
->cm_id
, &conn_param
);
2571 perror("rdma_connect");
2572 ERROR(errp
, "connecting to destination!");
2573 goto err_rdma_source_connect
;
2577 ret
= qemu_get_cm_event_timeout(rdma
, &cm_event
, 5000, errp
);
2579 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
2582 perror("rdma_get_cm_event after rdma_connect");
2583 ERROR(errp
, "connecting to destination!");
2584 goto err_rdma_source_connect
;
2587 if (cm_event
->event
!= RDMA_CM_EVENT_ESTABLISHED
) {
2588 error_report("rdma_get_cm_event != EVENT_ESTABLISHED after rdma_connect");
2589 ERROR(errp
, "connecting to destination!");
2590 rdma_ack_cm_event(cm_event
);
2591 goto err_rdma_source_connect
;
2593 rdma
->connected
= true;
2595 memcpy(&cap
, cm_event
->param
.conn
.private_data
, sizeof(cap
));
2596 network_to_caps(&cap
);
2599 * Verify that the *requested* capabilities are supported by the destination
2600 * and disable them otherwise.
2602 if (rdma
->pin_all
&& !(cap
.flags
& RDMA_CAPABILITY_PIN_ALL
)) {
2603 ERROR(errp
, "Server cannot support pinning all memory. "
2604 "Will register memory dynamically.");
2605 rdma
->pin_all
= false;
2608 trace_qemu_rdma_connect_pin_all_outcome(rdma
->pin_all
);
2610 rdma_ack_cm_event(cm_event
);
2612 rdma
->control_ready_expected
= 1;
2616 err_rdma_source_connect
:
2617 qemu_rdma_cleanup(rdma
);
2621 static int qemu_rdma_dest_init(RDMAContext
*rdma
, Error
**errp
)
2624 struct rdma_cm_id
*listen_id
;
2625 char ip
[40] = "unknown";
2626 struct rdma_addrinfo
*res
, *e
;
2630 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2631 rdma
->wr_data
[idx
].control_len
= 0;
2632 rdma
->wr_data
[idx
].control_curr
= NULL
;
2635 if (!rdma
->host
|| !rdma
->host
[0]) {
2636 ERROR(errp
, "RDMA host is not set!");
2637 rdma
->error_state
= -EINVAL
;
2640 /* create CM channel */
2641 rdma
->channel
= rdma_create_event_channel();
2642 if (!rdma
->channel
) {
2643 ERROR(errp
, "could not create rdma event channel");
2644 rdma
->error_state
= -EINVAL
;
2649 ret
= rdma_create_id(rdma
->channel
, &listen_id
, NULL
, RDMA_PS_TCP
);
2651 ERROR(errp
, "could not create cm_id!");
2652 goto err_dest_init_create_listen_id
;
2655 snprintf(port_str
, 16, "%d", rdma
->port
);
2656 port_str
[15] = '\0';
2658 ret
= rdma_getaddrinfo(rdma
->host
, port_str
, NULL
, &res
);
2660 ERROR(errp
, "could not rdma_getaddrinfo address %s", rdma
->host
);
2661 goto err_dest_init_bind_addr
;
2664 ret
= rdma_set_option(listen_id
, RDMA_OPTION_ID
, RDMA_OPTION_ID_REUSEADDR
,
2665 &reuse
, sizeof reuse
);
2667 ERROR(errp
, "Error: could not set REUSEADDR option");
2668 goto err_dest_init_bind_addr
;
2670 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
2671 inet_ntop(e
->ai_family
,
2672 &((struct sockaddr_in
*) e
->ai_dst_addr
)->sin_addr
, ip
, sizeof ip
);
2673 trace_qemu_rdma_dest_init_trying(rdma
->host
, ip
);
2674 ret
= rdma_bind_addr(listen_id
, e
->ai_dst_addr
);
2678 if (e
->ai_family
== AF_INET6
) {
2679 ret
= qemu_rdma_broken_ipv6_kernel(listen_id
->verbs
, errp
);
2687 rdma_freeaddrinfo(res
);
2689 ERROR(errp
, "Error: could not rdma_bind_addr!");
2690 goto err_dest_init_bind_addr
;
2693 rdma
->listen_id
= listen_id
;
2694 qemu_rdma_dump_gid("dest_init", listen_id
);
2697 err_dest_init_bind_addr
:
2698 rdma_destroy_id(listen_id
);
2699 err_dest_init_create_listen_id
:
2700 rdma_destroy_event_channel(rdma
->channel
);
2701 rdma
->channel
= NULL
;
2702 rdma
->error_state
= ret
;
2707 static void qemu_rdma_return_path_dest_init(RDMAContext
*rdma_return_path
,
2712 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2713 rdma_return_path
->wr_data
[idx
].control_len
= 0;
2714 rdma_return_path
->wr_data
[idx
].control_curr
= NULL
;
2717 /*the CM channel and CM id is shared*/
2718 rdma_return_path
->channel
= rdma
->channel
;
2719 rdma_return_path
->listen_id
= rdma
->listen_id
;
2721 rdma
->return_path
= rdma_return_path
;
2722 rdma_return_path
->return_path
= rdma
;
2723 rdma_return_path
->is_return_path
= true;
2726 static RDMAContext
*qemu_rdma_data_init(const char *host_port
, Error
**errp
)
2728 RDMAContext
*rdma
= NULL
;
2729 InetSocketAddress
*addr
;
2732 rdma
= g_new0(RDMAContext
, 1);
2733 rdma
->current_index
= -1;
2734 rdma
->current_chunk
= -1;
2736 addr
= g_new(InetSocketAddress
, 1);
2737 if (!inet_parse(addr
, host_port
, NULL
)) {
2738 rdma
->port
= atoi(addr
->port
);
2739 rdma
->host
= g_strdup(addr
->host
);
2740 rdma
->host_port
= g_strdup(host_port
);
2742 ERROR(errp
, "bad RDMA migration address '%s'", host_port
);
2747 qapi_free_InetSocketAddress(addr
);
2754 * QEMUFile interface to the control channel.
2755 * SEND messages for control only.
2756 * VM's ram is handled with regular RDMA messages.
2758 static ssize_t
qio_channel_rdma_writev(QIOChannel
*ioc
,
2759 const struct iovec
*iov
,
2766 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
2772 RCU_READ_LOCK_GUARD();
2773 rdma
= qatomic_rcu_read(&rioc
->rdmaout
);
2776 error_setg(errp
, "RDMA control channel output is not set");
2780 CHECK_ERROR_STATE();
2783 * Push out any writes that
2784 * we're queued up for VM's ram.
2786 ret
= qemu_rdma_write_flush(rdma
);
2788 rdma
->error_state
= ret
;
2789 error_setg(errp
, "qemu_rdma_write_flush returned %d", ret
);
2793 for (i
= 0; i
< niov
; i
++) {
2794 size_t remaining
= iov
[i
].iov_len
;
2795 uint8_t * data
= (void *)iov
[i
].iov_base
;
2797 RDMAControlHeader head
= {};
2799 len
= MIN(remaining
, RDMA_SEND_INCREMENT
);
2803 head
.type
= RDMA_CONTROL_QEMU_FILE
;
2805 ret
= qemu_rdma_exchange_send(rdma
, &head
, data
, NULL
, NULL
, NULL
);
2808 rdma
->error_state
= ret
;
2809 error_setg(errp
, "qemu_rdma_exchange_send returned %d", ret
);
2821 static size_t qemu_rdma_fill(RDMAContext
*rdma
, uint8_t *buf
,
2822 size_t size
, int idx
)
2826 if (rdma
->wr_data
[idx
].control_len
) {
2827 trace_qemu_rdma_fill(rdma
->wr_data
[idx
].control_len
, size
);
2829 len
= MIN(size
, rdma
->wr_data
[idx
].control_len
);
2830 memcpy(buf
, rdma
->wr_data
[idx
].control_curr
, len
);
2831 rdma
->wr_data
[idx
].control_curr
+= len
;
2832 rdma
->wr_data
[idx
].control_len
-= len
;
2839 * QEMUFile interface to the control channel.
2840 * RDMA links don't use bytestreams, so we have to
2841 * return bytes to QEMUFile opportunistically.
2843 static ssize_t
qio_channel_rdma_readv(QIOChannel
*ioc
,
2844 const struct iovec
*iov
,
2851 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
2853 RDMAControlHeader head
;
2858 RCU_READ_LOCK_GUARD();
2859 rdma
= qatomic_rcu_read(&rioc
->rdmain
);
2862 error_setg(errp
, "RDMA control channel input is not set");
2866 CHECK_ERROR_STATE();
2868 for (i
= 0; i
< niov
; i
++) {
2869 size_t want
= iov
[i
].iov_len
;
2870 uint8_t *data
= (void *)iov
[i
].iov_base
;
2873 * First, we hold on to the last SEND message we
2874 * were given and dish out the bytes until we run
2877 len
= qemu_rdma_fill(rdma
, data
, want
, 0);
2880 /* Got what we needed, so go to next iovec */
2885 /* If we got any data so far, then don't wait
2886 * for more, just return what we have */
2892 /* We've got nothing at all, so lets wait for
2895 ret
= qemu_rdma_exchange_recv(rdma
, &head
, RDMA_CONTROL_QEMU_FILE
);
2898 rdma
->error_state
= ret
;
2899 error_setg(errp
, "qemu_rdma_exchange_recv returned %d", ret
);
2904 * SEND was received with new bytes, now try again.
2906 len
= qemu_rdma_fill(rdma
, data
, want
, 0);
2910 /* Still didn't get enough, so lets just return */
2913 return QIO_CHANNEL_ERR_BLOCK
;
2923 * Block until all the outstanding chunks have been delivered by the hardware.
2925 static int qemu_rdma_drain_cq(RDMAContext
*rdma
)
2929 if (qemu_rdma_write_flush(rdma
) < 0) {
2933 while (rdma
->nb_sent
) {
2934 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RDMA_WRITE
, NULL
);
2936 error_report("rdma migration: complete polling error!");
2941 qemu_rdma_unregister_waiting(rdma
);
2947 static int qio_channel_rdma_set_blocking(QIOChannel
*ioc
,
2951 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
2952 /* XXX we should make readv/writev actually honour this :-) */
2953 rioc
->blocking
= blocking
;
2958 typedef struct QIOChannelRDMASource QIOChannelRDMASource
;
2959 struct QIOChannelRDMASource
{
2961 QIOChannelRDMA
*rioc
;
2962 GIOCondition condition
;
2966 qio_channel_rdma_source_prepare(GSource
*source
,
2969 QIOChannelRDMASource
*rsource
= (QIOChannelRDMASource
*)source
;
2971 GIOCondition cond
= 0;
2974 RCU_READ_LOCK_GUARD();
2975 if (rsource
->condition
== G_IO_IN
) {
2976 rdma
= qatomic_rcu_read(&rsource
->rioc
->rdmain
);
2978 rdma
= qatomic_rcu_read(&rsource
->rioc
->rdmaout
);
2982 error_report("RDMAContext is NULL when prepare Gsource");
2986 if (rdma
->wr_data
[0].control_len
) {
2991 return cond
& rsource
->condition
;
2995 qio_channel_rdma_source_check(GSource
*source
)
2997 QIOChannelRDMASource
*rsource
= (QIOChannelRDMASource
*)source
;
2999 GIOCondition cond
= 0;
3001 RCU_READ_LOCK_GUARD();
3002 if (rsource
->condition
== G_IO_IN
) {
3003 rdma
= qatomic_rcu_read(&rsource
->rioc
->rdmain
);
3005 rdma
= qatomic_rcu_read(&rsource
->rioc
->rdmaout
);
3009 error_report("RDMAContext is NULL when check Gsource");
3013 if (rdma
->wr_data
[0].control_len
) {
3018 return cond
& rsource
->condition
;
3022 qio_channel_rdma_source_dispatch(GSource
*source
,
3023 GSourceFunc callback
,
3026 QIOChannelFunc func
= (QIOChannelFunc
)callback
;
3027 QIOChannelRDMASource
*rsource
= (QIOChannelRDMASource
*)source
;
3029 GIOCondition cond
= 0;
3031 RCU_READ_LOCK_GUARD();
3032 if (rsource
->condition
== G_IO_IN
) {
3033 rdma
= qatomic_rcu_read(&rsource
->rioc
->rdmain
);
3035 rdma
= qatomic_rcu_read(&rsource
->rioc
->rdmaout
);
3039 error_report("RDMAContext is NULL when dispatch Gsource");
3043 if (rdma
->wr_data
[0].control_len
) {
3048 return (*func
)(QIO_CHANNEL(rsource
->rioc
),
3049 (cond
& rsource
->condition
),
3054 qio_channel_rdma_source_finalize(GSource
*source
)
3056 QIOChannelRDMASource
*ssource
= (QIOChannelRDMASource
*)source
;
3058 object_unref(OBJECT(ssource
->rioc
));
3061 static GSourceFuncs qio_channel_rdma_source_funcs
= {
3062 qio_channel_rdma_source_prepare
,
3063 qio_channel_rdma_source_check
,
3064 qio_channel_rdma_source_dispatch
,
3065 qio_channel_rdma_source_finalize
3068 static GSource
*qio_channel_rdma_create_watch(QIOChannel
*ioc
,
3069 GIOCondition condition
)
3071 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
3072 QIOChannelRDMASource
*ssource
;
3075 source
= g_source_new(&qio_channel_rdma_source_funcs
,
3076 sizeof(QIOChannelRDMASource
));
3077 ssource
= (QIOChannelRDMASource
*)source
;
3079 ssource
->rioc
= rioc
;
3080 object_ref(OBJECT(rioc
));
3082 ssource
->condition
= condition
;
3087 static void qio_channel_rdma_set_aio_fd_handler(QIOChannel
*ioc
,
3088 AioContext
*read_ctx
,
3090 AioContext
*write_ctx
,
3091 IOHandler
*io_write
,
3094 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
3096 aio_set_fd_handler(read_ctx
, rioc
->rdmain
->recv_comp_channel
->fd
,
3097 io_read
, io_write
, NULL
, NULL
, opaque
);
3098 aio_set_fd_handler(read_ctx
, rioc
->rdmain
->send_comp_channel
->fd
,
3099 io_read
, io_write
, NULL
, NULL
, opaque
);
3101 aio_set_fd_handler(write_ctx
, rioc
->rdmaout
->recv_comp_channel
->fd
,
3102 io_read
, io_write
, NULL
, NULL
, opaque
);
3103 aio_set_fd_handler(write_ctx
, rioc
->rdmaout
->send_comp_channel
->fd
,
3104 io_read
, io_write
, NULL
, NULL
, opaque
);
3108 struct rdma_close_rcu
{
3109 struct rcu_head rcu
;
3110 RDMAContext
*rdmain
;
3111 RDMAContext
*rdmaout
;
3114 /* callback from qio_channel_rdma_close via call_rcu */
3115 static void qio_channel_rdma_close_rcu(struct rdma_close_rcu
*rcu
)
3118 qemu_rdma_cleanup(rcu
->rdmain
);
3122 qemu_rdma_cleanup(rcu
->rdmaout
);
3125 g_free(rcu
->rdmain
);
3126 g_free(rcu
->rdmaout
);
3130 static int qio_channel_rdma_close(QIOChannel
*ioc
,
3133 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
3134 RDMAContext
*rdmain
, *rdmaout
;
3135 struct rdma_close_rcu
*rcu
= g_new(struct rdma_close_rcu
, 1);
3137 trace_qemu_rdma_close();
3139 rdmain
= rioc
->rdmain
;
3141 qatomic_rcu_set(&rioc
->rdmain
, NULL
);
3144 rdmaout
= rioc
->rdmaout
;
3146 qatomic_rcu_set(&rioc
->rdmaout
, NULL
);
3149 rcu
->rdmain
= rdmain
;
3150 rcu
->rdmaout
= rdmaout
;
3151 call_rcu(rcu
, qio_channel_rdma_close_rcu
, rcu
);
3157 qio_channel_rdma_shutdown(QIOChannel
*ioc
,
3158 QIOChannelShutdown how
,
3161 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
3162 RDMAContext
*rdmain
, *rdmaout
;
3164 RCU_READ_LOCK_GUARD();
3166 rdmain
= qatomic_rcu_read(&rioc
->rdmain
);
3167 rdmaout
= qatomic_rcu_read(&rioc
->rdmain
);
3170 case QIO_CHANNEL_SHUTDOWN_READ
:
3172 rdmain
->error_state
= -1;
3175 case QIO_CHANNEL_SHUTDOWN_WRITE
:
3177 rdmaout
->error_state
= -1;
3180 case QIO_CHANNEL_SHUTDOWN_BOTH
:
3183 rdmain
->error_state
= -1;
3186 rdmaout
->error_state
= -1;
3197 * This means that 'block_offset' is a full virtual address that does not
3198 * belong to a RAMBlock of the virtual machine and instead
3199 * represents a private malloc'd memory area that the caller wishes to
3203 * Offset is an offset to be added to block_offset and used
3204 * to also lookup the corresponding RAMBlock.
3206 * @size : Number of bytes to transfer
3208 * @pages_sent : User-specificed pointer to indicate how many pages were
3209 * sent. Usually, this will not be more than a few bytes of
3210 * the protocol because most transfers are sent asynchronously.
3212 static int qemu_rdma_save_page(QEMUFile
*f
, ram_addr_t block_offset
,
3213 ram_addr_t offset
, size_t size
)
3215 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(qemu_file_get_ioc(f
));
3219 if (migration_in_postcopy()) {
3220 return RAM_SAVE_CONTROL_NOT_SUPP
;
3223 RCU_READ_LOCK_GUARD();
3224 rdma
= qatomic_rcu_read(&rioc
->rdmaout
);
3230 CHECK_ERROR_STATE();
3235 * Add this page to the current 'chunk'. If the chunk
3236 * is full, or the page doesn't belong to the current chunk,
3237 * an actual RDMA write will occur and a new chunk will be formed.
3239 ret
= qemu_rdma_write(rdma
, block_offset
, offset
, size
);
3241 error_report("rdma migration: write error! %d", ret
);
3246 * Drain the Completion Queue if possible, but do not block,
3249 * If nothing to poll, the end of the iteration will do this
3250 * again to make sure we don't overflow the request queue.
3253 uint64_t wr_id
, wr_id_in
;
3254 ret
= qemu_rdma_poll(rdma
, rdma
->recv_cq
, &wr_id_in
, NULL
);
3257 error_report("rdma migration: polling error! %d", ret
);
3261 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
3263 if (wr_id
== RDMA_WRID_NONE
) {
3269 uint64_t wr_id
, wr_id_in
;
3270 ret
= qemu_rdma_poll(rdma
, rdma
->send_cq
, &wr_id_in
, NULL
);
3273 error_report("rdma migration: polling error! %d", ret
);
3277 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
3279 if (wr_id
== RDMA_WRID_NONE
) {
3284 return RAM_SAVE_CONTROL_DELAYED
;
3286 rdma
->error_state
= ret
;
3290 static void rdma_accept_incoming_migration(void *opaque
);
3292 static void rdma_cm_poll_handler(void *opaque
)
3294 RDMAContext
*rdma
= opaque
;
3296 struct rdma_cm_event
*cm_event
;
3297 MigrationIncomingState
*mis
= migration_incoming_get_current();
3299 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
3301 error_report("get_cm_event failed %d", errno
);
3305 if (cm_event
->event
== RDMA_CM_EVENT_DISCONNECTED
||
3306 cm_event
->event
== RDMA_CM_EVENT_DEVICE_REMOVAL
) {
3307 if (!rdma
->error_state
&&
3308 migration_incoming_get_current()->state
!=
3309 MIGRATION_STATUS_COMPLETED
) {
3310 error_report("receive cm event, cm event is %d", cm_event
->event
);
3311 rdma
->error_state
= -EPIPE
;
3312 if (rdma
->return_path
) {
3313 rdma
->return_path
->error_state
= -EPIPE
;
3316 rdma_ack_cm_event(cm_event
);
3317 if (mis
->loadvm_co
) {
3318 qemu_coroutine_enter(mis
->loadvm_co
);
3322 rdma_ack_cm_event(cm_event
);
3325 static int qemu_rdma_accept(RDMAContext
*rdma
)
3327 RDMACapabilities cap
;
3328 struct rdma_conn_param conn_param
= {
3329 .responder_resources
= 2,
3330 .private_data
= &cap
,
3331 .private_data_len
= sizeof(cap
),
3333 RDMAContext
*rdma_return_path
= NULL
;
3334 struct rdma_cm_event
*cm_event
;
3335 struct ibv_context
*verbs
;
3339 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
3341 goto err_rdma_dest_wait
;
3344 if (cm_event
->event
!= RDMA_CM_EVENT_CONNECT_REQUEST
) {
3345 rdma_ack_cm_event(cm_event
);
3347 goto err_rdma_dest_wait
;
3351 * initialize the RDMAContext for return path for postcopy after first
3352 * connection request reached.
3354 if ((migrate_postcopy() || migrate_return_path())
3355 && !rdma
->is_return_path
) {
3356 rdma_return_path
= qemu_rdma_data_init(rdma
->host_port
, NULL
);
3357 if (rdma_return_path
== NULL
) {
3358 rdma_ack_cm_event(cm_event
);
3360 goto err_rdma_dest_wait
;
3363 qemu_rdma_return_path_dest_init(rdma_return_path
, rdma
);
3366 memcpy(&cap
, cm_event
->param
.conn
.private_data
, sizeof(cap
));
3368 network_to_caps(&cap
);
3370 if (cap
.version
< 1 || cap
.version
> RDMA_CONTROL_VERSION_CURRENT
) {
3371 error_report("Unknown source RDMA version: %d, bailing...",
3373 rdma_ack_cm_event(cm_event
);
3375 goto err_rdma_dest_wait
;
3379 * Respond with only the capabilities this version of QEMU knows about.
3381 cap
.flags
&= known_capabilities
;
3384 * Enable the ones that we do know about.
3385 * Add other checks here as new ones are introduced.
3387 if (cap
.flags
& RDMA_CAPABILITY_PIN_ALL
) {
3388 rdma
->pin_all
= true;
3391 rdma
->cm_id
= cm_event
->id
;
3392 verbs
= cm_event
->id
->verbs
;
3394 rdma_ack_cm_event(cm_event
);
3396 trace_qemu_rdma_accept_pin_state(rdma
->pin_all
);
3398 caps_to_network(&cap
);
3400 trace_qemu_rdma_accept_pin_verbsc(verbs
);
3403 rdma
->verbs
= verbs
;
3404 } else if (rdma
->verbs
!= verbs
) {
3405 error_report("ibv context not matching %p, %p!", rdma
->verbs
,
3408 goto err_rdma_dest_wait
;
3411 qemu_rdma_dump_id("dest_init", verbs
);
3413 ret
= qemu_rdma_alloc_pd_cq(rdma
);
3415 error_report("rdma migration: error allocating pd and cq!");
3416 goto err_rdma_dest_wait
;
3419 ret
= qemu_rdma_alloc_qp(rdma
);
3421 error_report("rdma migration: error allocating qp!");
3422 goto err_rdma_dest_wait
;
3425 qemu_rdma_init_ram_blocks(rdma
);
3427 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
3428 ret
= qemu_rdma_reg_control(rdma
, idx
);
3430 error_report("rdma: error registering %d control", idx
);
3431 goto err_rdma_dest_wait
;
3435 /* Accept the second connection request for return path */
3436 if ((migrate_postcopy() || migrate_return_path())
3437 && !rdma
->is_return_path
) {
3438 qemu_set_fd_handler(rdma
->channel
->fd
, rdma_accept_incoming_migration
,
3440 (void *)(intptr_t)rdma
->return_path
);
3442 qemu_set_fd_handler(rdma
->channel
->fd
, rdma_cm_poll_handler
,
3446 ret
= rdma_accept(rdma
->cm_id
, &conn_param
);
3448 error_report("rdma_accept returns %d", ret
);
3449 goto err_rdma_dest_wait
;
3452 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
3454 error_report("rdma_accept get_cm_event failed %d", ret
);
3455 goto err_rdma_dest_wait
;
3458 if (cm_event
->event
!= RDMA_CM_EVENT_ESTABLISHED
) {
3459 error_report("rdma_accept not event established");
3460 rdma_ack_cm_event(cm_event
);
3462 goto err_rdma_dest_wait
;
3465 rdma_ack_cm_event(cm_event
);
3466 rdma
->connected
= true;
3468 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
3470 error_report("rdma migration: error posting second control recv");
3471 goto err_rdma_dest_wait
;
3474 qemu_rdma_dump_gid("dest_connect", rdma
->cm_id
);
3479 rdma
->error_state
= ret
;
3480 qemu_rdma_cleanup(rdma
);
3481 g_free(rdma_return_path
);
3485 static int dest_ram_sort_func(const void *a
, const void *b
)
3487 unsigned int a_index
= ((const RDMALocalBlock
*)a
)->src_index
;
3488 unsigned int b_index
= ((const RDMALocalBlock
*)b
)->src_index
;
3490 return (a_index
< b_index
) ? -1 : (a_index
!= b_index
);
3494 * During each iteration of the migration, we listen for instructions
3495 * by the source VM to perform dynamic page registrations before they
3496 * can perform RDMA operations.
3498 * We respond with the 'rkey'.
3500 * Keep doing this until the source tells us to stop.
3502 static int qemu_rdma_registration_handle(QEMUFile
*f
)
3504 RDMAControlHeader reg_resp
= { .len
= sizeof(RDMARegisterResult
),
3505 .type
= RDMA_CONTROL_REGISTER_RESULT
,
3508 RDMAControlHeader unreg_resp
= { .len
= 0,
3509 .type
= RDMA_CONTROL_UNREGISTER_FINISHED
,
3512 RDMAControlHeader blocks
= { .type
= RDMA_CONTROL_RAM_BLOCKS_RESULT
,
3514 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(qemu_file_get_ioc(f
));
3516 RDMALocalBlocks
*local
;
3517 RDMAControlHeader head
;
3518 RDMARegister
*reg
, *registers
;
3520 RDMARegisterResult
*reg_result
;
3521 static RDMARegisterResult results
[RDMA_CONTROL_MAX_COMMANDS_PER_MESSAGE
];
3522 RDMALocalBlock
*block
;
3529 RCU_READ_LOCK_GUARD();
3530 rdma
= qatomic_rcu_read(&rioc
->rdmain
);
3536 CHECK_ERROR_STATE();
3538 local
= &rdma
->local_ram_blocks
;
3540 trace_qemu_rdma_registration_handle_wait();
3542 ret
= qemu_rdma_exchange_recv(rdma
, &head
, RDMA_CONTROL_NONE
);
3548 if (head
.repeat
> RDMA_CONTROL_MAX_COMMANDS_PER_MESSAGE
) {
3549 error_report("rdma: Too many requests in this message (%d)."
3550 "Bailing.", head
.repeat
);
3555 switch (head
.type
) {
3556 case RDMA_CONTROL_COMPRESS
:
3557 comp
= (RDMACompress
*) rdma
->wr_data
[idx
].control_curr
;
3558 network_to_compress(comp
);
3560 trace_qemu_rdma_registration_handle_compress(comp
->length
,
3563 if (comp
->block_idx
>= rdma
->local_ram_blocks
.nb_blocks
) {
3564 error_report("rdma: 'compress' bad block index %u (vs %d)",
3565 (unsigned int)comp
->block_idx
,
3566 rdma
->local_ram_blocks
.nb_blocks
);
3570 block
= &(rdma
->local_ram_blocks
.block
[comp
->block_idx
]);
3572 host_addr
= block
->local_host_addr
+
3573 (comp
->offset
- block
->offset
);
3575 ram_handle_compressed(host_addr
, comp
->value
, comp
->length
);
3578 case RDMA_CONTROL_REGISTER_FINISHED
:
3579 trace_qemu_rdma_registration_handle_finished();
3582 case RDMA_CONTROL_RAM_BLOCKS_REQUEST
:
3583 trace_qemu_rdma_registration_handle_ram_blocks();
3585 /* Sort our local RAM Block list so it's the same as the source,
3586 * we can do this since we've filled in a src_index in the list
3587 * as we received the RAMBlock list earlier.
3589 qsort(rdma
->local_ram_blocks
.block
,
3590 rdma
->local_ram_blocks
.nb_blocks
,
3591 sizeof(RDMALocalBlock
), dest_ram_sort_func
);
3592 for (i
= 0; i
< local
->nb_blocks
; i
++) {
3593 local
->block
[i
].index
= i
;
3596 if (rdma
->pin_all
) {
3597 ret
= qemu_rdma_reg_whole_ram_blocks(rdma
);
3599 error_report("rdma migration: error dest "
3600 "registering ram blocks");
3606 * Dest uses this to prepare to transmit the RAMBlock descriptions
3607 * to the source VM after connection setup.
3608 * Both sides use the "remote" structure to communicate and update
3609 * their "local" descriptions with what was sent.
3611 for (i
= 0; i
< local
->nb_blocks
; i
++) {
3612 rdma
->dest_blocks
[i
].remote_host_addr
=
3613 (uintptr_t)(local
->block
[i
].local_host_addr
);
3615 if (rdma
->pin_all
) {
3616 rdma
->dest_blocks
[i
].remote_rkey
= local
->block
[i
].mr
->rkey
;
3619 rdma
->dest_blocks
[i
].offset
= local
->block
[i
].offset
;
3620 rdma
->dest_blocks
[i
].length
= local
->block
[i
].length
;
3622 dest_block_to_network(&rdma
->dest_blocks
[i
]);
3623 trace_qemu_rdma_registration_handle_ram_blocks_loop(
3624 local
->block
[i
].block_name
,
3625 local
->block
[i
].offset
,
3626 local
->block
[i
].length
,
3627 local
->block
[i
].local_host_addr
,
3628 local
->block
[i
].src_index
);
3631 blocks
.len
= rdma
->local_ram_blocks
.nb_blocks
3632 * sizeof(RDMADestBlock
);
3635 ret
= qemu_rdma_post_send_control(rdma
,
3636 (uint8_t *) rdma
->dest_blocks
, &blocks
);
3639 error_report("rdma migration: error sending remote info");
3644 case RDMA_CONTROL_REGISTER_REQUEST
:
3645 trace_qemu_rdma_registration_handle_register(head
.repeat
);
3647 reg_resp
.repeat
= head
.repeat
;
3648 registers
= (RDMARegister
*) rdma
->wr_data
[idx
].control_curr
;
3650 for (count
= 0; count
< head
.repeat
; count
++) {
3652 uint8_t *chunk_start
, *chunk_end
;
3654 reg
= ®isters
[count
];
3655 network_to_register(reg
);
3657 reg_result
= &results
[count
];
3659 trace_qemu_rdma_registration_handle_register_loop(count
,
3660 reg
->current_index
, reg
->key
.current_addr
, reg
->chunks
);
3662 if (reg
->current_index
>= rdma
->local_ram_blocks
.nb_blocks
) {
3663 error_report("rdma: 'register' bad block index %u (vs %d)",
3664 (unsigned int)reg
->current_index
,
3665 rdma
->local_ram_blocks
.nb_blocks
);
3669 block
= &(rdma
->local_ram_blocks
.block
[reg
->current_index
]);
3670 if (block
->is_ram_block
) {
3671 if (block
->offset
> reg
->key
.current_addr
) {
3672 error_report("rdma: bad register address for block %s"
3673 " offset: %" PRIx64
" current_addr: %" PRIx64
,
3674 block
->block_name
, block
->offset
,
3675 reg
->key
.current_addr
);
3679 host_addr
= (block
->local_host_addr
+
3680 (reg
->key
.current_addr
- block
->offset
));
3681 chunk
= ram_chunk_index(block
->local_host_addr
,
3682 (uint8_t *) host_addr
);
3684 chunk
= reg
->key
.chunk
;
3685 host_addr
= block
->local_host_addr
+
3686 (reg
->key
.chunk
* (1UL << RDMA_REG_CHUNK_SHIFT
));
3687 /* Check for particularly bad chunk value */
3688 if (host_addr
< (void *)block
->local_host_addr
) {
3689 error_report("rdma: bad chunk for block %s"
3691 block
->block_name
, reg
->key
.chunk
);
3696 chunk_start
= ram_chunk_start(block
, chunk
);
3697 chunk_end
= ram_chunk_end(block
, chunk
+ reg
->chunks
);
3698 /* avoid "-Waddress-of-packed-member" warning */
3699 uint32_t tmp_rkey
= 0;
3700 if (qemu_rdma_register_and_get_keys(rdma
, block
,
3701 (uintptr_t)host_addr
, NULL
, &tmp_rkey
,
3702 chunk
, chunk_start
, chunk_end
)) {
3703 error_report("cannot get rkey");
3707 reg_result
->rkey
= tmp_rkey
;
3709 reg_result
->host_addr
= (uintptr_t)block
->local_host_addr
;
3711 trace_qemu_rdma_registration_handle_register_rkey(
3714 result_to_network(reg_result
);
3717 ret
= qemu_rdma_post_send_control(rdma
,
3718 (uint8_t *) results
, ®_resp
);
3721 error_report("Failed to send control buffer");
3725 case RDMA_CONTROL_UNREGISTER_REQUEST
:
3726 trace_qemu_rdma_registration_handle_unregister(head
.repeat
);
3727 unreg_resp
.repeat
= head
.repeat
;
3728 registers
= (RDMARegister
*) rdma
->wr_data
[idx
].control_curr
;
3730 for (count
= 0; count
< head
.repeat
; count
++) {
3731 reg
= ®isters
[count
];
3732 network_to_register(reg
);
3734 trace_qemu_rdma_registration_handle_unregister_loop(count
,
3735 reg
->current_index
, reg
->key
.chunk
);
3737 block
= &(rdma
->local_ram_blocks
.block
[reg
->current_index
]);
3739 ret
= ibv_dereg_mr(block
->pmr
[reg
->key
.chunk
]);
3740 block
->pmr
[reg
->key
.chunk
] = NULL
;
3743 perror("rdma unregistration chunk failed");
3748 rdma
->total_registrations
--;
3750 trace_qemu_rdma_registration_handle_unregister_success(
3754 ret
= qemu_rdma_post_send_control(rdma
, NULL
, &unreg_resp
);
3757 error_report("Failed to send control buffer");
3761 case RDMA_CONTROL_REGISTER_RESULT
:
3762 error_report("Invalid RESULT message at dest.");
3766 error_report("Unknown control message %s", control_desc(head
.type
));
3773 rdma
->error_state
= ret
;
3779 * Called via a ram_control_load_hook during the initial RAM load section which
3780 * lists the RAMBlocks by name. This lets us know the order of the RAMBlocks
3782 * We've already built our local RAMBlock list, but not yet sent the list to
3786 rdma_block_notification_handle(QEMUFile
*f
, const char *name
)
3789 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(qemu_file_get_ioc(f
));
3793 RCU_READ_LOCK_GUARD();
3794 rdma
= qatomic_rcu_read(&rioc
->rdmain
);
3800 /* Find the matching RAMBlock in our local list */
3801 for (curr
= 0; curr
< rdma
->local_ram_blocks
.nb_blocks
; curr
++) {
3802 if (!strcmp(rdma
->local_ram_blocks
.block
[curr
].block_name
, name
)) {
3809 error_report("RAMBlock '%s' not found on destination", name
);
3813 rdma
->local_ram_blocks
.block
[curr
].src_index
= rdma
->next_src_index
;
3814 trace_rdma_block_notification_handle(name
, rdma
->next_src_index
);
3815 rdma
->next_src_index
++;
3820 static int rdma_load_hook(QEMUFile
*f
, uint64_t flags
, void *data
)
3823 case RAM_CONTROL_BLOCK_REG
:
3824 return rdma_block_notification_handle(f
, data
);
3826 case RAM_CONTROL_HOOK
:
3827 return qemu_rdma_registration_handle(f
);
3830 /* Shouldn't be called with any other values */
3835 static int qemu_rdma_registration_start(QEMUFile
*f
,
3836 uint64_t flags
, void *data
)
3838 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(qemu_file_get_ioc(f
));
3841 if (migration_in_postcopy()) {
3845 RCU_READ_LOCK_GUARD();
3846 rdma
= qatomic_rcu_read(&rioc
->rdmaout
);
3851 CHECK_ERROR_STATE();
3853 trace_qemu_rdma_registration_start(flags
);
3854 qemu_put_be64(f
, RAM_SAVE_FLAG_HOOK
);
3861 * Inform dest that dynamic registrations are done for now.
3862 * First, flush writes, if any.
3864 static int qemu_rdma_registration_stop(QEMUFile
*f
,
3865 uint64_t flags
, void *data
)
3867 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(qemu_file_get_ioc(f
));
3869 RDMAControlHeader head
= { .len
= 0, .repeat
= 1 };
3872 if (migration_in_postcopy()) {
3876 RCU_READ_LOCK_GUARD();
3877 rdma
= qatomic_rcu_read(&rioc
->rdmaout
);
3882 CHECK_ERROR_STATE();
3885 ret
= qemu_rdma_drain_cq(rdma
);
3891 if (flags
== RAM_CONTROL_SETUP
) {
3892 RDMAControlHeader resp
= {.type
= RDMA_CONTROL_RAM_BLOCKS_RESULT
};
3893 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
3894 int reg_result_idx
, i
, nb_dest_blocks
;
3896 head
.type
= RDMA_CONTROL_RAM_BLOCKS_REQUEST
;
3897 trace_qemu_rdma_registration_stop_ram();
3900 * Make sure that we parallelize the pinning on both sides.
3901 * For very large guests, doing this serially takes a really
3902 * long time, so we have to 'interleave' the pinning locally
3903 * with the control messages by performing the pinning on this
3904 * side before we receive the control response from the other
3905 * side that the pinning has completed.
3907 ret
= qemu_rdma_exchange_send(rdma
, &head
, NULL
, &resp
,
3908 ®_result_idx
, rdma
->pin_all
?
3909 qemu_rdma_reg_whole_ram_blocks
: NULL
);
3911 fprintf(stderr
, "receiving remote info!");
3915 nb_dest_blocks
= resp
.len
/ sizeof(RDMADestBlock
);
3918 * The protocol uses two different sets of rkeys (mutually exclusive):
3919 * 1. One key to represent the virtual address of the entire ram block.
3920 * (dynamic chunk registration disabled - pin everything with one rkey.)
3921 * 2. One to represent individual chunks within a ram block.
3922 * (dynamic chunk registration enabled - pin individual chunks.)
3924 * Once the capability is successfully negotiated, the destination transmits
3925 * the keys to use (or sends them later) including the virtual addresses
3926 * and then propagates the remote ram block descriptions to his local copy.
3929 if (local
->nb_blocks
!= nb_dest_blocks
) {
3930 fprintf(stderr
, "ram blocks mismatch (Number of blocks %d vs %d) "
3931 "Your QEMU command line parameters are probably "
3932 "not identical on both the source and destination.",
3933 local
->nb_blocks
, nb_dest_blocks
);
3934 rdma
->error_state
= -EINVAL
;
3938 qemu_rdma_move_header(rdma
, reg_result_idx
, &resp
);
3939 memcpy(rdma
->dest_blocks
,
3940 rdma
->wr_data
[reg_result_idx
].control_curr
, resp
.len
);
3941 for (i
= 0; i
< nb_dest_blocks
; i
++) {
3942 network_to_dest_block(&rdma
->dest_blocks
[i
]);
3944 /* We require that the blocks are in the same order */
3945 if (rdma
->dest_blocks
[i
].length
!= local
->block
[i
].length
) {
3946 fprintf(stderr
, "Block %s/%d has a different length %" PRIu64
3947 "vs %" PRIu64
, local
->block
[i
].block_name
, i
,
3948 local
->block
[i
].length
,
3949 rdma
->dest_blocks
[i
].length
);
3950 rdma
->error_state
= -EINVAL
;
3953 local
->block
[i
].remote_host_addr
=
3954 rdma
->dest_blocks
[i
].remote_host_addr
;
3955 local
->block
[i
].remote_rkey
= rdma
->dest_blocks
[i
].remote_rkey
;
3959 trace_qemu_rdma_registration_stop(flags
);
3961 head
.type
= RDMA_CONTROL_REGISTER_FINISHED
;
3962 ret
= qemu_rdma_exchange_send(rdma
, &head
, NULL
, NULL
, NULL
, NULL
);
3970 rdma
->error_state
= ret
;
3974 static const QEMUFileHooks rdma_read_hooks
= {
3975 .hook_ram_load
= rdma_load_hook
,
3978 static const QEMUFileHooks rdma_write_hooks
= {
3979 .before_ram_iterate
= qemu_rdma_registration_start
,
3980 .after_ram_iterate
= qemu_rdma_registration_stop
,
3981 .save_page
= qemu_rdma_save_page
,
3985 static void qio_channel_rdma_finalize(Object
*obj
)
3987 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(obj
);
3989 qemu_rdma_cleanup(rioc
->rdmain
);
3990 g_free(rioc
->rdmain
);
3991 rioc
->rdmain
= NULL
;
3993 if (rioc
->rdmaout
) {
3994 qemu_rdma_cleanup(rioc
->rdmaout
);
3995 g_free(rioc
->rdmaout
);
3996 rioc
->rdmaout
= NULL
;
4000 static void qio_channel_rdma_class_init(ObjectClass
*klass
,
4001 void *class_data G_GNUC_UNUSED
)
4003 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
4005 ioc_klass
->io_writev
= qio_channel_rdma_writev
;
4006 ioc_klass
->io_readv
= qio_channel_rdma_readv
;
4007 ioc_klass
->io_set_blocking
= qio_channel_rdma_set_blocking
;
4008 ioc_klass
->io_close
= qio_channel_rdma_close
;
4009 ioc_klass
->io_create_watch
= qio_channel_rdma_create_watch
;
4010 ioc_klass
->io_set_aio_fd_handler
= qio_channel_rdma_set_aio_fd_handler
;
4011 ioc_klass
->io_shutdown
= qio_channel_rdma_shutdown
;
4014 static const TypeInfo qio_channel_rdma_info
= {
4015 .parent
= TYPE_QIO_CHANNEL
,
4016 .name
= TYPE_QIO_CHANNEL_RDMA
,
4017 .instance_size
= sizeof(QIOChannelRDMA
),
4018 .instance_finalize
= qio_channel_rdma_finalize
,
4019 .class_init
= qio_channel_rdma_class_init
,
4022 static void qio_channel_rdma_register_types(void)
4024 type_register_static(&qio_channel_rdma_info
);
4027 type_init(qio_channel_rdma_register_types
);
4029 static QEMUFile
*rdma_new_input(RDMAContext
*rdma
)
4031 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(object_new(TYPE_QIO_CHANNEL_RDMA
));
4033 rioc
->file
= qemu_file_new_input(QIO_CHANNEL(rioc
));
4034 rioc
->rdmain
= rdma
;
4035 rioc
->rdmaout
= rdma
->return_path
;
4036 qemu_file_set_hooks(rioc
->file
, &rdma_read_hooks
);
4041 static QEMUFile
*rdma_new_output(RDMAContext
*rdma
)
4043 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(object_new(TYPE_QIO_CHANNEL_RDMA
));
4045 rioc
->file
= qemu_file_new_output(QIO_CHANNEL(rioc
));
4046 rioc
->rdmaout
= rdma
;
4047 rioc
->rdmain
= rdma
->return_path
;
4048 qemu_file_set_hooks(rioc
->file
, &rdma_write_hooks
);
4053 static void rdma_accept_incoming_migration(void *opaque
)
4055 RDMAContext
*rdma
= opaque
;
4058 Error
*local_err
= NULL
;
4060 trace_qemu_rdma_accept_incoming_migration();
4061 ret
= qemu_rdma_accept(rdma
);
4064 fprintf(stderr
, "RDMA ERROR: Migration initialization failed\n");
4068 trace_qemu_rdma_accept_incoming_migration_accepted();
4070 if (rdma
->is_return_path
) {
4074 f
= rdma_new_input(rdma
);
4076 fprintf(stderr
, "RDMA ERROR: could not open RDMA for input\n");
4077 qemu_rdma_cleanup(rdma
);
4081 rdma
->migration_started_on_destination
= 1;
4082 migration_fd_process_incoming(f
, &local_err
);
4084 error_reportf_err(local_err
, "RDMA ERROR:");
4088 void rdma_start_incoming_migration(const char *host_port
, Error
**errp
)
4093 trace_rdma_start_incoming_migration();
4095 /* Avoid ram_block_discard_disable(), cannot change during migration. */
4096 if (ram_block_discard_is_required()) {
4097 error_setg(errp
, "RDMA: cannot disable RAM discard");
4101 rdma
= qemu_rdma_data_init(host_port
, errp
);
4106 ret
= qemu_rdma_dest_init(rdma
, errp
);
4111 trace_rdma_start_incoming_migration_after_dest_init();
4113 ret
= rdma_listen(rdma
->listen_id
, 5);
4116 ERROR(errp
, "listening on socket!");
4120 trace_rdma_start_incoming_migration_after_rdma_listen();
4122 qemu_set_fd_handler(rdma
->channel
->fd
, rdma_accept_incoming_migration
,
4123 NULL
, (void *)(intptr_t)rdma
);
4127 qemu_rdma_cleanup(rdma
);
4131 g_free(rdma
->host_port
);
4136 void rdma_start_outgoing_migration(void *opaque
,
4137 const char *host_port
, Error
**errp
)
4139 MigrationState
*s
= opaque
;
4140 RDMAContext
*rdma_return_path
= NULL
;
4144 /* Avoid ram_block_discard_disable(), cannot change during migration. */
4145 if (ram_block_discard_is_required()) {
4146 error_setg(errp
, "RDMA: cannot disable RAM discard");
4150 rdma
= qemu_rdma_data_init(host_port
, errp
);
4155 ret
= qemu_rdma_source_init(rdma
, migrate_rdma_pin_all(), errp
);
4161 trace_rdma_start_outgoing_migration_after_rdma_source_init();
4162 ret
= qemu_rdma_connect(rdma
, false, errp
);
4168 /* RDMA postcopy need a separate queue pair for return path */
4169 if (migrate_postcopy() || migrate_return_path()) {
4170 rdma_return_path
= qemu_rdma_data_init(host_port
, errp
);
4172 if (rdma_return_path
== NULL
) {
4173 goto return_path_err
;
4176 ret
= qemu_rdma_source_init(rdma_return_path
,
4177 migrate_rdma_pin_all(), errp
);
4180 goto return_path_err
;
4183 ret
= qemu_rdma_connect(rdma_return_path
, true, errp
);
4186 goto return_path_err
;
4189 rdma
->return_path
= rdma_return_path
;
4190 rdma_return_path
->return_path
= rdma
;
4191 rdma_return_path
->is_return_path
= true;
4194 trace_rdma_start_outgoing_migration_after_rdma_connect();
4196 s
->to_dst_file
= rdma_new_output(rdma
);
4197 migrate_fd_connect(s
, NULL
);
4200 qemu_rdma_cleanup(rdma
);
4203 g_free(rdma_return_path
);