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"
43 #define RDMA_RESOLVE_TIMEOUT_MS 10000
45 /* Do not merge data if larger than this. */
46 #define RDMA_MERGE_MAX (2 * 1024 * 1024)
47 #define RDMA_SIGNALED_SEND_MAX (RDMA_MERGE_MAX / 4096)
49 #define RDMA_REG_CHUNK_SHIFT 20 /* 1 MB */
52 * This is only for non-live state being migrated.
53 * Instead of RDMA_WRITE messages, we use RDMA_SEND
54 * messages for that state, which requires a different
55 * delivery design than main memory.
57 #define RDMA_SEND_INCREMENT 32768
60 * Maximum size infiniband SEND message
62 #define RDMA_CONTROL_MAX_BUFFER (512 * 1024)
63 #define RDMA_CONTROL_MAX_COMMANDS_PER_MESSAGE 4096
65 #define RDMA_CONTROL_VERSION_CURRENT 1
67 * Capabilities for negotiation.
69 #define RDMA_CAPABILITY_PIN_ALL 0x01
72 * Add the other flags above to this list of known capabilities
73 * as they are introduced.
75 static uint32_t known_capabilities
= RDMA_CAPABILITY_PIN_ALL
;
78 * A work request ID is 64-bits and we split up these bits
81 * bits 0-15 : type of control message, 2^16
82 * bits 16-29: ram block index, 2^14
83 * bits 30-63: ram block chunk number, 2^34
85 * The last two bit ranges are only used for RDMA writes,
86 * in order to track their completion and potentially
87 * also track unregistration status of the message.
89 #define RDMA_WRID_TYPE_SHIFT 0UL
90 #define RDMA_WRID_BLOCK_SHIFT 16UL
91 #define RDMA_WRID_CHUNK_SHIFT 30UL
93 #define RDMA_WRID_TYPE_MASK \
94 ((1UL << RDMA_WRID_BLOCK_SHIFT) - 1UL)
96 #define RDMA_WRID_BLOCK_MASK \
97 (~RDMA_WRID_TYPE_MASK & ((1UL << RDMA_WRID_CHUNK_SHIFT) - 1UL))
99 #define RDMA_WRID_CHUNK_MASK (~RDMA_WRID_BLOCK_MASK & ~RDMA_WRID_TYPE_MASK)
102 * RDMA migration protocol:
103 * 1. RDMA Writes (data messages, i.e. RAM)
104 * 2. IB Send/Recv (control channel messages)
108 RDMA_WRID_RDMA_WRITE
= 1,
109 RDMA_WRID_SEND_CONTROL
= 2000,
110 RDMA_WRID_RECV_CONTROL
= 4000,
114 * Work request IDs for IB SEND messages only (not RDMA writes).
115 * This is used by the migration protocol to transmit
116 * control messages (such as device state and registration commands)
118 * We could use more WRs, but we have enough for now.
128 * SEND/RECV IB Control Messages.
131 RDMA_CONTROL_NONE
= 0,
133 RDMA_CONTROL_READY
, /* ready to receive */
134 RDMA_CONTROL_QEMU_FILE
, /* QEMUFile-transmitted bytes */
135 RDMA_CONTROL_RAM_BLOCKS_REQUEST
, /* RAMBlock synchronization */
136 RDMA_CONTROL_RAM_BLOCKS_RESULT
, /* RAMBlock synchronization */
137 RDMA_CONTROL_COMPRESS
, /* page contains repeat values */
138 RDMA_CONTROL_REGISTER_REQUEST
, /* dynamic page registration */
139 RDMA_CONTROL_REGISTER_RESULT
, /* key to use after registration */
140 RDMA_CONTROL_REGISTER_FINISHED
, /* current iteration finished */
141 RDMA_CONTROL_UNREGISTER_REQUEST
, /* dynamic UN-registration */
142 RDMA_CONTROL_UNREGISTER_FINISHED
, /* unpinning finished */
147 * Memory and MR structures used to represent an IB Send/Recv work request.
148 * This is *not* used for RDMA writes, only IB Send/Recv.
151 uint8_t control
[RDMA_CONTROL_MAX_BUFFER
]; /* actual buffer to register */
152 struct ibv_mr
*control_mr
; /* registration metadata */
153 size_t control_len
; /* length of the message */
154 uint8_t *control_curr
; /* start of unconsumed bytes */
155 } RDMAWorkRequestData
;
158 * Negotiate RDMA capabilities during connection-setup time.
165 static void caps_to_network(RDMACapabilities
*cap
)
167 cap
->version
= htonl(cap
->version
);
168 cap
->flags
= htonl(cap
->flags
);
171 static void network_to_caps(RDMACapabilities
*cap
)
173 cap
->version
= ntohl(cap
->version
);
174 cap
->flags
= ntohl(cap
->flags
);
178 * Representation of a RAMBlock from an RDMA perspective.
179 * This is not transmitted, only local.
180 * This and subsequent structures cannot be linked lists
181 * because we're using a single IB message to transmit
182 * the information. It's small anyway, so a list is overkill.
184 typedef struct RDMALocalBlock
{
186 uint8_t *local_host_addr
; /* local virtual address */
187 uint64_t remote_host_addr
; /* remote virtual address */
190 struct ibv_mr
**pmr
; /* MRs for chunk-level registration */
191 struct ibv_mr
*mr
; /* MR for non-chunk-level registration */
192 uint32_t *remote_keys
; /* rkeys for chunk-level registration */
193 uint32_t remote_rkey
; /* rkeys for non-chunk-level registration */
194 int index
; /* which block are we */
195 unsigned int src_index
; /* (Only used on dest) */
198 unsigned long *transit_bitmap
;
199 unsigned long *unregister_bitmap
;
203 * Also represents a RAMblock, but only on the dest.
204 * This gets transmitted by the dest during connection-time
205 * to the source VM and then is used to populate the
206 * corresponding RDMALocalBlock with
207 * the information needed to perform the actual RDMA.
209 typedef struct QEMU_PACKED RDMADestBlock
{
210 uint64_t remote_host_addr
;
213 uint32_t remote_rkey
;
217 static const char *control_desc(unsigned int rdma_control
)
219 static const char *strs
[] = {
220 [RDMA_CONTROL_NONE
] = "NONE",
221 [RDMA_CONTROL_ERROR
] = "ERROR",
222 [RDMA_CONTROL_READY
] = "READY",
223 [RDMA_CONTROL_QEMU_FILE
] = "QEMU FILE",
224 [RDMA_CONTROL_RAM_BLOCKS_REQUEST
] = "RAM BLOCKS REQUEST",
225 [RDMA_CONTROL_RAM_BLOCKS_RESULT
] = "RAM BLOCKS RESULT",
226 [RDMA_CONTROL_COMPRESS
] = "COMPRESS",
227 [RDMA_CONTROL_REGISTER_REQUEST
] = "REGISTER REQUEST",
228 [RDMA_CONTROL_REGISTER_RESULT
] = "REGISTER RESULT",
229 [RDMA_CONTROL_REGISTER_FINISHED
] = "REGISTER FINISHED",
230 [RDMA_CONTROL_UNREGISTER_REQUEST
] = "UNREGISTER REQUEST",
231 [RDMA_CONTROL_UNREGISTER_FINISHED
] = "UNREGISTER FINISHED",
234 if (rdma_control
> RDMA_CONTROL_UNREGISTER_FINISHED
) {
235 return "??BAD CONTROL VALUE??";
238 return strs
[rdma_control
];
241 static uint64_t htonll(uint64_t v
)
243 union { uint32_t lv
[2]; uint64_t llv
; } u
;
244 u
.lv
[0] = htonl(v
>> 32);
245 u
.lv
[1] = htonl(v
& 0xFFFFFFFFULL
);
249 static uint64_t ntohll(uint64_t v
)
251 union { uint32_t lv
[2]; uint64_t llv
; } u
;
253 return ((uint64_t)ntohl(u
.lv
[0]) << 32) | (uint64_t) ntohl(u
.lv
[1]);
256 static void dest_block_to_network(RDMADestBlock
*db
)
258 db
->remote_host_addr
= htonll(db
->remote_host_addr
);
259 db
->offset
= htonll(db
->offset
);
260 db
->length
= htonll(db
->length
);
261 db
->remote_rkey
= htonl(db
->remote_rkey
);
264 static void network_to_dest_block(RDMADestBlock
*db
)
266 db
->remote_host_addr
= ntohll(db
->remote_host_addr
);
267 db
->offset
= ntohll(db
->offset
);
268 db
->length
= ntohll(db
->length
);
269 db
->remote_rkey
= ntohl(db
->remote_rkey
);
273 * Virtual address of the above structures used for transmitting
274 * the RAMBlock descriptions at connection-time.
275 * This structure is *not* transmitted.
277 typedef struct RDMALocalBlocks
{
279 bool init
; /* main memory init complete */
280 RDMALocalBlock
*block
;
284 * Main data structure for RDMA state.
285 * While there is only one copy of this structure being allocated right now,
286 * this is the place where one would start if you wanted to consider
287 * having more than one RDMA connection open at the same time.
289 typedef struct RDMAContext
{
294 RDMAWorkRequestData wr_data
[RDMA_WRID_MAX
];
297 * This is used by *_exchange_send() to figure out whether or not
298 * the initial "READY" message has already been received or not.
299 * This is because other functions may potentially poll() and detect
300 * the READY message before send() does, in which case we need to
301 * know if it completed.
303 int control_ready_expected
;
305 /* number of outstanding writes */
308 /* store info about current buffer so that we can
309 merge it with future sends */
310 uint64_t current_addr
;
311 uint64_t current_length
;
312 /* index of ram block the current buffer belongs to */
314 /* index of the chunk in the current ram block */
320 * infiniband-specific variables for opening the device
321 * and maintaining connection state and so forth.
323 * cm_id also has ibv_context, rdma_event_channel, and ibv_qp in
324 * cm_id->verbs, cm_id->channel, and cm_id->qp.
326 struct rdma_cm_id
*cm_id
; /* connection manager ID */
327 struct rdma_cm_id
*listen_id
;
330 struct ibv_context
*verbs
;
331 struct rdma_event_channel
*channel
;
332 struct ibv_qp
*qp
; /* queue pair */
333 struct ibv_comp_channel
*recv_comp_channel
; /* recv completion channel */
334 struct ibv_comp_channel
*send_comp_channel
; /* send completion channel */
335 struct ibv_pd
*pd
; /* protection domain */
336 struct ibv_cq
*recv_cq
; /* recvieve completion queue */
337 struct ibv_cq
*send_cq
; /* send completion queue */
340 * If a previous write failed (perhaps because of a failed
341 * memory registration, then do not attempt any future work
342 * and remember the error state.
349 * Description of ram blocks used throughout the code.
351 RDMALocalBlocks local_ram_blocks
;
352 RDMADestBlock
*dest_blocks
;
354 /* Index of the next RAMBlock received during block registration */
355 unsigned int next_src_index
;
358 * Migration on *destination* started.
359 * Then use coroutine yield function.
360 * Source runs in a thread, so we don't care.
362 int migration_started_on_destination
;
364 int total_registrations
;
367 int unregister_current
, unregister_next
;
368 uint64_t unregistrations
[RDMA_SIGNALED_SEND_MAX
];
370 GHashTable
*blockmap
;
372 /* the RDMAContext for return path */
373 struct RDMAContext
*return_path
;
377 #define TYPE_QIO_CHANNEL_RDMA "qio-channel-rdma"
378 OBJECT_DECLARE_SIMPLE_TYPE(QIOChannelRDMA
, QIO_CHANNEL_RDMA
)
382 struct QIOChannelRDMA
{
385 RDMAContext
*rdmaout
;
387 bool blocking
; /* XXX we don't actually honour this yet */
391 * Main structure for IB Send/Recv control messages.
392 * This gets prepended at the beginning of every Send/Recv.
394 typedef struct QEMU_PACKED
{
395 uint32_t len
; /* Total length of data portion */
396 uint32_t type
; /* which control command to perform */
397 uint32_t repeat
; /* number of commands in data portion of same type */
401 static void control_to_network(RDMAControlHeader
*control
)
403 control
->type
= htonl(control
->type
);
404 control
->len
= htonl(control
->len
);
405 control
->repeat
= htonl(control
->repeat
);
408 static void network_to_control(RDMAControlHeader
*control
)
410 control
->type
= ntohl(control
->type
);
411 control
->len
= ntohl(control
->len
);
412 control
->repeat
= ntohl(control
->repeat
);
416 * Register a single Chunk.
417 * Information sent by the source VM to inform the dest
418 * to register an single chunk of memory before we can perform
419 * the actual RDMA operation.
421 typedef struct QEMU_PACKED
{
423 uint64_t current_addr
; /* offset into the ram_addr_t space */
424 uint64_t chunk
; /* chunk to lookup if unregistering */
426 uint32_t current_index
; /* which ramblock the chunk belongs to */
428 uint64_t chunks
; /* how many sequential chunks to register */
431 static bool rdma_errored(RDMAContext
*rdma
)
433 if (rdma
->errored
&& !rdma
->error_reported
) {
434 error_report("RDMA is in an error state waiting migration"
436 rdma
->error_reported
= true;
438 return rdma
->errored
;
441 static void register_to_network(RDMAContext
*rdma
, RDMARegister
*reg
)
443 RDMALocalBlock
*local_block
;
444 local_block
= &rdma
->local_ram_blocks
.block
[reg
->current_index
];
446 if (local_block
->is_ram_block
) {
448 * current_addr as passed in is an address in the local ram_addr_t
449 * space, we need to translate this for the destination
451 reg
->key
.current_addr
-= local_block
->offset
;
452 reg
->key
.current_addr
+= rdma
->dest_blocks
[reg
->current_index
].offset
;
454 reg
->key
.current_addr
= htonll(reg
->key
.current_addr
);
455 reg
->current_index
= htonl(reg
->current_index
);
456 reg
->chunks
= htonll(reg
->chunks
);
459 static void network_to_register(RDMARegister
*reg
)
461 reg
->key
.current_addr
= ntohll(reg
->key
.current_addr
);
462 reg
->current_index
= ntohl(reg
->current_index
);
463 reg
->chunks
= ntohll(reg
->chunks
);
466 typedef struct QEMU_PACKED
{
467 uint32_t value
; /* if zero, we will madvise() */
468 uint32_t block_idx
; /* which ram block index */
469 uint64_t offset
; /* Address in remote ram_addr_t space */
470 uint64_t length
; /* length of the chunk */
473 static void compress_to_network(RDMAContext
*rdma
, RDMACompress
*comp
)
475 comp
->value
= htonl(comp
->value
);
477 * comp->offset as passed in is an address in the local ram_addr_t
478 * space, we need to translate this for the destination
480 comp
->offset
-= rdma
->local_ram_blocks
.block
[comp
->block_idx
].offset
;
481 comp
->offset
+= rdma
->dest_blocks
[comp
->block_idx
].offset
;
482 comp
->block_idx
= htonl(comp
->block_idx
);
483 comp
->offset
= htonll(comp
->offset
);
484 comp
->length
= htonll(comp
->length
);
487 static void network_to_compress(RDMACompress
*comp
)
489 comp
->value
= ntohl(comp
->value
);
490 comp
->block_idx
= ntohl(comp
->block_idx
);
491 comp
->offset
= ntohll(comp
->offset
);
492 comp
->length
= ntohll(comp
->length
);
496 * The result of the dest's memory registration produces an "rkey"
497 * which the source VM must reference in order to perform
498 * the RDMA operation.
500 typedef struct QEMU_PACKED
{
504 } RDMARegisterResult
;
506 static void result_to_network(RDMARegisterResult
*result
)
508 result
->rkey
= htonl(result
->rkey
);
509 result
->host_addr
= htonll(result
->host_addr
);
512 static void network_to_result(RDMARegisterResult
*result
)
514 result
->rkey
= ntohl(result
->rkey
);
515 result
->host_addr
= ntohll(result
->host_addr
);
518 static int qemu_rdma_exchange_send(RDMAContext
*rdma
, RDMAControlHeader
*head
,
519 uint8_t *data
, RDMAControlHeader
*resp
,
521 int (*callback
)(RDMAContext
*rdma
));
523 static inline uint64_t ram_chunk_index(const uint8_t *start
,
526 return ((uintptr_t) host
- (uintptr_t) start
) >> RDMA_REG_CHUNK_SHIFT
;
529 static inline uint8_t *ram_chunk_start(const RDMALocalBlock
*rdma_ram_block
,
532 return (uint8_t *)(uintptr_t)(rdma_ram_block
->local_host_addr
+
533 (i
<< RDMA_REG_CHUNK_SHIFT
));
536 static inline uint8_t *ram_chunk_end(const RDMALocalBlock
*rdma_ram_block
,
539 uint8_t *result
= ram_chunk_start(rdma_ram_block
, i
) +
540 (1UL << RDMA_REG_CHUNK_SHIFT
);
542 if (result
> (rdma_ram_block
->local_host_addr
+ rdma_ram_block
->length
)) {
543 result
= rdma_ram_block
->local_host_addr
+ rdma_ram_block
->length
;
549 static void rdma_add_block(RDMAContext
*rdma
, const char *block_name
,
551 ram_addr_t block_offset
, uint64_t length
)
553 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
554 RDMALocalBlock
*block
;
555 RDMALocalBlock
*old
= local
->block
;
557 local
->block
= g_new0(RDMALocalBlock
, local
->nb_blocks
+ 1);
559 if (local
->nb_blocks
) {
562 if (rdma
->blockmap
) {
563 for (x
= 0; x
< local
->nb_blocks
; x
++) {
564 g_hash_table_remove(rdma
->blockmap
,
565 (void *)(uintptr_t)old
[x
].offset
);
566 g_hash_table_insert(rdma
->blockmap
,
567 (void *)(uintptr_t)old
[x
].offset
,
571 memcpy(local
->block
, old
, sizeof(RDMALocalBlock
) * local
->nb_blocks
);
575 block
= &local
->block
[local
->nb_blocks
];
577 block
->block_name
= g_strdup(block_name
);
578 block
->local_host_addr
= host_addr
;
579 block
->offset
= block_offset
;
580 block
->length
= length
;
581 block
->index
= local
->nb_blocks
;
582 block
->src_index
= ~0U; /* Filled in by the receipt of the block list */
583 block
->nb_chunks
= ram_chunk_index(host_addr
, host_addr
+ length
) + 1UL;
584 block
->transit_bitmap
= bitmap_new(block
->nb_chunks
);
585 bitmap_clear(block
->transit_bitmap
, 0, block
->nb_chunks
);
586 block
->unregister_bitmap
= bitmap_new(block
->nb_chunks
);
587 bitmap_clear(block
->unregister_bitmap
, 0, block
->nb_chunks
);
588 block
->remote_keys
= g_new0(uint32_t, block
->nb_chunks
);
590 block
->is_ram_block
= local
->init
? false : true;
592 if (rdma
->blockmap
) {
593 g_hash_table_insert(rdma
->blockmap
, (void *)(uintptr_t)block_offset
, block
);
596 trace_rdma_add_block(block_name
, local
->nb_blocks
,
597 (uintptr_t) block
->local_host_addr
,
598 block
->offset
, block
->length
,
599 (uintptr_t) (block
->local_host_addr
+ block
->length
),
600 BITS_TO_LONGS(block
->nb_chunks
) *
601 sizeof(unsigned long) * 8,
608 * Memory regions need to be registered with the device and queue pairs setup
609 * in advanced before the migration starts. This tells us where the RAM blocks
610 * are so that we can register them individually.
612 static int qemu_rdma_init_one_block(RAMBlock
*rb
, void *opaque
)
614 const char *block_name
= qemu_ram_get_idstr(rb
);
615 void *host_addr
= qemu_ram_get_host_addr(rb
);
616 ram_addr_t block_offset
= qemu_ram_get_offset(rb
);
617 ram_addr_t length
= qemu_ram_get_used_length(rb
);
618 rdma_add_block(opaque
, block_name
, host_addr
, block_offset
, length
);
623 * Identify the RAMBlocks and their quantity. They will be references to
624 * identify chunk boundaries inside each RAMBlock and also be referenced
625 * during dynamic page registration.
627 static void qemu_rdma_init_ram_blocks(RDMAContext
*rdma
)
629 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
632 assert(rdma
->blockmap
== NULL
);
633 memset(local
, 0, sizeof *local
);
634 ret
= foreach_not_ignored_block(qemu_rdma_init_one_block
, rdma
);
636 trace_qemu_rdma_init_ram_blocks(local
->nb_blocks
);
637 rdma
->dest_blocks
= g_new0(RDMADestBlock
,
638 rdma
->local_ram_blocks
.nb_blocks
);
643 * Note: If used outside of cleanup, the caller must ensure that the destination
644 * block structures are also updated
646 static void rdma_delete_block(RDMAContext
*rdma
, RDMALocalBlock
*block
)
648 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
649 RDMALocalBlock
*old
= local
->block
;
652 if (rdma
->blockmap
) {
653 g_hash_table_remove(rdma
->blockmap
, (void *)(uintptr_t)block
->offset
);
658 for (j
= 0; j
< block
->nb_chunks
; j
++) {
659 if (!block
->pmr
[j
]) {
662 ibv_dereg_mr(block
->pmr
[j
]);
663 rdma
->total_registrations
--;
670 ibv_dereg_mr(block
->mr
);
671 rdma
->total_registrations
--;
675 g_free(block
->transit_bitmap
);
676 block
->transit_bitmap
= NULL
;
678 g_free(block
->unregister_bitmap
);
679 block
->unregister_bitmap
= NULL
;
681 g_free(block
->remote_keys
);
682 block
->remote_keys
= NULL
;
684 g_free(block
->block_name
);
685 block
->block_name
= NULL
;
687 if (rdma
->blockmap
) {
688 for (x
= 0; x
< local
->nb_blocks
; x
++) {
689 g_hash_table_remove(rdma
->blockmap
,
690 (void *)(uintptr_t)old
[x
].offset
);
694 if (local
->nb_blocks
> 1) {
696 local
->block
= g_new0(RDMALocalBlock
, local
->nb_blocks
- 1);
699 memcpy(local
->block
, old
, sizeof(RDMALocalBlock
) * block
->index
);
702 if (block
->index
< (local
->nb_blocks
- 1)) {
703 memcpy(local
->block
+ block
->index
, old
+ (block
->index
+ 1),
704 sizeof(RDMALocalBlock
) *
705 (local
->nb_blocks
- (block
->index
+ 1)));
706 for (x
= block
->index
; x
< local
->nb_blocks
- 1; x
++) {
707 local
->block
[x
].index
--;
711 assert(block
== local
->block
);
715 trace_rdma_delete_block(block
, (uintptr_t)block
->local_host_addr
,
716 block
->offset
, block
->length
,
717 (uintptr_t)(block
->local_host_addr
+ block
->length
),
718 BITS_TO_LONGS(block
->nb_chunks
) *
719 sizeof(unsigned long) * 8, block
->nb_chunks
);
725 if (local
->nb_blocks
&& rdma
->blockmap
) {
726 for (x
= 0; x
< local
->nb_blocks
; x
++) {
727 g_hash_table_insert(rdma
->blockmap
,
728 (void *)(uintptr_t)local
->block
[x
].offset
,
735 * Put in the log file which RDMA device was opened and the details
736 * associated with that device.
738 static void qemu_rdma_dump_id(const char *who
, struct ibv_context
*verbs
)
740 struct ibv_port_attr port
;
742 if (ibv_query_port(verbs
, 1, &port
)) {
743 error_report("Failed to query port information");
747 printf("%s RDMA Device opened: kernel name %s "
748 "uverbs device name %s, "
749 "infiniband_verbs class device path %s, "
750 "infiniband class device path %s, "
751 "transport: (%d) %s\n",
754 verbs
->device
->dev_name
,
755 verbs
->device
->dev_path
,
756 verbs
->device
->ibdev_path
,
758 (port
.link_layer
== IBV_LINK_LAYER_INFINIBAND
) ? "Infiniband" :
759 ((port
.link_layer
== IBV_LINK_LAYER_ETHERNET
)
760 ? "Ethernet" : "Unknown"));
764 * Put in the log file the RDMA gid addressing information,
765 * useful for folks who have trouble understanding the
766 * RDMA device hierarchy in the kernel.
768 static void qemu_rdma_dump_gid(const char *who
, struct rdma_cm_id
*id
)
772 inet_ntop(AF_INET6
, &id
->route
.addr
.addr
.ibaddr
.sgid
, sgid
, sizeof sgid
);
773 inet_ntop(AF_INET6
, &id
->route
.addr
.addr
.ibaddr
.dgid
, dgid
, sizeof dgid
);
774 trace_qemu_rdma_dump_gid(who
, sgid
, dgid
);
778 * As of now, IPv6 over RoCE / iWARP is not supported by linux.
779 * We will try the next addrinfo struct, and fail if there are
780 * no other valid addresses to bind against.
782 * If user is listening on '[::]', then we will not have a opened a device
783 * yet and have no way of verifying if the device is RoCE or not.
785 * In this case, the source VM will throw an error for ALL types of
786 * connections (both IPv4 and IPv6) if the destination machine does not have
787 * a regular infiniband network available for use.
789 * The only way to guarantee that an error is thrown for broken kernels is
790 * for the management software to choose a *specific* interface at bind time
791 * and validate what time of hardware it is.
793 * Unfortunately, this puts the user in a fix:
795 * If the source VM connects with an IPv4 address without knowing that the
796 * destination has bound to '[::]' the migration will unconditionally fail
797 * unless the management software is explicitly listening on the IPv4
798 * address while using a RoCE-based device.
800 * If the source VM connects with an IPv6 address, then we're OK because we can
801 * throw an error on the source (and similarly on the destination).
803 * But in mixed environments, this will be broken for a while until it is fixed
806 * We do provide a *tiny* bit of help in this function: We can list all of the
807 * devices in the system and check to see if all the devices are RoCE or
810 * If we detect that we have a *pure* RoCE environment, then we can safely
811 * thrown an error even if the management software has specified '[::]' as the
814 * However, if there is are multiple hetergeneous devices, then we cannot make
815 * this assumption and the user just has to be sure they know what they are
818 * Patches are being reviewed on linux-rdma.
820 static int qemu_rdma_broken_ipv6_kernel(struct ibv_context
*verbs
, Error
**errp
)
822 /* This bug only exists in linux, to our knowledge. */
824 struct ibv_port_attr port_attr
;
827 * Verbs are only NULL if management has bound to '[::]'.
829 * Let's iterate through all the devices and see if there any pure IB
830 * devices (non-ethernet).
832 * If not, then we can safely proceed with the migration.
833 * Otherwise, there are no guarantees until the bug is fixed in linux.
837 struct ibv_device
**dev_list
= ibv_get_device_list(&num_devices
);
838 bool roce_found
= false;
839 bool ib_found
= false;
841 for (x
= 0; x
< num_devices
; x
++) {
842 verbs
= ibv_open_device(dev_list
[x
]);
844 * ibv_open_device() is not documented to set errno. If
845 * it does, it's somebody else's doc bug. If it doesn't,
846 * the use of errno below is wrong.
847 * TODO Find out whether ibv_open_device() sets errno.
850 if (errno
== EPERM
) {
853 error_setg_errno(errp
, errno
,
854 "could not open RDMA device context");
859 if (ibv_query_port(verbs
, 1, &port_attr
)) {
860 ibv_close_device(verbs
);
862 "RDMA ERROR: Could not query initial IB port");
866 if (port_attr
.link_layer
== IBV_LINK_LAYER_INFINIBAND
) {
868 } else if (port_attr
.link_layer
== IBV_LINK_LAYER_ETHERNET
) {
872 ibv_close_device(verbs
);
878 fprintf(stderr
, "WARN: migrations may fail:"
879 " IPv6 over RoCE / iWARP in linux"
880 " is broken. But since you appear to have a"
881 " mixed RoCE / IB environment, be sure to only"
882 " migrate over the IB fabric until the kernel "
883 " fixes the bug.\n");
885 error_setg(errp
, "RDMA ERROR: "
886 "You only have RoCE / iWARP devices in your systems"
887 " and your management software has specified '[::]'"
888 ", but IPv6 over RoCE / iWARP is not supported in Linux.");
897 * If we have a verbs context, that means that some other than '[::]' was
898 * used by the management software for binding. In which case we can
899 * actually warn the user about a potentially broken kernel.
902 /* IB ports start with 1, not 0 */
903 if (ibv_query_port(verbs
, 1, &port_attr
)) {
904 error_setg(errp
, "RDMA ERROR: Could not query initial IB port");
908 if (port_attr
.link_layer
== IBV_LINK_LAYER_ETHERNET
) {
909 error_setg(errp
, "RDMA ERROR: "
910 "Linux kernel's RoCE / iWARP does not support IPv6 "
911 "(but patches on linux-rdma in progress)");
921 * Figure out which RDMA device corresponds to the requested IP hostname
922 * Also create the initial connection manager identifiers for opening
925 static int qemu_rdma_resolve_host(RDMAContext
*rdma
, Error
**errp
)
929 struct rdma_addrinfo
*res
;
931 struct rdma_cm_event
*cm_event
;
932 char ip
[40] = "unknown";
933 struct rdma_addrinfo
*e
;
935 if (rdma
->host
== NULL
|| !strcmp(rdma
->host
, "")) {
936 error_setg(errp
, "RDMA ERROR: RDMA hostname has not been set");
940 /* create CM channel */
941 rdma
->channel
= rdma_create_event_channel();
942 if (!rdma
->channel
) {
943 error_setg(errp
, "RDMA ERROR: could not create CM channel");
948 ret
= rdma_create_id(rdma
->channel
, &rdma
->cm_id
, NULL
, RDMA_PS_TCP
);
950 error_setg(errp
, "RDMA ERROR: could not create channel id");
951 goto err_resolve_create_id
;
954 snprintf(port_str
, 16, "%d", rdma
->port
);
957 ret
= rdma_getaddrinfo(rdma
->host
, port_str
, NULL
, &res
);
959 error_setg(errp
, "RDMA ERROR: could not rdma_getaddrinfo address %s",
961 goto err_resolve_get_addr
;
964 /* Try all addresses, saving the first error in @err */
965 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
966 Error
**local_errp
= err
? NULL
: &err
;
968 inet_ntop(e
->ai_family
,
969 &((struct sockaddr_in
*) e
->ai_dst_addr
)->sin_addr
, ip
, sizeof ip
);
970 trace_qemu_rdma_resolve_host_trying(rdma
->host
, ip
);
972 ret
= rdma_resolve_addr(rdma
->cm_id
, NULL
, e
->ai_dst_addr
,
973 RDMA_RESOLVE_TIMEOUT_MS
);
975 if (e
->ai_family
== AF_INET6
) {
976 ret
= qemu_rdma_broken_ipv6_kernel(rdma
->cm_id
->verbs
,
987 rdma_freeaddrinfo(res
);
989 error_propagate(errp
, err
);
991 error_setg(errp
, "RDMA ERROR: could not resolve address %s",
994 goto err_resolve_get_addr
;
997 rdma_freeaddrinfo(res
);
998 qemu_rdma_dump_gid("source_resolve_addr", rdma
->cm_id
);
1000 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
1002 error_setg(errp
, "RDMA ERROR: could not perform event_addr_resolved");
1003 goto err_resolve_get_addr
;
1006 if (cm_event
->event
!= RDMA_CM_EVENT_ADDR_RESOLVED
) {
1008 "RDMA ERROR: result not equal to event_addr_resolved %s",
1009 rdma_event_str(cm_event
->event
));
1010 error_report("rdma_resolve_addr");
1011 rdma_ack_cm_event(cm_event
);
1012 goto err_resolve_get_addr
;
1014 rdma_ack_cm_event(cm_event
);
1017 ret
= rdma_resolve_route(rdma
->cm_id
, RDMA_RESOLVE_TIMEOUT_MS
);
1019 error_setg(errp
, "RDMA ERROR: could not resolve rdma route");
1020 goto err_resolve_get_addr
;
1023 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
1025 error_setg(errp
, "RDMA ERROR: could not perform event_route_resolved");
1026 goto err_resolve_get_addr
;
1028 if (cm_event
->event
!= RDMA_CM_EVENT_ROUTE_RESOLVED
) {
1029 error_setg(errp
, "RDMA ERROR: "
1030 "result not equal to event_route_resolved: %s",
1031 rdma_event_str(cm_event
->event
));
1032 rdma_ack_cm_event(cm_event
);
1033 goto err_resolve_get_addr
;
1035 rdma_ack_cm_event(cm_event
);
1036 rdma
->verbs
= rdma
->cm_id
->verbs
;
1037 qemu_rdma_dump_id("source_resolve_host", rdma
->cm_id
->verbs
);
1038 qemu_rdma_dump_gid("source_resolve_host", rdma
->cm_id
);
1041 err_resolve_get_addr
:
1042 rdma_destroy_id(rdma
->cm_id
);
1044 err_resolve_create_id
:
1045 rdma_destroy_event_channel(rdma
->channel
);
1046 rdma
->channel
= NULL
;
1051 * Create protection domain and completion queues
1053 static int qemu_rdma_alloc_pd_cq(RDMAContext
*rdma
)
1056 rdma
->pd
= ibv_alloc_pd(rdma
->verbs
);
1058 error_report("failed to allocate protection domain");
1062 /* create receive completion channel */
1063 rdma
->recv_comp_channel
= ibv_create_comp_channel(rdma
->verbs
);
1064 if (!rdma
->recv_comp_channel
) {
1065 error_report("failed to allocate receive completion channel");
1066 goto err_alloc_pd_cq
;
1070 * Completion queue can be filled by read work requests.
1072 rdma
->recv_cq
= ibv_create_cq(rdma
->verbs
, (RDMA_SIGNALED_SEND_MAX
* 3),
1073 NULL
, rdma
->recv_comp_channel
, 0);
1074 if (!rdma
->recv_cq
) {
1075 error_report("failed to allocate receive completion queue");
1076 goto err_alloc_pd_cq
;
1079 /* create send completion channel */
1080 rdma
->send_comp_channel
= ibv_create_comp_channel(rdma
->verbs
);
1081 if (!rdma
->send_comp_channel
) {
1082 error_report("failed to allocate send completion channel");
1083 goto err_alloc_pd_cq
;
1086 rdma
->send_cq
= ibv_create_cq(rdma
->verbs
, (RDMA_SIGNALED_SEND_MAX
* 3),
1087 NULL
, rdma
->send_comp_channel
, 0);
1088 if (!rdma
->send_cq
) {
1089 error_report("failed to allocate send completion queue");
1090 goto err_alloc_pd_cq
;
1097 ibv_dealloc_pd(rdma
->pd
);
1099 if (rdma
->recv_comp_channel
) {
1100 ibv_destroy_comp_channel(rdma
->recv_comp_channel
);
1102 if (rdma
->send_comp_channel
) {
1103 ibv_destroy_comp_channel(rdma
->send_comp_channel
);
1105 if (rdma
->recv_cq
) {
1106 ibv_destroy_cq(rdma
->recv_cq
);
1107 rdma
->recv_cq
= NULL
;
1110 rdma
->recv_comp_channel
= NULL
;
1111 rdma
->send_comp_channel
= NULL
;
1117 * Create queue pairs.
1119 static int qemu_rdma_alloc_qp(RDMAContext
*rdma
)
1121 struct ibv_qp_init_attr attr
= { 0 };
1124 attr
.cap
.max_send_wr
= RDMA_SIGNALED_SEND_MAX
;
1125 attr
.cap
.max_recv_wr
= 3;
1126 attr
.cap
.max_send_sge
= 1;
1127 attr
.cap
.max_recv_sge
= 1;
1128 attr
.send_cq
= rdma
->send_cq
;
1129 attr
.recv_cq
= rdma
->recv_cq
;
1130 attr
.qp_type
= IBV_QPT_RC
;
1132 ret
= rdma_create_qp(rdma
->cm_id
, rdma
->pd
, &attr
);
1137 rdma
->qp
= rdma
->cm_id
->qp
;
1141 /* Check whether On-Demand Paging is supported by RDAM device */
1142 static bool rdma_support_odp(struct ibv_context
*dev
)
1144 struct ibv_device_attr_ex attr
= {0};
1145 int ret
= ibv_query_device_ex(dev
, NULL
, &attr
);
1150 if (attr
.odp_caps
.general_caps
& IBV_ODP_SUPPORT
) {
1158 * ibv_advise_mr to avoid RNR NAK error as far as possible.
1159 * The responder mr registering with ODP will sent RNR NAK back to
1160 * the requester in the face of the page fault.
1162 static void qemu_rdma_advise_prefetch_mr(struct ibv_pd
*pd
, uint64_t addr
,
1163 uint32_t len
, uint32_t lkey
,
1164 const char *name
, bool wr
)
1166 #ifdef HAVE_IBV_ADVISE_MR
1168 int advice
= wr
? IBV_ADVISE_MR_ADVICE_PREFETCH_WRITE
:
1169 IBV_ADVISE_MR_ADVICE_PREFETCH
;
1170 struct ibv_sge sg_list
= {.lkey
= lkey
, .addr
= addr
, .length
= len
};
1172 ret
= ibv_advise_mr(pd
, advice
,
1173 IBV_ADVISE_MR_FLAG_FLUSH
, &sg_list
, 1);
1174 /* ignore the error */
1175 trace_qemu_rdma_advise_mr(name
, len
, addr
, strerror(ret
));
1179 static int qemu_rdma_reg_whole_ram_blocks(RDMAContext
*rdma
)
1182 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
1184 for (i
= 0; i
< local
->nb_blocks
; i
++) {
1185 int access
= IBV_ACCESS_LOCAL_WRITE
| IBV_ACCESS_REMOTE_WRITE
;
1187 local
->block
[i
].mr
=
1188 ibv_reg_mr(rdma
->pd
,
1189 local
->block
[i
].local_host_addr
,
1190 local
->block
[i
].length
, access
1193 * ibv_reg_mr() is not documented to set errno. If it does,
1194 * it's somebody else's doc bug. If it doesn't, the use of
1195 * errno below is wrong.
1196 * TODO Find out whether ibv_reg_mr() sets errno.
1198 if (!local
->block
[i
].mr
&&
1199 errno
== ENOTSUP
&& rdma_support_odp(rdma
->verbs
)) {
1200 access
|= IBV_ACCESS_ON_DEMAND
;
1201 /* register ODP mr */
1202 local
->block
[i
].mr
=
1203 ibv_reg_mr(rdma
->pd
,
1204 local
->block
[i
].local_host_addr
,
1205 local
->block
[i
].length
, access
);
1206 trace_qemu_rdma_register_odp_mr(local
->block
[i
].block_name
);
1208 if (local
->block
[i
].mr
) {
1209 qemu_rdma_advise_prefetch_mr(rdma
->pd
,
1210 (uintptr_t)local
->block
[i
].local_host_addr
,
1211 local
->block
[i
].length
,
1212 local
->block
[i
].mr
->lkey
,
1213 local
->block
[i
].block_name
,
1218 if (!local
->block
[i
].mr
) {
1219 perror("Failed to register local dest ram block!");
1222 rdma
->total_registrations
++;
1225 if (i
>= local
->nb_blocks
) {
1229 for (i
--; i
>= 0; i
--) {
1230 ibv_dereg_mr(local
->block
[i
].mr
);
1231 local
->block
[i
].mr
= NULL
;
1232 rdma
->total_registrations
--;
1240 * Find the ram block that corresponds to the page requested to be
1241 * transmitted by QEMU.
1243 * Once the block is found, also identify which 'chunk' within that
1244 * block that the page belongs to.
1246 static void qemu_rdma_search_ram_block(RDMAContext
*rdma
,
1247 uintptr_t block_offset
,
1250 uint64_t *block_index
,
1251 uint64_t *chunk_index
)
1253 uint64_t current_addr
= block_offset
+ offset
;
1254 RDMALocalBlock
*block
= g_hash_table_lookup(rdma
->blockmap
,
1255 (void *) block_offset
);
1257 assert(current_addr
>= block
->offset
);
1258 assert((current_addr
+ length
) <= (block
->offset
+ block
->length
));
1260 *block_index
= block
->index
;
1261 *chunk_index
= ram_chunk_index(block
->local_host_addr
,
1262 block
->local_host_addr
+ (current_addr
- block
->offset
));
1266 * Register a chunk with IB. If the chunk was already registered
1267 * previously, then skip.
1269 * Also return the keys associated with the registration needed
1270 * to perform the actual RDMA operation.
1272 static int qemu_rdma_register_and_get_keys(RDMAContext
*rdma
,
1273 RDMALocalBlock
*block
, uintptr_t host_addr
,
1274 uint32_t *lkey
, uint32_t *rkey
, int chunk
,
1275 uint8_t *chunk_start
, uint8_t *chunk_end
)
1279 *lkey
= block
->mr
->lkey
;
1282 *rkey
= block
->mr
->rkey
;
1287 /* allocate memory to store chunk MRs */
1289 block
->pmr
= g_new0(struct ibv_mr
*, block
->nb_chunks
);
1293 * If 'rkey', then we're the destination, so grant access to the source.
1295 * If 'lkey', then we're the source VM, so grant access only to ourselves.
1297 if (!block
->pmr
[chunk
]) {
1298 uint64_t len
= chunk_end
- chunk_start
;
1299 int access
= rkey
? IBV_ACCESS_LOCAL_WRITE
| IBV_ACCESS_REMOTE_WRITE
:
1302 trace_qemu_rdma_register_and_get_keys(len
, chunk_start
);
1304 block
->pmr
[chunk
] = ibv_reg_mr(rdma
->pd
, chunk_start
, len
, access
);
1306 * ibv_reg_mr() is not documented to set errno. If it does,
1307 * it's somebody else's doc bug. If it doesn't, the use of
1308 * errno below is wrong.
1309 * TODO Find out whether ibv_reg_mr() sets errno.
1311 if (!block
->pmr
[chunk
] &&
1312 errno
== ENOTSUP
&& rdma_support_odp(rdma
->verbs
)) {
1313 access
|= IBV_ACCESS_ON_DEMAND
;
1314 /* register ODP mr */
1315 block
->pmr
[chunk
] = ibv_reg_mr(rdma
->pd
, chunk_start
, len
, access
);
1316 trace_qemu_rdma_register_odp_mr(block
->block_name
);
1318 if (block
->pmr
[chunk
]) {
1319 qemu_rdma_advise_prefetch_mr(rdma
->pd
, (uintptr_t)chunk_start
,
1320 len
, block
->pmr
[chunk
]->lkey
,
1321 block
->block_name
, rkey
);
1326 if (!block
->pmr
[chunk
]) {
1327 perror("Failed to register chunk!");
1328 fprintf(stderr
, "Chunk details: block: %d chunk index %d"
1329 " start %" PRIuPTR
" end %" PRIuPTR
1331 " local %" PRIuPTR
" registrations: %d\n",
1332 block
->index
, chunk
, (uintptr_t)chunk_start
,
1333 (uintptr_t)chunk_end
, host_addr
,
1334 (uintptr_t)block
->local_host_addr
,
1335 rdma
->total_registrations
);
1338 rdma
->total_registrations
++;
1341 *lkey
= block
->pmr
[chunk
]->lkey
;
1344 *rkey
= block
->pmr
[chunk
]->rkey
;
1350 * Register (at connection time) the memory used for control
1353 static int qemu_rdma_reg_control(RDMAContext
*rdma
, int idx
)
1355 rdma
->wr_data
[idx
].control_mr
= ibv_reg_mr(rdma
->pd
,
1356 rdma
->wr_data
[idx
].control
, RDMA_CONTROL_MAX_BUFFER
,
1357 IBV_ACCESS_LOCAL_WRITE
| IBV_ACCESS_REMOTE_WRITE
);
1358 if (rdma
->wr_data
[idx
].control_mr
) {
1359 rdma
->total_registrations
++;
1362 error_report("qemu_rdma_reg_control failed");
1367 * Perform a non-optimized memory unregistration after every transfer
1368 * for demonstration purposes, only if pin-all is not requested.
1370 * Potential optimizations:
1371 * 1. Start a new thread to run this function continuously
1373 - and for receipt of unregister messages
1375 * 3. Use workload hints.
1377 static int qemu_rdma_unregister_waiting(RDMAContext
*rdma
)
1379 while (rdma
->unregistrations
[rdma
->unregister_current
]) {
1381 uint64_t wr_id
= rdma
->unregistrations
[rdma
->unregister_current
];
1383 (wr_id
& RDMA_WRID_CHUNK_MASK
) >> RDMA_WRID_CHUNK_SHIFT
;
1385 (wr_id
& RDMA_WRID_BLOCK_MASK
) >> RDMA_WRID_BLOCK_SHIFT
;
1386 RDMALocalBlock
*block
=
1387 &(rdma
->local_ram_blocks
.block
[index
]);
1388 RDMARegister reg
= { .current_index
= index
};
1389 RDMAControlHeader resp
= { .type
= RDMA_CONTROL_UNREGISTER_FINISHED
,
1391 RDMAControlHeader head
= { .len
= sizeof(RDMARegister
),
1392 .type
= RDMA_CONTROL_UNREGISTER_REQUEST
,
1396 trace_qemu_rdma_unregister_waiting_proc(chunk
,
1397 rdma
->unregister_current
);
1399 rdma
->unregistrations
[rdma
->unregister_current
] = 0;
1400 rdma
->unregister_current
++;
1402 if (rdma
->unregister_current
== RDMA_SIGNALED_SEND_MAX
) {
1403 rdma
->unregister_current
= 0;
1408 * Unregistration is speculative (because migration is single-threaded
1409 * and we cannot break the protocol's inifinband message ordering).
1410 * Thus, if the memory is currently being used for transmission,
1411 * then abort the attempt to unregister and try again
1412 * later the next time a completion is received for this memory.
1414 clear_bit(chunk
, block
->unregister_bitmap
);
1416 if (test_bit(chunk
, block
->transit_bitmap
)) {
1417 trace_qemu_rdma_unregister_waiting_inflight(chunk
);
1421 trace_qemu_rdma_unregister_waiting_send(chunk
);
1423 ret
= ibv_dereg_mr(block
->pmr
[chunk
]);
1424 block
->pmr
[chunk
] = NULL
;
1425 block
->remote_keys
[chunk
] = 0;
1429 * FIXME perror() is problematic, bcause ibv_dereg_mr() is
1430 * not documented to set errno. Will go away later in
1433 perror("unregistration chunk failed");
1436 rdma
->total_registrations
--;
1438 reg
.key
.chunk
= chunk
;
1439 register_to_network(rdma
, ®
);
1440 ret
= qemu_rdma_exchange_send(rdma
, &head
, (uint8_t *) ®
,
1446 trace_qemu_rdma_unregister_waiting_complete(chunk
);
1452 static uint64_t qemu_rdma_make_wrid(uint64_t wr_id
, uint64_t index
,
1455 uint64_t result
= wr_id
& RDMA_WRID_TYPE_MASK
;
1457 result
|= (index
<< RDMA_WRID_BLOCK_SHIFT
);
1458 result
|= (chunk
<< RDMA_WRID_CHUNK_SHIFT
);
1464 * Consult the connection manager to see a work request
1465 * (of any kind) has completed.
1466 * Return the work request ID that completed.
1468 static int qemu_rdma_poll(RDMAContext
*rdma
, struct ibv_cq
*cq
,
1469 uint64_t *wr_id_out
, uint32_t *byte_len
)
1475 ret
= ibv_poll_cq(cq
, 1, &wc
);
1478 *wr_id_out
= RDMA_WRID_NONE
;
1483 error_report("ibv_poll_cq failed");
1487 wr_id
= wc
.wr_id
& RDMA_WRID_TYPE_MASK
;
1489 if (wc
.status
!= IBV_WC_SUCCESS
) {
1490 fprintf(stderr
, "ibv_poll_cq wc.status=%d %s!\n",
1491 wc
.status
, ibv_wc_status_str(wc
.status
));
1492 fprintf(stderr
, "ibv_poll_cq wrid=%" PRIu64
"!\n", wr_id
);
1497 if (rdma
->control_ready_expected
&&
1498 (wr_id
>= RDMA_WRID_RECV_CONTROL
)) {
1499 trace_qemu_rdma_poll_recv(wr_id
- RDMA_WRID_RECV_CONTROL
, wr_id
,
1501 rdma
->control_ready_expected
= 0;
1504 if (wr_id
== RDMA_WRID_RDMA_WRITE
) {
1506 (wc
.wr_id
& RDMA_WRID_CHUNK_MASK
) >> RDMA_WRID_CHUNK_SHIFT
;
1508 (wc
.wr_id
& RDMA_WRID_BLOCK_MASK
) >> RDMA_WRID_BLOCK_SHIFT
;
1509 RDMALocalBlock
*block
= &(rdma
->local_ram_blocks
.block
[index
]);
1511 trace_qemu_rdma_poll_write(wr_id
, rdma
->nb_sent
,
1512 index
, chunk
, block
->local_host_addr
,
1513 (void *)(uintptr_t)block
->remote_host_addr
);
1515 clear_bit(chunk
, block
->transit_bitmap
);
1517 if (rdma
->nb_sent
> 0) {
1521 trace_qemu_rdma_poll_other(wr_id
, rdma
->nb_sent
);
1524 *wr_id_out
= wc
.wr_id
;
1526 *byte_len
= wc
.byte_len
;
1532 /* Wait for activity on the completion channel.
1533 * Returns 0 on success, none-0 on error.
1535 static int qemu_rdma_wait_comp_channel(RDMAContext
*rdma
,
1536 struct ibv_comp_channel
*comp_channel
)
1538 struct rdma_cm_event
*cm_event
;
1542 * Coroutine doesn't start until migration_fd_process_incoming()
1543 * so don't yield unless we know we're running inside of a coroutine.
1545 if (rdma
->migration_started_on_destination
&&
1546 migration_incoming_get_current()->state
== MIGRATION_STATUS_ACTIVE
) {
1547 yield_until_fd_readable(comp_channel
->fd
);
1549 /* This is the source side, we're in a separate thread
1550 * or destination prior to migration_fd_process_incoming()
1551 * after postcopy, the destination also in a separate thread.
1552 * we can't yield; so we have to poll the fd.
1553 * But we need to be able to handle 'cancel' or an error
1554 * without hanging forever.
1556 while (!rdma
->errored
&& !rdma
->received_error
) {
1558 pfds
[0].fd
= comp_channel
->fd
;
1559 pfds
[0].events
= G_IO_IN
| G_IO_HUP
| G_IO_ERR
;
1560 pfds
[0].revents
= 0;
1562 pfds
[1].fd
= rdma
->channel
->fd
;
1563 pfds
[1].events
= G_IO_IN
| G_IO_HUP
| G_IO_ERR
;
1564 pfds
[1].revents
= 0;
1566 /* 0.1s timeout, should be fine for a 'cancel' */
1567 switch (qemu_poll_ns(pfds
, 2, 100 * 1000 * 1000)) {
1569 case 1: /* fd active */
1570 if (pfds
[0].revents
) {
1574 if (pfds
[1].revents
) {
1575 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
1577 error_report("failed to get cm event while wait "
1578 "completion channel");
1582 error_report("receive cm event while wait comp channel,"
1583 "cm event is %d", cm_event
->event
);
1584 if (cm_event
->event
== RDMA_CM_EVENT_DISCONNECTED
||
1585 cm_event
->event
== RDMA_CM_EVENT_DEVICE_REMOVAL
) {
1586 rdma_ack_cm_event(cm_event
);
1589 rdma_ack_cm_event(cm_event
);
1593 case 0: /* Timeout, go around again */
1596 default: /* Error of some type -
1597 * I don't trust errno from qemu_poll_ns
1599 error_report("%s: poll failed", __func__
);
1603 if (migrate_get_current()->state
== MIGRATION_STATUS_CANCELLING
) {
1604 /* Bail out and let the cancellation happen */
1610 if (rdma
->received_error
) {
1613 return -rdma
->errored
;
1616 static struct ibv_comp_channel
*to_channel(RDMAContext
*rdma
, uint64_t wrid
)
1618 return wrid
< RDMA_WRID_RECV_CONTROL
? rdma
->send_comp_channel
:
1619 rdma
->recv_comp_channel
;
1622 static struct ibv_cq
*to_cq(RDMAContext
*rdma
, uint64_t wrid
)
1624 return wrid
< RDMA_WRID_RECV_CONTROL
? rdma
->send_cq
: rdma
->recv_cq
;
1628 * Block until the next work request has completed.
1630 * First poll to see if a work request has already completed,
1633 * If we encounter completed work requests for IDs other than
1634 * the one we're interested in, then that's generally an error.
1636 * The only exception is actual RDMA Write completions. These
1637 * completions only need to be recorded, but do not actually
1638 * need further processing.
1640 static int qemu_rdma_block_for_wrid(RDMAContext
*rdma
,
1641 uint64_t wrid_requested
,
1644 int num_cq_events
= 0, ret
;
1647 uint64_t wr_id
= RDMA_WRID_NONE
, wr_id_in
;
1648 struct ibv_comp_channel
*ch
= to_channel(rdma
, wrid_requested
);
1649 struct ibv_cq
*poll_cq
= to_cq(rdma
, wrid_requested
);
1651 if (ibv_req_notify_cq(poll_cq
, 0)) {
1655 while (wr_id
!= wrid_requested
) {
1656 ret
= qemu_rdma_poll(rdma
, poll_cq
, &wr_id_in
, byte_len
);
1661 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
1663 if (wr_id
== RDMA_WRID_NONE
) {
1666 if (wr_id
!= wrid_requested
) {
1667 trace_qemu_rdma_block_for_wrid_miss(wrid_requested
, wr_id
);
1671 if (wr_id
== wrid_requested
) {
1676 ret
= qemu_rdma_wait_comp_channel(rdma
, ch
);
1678 goto err_block_for_wrid
;
1681 ret
= ibv_get_cq_event(ch
, &cq
, &cq_ctx
);
1684 * FIXME perror() is problematic, because ibv_reg_mr() is
1685 * not documented to set errno. Will go away later in
1688 perror("ibv_get_cq_event");
1689 goto err_block_for_wrid
;
1694 if (ibv_req_notify_cq(cq
, 0)) {
1695 goto err_block_for_wrid
;
1698 while (wr_id
!= wrid_requested
) {
1699 ret
= qemu_rdma_poll(rdma
, poll_cq
, &wr_id_in
, byte_len
);
1701 goto err_block_for_wrid
;
1704 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
1706 if (wr_id
== RDMA_WRID_NONE
) {
1709 if (wr_id
!= wrid_requested
) {
1710 trace_qemu_rdma_block_for_wrid_miss(wrid_requested
, wr_id
);
1714 if (wr_id
== wrid_requested
) {
1715 goto success_block_for_wrid
;
1719 success_block_for_wrid
:
1720 if (num_cq_events
) {
1721 ibv_ack_cq_events(cq
, num_cq_events
);
1726 if (num_cq_events
) {
1727 ibv_ack_cq_events(cq
, num_cq_events
);
1730 rdma
->errored
= true;
1735 * Post a SEND message work request for the control channel
1736 * containing some data and block until the post completes.
1738 static int qemu_rdma_post_send_control(RDMAContext
*rdma
, uint8_t *buf
,
1739 RDMAControlHeader
*head
)
1742 RDMAWorkRequestData
*wr
= &rdma
->wr_data
[RDMA_WRID_CONTROL
];
1743 struct ibv_send_wr
*bad_wr
;
1744 struct ibv_sge sge
= {
1745 .addr
= (uintptr_t)(wr
->control
),
1746 .length
= head
->len
+ sizeof(RDMAControlHeader
),
1747 .lkey
= wr
->control_mr
->lkey
,
1749 struct ibv_send_wr send_wr
= {
1750 .wr_id
= RDMA_WRID_SEND_CONTROL
,
1751 .opcode
= IBV_WR_SEND
,
1752 .send_flags
= IBV_SEND_SIGNALED
,
1757 trace_qemu_rdma_post_send_control(control_desc(head
->type
));
1760 * We don't actually need to do a memcpy() in here if we used
1761 * the "sge" properly, but since we're only sending control messages
1762 * (not RAM in a performance-critical path), then its OK for now.
1764 * The copy makes the RDMAControlHeader simpler to manipulate
1765 * for the time being.
1767 assert(head
->len
<= RDMA_CONTROL_MAX_BUFFER
- sizeof(*head
));
1768 memcpy(wr
->control
, head
, sizeof(RDMAControlHeader
));
1769 control_to_network((void *) wr
->control
);
1772 memcpy(wr
->control
+ sizeof(RDMAControlHeader
), buf
, head
->len
);
1776 ret
= ibv_post_send(rdma
->qp
, &send_wr
, &bad_wr
);
1779 error_report("Failed to use post IB SEND for control");
1783 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_SEND_CONTROL
, NULL
);
1785 error_report("rdma migration: send polling control error");
1793 * Post a RECV work request in anticipation of some future receipt
1794 * of data on the control channel.
1796 static int qemu_rdma_post_recv_control(RDMAContext
*rdma
, int idx
)
1798 struct ibv_recv_wr
*bad_wr
;
1799 struct ibv_sge sge
= {
1800 .addr
= (uintptr_t)(rdma
->wr_data
[idx
].control
),
1801 .length
= RDMA_CONTROL_MAX_BUFFER
,
1802 .lkey
= rdma
->wr_data
[idx
].control_mr
->lkey
,
1805 struct ibv_recv_wr recv_wr
= {
1806 .wr_id
= RDMA_WRID_RECV_CONTROL
+ idx
,
1812 if (ibv_post_recv(rdma
->qp
, &recv_wr
, &bad_wr
)) {
1820 * Block and wait for a RECV control channel message to arrive.
1822 static int qemu_rdma_exchange_get_response(RDMAContext
*rdma
,
1823 RDMAControlHeader
*head
, uint32_t expecting
, int idx
)
1826 int ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RECV_CONTROL
+ idx
,
1830 error_report("rdma migration: recv polling control error!");
1834 network_to_control((void *) rdma
->wr_data
[idx
].control
);
1835 memcpy(head
, rdma
->wr_data
[idx
].control
, sizeof(RDMAControlHeader
));
1837 trace_qemu_rdma_exchange_get_response_start(control_desc(expecting
));
1839 if (expecting
== RDMA_CONTROL_NONE
) {
1840 trace_qemu_rdma_exchange_get_response_none(control_desc(head
->type
),
1842 } else if (head
->type
!= expecting
|| head
->type
== RDMA_CONTROL_ERROR
) {
1843 error_report("Was expecting a %s (%d) control message"
1844 ", but got: %s (%d), length: %d",
1845 control_desc(expecting
), expecting
,
1846 control_desc(head
->type
), head
->type
, head
->len
);
1847 if (head
->type
== RDMA_CONTROL_ERROR
) {
1848 rdma
->received_error
= true;
1852 if (head
->len
> RDMA_CONTROL_MAX_BUFFER
- sizeof(*head
)) {
1853 error_report("too long length: %d", head
->len
);
1856 if (sizeof(*head
) + head
->len
!= byte_len
) {
1857 error_report("Malformed length: %d byte_len %d", head
->len
, byte_len
);
1865 * When a RECV work request has completed, the work request's
1866 * buffer is pointed at the header.
1868 * This will advance the pointer to the data portion
1869 * of the control message of the work request's buffer that
1870 * was populated after the work request finished.
1872 static void qemu_rdma_move_header(RDMAContext
*rdma
, int idx
,
1873 RDMAControlHeader
*head
)
1875 rdma
->wr_data
[idx
].control_len
= head
->len
;
1876 rdma
->wr_data
[idx
].control_curr
=
1877 rdma
->wr_data
[idx
].control
+ sizeof(RDMAControlHeader
);
1881 * This is an 'atomic' high-level operation to deliver a single, unified
1882 * control-channel message.
1884 * Additionally, if the user is expecting some kind of reply to this message,
1885 * they can request a 'resp' response message be filled in by posting an
1886 * additional work request on behalf of the user and waiting for an additional
1889 * The extra (optional) response is used during registration to us from having
1890 * to perform an *additional* exchange of message just to provide a response by
1891 * instead piggy-backing on the acknowledgement.
1893 static int qemu_rdma_exchange_send(RDMAContext
*rdma
, RDMAControlHeader
*head
,
1894 uint8_t *data
, RDMAControlHeader
*resp
,
1896 int (*callback
)(RDMAContext
*rdma
))
1901 * Wait until the dest is ready before attempting to deliver the message
1902 * by waiting for a READY message.
1904 if (rdma
->control_ready_expected
) {
1905 RDMAControlHeader resp_ignored
;
1907 ret
= qemu_rdma_exchange_get_response(rdma
, &resp_ignored
,
1916 * If the user is expecting a response, post a WR in anticipation of it.
1919 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_DATA
);
1921 error_report("rdma migration: error posting"
1922 " extra control recv for anticipated result!");
1928 * Post a WR to replace the one we just consumed for the READY message.
1930 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
1932 error_report("rdma migration: error posting first control recv!");
1937 * Deliver the control message that was requested.
1939 ret
= qemu_rdma_post_send_control(rdma
, data
, head
);
1942 error_report("Failed to send control buffer!");
1947 * If we're expecting a response, block and wait for it.
1951 trace_qemu_rdma_exchange_send_issue_callback();
1952 ret
= callback(rdma
);
1958 trace_qemu_rdma_exchange_send_waiting(control_desc(resp
->type
));
1959 ret
= qemu_rdma_exchange_get_response(rdma
, resp
,
1960 resp
->type
, RDMA_WRID_DATA
);
1966 qemu_rdma_move_header(rdma
, RDMA_WRID_DATA
, resp
);
1968 *resp_idx
= RDMA_WRID_DATA
;
1970 trace_qemu_rdma_exchange_send_received(control_desc(resp
->type
));
1973 rdma
->control_ready_expected
= 1;
1979 * This is an 'atomic' high-level operation to receive a single, unified
1980 * control-channel message.
1982 static int qemu_rdma_exchange_recv(RDMAContext
*rdma
, RDMAControlHeader
*head
,
1985 RDMAControlHeader ready
= {
1987 .type
= RDMA_CONTROL_READY
,
1993 * Inform the source that we're ready to receive a message.
1995 ret
= qemu_rdma_post_send_control(rdma
, NULL
, &ready
);
1998 error_report("Failed to send control buffer!");
2003 * Block and wait for the message.
2005 ret
= qemu_rdma_exchange_get_response(rdma
, head
,
2006 expecting
, RDMA_WRID_READY
);
2012 qemu_rdma_move_header(rdma
, RDMA_WRID_READY
, head
);
2015 * Post a new RECV work request to replace the one we just consumed.
2017 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
2019 error_report("rdma migration: error posting second control recv!");
2027 * Write an actual chunk of memory using RDMA.
2029 * If we're using dynamic registration on the dest-side, we have to
2030 * send a registration command first.
2032 static int qemu_rdma_write_one(RDMAContext
*rdma
,
2033 int current_index
, uint64_t current_addr
,
2037 struct ibv_send_wr send_wr
= { 0 };
2038 struct ibv_send_wr
*bad_wr
;
2039 int reg_result_idx
, ret
, count
= 0;
2040 uint64_t chunk
, chunks
;
2041 uint8_t *chunk_start
, *chunk_end
;
2042 RDMALocalBlock
*block
= &(rdma
->local_ram_blocks
.block
[current_index
]);
2044 RDMARegisterResult
*reg_result
;
2045 RDMAControlHeader resp
= { .type
= RDMA_CONTROL_REGISTER_RESULT
};
2046 RDMAControlHeader head
= { .len
= sizeof(RDMARegister
),
2047 .type
= RDMA_CONTROL_REGISTER_REQUEST
,
2052 sge
.addr
= (uintptr_t)(block
->local_host_addr
+
2053 (current_addr
- block
->offset
));
2054 sge
.length
= length
;
2056 chunk
= ram_chunk_index(block
->local_host_addr
,
2057 (uint8_t *)(uintptr_t)sge
.addr
);
2058 chunk_start
= ram_chunk_start(block
, chunk
);
2060 if (block
->is_ram_block
) {
2061 chunks
= length
/ (1UL << RDMA_REG_CHUNK_SHIFT
);
2063 if (chunks
&& ((length
% (1UL << RDMA_REG_CHUNK_SHIFT
)) == 0)) {
2067 chunks
= block
->length
/ (1UL << RDMA_REG_CHUNK_SHIFT
);
2069 if (chunks
&& ((block
->length
% (1UL << RDMA_REG_CHUNK_SHIFT
)) == 0)) {
2074 trace_qemu_rdma_write_one_top(chunks
+ 1,
2076 (1UL << RDMA_REG_CHUNK_SHIFT
) / 1024 / 1024);
2078 chunk_end
= ram_chunk_end(block
, chunk
+ chunks
);
2081 while (test_bit(chunk
, block
->transit_bitmap
)) {
2083 trace_qemu_rdma_write_one_block(count
++, current_index
, chunk
,
2084 sge
.addr
, length
, rdma
->nb_sent
, block
->nb_chunks
);
2086 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RDMA_WRITE
, NULL
);
2089 error_report("Failed to Wait for previous write to complete "
2090 "block %d chunk %" PRIu64
2091 " current %" PRIu64
" len %" PRIu64
" %d",
2092 current_index
, chunk
, sge
.addr
, length
, rdma
->nb_sent
);
2097 if (!rdma
->pin_all
|| !block
->is_ram_block
) {
2098 if (!block
->remote_keys
[chunk
]) {
2100 * This chunk has not yet been registered, so first check to see
2101 * if the entire chunk is zero. If so, tell the other size to
2102 * memset() + madvise() the entire chunk without RDMA.
2105 if (buffer_is_zero((void *)(uintptr_t)sge
.addr
, length
)) {
2106 RDMACompress comp
= {
2107 .offset
= current_addr
,
2109 .block_idx
= current_index
,
2113 head
.len
= sizeof(comp
);
2114 head
.type
= RDMA_CONTROL_COMPRESS
;
2116 trace_qemu_rdma_write_one_zero(chunk
, sge
.length
,
2117 current_index
, current_addr
);
2119 compress_to_network(rdma
, &comp
);
2120 ret
= qemu_rdma_exchange_send(rdma
, &head
,
2121 (uint8_t *) &comp
, NULL
, NULL
, NULL
);
2128 * TODO: Here we are sending something, but we are not
2129 * accounting for anything transferred. The following is wrong:
2131 * stat64_add(&mig_stats.rdma_bytes, sge.length);
2133 * because we are using some kind of compression. I
2134 * would think that head.len would be the more similar
2135 * thing to a correct value.
2137 stat64_add(&mig_stats
.zero_pages
,
2138 sge
.length
/ qemu_target_page_size());
2143 * Otherwise, tell other side to register.
2145 reg
.current_index
= current_index
;
2146 if (block
->is_ram_block
) {
2147 reg
.key
.current_addr
= current_addr
;
2149 reg
.key
.chunk
= chunk
;
2151 reg
.chunks
= chunks
;
2153 trace_qemu_rdma_write_one_sendreg(chunk
, sge
.length
, current_index
,
2156 register_to_network(rdma
, ®
);
2157 ret
= qemu_rdma_exchange_send(rdma
, &head
, (uint8_t *) ®
,
2158 &resp
, ®_result_idx
, NULL
);
2163 /* try to overlap this single registration with the one we sent. */
2164 if (qemu_rdma_register_and_get_keys(rdma
, block
, sge
.addr
,
2165 &sge
.lkey
, NULL
, chunk
,
2166 chunk_start
, chunk_end
)) {
2167 error_report("cannot get lkey");
2171 reg_result
= (RDMARegisterResult
*)
2172 rdma
->wr_data
[reg_result_idx
].control_curr
;
2174 network_to_result(reg_result
);
2176 trace_qemu_rdma_write_one_recvregres(block
->remote_keys
[chunk
],
2177 reg_result
->rkey
, chunk
);
2179 block
->remote_keys
[chunk
] = reg_result
->rkey
;
2180 block
->remote_host_addr
= reg_result
->host_addr
;
2182 /* already registered before */
2183 if (qemu_rdma_register_and_get_keys(rdma
, block
, sge
.addr
,
2184 &sge
.lkey
, NULL
, chunk
,
2185 chunk_start
, chunk_end
)) {
2186 error_report("cannot get lkey!");
2191 send_wr
.wr
.rdma
.rkey
= block
->remote_keys
[chunk
];
2193 send_wr
.wr
.rdma
.rkey
= block
->remote_rkey
;
2195 if (qemu_rdma_register_and_get_keys(rdma
, block
, sge
.addr
,
2196 &sge
.lkey
, NULL
, chunk
,
2197 chunk_start
, chunk_end
)) {
2198 error_report("cannot get lkey!");
2204 * Encode the ram block index and chunk within this wrid.
2205 * We will use this information at the time of completion
2206 * to figure out which bitmap to check against and then which
2207 * chunk in the bitmap to look for.
2209 send_wr
.wr_id
= qemu_rdma_make_wrid(RDMA_WRID_RDMA_WRITE
,
2210 current_index
, chunk
);
2212 send_wr
.opcode
= IBV_WR_RDMA_WRITE
;
2213 send_wr
.send_flags
= IBV_SEND_SIGNALED
;
2214 send_wr
.sg_list
= &sge
;
2215 send_wr
.num_sge
= 1;
2216 send_wr
.wr
.rdma
.remote_addr
= block
->remote_host_addr
+
2217 (current_addr
- block
->offset
);
2219 trace_qemu_rdma_write_one_post(chunk
, sge
.addr
, send_wr
.wr
.rdma
.remote_addr
,
2223 * ibv_post_send() does not return negative error numbers,
2224 * per the specification they are positive - no idea why.
2226 ret
= ibv_post_send(rdma
->qp
, &send_wr
, &bad_wr
);
2228 if (ret
== ENOMEM
) {
2229 trace_qemu_rdma_write_one_queue_full();
2230 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RDMA_WRITE
, NULL
);
2232 error_report("rdma migration: failed to make "
2233 "room in full send queue!");
2239 } else if (ret
> 0) {
2241 * FIXME perror() is problematic, because whether
2242 * ibv_post_send() sets errno is unclear. Will go away later
2245 perror("rdma migration: post rdma write failed");
2249 set_bit(chunk
, block
->transit_bitmap
);
2250 stat64_add(&mig_stats
.normal_pages
, sge
.length
/ qemu_target_page_size());
2252 * We are adding to transferred the amount of data written, but no
2253 * overhead at all. I will asume that RDMA is magicaly and don't
2254 * need to transfer (at least) the addresses where it wants to
2255 * write the pages. Here it looks like it should be something
2257 * sizeof(send_wr) + sge.length
2258 * but this being RDMA, who knows.
2260 stat64_add(&mig_stats
.rdma_bytes
, sge
.length
);
2261 ram_transferred_add(sge
.length
);
2262 rdma
->total_writes
++;
2268 * Push out any unwritten RDMA operations.
2270 * We support sending out multiple chunks at the same time.
2271 * Not all of them need to get signaled in the completion queue.
2273 static int qemu_rdma_write_flush(RDMAContext
*rdma
)
2277 if (!rdma
->current_length
) {
2281 ret
= qemu_rdma_write_one(rdma
,
2282 rdma
->current_index
, rdma
->current_addr
, rdma
->current_length
);
2290 trace_qemu_rdma_write_flush(rdma
->nb_sent
);
2293 rdma
->current_length
= 0;
2294 rdma
->current_addr
= 0;
2299 static inline bool qemu_rdma_buffer_mergeable(RDMAContext
*rdma
,
2300 uint64_t offset
, uint64_t len
)
2302 RDMALocalBlock
*block
;
2306 if (rdma
->current_index
< 0) {
2310 if (rdma
->current_chunk
< 0) {
2314 block
= &(rdma
->local_ram_blocks
.block
[rdma
->current_index
]);
2315 host_addr
= block
->local_host_addr
+ (offset
- block
->offset
);
2316 chunk_end
= ram_chunk_end(block
, rdma
->current_chunk
);
2318 if (rdma
->current_length
== 0) {
2323 * Only merge into chunk sequentially.
2325 if (offset
!= (rdma
->current_addr
+ rdma
->current_length
)) {
2329 if (offset
< block
->offset
) {
2333 if ((offset
+ len
) > (block
->offset
+ block
->length
)) {
2337 if ((host_addr
+ len
) > chunk_end
) {
2345 * We're not actually writing here, but doing three things:
2347 * 1. Identify the chunk the buffer belongs to.
2348 * 2. If the chunk is full or the buffer doesn't belong to the current
2349 * chunk, then start a new chunk and flush() the old chunk.
2350 * 3. To keep the hardware busy, we also group chunks into batches
2351 * and only require that a batch gets acknowledged in the completion
2352 * queue instead of each individual chunk.
2354 static int qemu_rdma_write(RDMAContext
*rdma
,
2355 uint64_t block_offset
, uint64_t offset
,
2358 uint64_t current_addr
= block_offset
+ offset
;
2359 uint64_t index
= rdma
->current_index
;
2360 uint64_t chunk
= rdma
->current_chunk
;
2363 /* If we cannot merge it, we flush the current buffer first. */
2364 if (!qemu_rdma_buffer_mergeable(rdma
, current_addr
, len
)) {
2365 ret
= qemu_rdma_write_flush(rdma
);
2369 rdma
->current_length
= 0;
2370 rdma
->current_addr
= current_addr
;
2372 qemu_rdma_search_ram_block(rdma
, block_offset
,
2373 offset
, len
, &index
, &chunk
);
2374 rdma
->current_index
= index
;
2375 rdma
->current_chunk
= chunk
;
2379 rdma
->current_length
+= len
;
2381 /* flush it if buffer is too large */
2382 if (rdma
->current_length
>= RDMA_MERGE_MAX
) {
2383 return qemu_rdma_write_flush(rdma
);
2389 static void qemu_rdma_cleanup(RDMAContext
*rdma
)
2393 if (rdma
->cm_id
&& rdma
->connected
) {
2394 if ((rdma
->errored
||
2395 migrate_get_current()->state
== MIGRATION_STATUS_CANCELLING
) &&
2396 !rdma
->received_error
) {
2397 RDMAControlHeader head
= { .len
= 0,
2398 .type
= RDMA_CONTROL_ERROR
,
2401 error_report("Early error. Sending error.");
2402 qemu_rdma_post_send_control(rdma
, NULL
, &head
);
2405 rdma_disconnect(rdma
->cm_id
);
2406 trace_qemu_rdma_cleanup_disconnect();
2407 rdma
->connected
= false;
2410 if (rdma
->channel
) {
2411 qemu_set_fd_handler(rdma
->channel
->fd
, NULL
, NULL
, NULL
);
2413 g_free(rdma
->dest_blocks
);
2414 rdma
->dest_blocks
= NULL
;
2416 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2417 if (rdma
->wr_data
[idx
].control_mr
) {
2418 rdma
->total_registrations
--;
2419 ibv_dereg_mr(rdma
->wr_data
[idx
].control_mr
);
2421 rdma
->wr_data
[idx
].control_mr
= NULL
;
2424 if (rdma
->local_ram_blocks
.block
) {
2425 while (rdma
->local_ram_blocks
.nb_blocks
) {
2426 rdma_delete_block(rdma
, &rdma
->local_ram_blocks
.block
[0]);
2431 rdma_destroy_qp(rdma
->cm_id
);
2434 if (rdma
->recv_cq
) {
2435 ibv_destroy_cq(rdma
->recv_cq
);
2436 rdma
->recv_cq
= NULL
;
2438 if (rdma
->send_cq
) {
2439 ibv_destroy_cq(rdma
->send_cq
);
2440 rdma
->send_cq
= NULL
;
2442 if (rdma
->recv_comp_channel
) {
2443 ibv_destroy_comp_channel(rdma
->recv_comp_channel
);
2444 rdma
->recv_comp_channel
= NULL
;
2446 if (rdma
->send_comp_channel
) {
2447 ibv_destroy_comp_channel(rdma
->send_comp_channel
);
2448 rdma
->send_comp_channel
= NULL
;
2451 ibv_dealloc_pd(rdma
->pd
);
2455 rdma_destroy_id(rdma
->cm_id
);
2459 /* the destination side, listen_id and channel is shared */
2460 if (rdma
->listen_id
) {
2461 if (!rdma
->is_return_path
) {
2462 rdma_destroy_id(rdma
->listen_id
);
2464 rdma
->listen_id
= NULL
;
2466 if (rdma
->channel
) {
2467 if (!rdma
->is_return_path
) {
2468 rdma_destroy_event_channel(rdma
->channel
);
2470 rdma
->channel
= NULL
;
2474 if (rdma
->channel
) {
2475 rdma_destroy_event_channel(rdma
->channel
);
2476 rdma
->channel
= NULL
;
2479 g_free(rdma
->host_port
);
2481 rdma
->host_port
= NULL
;
2485 static int qemu_rdma_source_init(RDMAContext
*rdma
, bool pin_all
, Error
**errp
)
2490 * Will be validated against destination's actual capabilities
2491 * after the connect() completes.
2493 rdma
->pin_all
= pin_all
;
2495 ret
= qemu_rdma_resolve_host(rdma
, errp
);
2497 goto err_rdma_source_init
;
2500 ret
= qemu_rdma_alloc_pd_cq(rdma
);
2502 error_setg(errp
, "RDMA ERROR: "
2503 "rdma migration: error allocating pd and cq! Your mlock()"
2504 " limits may be too low. Please check $ ulimit -a # and "
2505 "search for 'ulimit -l' in the output");
2506 goto err_rdma_source_init
;
2509 ret
= qemu_rdma_alloc_qp(rdma
);
2511 error_setg(errp
, "RDMA ERROR: rdma migration: error allocating qp!");
2512 goto err_rdma_source_init
;
2515 qemu_rdma_init_ram_blocks(rdma
);
2517 /* Build the hash that maps from offset to RAMBlock */
2518 rdma
->blockmap
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
2519 for (idx
= 0; idx
< rdma
->local_ram_blocks
.nb_blocks
; idx
++) {
2520 g_hash_table_insert(rdma
->blockmap
,
2521 (void *)(uintptr_t)rdma
->local_ram_blocks
.block
[idx
].offset
,
2522 &rdma
->local_ram_blocks
.block
[idx
]);
2525 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2526 ret
= qemu_rdma_reg_control(rdma
, idx
);
2529 "RDMA ERROR: rdma migration: error registering %d control!",
2531 goto err_rdma_source_init
;
2537 err_rdma_source_init
:
2538 qemu_rdma_cleanup(rdma
);
2542 static int qemu_get_cm_event_timeout(RDMAContext
*rdma
,
2543 struct rdma_cm_event
**cm_event
,
2544 long msec
, Error
**errp
)
2547 struct pollfd poll_fd
= {
2548 .fd
= rdma
->channel
->fd
,
2554 ret
= poll(&poll_fd
, 1, msec
);
2555 } while (ret
< 0 && errno
== EINTR
);
2558 error_setg(errp
, "RDMA ERROR: poll cm event timeout");
2560 } else if (ret
< 0) {
2561 error_setg(errp
, "RDMA ERROR: failed to poll cm event, errno=%i",
2564 } else if (poll_fd
.revents
& POLLIN
) {
2565 if (rdma_get_cm_event(rdma
->channel
, cm_event
) < 0) {
2566 error_setg(errp
, "RDMA ERROR: failed to get cm event");
2571 error_setg(errp
, "RDMA ERROR: no POLLIN event, revent=%x",
2577 static int qemu_rdma_connect(RDMAContext
*rdma
, bool return_path
,
2580 RDMACapabilities cap
= {
2581 .version
= RDMA_CONTROL_VERSION_CURRENT
,
2584 struct rdma_conn_param conn_param
= { .initiator_depth
= 2,
2586 .private_data
= &cap
,
2587 .private_data_len
= sizeof(cap
),
2589 struct rdma_cm_event
*cm_event
;
2593 * Only negotiate the capability with destination if the user
2594 * on the source first requested the capability.
2596 if (rdma
->pin_all
) {
2597 trace_qemu_rdma_connect_pin_all_requested();
2598 cap
.flags
|= RDMA_CAPABILITY_PIN_ALL
;
2601 caps_to_network(&cap
);
2603 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
2605 error_setg(errp
, "RDMA ERROR: posting second control recv");
2606 goto err_rdma_source_connect
;
2609 ret
= rdma_connect(rdma
->cm_id
, &conn_param
);
2611 perror("rdma_connect");
2612 error_setg(errp
, "RDMA ERROR: connecting to destination!");
2613 goto err_rdma_source_connect
;
2617 ret
= qemu_get_cm_event_timeout(rdma
, &cm_event
, 5000, errp
);
2619 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
2621 error_setg(errp
, "RDMA ERROR: failed to get cm event");
2626 * FIXME perror() is wrong, because
2627 * qemu_get_cm_event_timeout() can fail without setting errno.
2628 * Will go away later in this series.
2630 perror("rdma_get_cm_event after rdma_connect");
2631 goto err_rdma_source_connect
;
2634 if (cm_event
->event
!= RDMA_CM_EVENT_ESTABLISHED
) {
2635 error_report("rdma_get_cm_event != EVENT_ESTABLISHED after rdma_connect");
2636 error_setg(errp
, "RDMA ERROR: connecting to destination!");
2637 rdma_ack_cm_event(cm_event
);
2638 goto err_rdma_source_connect
;
2640 rdma
->connected
= true;
2642 memcpy(&cap
, cm_event
->param
.conn
.private_data
, sizeof(cap
));
2643 network_to_caps(&cap
);
2646 * Verify that the *requested* capabilities are supported by the destination
2647 * and disable them otherwise.
2649 if (rdma
->pin_all
&& !(cap
.flags
& RDMA_CAPABILITY_PIN_ALL
)) {
2650 warn_report("RDMA: Server cannot support pinning all memory. "
2651 "Will register memory dynamically.");
2652 rdma
->pin_all
= false;
2655 trace_qemu_rdma_connect_pin_all_outcome(rdma
->pin_all
);
2657 rdma_ack_cm_event(cm_event
);
2659 rdma
->control_ready_expected
= 1;
2663 err_rdma_source_connect
:
2664 qemu_rdma_cleanup(rdma
);
2668 static int qemu_rdma_dest_init(RDMAContext
*rdma
, Error
**errp
)
2672 struct rdma_cm_id
*listen_id
;
2673 char ip
[40] = "unknown";
2674 struct rdma_addrinfo
*res
, *e
;
2678 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2679 rdma
->wr_data
[idx
].control_len
= 0;
2680 rdma
->wr_data
[idx
].control_curr
= NULL
;
2683 if (!rdma
->host
|| !rdma
->host
[0]) {
2684 error_setg(errp
, "RDMA ERROR: RDMA host is not set!");
2685 rdma
->errored
= true;
2688 /* create CM channel */
2689 rdma
->channel
= rdma_create_event_channel();
2690 if (!rdma
->channel
) {
2691 error_setg(errp
, "RDMA ERROR: could not create rdma event channel");
2692 rdma
->errored
= true;
2697 ret
= rdma_create_id(rdma
->channel
, &listen_id
, NULL
, RDMA_PS_TCP
);
2699 error_setg(errp
, "RDMA ERROR: could not create cm_id!");
2700 goto err_dest_init_create_listen_id
;
2703 snprintf(port_str
, 16, "%d", rdma
->port
);
2704 port_str
[15] = '\0';
2706 ret
= rdma_getaddrinfo(rdma
->host
, port_str
, NULL
, &res
);
2708 error_setg(errp
, "RDMA ERROR: could not rdma_getaddrinfo address %s",
2710 goto err_dest_init_bind_addr
;
2713 ret
= rdma_set_option(listen_id
, RDMA_OPTION_ID
, RDMA_OPTION_ID_REUSEADDR
,
2714 &reuse
, sizeof reuse
);
2716 error_setg(errp
, "RDMA ERROR: Error: could not set REUSEADDR option");
2717 goto err_dest_init_bind_addr
;
2720 /* Try all addresses, saving the first error in @err */
2721 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
2722 Error
**local_errp
= err
? NULL
: &err
;
2724 inet_ntop(e
->ai_family
,
2725 &((struct sockaddr_in
*) e
->ai_dst_addr
)->sin_addr
, ip
, sizeof ip
);
2726 trace_qemu_rdma_dest_init_trying(rdma
->host
, ip
);
2727 ret
= rdma_bind_addr(listen_id
, e
->ai_dst_addr
);
2731 if (e
->ai_family
== AF_INET6
) {
2732 ret
= qemu_rdma_broken_ipv6_kernel(listen_id
->verbs
,
2742 rdma_freeaddrinfo(res
);
2745 error_propagate(errp
, err
);
2747 error_setg(errp
, "RDMA ERROR: Error: could not rdma_bind_addr!");
2749 goto err_dest_init_bind_addr
;
2752 rdma
->listen_id
= listen_id
;
2753 qemu_rdma_dump_gid("dest_init", listen_id
);
2756 err_dest_init_bind_addr
:
2757 rdma_destroy_id(listen_id
);
2758 err_dest_init_create_listen_id
:
2759 rdma_destroy_event_channel(rdma
->channel
);
2760 rdma
->channel
= NULL
;
2761 rdma
->errored
= true;
2766 static void qemu_rdma_return_path_dest_init(RDMAContext
*rdma_return_path
,
2771 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2772 rdma_return_path
->wr_data
[idx
].control_len
= 0;
2773 rdma_return_path
->wr_data
[idx
].control_curr
= NULL
;
2776 /*the CM channel and CM id is shared*/
2777 rdma_return_path
->channel
= rdma
->channel
;
2778 rdma_return_path
->listen_id
= rdma
->listen_id
;
2780 rdma
->return_path
= rdma_return_path
;
2781 rdma_return_path
->return_path
= rdma
;
2782 rdma_return_path
->is_return_path
= true;
2785 static RDMAContext
*qemu_rdma_data_init(const char *host_port
, Error
**errp
)
2787 RDMAContext
*rdma
= NULL
;
2788 InetSocketAddress
*addr
;
2790 rdma
= g_new0(RDMAContext
, 1);
2791 rdma
->current_index
= -1;
2792 rdma
->current_chunk
= -1;
2794 addr
= g_new(InetSocketAddress
, 1);
2795 if (!inet_parse(addr
, host_port
, NULL
)) {
2796 rdma
->port
= atoi(addr
->port
);
2797 rdma
->host
= g_strdup(addr
->host
);
2798 rdma
->host_port
= g_strdup(host_port
);
2800 error_setg(errp
, "RDMA ERROR: bad RDMA migration address '%s'",
2806 qapi_free_InetSocketAddress(addr
);
2811 * QEMUFile interface to the control channel.
2812 * SEND messages for control only.
2813 * VM's ram is handled with regular RDMA messages.
2815 static ssize_t
qio_channel_rdma_writev(QIOChannel
*ioc
,
2816 const struct iovec
*iov
,
2823 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
2829 RCU_READ_LOCK_GUARD();
2830 rdma
= qatomic_rcu_read(&rioc
->rdmaout
);
2833 error_setg(errp
, "RDMA control channel output is not set");
2837 if (rdma
->errored
) {
2839 "RDMA is in an error state waiting migration to abort!");
2844 * Push out any writes that
2845 * we're queued up for VM's ram.
2847 ret
= qemu_rdma_write_flush(rdma
);
2849 rdma
->errored
= true;
2850 error_setg(errp
, "qemu_rdma_write_flush failed");
2854 for (i
= 0; i
< niov
; i
++) {
2855 size_t remaining
= iov
[i
].iov_len
;
2856 uint8_t * data
= (void *)iov
[i
].iov_base
;
2858 RDMAControlHeader head
= {};
2860 len
= MIN(remaining
, RDMA_SEND_INCREMENT
);
2864 head
.type
= RDMA_CONTROL_QEMU_FILE
;
2866 ret
= qemu_rdma_exchange_send(rdma
, &head
, data
, NULL
, NULL
, NULL
);
2869 rdma
->errored
= true;
2870 error_setg(errp
, "qemu_rdma_exchange_send failed");
2882 static size_t qemu_rdma_fill(RDMAContext
*rdma
, uint8_t *buf
,
2883 size_t size
, int idx
)
2887 if (rdma
->wr_data
[idx
].control_len
) {
2888 trace_qemu_rdma_fill(rdma
->wr_data
[idx
].control_len
, size
);
2890 len
= MIN(size
, rdma
->wr_data
[idx
].control_len
);
2891 memcpy(buf
, rdma
->wr_data
[idx
].control_curr
, len
);
2892 rdma
->wr_data
[idx
].control_curr
+= len
;
2893 rdma
->wr_data
[idx
].control_len
-= len
;
2900 * QEMUFile interface to the control channel.
2901 * RDMA links don't use bytestreams, so we have to
2902 * return bytes to QEMUFile opportunistically.
2904 static ssize_t
qio_channel_rdma_readv(QIOChannel
*ioc
,
2905 const struct iovec
*iov
,
2912 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
2914 RDMAControlHeader head
;
2919 RCU_READ_LOCK_GUARD();
2920 rdma
= qatomic_rcu_read(&rioc
->rdmain
);
2923 error_setg(errp
, "RDMA control channel input is not set");
2927 if (rdma
->errored
) {
2929 "RDMA is in an error state waiting migration to abort!");
2933 for (i
= 0; i
< niov
; i
++) {
2934 size_t want
= iov
[i
].iov_len
;
2935 uint8_t *data
= (void *)iov
[i
].iov_base
;
2938 * First, we hold on to the last SEND message we
2939 * were given and dish out the bytes until we run
2942 len
= qemu_rdma_fill(rdma
, data
, want
, 0);
2945 /* Got what we needed, so go to next iovec */
2950 /* If we got any data so far, then don't wait
2951 * for more, just return what we have */
2957 /* We've got nothing at all, so lets wait for
2960 ret
= qemu_rdma_exchange_recv(rdma
, &head
, RDMA_CONTROL_QEMU_FILE
);
2963 rdma
->errored
= true;
2964 error_setg(errp
, "qemu_rdma_exchange_recv failed");
2969 * SEND was received with new bytes, now try again.
2971 len
= qemu_rdma_fill(rdma
, data
, want
, 0);
2975 /* Still didn't get enough, so lets just return */
2978 return QIO_CHANNEL_ERR_BLOCK
;
2988 * Block until all the outstanding chunks have been delivered by the hardware.
2990 static int qemu_rdma_drain_cq(RDMAContext
*rdma
)
2994 if (qemu_rdma_write_flush(rdma
) < 0) {
2998 while (rdma
->nb_sent
) {
2999 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RDMA_WRITE
, NULL
);
3001 error_report("rdma migration: complete polling error!");
3006 qemu_rdma_unregister_waiting(rdma
);
3012 static int qio_channel_rdma_set_blocking(QIOChannel
*ioc
,
3016 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
3017 /* XXX we should make readv/writev actually honour this :-) */
3018 rioc
->blocking
= blocking
;
3023 typedef struct QIOChannelRDMASource QIOChannelRDMASource
;
3024 struct QIOChannelRDMASource
{
3026 QIOChannelRDMA
*rioc
;
3027 GIOCondition condition
;
3031 qio_channel_rdma_source_prepare(GSource
*source
,
3034 QIOChannelRDMASource
*rsource
= (QIOChannelRDMASource
*)source
;
3036 GIOCondition cond
= 0;
3039 RCU_READ_LOCK_GUARD();
3040 if (rsource
->condition
== G_IO_IN
) {
3041 rdma
= qatomic_rcu_read(&rsource
->rioc
->rdmain
);
3043 rdma
= qatomic_rcu_read(&rsource
->rioc
->rdmaout
);
3047 error_report("RDMAContext is NULL when prepare Gsource");
3051 if (rdma
->wr_data
[0].control_len
) {
3056 return cond
& rsource
->condition
;
3060 qio_channel_rdma_source_check(GSource
*source
)
3062 QIOChannelRDMASource
*rsource
= (QIOChannelRDMASource
*)source
;
3064 GIOCondition cond
= 0;
3066 RCU_READ_LOCK_GUARD();
3067 if (rsource
->condition
== G_IO_IN
) {
3068 rdma
= qatomic_rcu_read(&rsource
->rioc
->rdmain
);
3070 rdma
= qatomic_rcu_read(&rsource
->rioc
->rdmaout
);
3074 error_report("RDMAContext is NULL when check Gsource");
3078 if (rdma
->wr_data
[0].control_len
) {
3083 return cond
& rsource
->condition
;
3087 qio_channel_rdma_source_dispatch(GSource
*source
,
3088 GSourceFunc callback
,
3091 QIOChannelFunc func
= (QIOChannelFunc
)callback
;
3092 QIOChannelRDMASource
*rsource
= (QIOChannelRDMASource
*)source
;
3094 GIOCondition cond
= 0;
3096 RCU_READ_LOCK_GUARD();
3097 if (rsource
->condition
== G_IO_IN
) {
3098 rdma
= qatomic_rcu_read(&rsource
->rioc
->rdmain
);
3100 rdma
= qatomic_rcu_read(&rsource
->rioc
->rdmaout
);
3104 error_report("RDMAContext is NULL when dispatch Gsource");
3108 if (rdma
->wr_data
[0].control_len
) {
3113 return (*func
)(QIO_CHANNEL(rsource
->rioc
),
3114 (cond
& rsource
->condition
),
3119 qio_channel_rdma_source_finalize(GSource
*source
)
3121 QIOChannelRDMASource
*ssource
= (QIOChannelRDMASource
*)source
;
3123 object_unref(OBJECT(ssource
->rioc
));
3126 static GSourceFuncs qio_channel_rdma_source_funcs
= {
3127 qio_channel_rdma_source_prepare
,
3128 qio_channel_rdma_source_check
,
3129 qio_channel_rdma_source_dispatch
,
3130 qio_channel_rdma_source_finalize
3133 static GSource
*qio_channel_rdma_create_watch(QIOChannel
*ioc
,
3134 GIOCondition condition
)
3136 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
3137 QIOChannelRDMASource
*ssource
;
3140 source
= g_source_new(&qio_channel_rdma_source_funcs
,
3141 sizeof(QIOChannelRDMASource
));
3142 ssource
= (QIOChannelRDMASource
*)source
;
3144 ssource
->rioc
= rioc
;
3145 object_ref(OBJECT(rioc
));
3147 ssource
->condition
= condition
;
3152 static void qio_channel_rdma_set_aio_fd_handler(QIOChannel
*ioc
,
3153 AioContext
*read_ctx
,
3155 AioContext
*write_ctx
,
3156 IOHandler
*io_write
,
3159 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
3161 aio_set_fd_handler(read_ctx
, rioc
->rdmain
->recv_comp_channel
->fd
,
3162 io_read
, io_write
, NULL
, NULL
, opaque
);
3163 aio_set_fd_handler(read_ctx
, rioc
->rdmain
->send_comp_channel
->fd
,
3164 io_read
, io_write
, NULL
, NULL
, opaque
);
3166 aio_set_fd_handler(write_ctx
, rioc
->rdmaout
->recv_comp_channel
->fd
,
3167 io_read
, io_write
, NULL
, NULL
, opaque
);
3168 aio_set_fd_handler(write_ctx
, rioc
->rdmaout
->send_comp_channel
->fd
,
3169 io_read
, io_write
, NULL
, NULL
, opaque
);
3173 struct rdma_close_rcu
{
3174 struct rcu_head rcu
;
3175 RDMAContext
*rdmain
;
3176 RDMAContext
*rdmaout
;
3179 /* callback from qio_channel_rdma_close via call_rcu */
3180 static void qio_channel_rdma_close_rcu(struct rdma_close_rcu
*rcu
)
3183 qemu_rdma_cleanup(rcu
->rdmain
);
3187 qemu_rdma_cleanup(rcu
->rdmaout
);
3190 g_free(rcu
->rdmain
);
3191 g_free(rcu
->rdmaout
);
3195 static int qio_channel_rdma_close(QIOChannel
*ioc
,
3198 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
3199 RDMAContext
*rdmain
, *rdmaout
;
3200 struct rdma_close_rcu
*rcu
= g_new(struct rdma_close_rcu
, 1);
3202 trace_qemu_rdma_close();
3204 rdmain
= rioc
->rdmain
;
3206 qatomic_rcu_set(&rioc
->rdmain
, NULL
);
3209 rdmaout
= rioc
->rdmaout
;
3211 qatomic_rcu_set(&rioc
->rdmaout
, NULL
);
3214 rcu
->rdmain
= rdmain
;
3215 rcu
->rdmaout
= rdmaout
;
3216 call_rcu(rcu
, qio_channel_rdma_close_rcu
, rcu
);
3222 qio_channel_rdma_shutdown(QIOChannel
*ioc
,
3223 QIOChannelShutdown how
,
3226 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
3227 RDMAContext
*rdmain
, *rdmaout
;
3229 RCU_READ_LOCK_GUARD();
3231 rdmain
= qatomic_rcu_read(&rioc
->rdmain
);
3232 rdmaout
= qatomic_rcu_read(&rioc
->rdmain
);
3235 case QIO_CHANNEL_SHUTDOWN_READ
:
3237 rdmain
->errored
= true;
3240 case QIO_CHANNEL_SHUTDOWN_WRITE
:
3242 rdmaout
->errored
= true;
3245 case QIO_CHANNEL_SHUTDOWN_BOTH
:
3248 rdmain
->errored
= true;
3251 rdmaout
->errored
= true;
3262 * This means that 'block_offset' is a full virtual address that does not
3263 * belong to a RAMBlock of the virtual machine and instead
3264 * represents a private malloc'd memory area that the caller wishes to
3268 * Offset is an offset to be added to block_offset and used
3269 * to also lookup the corresponding RAMBlock.
3271 * @size : Number of bytes to transfer
3273 * @pages_sent : User-specificed pointer to indicate how many pages were
3274 * sent. Usually, this will not be more than a few bytes of
3275 * the protocol because most transfers are sent asynchronously.
3277 static int qemu_rdma_save_page(QEMUFile
*f
, ram_addr_t block_offset
,
3278 ram_addr_t offset
, size_t size
)
3280 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(qemu_file_get_ioc(f
));
3284 if (migration_in_postcopy()) {
3285 return RAM_SAVE_CONTROL_NOT_SUPP
;
3288 RCU_READ_LOCK_GUARD();
3289 rdma
= qatomic_rcu_read(&rioc
->rdmaout
);
3295 if (rdma_errored(rdma
)) {
3302 * Add this page to the current 'chunk'. If the chunk
3303 * is full, or the page doesn't belong to the current chunk,
3304 * an actual RDMA write will occur and a new chunk will be formed.
3306 ret
= qemu_rdma_write(rdma
, block_offset
, offset
, size
);
3308 error_report("rdma migration: write error");
3313 * Drain the Completion Queue if possible, but do not block,
3316 * If nothing to poll, the end of the iteration will do this
3317 * again to make sure we don't overflow the request queue.
3320 uint64_t wr_id
, wr_id_in
;
3321 ret
= qemu_rdma_poll(rdma
, rdma
->recv_cq
, &wr_id_in
, NULL
);
3324 error_report("rdma migration: polling error");
3328 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
3330 if (wr_id
== RDMA_WRID_NONE
) {
3336 uint64_t wr_id
, wr_id_in
;
3337 ret
= qemu_rdma_poll(rdma
, rdma
->send_cq
, &wr_id_in
, NULL
);
3340 error_report("rdma migration: polling error");
3344 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
3346 if (wr_id
== RDMA_WRID_NONE
) {
3351 return RAM_SAVE_CONTROL_DELAYED
;
3354 rdma
->errored
= true;
3358 static void rdma_accept_incoming_migration(void *opaque
);
3360 static void rdma_cm_poll_handler(void *opaque
)
3362 RDMAContext
*rdma
= opaque
;
3364 struct rdma_cm_event
*cm_event
;
3365 MigrationIncomingState
*mis
= migration_incoming_get_current();
3367 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
3369 error_report("get_cm_event failed %d", errno
);
3373 if (cm_event
->event
== RDMA_CM_EVENT_DISCONNECTED
||
3374 cm_event
->event
== RDMA_CM_EVENT_DEVICE_REMOVAL
) {
3375 if (!rdma
->errored
&&
3376 migration_incoming_get_current()->state
!=
3377 MIGRATION_STATUS_COMPLETED
) {
3378 error_report("receive cm event, cm event is %d", cm_event
->event
);
3379 rdma
->errored
= true;
3380 if (rdma
->return_path
) {
3381 rdma
->return_path
->errored
= true;
3384 rdma_ack_cm_event(cm_event
);
3385 if (mis
->loadvm_co
) {
3386 qemu_coroutine_enter(mis
->loadvm_co
);
3390 rdma_ack_cm_event(cm_event
);
3393 static int qemu_rdma_accept(RDMAContext
*rdma
)
3395 RDMACapabilities cap
;
3396 struct rdma_conn_param conn_param
= {
3397 .responder_resources
= 2,
3398 .private_data
= &cap
,
3399 .private_data_len
= sizeof(cap
),
3401 RDMAContext
*rdma_return_path
= NULL
;
3402 struct rdma_cm_event
*cm_event
;
3403 struct ibv_context
*verbs
;
3407 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
3409 goto err_rdma_dest_wait
;
3412 if (cm_event
->event
!= RDMA_CM_EVENT_CONNECT_REQUEST
) {
3413 rdma_ack_cm_event(cm_event
);
3414 goto err_rdma_dest_wait
;
3418 * initialize the RDMAContext for return path for postcopy after first
3419 * connection request reached.
3421 if ((migrate_postcopy() || migrate_return_path())
3422 && !rdma
->is_return_path
) {
3423 rdma_return_path
= qemu_rdma_data_init(rdma
->host_port
, NULL
);
3424 if (rdma_return_path
== NULL
) {
3425 rdma_ack_cm_event(cm_event
);
3426 goto err_rdma_dest_wait
;
3429 qemu_rdma_return_path_dest_init(rdma_return_path
, rdma
);
3432 memcpy(&cap
, cm_event
->param
.conn
.private_data
, sizeof(cap
));
3434 network_to_caps(&cap
);
3436 if (cap
.version
< 1 || cap
.version
> RDMA_CONTROL_VERSION_CURRENT
) {
3437 error_report("Unknown source RDMA version: %d, bailing...",
3439 rdma_ack_cm_event(cm_event
);
3440 goto err_rdma_dest_wait
;
3444 * Respond with only the capabilities this version of QEMU knows about.
3446 cap
.flags
&= known_capabilities
;
3449 * Enable the ones that we do know about.
3450 * Add other checks here as new ones are introduced.
3452 if (cap
.flags
& RDMA_CAPABILITY_PIN_ALL
) {
3453 rdma
->pin_all
= true;
3456 rdma
->cm_id
= cm_event
->id
;
3457 verbs
= cm_event
->id
->verbs
;
3459 rdma_ack_cm_event(cm_event
);
3461 trace_qemu_rdma_accept_pin_state(rdma
->pin_all
);
3463 caps_to_network(&cap
);
3465 trace_qemu_rdma_accept_pin_verbsc(verbs
);
3468 rdma
->verbs
= verbs
;
3469 } else if (rdma
->verbs
!= verbs
) {
3470 error_report("ibv context not matching %p, %p!", rdma
->verbs
,
3472 goto err_rdma_dest_wait
;
3475 qemu_rdma_dump_id("dest_init", verbs
);
3477 ret
= qemu_rdma_alloc_pd_cq(rdma
);
3479 error_report("rdma migration: error allocating pd and cq!");
3480 goto err_rdma_dest_wait
;
3483 ret
= qemu_rdma_alloc_qp(rdma
);
3485 error_report("rdma migration: error allocating qp!");
3486 goto err_rdma_dest_wait
;
3489 qemu_rdma_init_ram_blocks(rdma
);
3491 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
3492 ret
= qemu_rdma_reg_control(rdma
, idx
);
3494 error_report("rdma: error registering %d control", idx
);
3495 goto err_rdma_dest_wait
;
3499 /* Accept the second connection request for return path */
3500 if ((migrate_postcopy() || migrate_return_path())
3501 && !rdma
->is_return_path
) {
3502 qemu_set_fd_handler(rdma
->channel
->fd
, rdma_accept_incoming_migration
,
3504 (void *)(intptr_t)rdma
->return_path
);
3506 qemu_set_fd_handler(rdma
->channel
->fd
, rdma_cm_poll_handler
,
3510 ret
= rdma_accept(rdma
->cm_id
, &conn_param
);
3512 error_report("rdma_accept failed");
3513 goto err_rdma_dest_wait
;
3516 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
3518 error_report("rdma_accept get_cm_event failed");
3519 goto err_rdma_dest_wait
;
3522 if (cm_event
->event
!= RDMA_CM_EVENT_ESTABLISHED
) {
3523 error_report("rdma_accept not event established");
3524 rdma_ack_cm_event(cm_event
);
3525 goto err_rdma_dest_wait
;
3528 rdma_ack_cm_event(cm_event
);
3529 rdma
->connected
= true;
3531 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
3533 error_report("rdma migration: error posting second control recv");
3534 goto err_rdma_dest_wait
;
3537 qemu_rdma_dump_gid("dest_connect", rdma
->cm_id
);
3542 rdma
->errored
= true;
3543 qemu_rdma_cleanup(rdma
);
3544 g_free(rdma_return_path
);
3548 static int dest_ram_sort_func(const void *a
, const void *b
)
3550 unsigned int a_index
= ((const RDMALocalBlock
*)a
)->src_index
;
3551 unsigned int b_index
= ((const RDMALocalBlock
*)b
)->src_index
;
3553 return (a_index
< b_index
) ? -1 : (a_index
!= b_index
);
3557 * During each iteration of the migration, we listen for instructions
3558 * by the source VM to perform dynamic page registrations before they
3559 * can perform RDMA operations.
3561 * We respond with the 'rkey'.
3563 * Keep doing this until the source tells us to stop.
3565 static int qemu_rdma_registration_handle(QEMUFile
*f
)
3567 RDMAControlHeader reg_resp
= { .len
= sizeof(RDMARegisterResult
),
3568 .type
= RDMA_CONTROL_REGISTER_RESULT
,
3571 RDMAControlHeader unreg_resp
= { .len
= 0,
3572 .type
= RDMA_CONTROL_UNREGISTER_FINISHED
,
3575 RDMAControlHeader blocks
= { .type
= RDMA_CONTROL_RAM_BLOCKS_RESULT
,
3577 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(qemu_file_get_ioc(f
));
3579 RDMALocalBlocks
*local
;
3580 RDMAControlHeader head
;
3581 RDMARegister
*reg
, *registers
;
3583 RDMARegisterResult
*reg_result
;
3584 static RDMARegisterResult results
[RDMA_CONTROL_MAX_COMMANDS_PER_MESSAGE
];
3585 RDMALocalBlock
*block
;
3592 RCU_READ_LOCK_GUARD();
3593 rdma
= qatomic_rcu_read(&rioc
->rdmain
);
3599 if (rdma_errored(rdma
)) {
3603 local
= &rdma
->local_ram_blocks
;
3605 trace_qemu_rdma_registration_handle_wait();
3607 ret
= qemu_rdma_exchange_recv(rdma
, &head
, RDMA_CONTROL_NONE
);
3613 if (head
.repeat
> RDMA_CONTROL_MAX_COMMANDS_PER_MESSAGE
) {
3614 error_report("rdma: Too many requests in this message (%d)."
3615 "Bailing.", head
.repeat
);
3619 switch (head
.type
) {
3620 case RDMA_CONTROL_COMPRESS
:
3621 comp
= (RDMACompress
*) rdma
->wr_data
[idx
].control_curr
;
3622 network_to_compress(comp
);
3624 trace_qemu_rdma_registration_handle_compress(comp
->length
,
3627 if (comp
->block_idx
>= rdma
->local_ram_blocks
.nb_blocks
) {
3628 error_report("rdma: 'compress' bad block index %u (vs %d)",
3629 (unsigned int)comp
->block_idx
,
3630 rdma
->local_ram_blocks
.nb_blocks
);
3633 block
= &(rdma
->local_ram_blocks
.block
[comp
->block_idx
]);
3635 host_addr
= block
->local_host_addr
+
3636 (comp
->offset
- block
->offset
);
3638 ram_handle_compressed(host_addr
, comp
->value
, comp
->length
);
3641 case RDMA_CONTROL_REGISTER_FINISHED
:
3642 trace_qemu_rdma_registration_handle_finished();
3645 case RDMA_CONTROL_RAM_BLOCKS_REQUEST
:
3646 trace_qemu_rdma_registration_handle_ram_blocks();
3648 /* Sort our local RAM Block list so it's the same as the source,
3649 * we can do this since we've filled in a src_index in the list
3650 * as we received the RAMBlock list earlier.
3652 qsort(rdma
->local_ram_blocks
.block
,
3653 rdma
->local_ram_blocks
.nb_blocks
,
3654 sizeof(RDMALocalBlock
), dest_ram_sort_func
);
3655 for (i
= 0; i
< local
->nb_blocks
; i
++) {
3656 local
->block
[i
].index
= i
;
3659 if (rdma
->pin_all
) {
3660 ret
= qemu_rdma_reg_whole_ram_blocks(rdma
);
3662 error_report("rdma migration: error dest "
3663 "registering ram blocks");
3669 * Dest uses this to prepare to transmit the RAMBlock descriptions
3670 * to the source VM after connection setup.
3671 * Both sides use the "remote" structure to communicate and update
3672 * their "local" descriptions with what was sent.
3674 for (i
= 0; i
< local
->nb_blocks
; i
++) {
3675 rdma
->dest_blocks
[i
].remote_host_addr
=
3676 (uintptr_t)(local
->block
[i
].local_host_addr
);
3678 if (rdma
->pin_all
) {
3679 rdma
->dest_blocks
[i
].remote_rkey
= local
->block
[i
].mr
->rkey
;
3682 rdma
->dest_blocks
[i
].offset
= local
->block
[i
].offset
;
3683 rdma
->dest_blocks
[i
].length
= local
->block
[i
].length
;
3685 dest_block_to_network(&rdma
->dest_blocks
[i
]);
3686 trace_qemu_rdma_registration_handle_ram_blocks_loop(
3687 local
->block
[i
].block_name
,
3688 local
->block
[i
].offset
,
3689 local
->block
[i
].length
,
3690 local
->block
[i
].local_host_addr
,
3691 local
->block
[i
].src_index
);
3694 blocks
.len
= rdma
->local_ram_blocks
.nb_blocks
3695 * sizeof(RDMADestBlock
);
3698 ret
= qemu_rdma_post_send_control(rdma
,
3699 (uint8_t *) rdma
->dest_blocks
, &blocks
);
3702 error_report("rdma migration: error sending remote info");
3707 case RDMA_CONTROL_REGISTER_REQUEST
:
3708 trace_qemu_rdma_registration_handle_register(head
.repeat
);
3710 reg_resp
.repeat
= head
.repeat
;
3711 registers
= (RDMARegister
*) rdma
->wr_data
[idx
].control_curr
;
3713 for (count
= 0; count
< head
.repeat
; count
++) {
3715 uint8_t *chunk_start
, *chunk_end
;
3717 reg
= ®isters
[count
];
3718 network_to_register(reg
);
3720 reg_result
= &results
[count
];
3722 trace_qemu_rdma_registration_handle_register_loop(count
,
3723 reg
->current_index
, reg
->key
.current_addr
, reg
->chunks
);
3725 if (reg
->current_index
>= rdma
->local_ram_blocks
.nb_blocks
) {
3726 error_report("rdma: 'register' bad block index %u (vs %d)",
3727 (unsigned int)reg
->current_index
,
3728 rdma
->local_ram_blocks
.nb_blocks
);
3731 block
= &(rdma
->local_ram_blocks
.block
[reg
->current_index
]);
3732 if (block
->is_ram_block
) {
3733 if (block
->offset
> reg
->key
.current_addr
) {
3734 error_report("rdma: bad register address for block %s"
3735 " offset: %" PRIx64
" current_addr: %" PRIx64
,
3736 block
->block_name
, block
->offset
,
3737 reg
->key
.current_addr
);
3740 host_addr
= (block
->local_host_addr
+
3741 (reg
->key
.current_addr
- block
->offset
));
3742 chunk
= ram_chunk_index(block
->local_host_addr
,
3743 (uint8_t *) host_addr
);
3745 chunk
= reg
->key
.chunk
;
3746 host_addr
= block
->local_host_addr
+
3747 (reg
->key
.chunk
* (1UL << RDMA_REG_CHUNK_SHIFT
));
3748 /* Check for particularly bad chunk value */
3749 if (host_addr
< (void *)block
->local_host_addr
) {
3750 error_report("rdma: bad chunk for block %s"
3752 block
->block_name
, reg
->key
.chunk
);
3756 chunk_start
= ram_chunk_start(block
, chunk
);
3757 chunk_end
= ram_chunk_end(block
, chunk
+ reg
->chunks
);
3758 /* avoid "-Waddress-of-packed-member" warning */
3759 uint32_t tmp_rkey
= 0;
3760 if (qemu_rdma_register_and_get_keys(rdma
, block
,
3761 (uintptr_t)host_addr
, NULL
, &tmp_rkey
,
3762 chunk
, chunk_start
, chunk_end
)) {
3763 error_report("cannot get rkey");
3766 reg_result
->rkey
= tmp_rkey
;
3768 reg_result
->host_addr
= (uintptr_t)block
->local_host_addr
;
3770 trace_qemu_rdma_registration_handle_register_rkey(
3773 result_to_network(reg_result
);
3776 ret
= qemu_rdma_post_send_control(rdma
,
3777 (uint8_t *) results
, ®_resp
);
3780 error_report("Failed to send control buffer");
3784 case RDMA_CONTROL_UNREGISTER_REQUEST
:
3785 trace_qemu_rdma_registration_handle_unregister(head
.repeat
);
3786 unreg_resp
.repeat
= head
.repeat
;
3787 registers
= (RDMARegister
*) rdma
->wr_data
[idx
].control_curr
;
3789 for (count
= 0; count
< head
.repeat
; count
++) {
3790 reg
= ®isters
[count
];
3791 network_to_register(reg
);
3793 trace_qemu_rdma_registration_handle_unregister_loop(count
,
3794 reg
->current_index
, reg
->key
.chunk
);
3796 block
= &(rdma
->local_ram_blocks
.block
[reg
->current_index
]);
3798 ret
= ibv_dereg_mr(block
->pmr
[reg
->key
.chunk
]);
3799 block
->pmr
[reg
->key
.chunk
] = NULL
;
3802 perror("rdma unregistration chunk failed");
3806 rdma
->total_registrations
--;
3808 trace_qemu_rdma_registration_handle_unregister_success(
3812 ret
= qemu_rdma_post_send_control(rdma
, NULL
, &unreg_resp
);
3815 error_report("Failed to send control buffer");
3819 case RDMA_CONTROL_REGISTER_RESULT
:
3820 error_report("Invalid RESULT message at dest.");
3823 error_report("Unknown control message %s", control_desc(head
.type
));
3829 rdma
->errored
= true;
3834 * Called via a ram_control_load_hook during the initial RAM load section which
3835 * lists the RAMBlocks by name. This lets us know the order of the RAMBlocks
3837 * We've already built our local RAMBlock list, but not yet sent the list to
3841 rdma_block_notification_handle(QEMUFile
*f
, const char *name
)
3844 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(qemu_file_get_ioc(f
));
3848 RCU_READ_LOCK_GUARD();
3849 rdma
= qatomic_rcu_read(&rioc
->rdmain
);
3855 /* Find the matching RAMBlock in our local list */
3856 for (curr
= 0; curr
< rdma
->local_ram_blocks
.nb_blocks
; curr
++) {
3857 if (!strcmp(rdma
->local_ram_blocks
.block
[curr
].block_name
, name
)) {
3864 error_report("RAMBlock '%s' not found on destination", name
);
3868 rdma
->local_ram_blocks
.block
[curr
].src_index
= rdma
->next_src_index
;
3869 trace_rdma_block_notification_handle(name
, rdma
->next_src_index
);
3870 rdma
->next_src_index
++;
3875 static int rdma_load_hook(QEMUFile
*f
, uint64_t flags
, void *data
)
3878 case RAM_CONTROL_BLOCK_REG
:
3879 return rdma_block_notification_handle(f
, data
);
3881 case RAM_CONTROL_HOOK
:
3882 return qemu_rdma_registration_handle(f
);
3885 /* Shouldn't be called with any other values */
3890 static int qemu_rdma_registration_start(QEMUFile
*f
,
3891 uint64_t flags
, void *data
)
3893 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(qemu_file_get_ioc(f
));
3896 if (migration_in_postcopy()) {
3900 RCU_READ_LOCK_GUARD();
3901 rdma
= qatomic_rcu_read(&rioc
->rdmaout
);
3906 if (rdma_errored(rdma
)) {
3910 trace_qemu_rdma_registration_start(flags
);
3911 qemu_put_be64(f
, RAM_SAVE_FLAG_HOOK
);
3918 * Inform dest that dynamic registrations are done for now.
3919 * First, flush writes, if any.
3921 static int qemu_rdma_registration_stop(QEMUFile
*f
,
3922 uint64_t flags
, void *data
)
3924 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(qemu_file_get_ioc(f
));
3926 RDMAControlHeader head
= { .len
= 0, .repeat
= 1 };
3929 if (migration_in_postcopy()) {
3933 RCU_READ_LOCK_GUARD();
3934 rdma
= qatomic_rcu_read(&rioc
->rdmaout
);
3939 if (rdma_errored(rdma
)) {
3944 ret
= qemu_rdma_drain_cq(rdma
);
3950 if (flags
== RAM_CONTROL_SETUP
) {
3951 RDMAControlHeader resp
= {.type
= RDMA_CONTROL_RAM_BLOCKS_RESULT
};
3952 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
3953 int reg_result_idx
, i
, nb_dest_blocks
;
3955 head
.type
= RDMA_CONTROL_RAM_BLOCKS_REQUEST
;
3956 trace_qemu_rdma_registration_stop_ram();
3959 * Make sure that we parallelize the pinning on both sides.
3960 * For very large guests, doing this serially takes a really
3961 * long time, so we have to 'interleave' the pinning locally
3962 * with the control messages by performing the pinning on this
3963 * side before we receive the control response from the other
3964 * side that the pinning has completed.
3966 ret
= qemu_rdma_exchange_send(rdma
, &head
, NULL
, &resp
,
3967 ®_result_idx
, rdma
->pin_all
?
3968 qemu_rdma_reg_whole_ram_blocks
: NULL
);
3970 fprintf(stderr
, "receiving remote info!");
3974 nb_dest_blocks
= resp
.len
/ sizeof(RDMADestBlock
);
3977 * The protocol uses two different sets of rkeys (mutually exclusive):
3978 * 1. One key to represent the virtual address of the entire ram block.
3979 * (dynamic chunk registration disabled - pin everything with one rkey.)
3980 * 2. One to represent individual chunks within a ram block.
3981 * (dynamic chunk registration enabled - pin individual chunks.)
3983 * Once the capability is successfully negotiated, the destination transmits
3984 * the keys to use (or sends them later) including the virtual addresses
3985 * and then propagates the remote ram block descriptions to his local copy.
3988 if (local
->nb_blocks
!= nb_dest_blocks
) {
3989 fprintf(stderr
, "ram blocks mismatch (Number of blocks %d vs %d) "
3990 "Your QEMU command line parameters are probably "
3991 "not identical on both the source and destination.",
3992 local
->nb_blocks
, nb_dest_blocks
);
3993 rdma
->errored
= true;
3997 qemu_rdma_move_header(rdma
, reg_result_idx
, &resp
);
3998 memcpy(rdma
->dest_blocks
,
3999 rdma
->wr_data
[reg_result_idx
].control_curr
, resp
.len
);
4000 for (i
= 0; i
< nb_dest_blocks
; i
++) {
4001 network_to_dest_block(&rdma
->dest_blocks
[i
]);
4003 /* We require that the blocks are in the same order */
4004 if (rdma
->dest_blocks
[i
].length
!= local
->block
[i
].length
) {
4005 fprintf(stderr
, "Block %s/%d has a different length %" PRIu64
4006 "vs %" PRIu64
, local
->block
[i
].block_name
, i
,
4007 local
->block
[i
].length
,
4008 rdma
->dest_blocks
[i
].length
);
4009 rdma
->errored
= true;
4012 local
->block
[i
].remote_host_addr
=
4013 rdma
->dest_blocks
[i
].remote_host_addr
;
4014 local
->block
[i
].remote_rkey
= rdma
->dest_blocks
[i
].remote_rkey
;
4018 trace_qemu_rdma_registration_stop(flags
);
4020 head
.type
= RDMA_CONTROL_REGISTER_FINISHED
;
4021 ret
= qemu_rdma_exchange_send(rdma
, &head
, NULL
, NULL
, NULL
, NULL
);
4029 rdma
->errored
= true;
4033 static const QEMUFileHooks rdma_read_hooks
= {
4034 .hook_ram_load
= rdma_load_hook
,
4037 static const QEMUFileHooks rdma_write_hooks
= {
4038 .before_ram_iterate
= qemu_rdma_registration_start
,
4039 .after_ram_iterate
= qemu_rdma_registration_stop
,
4040 .save_page
= qemu_rdma_save_page
,
4044 static void qio_channel_rdma_finalize(Object
*obj
)
4046 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(obj
);
4048 qemu_rdma_cleanup(rioc
->rdmain
);
4049 g_free(rioc
->rdmain
);
4050 rioc
->rdmain
= NULL
;
4052 if (rioc
->rdmaout
) {
4053 qemu_rdma_cleanup(rioc
->rdmaout
);
4054 g_free(rioc
->rdmaout
);
4055 rioc
->rdmaout
= NULL
;
4059 static void qio_channel_rdma_class_init(ObjectClass
*klass
,
4060 void *class_data G_GNUC_UNUSED
)
4062 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
4064 ioc_klass
->io_writev
= qio_channel_rdma_writev
;
4065 ioc_klass
->io_readv
= qio_channel_rdma_readv
;
4066 ioc_klass
->io_set_blocking
= qio_channel_rdma_set_blocking
;
4067 ioc_klass
->io_close
= qio_channel_rdma_close
;
4068 ioc_klass
->io_create_watch
= qio_channel_rdma_create_watch
;
4069 ioc_klass
->io_set_aio_fd_handler
= qio_channel_rdma_set_aio_fd_handler
;
4070 ioc_klass
->io_shutdown
= qio_channel_rdma_shutdown
;
4073 static const TypeInfo qio_channel_rdma_info
= {
4074 .parent
= TYPE_QIO_CHANNEL
,
4075 .name
= TYPE_QIO_CHANNEL_RDMA
,
4076 .instance_size
= sizeof(QIOChannelRDMA
),
4077 .instance_finalize
= qio_channel_rdma_finalize
,
4078 .class_init
= qio_channel_rdma_class_init
,
4081 static void qio_channel_rdma_register_types(void)
4083 type_register_static(&qio_channel_rdma_info
);
4086 type_init(qio_channel_rdma_register_types
);
4088 static QEMUFile
*rdma_new_input(RDMAContext
*rdma
)
4090 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(object_new(TYPE_QIO_CHANNEL_RDMA
));
4092 rioc
->file
= qemu_file_new_input(QIO_CHANNEL(rioc
));
4093 rioc
->rdmain
= rdma
;
4094 rioc
->rdmaout
= rdma
->return_path
;
4095 qemu_file_set_hooks(rioc
->file
, &rdma_read_hooks
);
4100 static QEMUFile
*rdma_new_output(RDMAContext
*rdma
)
4102 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(object_new(TYPE_QIO_CHANNEL_RDMA
));
4104 rioc
->file
= qemu_file_new_output(QIO_CHANNEL(rioc
));
4105 rioc
->rdmaout
= rdma
;
4106 rioc
->rdmain
= rdma
->return_path
;
4107 qemu_file_set_hooks(rioc
->file
, &rdma_write_hooks
);
4112 static void rdma_accept_incoming_migration(void *opaque
)
4114 RDMAContext
*rdma
= opaque
;
4117 Error
*local_err
= NULL
;
4119 trace_qemu_rdma_accept_incoming_migration();
4120 ret
= qemu_rdma_accept(rdma
);
4123 fprintf(stderr
, "RDMA ERROR: Migration initialization failed\n");
4127 trace_qemu_rdma_accept_incoming_migration_accepted();
4129 if (rdma
->is_return_path
) {
4133 f
= rdma_new_input(rdma
);
4135 fprintf(stderr
, "RDMA ERROR: could not open RDMA for input\n");
4136 qemu_rdma_cleanup(rdma
);
4140 rdma
->migration_started_on_destination
= 1;
4141 migration_fd_process_incoming(f
, &local_err
);
4143 error_reportf_err(local_err
, "RDMA ERROR:");
4147 void rdma_start_incoming_migration(const char *host_port
, Error
**errp
)
4152 trace_rdma_start_incoming_migration();
4154 /* Avoid ram_block_discard_disable(), cannot change during migration. */
4155 if (ram_block_discard_is_required()) {
4156 error_setg(errp
, "RDMA: cannot disable RAM discard");
4160 rdma
= qemu_rdma_data_init(host_port
, errp
);
4165 ret
= qemu_rdma_dest_init(rdma
, errp
);
4170 trace_rdma_start_incoming_migration_after_dest_init();
4172 ret
= rdma_listen(rdma
->listen_id
, 5);
4175 error_setg(errp
, "RDMA ERROR: listening on socket!");
4179 trace_rdma_start_incoming_migration_after_rdma_listen();
4181 qemu_set_fd_handler(rdma
->channel
->fd
, rdma_accept_incoming_migration
,
4182 NULL
, (void *)(intptr_t)rdma
);
4186 qemu_rdma_cleanup(rdma
);
4190 g_free(rdma
->host_port
);
4195 void rdma_start_outgoing_migration(void *opaque
,
4196 const char *host_port
, Error
**errp
)
4198 MigrationState
*s
= opaque
;
4199 RDMAContext
*rdma_return_path
= NULL
;
4203 /* Avoid ram_block_discard_disable(), cannot change during migration. */
4204 if (ram_block_discard_is_required()) {
4205 error_setg(errp
, "RDMA: cannot disable RAM discard");
4209 rdma
= qemu_rdma_data_init(host_port
, errp
);
4214 ret
= qemu_rdma_source_init(rdma
, migrate_rdma_pin_all(), errp
);
4220 trace_rdma_start_outgoing_migration_after_rdma_source_init();
4221 ret
= qemu_rdma_connect(rdma
, false, errp
);
4227 /* RDMA postcopy need a separate queue pair for return path */
4228 if (migrate_postcopy() || migrate_return_path()) {
4229 rdma_return_path
= qemu_rdma_data_init(host_port
, errp
);
4231 if (rdma_return_path
== NULL
) {
4232 goto return_path_err
;
4235 ret
= qemu_rdma_source_init(rdma_return_path
,
4236 migrate_rdma_pin_all(), errp
);
4239 goto return_path_err
;
4242 ret
= qemu_rdma_connect(rdma_return_path
, true, errp
);
4245 goto return_path_err
;
4248 rdma
->return_path
= rdma_return_path
;
4249 rdma_return_path
->return_path
= rdma
;
4250 rdma_return_path
->is_return_path
= true;
4253 trace_rdma_start_outgoing_migration_after_rdma_connect();
4255 s
->to_dst_file
= rdma_new_output(rdma
);
4256 migrate_fd_connect(s
, NULL
);
4259 qemu_rdma_cleanup(rdma
);
4262 g_free(rdma_return_path
);