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
,
525 static inline uint64_t ram_chunk_index(const uint8_t *start
,
528 return ((uintptr_t) host
- (uintptr_t) start
) >> RDMA_REG_CHUNK_SHIFT
;
531 static inline uint8_t *ram_chunk_start(const RDMALocalBlock
*rdma_ram_block
,
534 return (uint8_t *)(uintptr_t)(rdma_ram_block
->local_host_addr
+
535 (i
<< RDMA_REG_CHUNK_SHIFT
));
538 static inline uint8_t *ram_chunk_end(const RDMALocalBlock
*rdma_ram_block
,
541 uint8_t *result
= ram_chunk_start(rdma_ram_block
, i
) +
542 (1UL << RDMA_REG_CHUNK_SHIFT
);
544 if (result
> (rdma_ram_block
->local_host_addr
+ rdma_ram_block
->length
)) {
545 result
= rdma_ram_block
->local_host_addr
+ rdma_ram_block
->length
;
551 static void rdma_add_block(RDMAContext
*rdma
, const char *block_name
,
553 ram_addr_t block_offset
, uint64_t length
)
555 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
556 RDMALocalBlock
*block
;
557 RDMALocalBlock
*old
= local
->block
;
559 local
->block
= g_new0(RDMALocalBlock
, local
->nb_blocks
+ 1);
561 if (local
->nb_blocks
) {
564 if (rdma
->blockmap
) {
565 for (x
= 0; x
< local
->nb_blocks
; x
++) {
566 g_hash_table_remove(rdma
->blockmap
,
567 (void *)(uintptr_t)old
[x
].offset
);
568 g_hash_table_insert(rdma
->blockmap
,
569 (void *)(uintptr_t)old
[x
].offset
,
573 memcpy(local
->block
, old
, sizeof(RDMALocalBlock
) * local
->nb_blocks
);
577 block
= &local
->block
[local
->nb_blocks
];
579 block
->block_name
= g_strdup(block_name
);
580 block
->local_host_addr
= host_addr
;
581 block
->offset
= block_offset
;
582 block
->length
= length
;
583 block
->index
= local
->nb_blocks
;
584 block
->src_index
= ~0U; /* Filled in by the receipt of the block list */
585 block
->nb_chunks
= ram_chunk_index(host_addr
, host_addr
+ length
) + 1UL;
586 block
->transit_bitmap
= bitmap_new(block
->nb_chunks
);
587 bitmap_clear(block
->transit_bitmap
, 0, block
->nb_chunks
);
588 block
->unregister_bitmap
= bitmap_new(block
->nb_chunks
);
589 bitmap_clear(block
->unregister_bitmap
, 0, block
->nb_chunks
);
590 block
->remote_keys
= g_new0(uint32_t, block
->nb_chunks
);
592 block
->is_ram_block
= local
->init
? false : true;
594 if (rdma
->blockmap
) {
595 g_hash_table_insert(rdma
->blockmap
, (void *)(uintptr_t)block_offset
, block
);
598 trace_rdma_add_block(block_name
, local
->nb_blocks
,
599 (uintptr_t) block
->local_host_addr
,
600 block
->offset
, block
->length
,
601 (uintptr_t) (block
->local_host_addr
+ block
->length
),
602 BITS_TO_LONGS(block
->nb_chunks
) *
603 sizeof(unsigned long) * 8,
610 * Memory regions need to be registered with the device and queue pairs setup
611 * in advanced before the migration starts. This tells us where the RAM blocks
612 * are so that we can register them individually.
614 static int qemu_rdma_init_one_block(RAMBlock
*rb
, void *opaque
)
616 const char *block_name
= qemu_ram_get_idstr(rb
);
617 void *host_addr
= qemu_ram_get_host_addr(rb
);
618 ram_addr_t block_offset
= qemu_ram_get_offset(rb
);
619 ram_addr_t length
= qemu_ram_get_used_length(rb
);
620 rdma_add_block(opaque
, block_name
, host_addr
, block_offset
, length
);
625 * Identify the RAMBlocks and their quantity. They will be references to
626 * identify chunk boundaries inside each RAMBlock and also be referenced
627 * during dynamic page registration.
629 static void qemu_rdma_init_ram_blocks(RDMAContext
*rdma
)
631 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
634 assert(rdma
->blockmap
== NULL
);
635 memset(local
, 0, sizeof *local
);
636 ret
= foreach_not_ignored_block(qemu_rdma_init_one_block
, rdma
);
638 trace_qemu_rdma_init_ram_blocks(local
->nb_blocks
);
639 rdma
->dest_blocks
= g_new0(RDMADestBlock
,
640 rdma
->local_ram_blocks
.nb_blocks
);
645 * Note: If used outside of cleanup, the caller must ensure that the destination
646 * block structures are also updated
648 static void rdma_delete_block(RDMAContext
*rdma
, RDMALocalBlock
*block
)
650 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
651 RDMALocalBlock
*old
= local
->block
;
654 if (rdma
->blockmap
) {
655 g_hash_table_remove(rdma
->blockmap
, (void *)(uintptr_t)block
->offset
);
660 for (j
= 0; j
< block
->nb_chunks
; j
++) {
661 if (!block
->pmr
[j
]) {
664 ibv_dereg_mr(block
->pmr
[j
]);
665 rdma
->total_registrations
--;
672 ibv_dereg_mr(block
->mr
);
673 rdma
->total_registrations
--;
677 g_free(block
->transit_bitmap
);
678 block
->transit_bitmap
= NULL
;
680 g_free(block
->unregister_bitmap
);
681 block
->unregister_bitmap
= NULL
;
683 g_free(block
->remote_keys
);
684 block
->remote_keys
= NULL
;
686 g_free(block
->block_name
);
687 block
->block_name
= NULL
;
689 if (rdma
->blockmap
) {
690 for (x
= 0; x
< local
->nb_blocks
; x
++) {
691 g_hash_table_remove(rdma
->blockmap
,
692 (void *)(uintptr_t)old
[x
].offset
);
696 if (local
->nb_blocks
> 1) {
698 local
->block
= g_new0(RDMALocalBlock
, local
->nb_blocks
- 1);
701 memcpy(local
->block
, old
, sizeof(RDMALocalBlock
) * block
->index
);
704 if (block
->index
< (local
->nb_blocks
- 1)) {
705 memcpy(local
->block
+ block
->index
, old
+ (block
->index
+ 1),
706 sizeof(RDMALocalBlock
) *
707 (local
->nb_blocks
- (block
->index
+ 1)));
708 for (x
= block
->index
; x
< local
->nb_blocks
- 1; x
++) {
709 local
->block
[x
].index
--;
713 assert(block
== local
->block
);
717 trace_rdma_delete_block(block
, (uintptr_t)block
->local_host_addr
,
718 block
->offset
, block
->length
,
719 (uintptr_t)(block
->local_host_addr
+ block
->length
),
720 BITS_TO_LONGS(block
->nb_chunks
) *
721 sizeof(unsigned long) * 8, block
->nb_chunks
);
727 if (local
->nb_blocks
&& rdma
->blockmap
) {
728 for (x
= 0; x
< local
->nb_blocks
; x
++) {
729 g_hash_table_insert(rdma
->blockmap
,
730 (void *)(uintptr_t)local
->block
[x
].offset
,
737 * Put in the log file which RDMA device was opened and the details
738 * associated with that device.
740 static void qemu_rdma_dump_id(const char *who
, struct ibv_context
*verbs
)
742 struct ibv_port_attr port
;
744 if (ibv_query_port(verbs
, 1, &port
)) {
745 error_report("Failed to query port information");
749 printf("%s RDMA Device opened: kernel name %s "
750 "uverbs device name %s, "
751 "infiniband_verbs class device path %s, "
752 "infiniband class device path %s, "
753 "transport: (%d) %s\n",
756 verbs
->device
->dev_name
,
757 verbs
->device
->dev_path
,
758 verbs
->device
->ibdev_path
,
760 (port
.link_layer
== IBV_LINK_LAYER_INFINIBAND
) ? "Infiniband" :
761 ((port
.link_layer
== IBV_LINK_LAYER_ETHERNET
)
762 ? "Ethernet" : "Unknown"));
766 * Put in the log file the RDMA gid addressing information,
767 * useful for folks who have trouble understanding the
768 * RDMA device hierarchy in the kernel.
770 static void qemu_rdma_dump_gid(const char *who
, struct rdma_cm_id
*id
)
774 inet_ntop(AF_INET6
, &id
->route
.addr
.addr
.ibaddr
.sgid
, sgid
, sizeof sgid
);
775 inet_ntop(AF_INET6
, &id
->route
.addr
.addr
.ibaddr
.dgid
, dgid
, sizeof dgid
);
776 trace_qemu_rdma_dump_gid(who
, sgid
, dgid
);
780 * As of now, IPv6 over RoCE / iWARP is not supported by linux.
781 * We will try the next addrinfo struct, and fail if there are
782 * no other valid addresses to bind against.
784 * If user is listening on '[::]', then we will not have a opened a device
785 * yet and have no way of verifying if the device is RoCE or not.
787 * In this case, the source VM will throw an error for ALL types of
788 * connections (both IPv4 and IPv6) if the destination machine does not have
789 * a regular infiniband network available for use.
791 * The only way to guarantee that an error is thrown for broken kernels is
792 * for the management software to choose a *specific* interface at bind time
793 * and validate what time of hardware it is.
795 * Unfortunately, this puts the user in a fix:
797 * If the source VM connects with an IPv4 address without knowing that the
798 * destination has bound to '[::]' the migration will unconditionally fail
799 * unless the management software is explicitly listening on the IPv4
800 * address while using a RoCE-based device.
802 * If the source VM connects with an IPv6 address, then we're OK because we can
803 * throw an error on the source (and similarly on the destination).
805 * But in mixed environments, this will be broken for a while until it is fixed
808 * We do provide a *tiny* bit of help in this function: We can list all of the
809 * devices in the system and check to see if all the devices are RoCE or
812 * If we detect that we have a *pure* RoCE environment, then we can safely
813 * thrown an error even if the management software has specified '[::]' as the
816 * However, if there is are multiple hetergeneous devices, then we cannot make
817 * this assumption and the user just has to be sure they know what they are
820 * Patches are being reviewed on linux-rdma.
822 static int qemu_rdma_broken_ipv6_kernel(struct ibv_context
*verbs
, Error
**errp
)
824 /* This bug only exists in linux, to our knowledge. */
826 struct ibv_port_attr port_attr
;
829 * Verbs are only NULL if management has bound to '[::]'.
831 * Let's iterate through all the devices and see if there any pure IB
832 * devices (non-ethernet).
834 * If not, then we can safely proceed with the migration.
835 * Otherwise, there are no guarantees until the bug is fixed in linux.
839 struct ibv_device
**dev_list
= ibv_get_device_list(&num_devices
);
840 bool roce_found
= false;
841 bool ib_found
= false;
843 for (x
= 0; x
< num_devices
; x
++) {
844 verbs
= ibv_open_device(dev_list
[x
]);
846 * ibv_open_device() is not documented to set errno. If
847 * it does, it's somebody else's doc bug. If it doesn't,
848 * the use of errno below is wrong.
849 * TODO Find out whether ibv_open_device() sets errno.
852 if (errno
== EPERM
) {
855 error_setg_errno(errp
, errno
,
856 "could not open RDMA device context");
861 if (ibv_query_port(verbs
, 1, &port_attr
)) {
862 ibv_close_device(verbs
);
864 "RDMA ERROR: Could not query initial IB port");
868 if (port_attr
.link_layer
== IBV_LINK_LAYER_INFINIBAND
) {
870 } else if (port_attr
.link_layer
== IBV_LINK_LAYER_ETHERNET
) {
874 ibv_close_device(verbs
);
880 fprintf(stderr
, "WARN: migrations may fail:"
881 " IPv6 over RoCE / iWARP in linux"
882 " is broken. But since you appear to have a"
883 " mixed RoCE / IB environment, be sure to only"
884 " migrate over the IB fabric until the kernel "
885 " fixes the bug.\n");
887 error_setg(errp
, "RDMA ERROR: "
888 "You only have RoCE / iWARP devices in your systems"
889 " and your management software has specified '[::]'"
890 ", but IPv6 over RoCE / iWARP is not supported in Linux.");
899 * If we have a verbs context, that means that some other than '[::]' was
900 * used by the management software for binding. In which case we can
901 * actually warn the user about a potentially broken kernel.
904 /* IB ports start with 1, not 0 */
905 if (ibv_query_port(verbs
, 1, &port_attr
)) {
906 error_setg(errp
, "RDMA ERROR: Could not query initial IB port");
910 if (port_attr
.link_layer
== IBV_LINK_LAYER_ETHERNET
) {
911 error_setg(errp
, "RDMA ERROR: "
912 "Linux kernel's RoCE / iWARP does not support IPv6 "
913 "(but patches on linux-rdma in progress)");
923 * Figure out which RDMA device corresponds to the requested IP hostname
924 * Also create the initial connection manager identifiers for opening
927 static int qemu_rdma_resolve_host(RDMAContext
*rdma
, Error
**errp
)
931 struct rdma_addrinfo
*res
;
933 struct rdma_cm_event
*cm_event
;
934 char ip
[40] = "unknown";
935 struct rdma_addrinfo
*e
;
937 if (rdma
->host
== NULL
|| !strcmp(rdma
->host
, "")) {
938 error_setg(errp
, "RDMA ERROR: RDMA hostname has not been set");
942 /* create CM channel */
943 rdma
->channel
= rdma_create_event_channel();
944 if (!rdma
->channel
) {
945 error_setg(errp
, "RDMA ERROR: could not create CM channel");
950 ret
= rdma_create_id(rdma
->channel
, &rdma
->cm_id
, NULL
, RDMA_PS_TCP
);
952 error_setg(errp
, "RDMA ERROR: could not create channel id");
953 goto err_resolve_create_id
;
956 snprintf(port_str
, 16, "%d", rdma
->port
);
959 ret
= rdma_getaddrinfo(rdma
->host
, port_str
, NULL
, &res
);
961 error_setg(errp
, "RDMA ERROR: could not rdma_getaddrinfo address %s",
963 goto err_resolve_get_addr
;
966 /* Try all addresses, saving the first error in @err */
967 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
968 Error
**local_errp
= err
? NULL
: &err
;
970 inet_ntop(e
->ai_family
,
971 &((struct sockaddr_in
*) e
->ai_dst_addr
)->sin_addr
, ip
, sizeof ip
);
972 trace_qemu_rdma_resolve_host_trying(rdma
->host
, ip
);
974 ret
= rdma_resolve_addr(rdma
->cm_id
, NULL
, e
->ai_dst_addr
,
975 RDMA_RESOLVE_TIMEOUT_MS
);
977 if (e
->ai_family
== AF_INET6
) {
978 ret
= qemu_rdma_broken_ipv6_kernel(rdma
->cm_id
->verbs
,
989 rdma_freeaddrinfo(res
);
991 error_propagate(errp
, err
);
993 error_setg(errp
, "RDMA ERROR: could not resolve address %s",
996 goto err_resolve_get_addr
;
999 rdma_freeaddrinfo(res
);
1000 qemu_rdma_dump_gid("source_resolve_addr", rdma
->cm_id
);
1002 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
1004 error_setg(errp
, "RDMA ERROR: could not perform event_addr_resolved");
1005 goto err_resolve_get_addr
;
1008 if (cm_event
->event
!= RDMA_CM_EVENT_ADDR_RESOLVED
) {
1010 "RDMA ERROR: result not equal to event_addr_resolved %s",
1011 rdma_event_str(cm_event
->event
));
1012 error_report("rdma_resolve_addr");
1013 rdma_ack_cm_event(cm_event
);
1014 goto err_resolve_get_addr
;
1016 rdma_ack_cm_event(cm_event
);
1019 ret
= rdma_resolve_route(rdma
->cm_id
, RDMA_RESOLVE_TIMEOUT_MS
);
1021 error_setg(errp
, "RDMA ERROR: could not resolve rdma route");
1022 goto err_resolve_get_addr
;
1025 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
1027 error_setg(errp
, "RDMA ERROR: could not perform event_route_resolved");
1028 goto err_resolve_get_addr
;
1030 if (cm_event
->event
!= RDMA_CM_EVENT_ROUTE_RESOLVED
) {
1031 error_setg(errp
, "RDMA ERROR: "
1032 "result not equal to event_route_resolved: %s",
1033 rdma_event_str(cm_event
->event
));
1034 rdma_ack_cm_event(cm_event
);
1035 goto err_resolve_get_addr
;
1037 rdma_ack_cm_event(cm_event
);
1038 rdma
->verbs
= rdma
->cm_id
->verbs
;
1039 qemu_rdma_dump_id("source_resolve_host", rdma
->cm_id
->verbs
);
1040 qemu_rdma_dump_gid("source_resolve_host", rdma
->cm_id
);
1043 err_resolve_get_addr
:
1044 rdma_destroy_id(rdma
->cm_id
);
1046 err_resolve_create_id
:
1047 rdma_destroy_event_channel(rdma
->channel
);
1048 rdma
->channel
= NULL
;
1053 * Create protection domain and completion queues
1055 static int qemu_rdma_alloc_pd_cq(RDMAContext
*rdma
)
1058 rdma
->pd
= ibv_alloc_pd(rdma
->verbs
);
1060 error_report("failed to allocate protection domain");
1064 /* create receive completion channel */
1065 rdma
->recv_comp_channel
= ibv_create_comp_channel(rdma
->verbs
);
1066 if (!rdma
->recv_comp_channel
) {
1067 error_report("failed to allocate receive completion channel");
1068 goto err_alloc_pd_cq
;
1072 * Completion queue can be filled by read work requests.
1074 rdma
->recv_cq
= ibv_create_cq(rdma
->verbs
, (RDMA_SIGNALED_SEND_MAX
* 3),
1075 NULL
, rdma
->recv_comp_channel
, 0);
1076 if (!rdma
->recv_cq
) {
1077 error_report("failed to allocate receive completion queue");
1078 goto err_alloc_pd_cq
;
1081 /* create send completion channel */
1082 rdma
->send_comp_channel
= ibv_create_comp_channel(rdma
->verbs
);
1083 if (!rdma
->send_comp_channel
) {
1084 error_report("failed to allocate send completion channel");
1085 goto err_alloc_pd_cq
;
1088 rdma
->send_cq
= ibv_create_cq(rdma
->verbs
, (RDMA_SIGNALED_SEND_MAX
* 3),
1089 NULL
, rdma
->send_comp_channel
, 0);
1090 if (!rdma
->send_cq
) {
1091 error_report("failed to allocate send completion queue");
1092 goto err_alloc_pd_cq
;
1099 ibv_dealloc_pd(rdma
->pd
);
1101 if (rdma
->recv_comp_channel
) {
1102 ibv_destroy_comp_channel(rdma
->recv_comp_channel
);
1104 if (rdma
->send_comp_channel
) {
1105 ibv_destroy_comp_channel(rdma
->send_comp_channel
);
1107 if (rdma
->recv_cq
) {
1108 ibv_destroy_cq(rdma
->recv_cq
);
1109 rdma
->recv_cq
= NULL
;
1112 rdma
->recv_comp_channel
= NULL
;
1113 rdma
->send_comp_channel
= NULL
;
1119 * Create queue pairs.
1121 static int qemu_rdma_alloc_qp(RDMAContext
*rdma
)
1123 struct ibv_qp_init_attr attr
= { 0 };
1126 attr
.cap
.max_send_wr
= RDMA_SIGNALED_SEND_MAX
;
1127 attr
.cap
.max_recv_wr
= 3;
1128 attr
.cap
.max_send_sge
= 1;
1129 attr
.cap
.max_recv_sge
= 1;
1130 attr
.send_cq
= rdma
->send_cq
;
1131 attr
.recv_cq
= rdma
->recv_cq
;
1132 attr
.qp_type
= IBV_QPT_RC
;
1134 ret
= rdma_create_qp(rdma
->cm_id
, rdma
->pd
, &attr
);
1139 rdma
->qp
= rdma
->cm_id
->qp
;
1143 /* Check whether On-Demand Paging is supported by RDAM device */
1144 static bool rdma_support_odp(struct ibv_context
*dev
)
1146 struct ibv_device_attr_ex attr
= {0};
1147 int ret
= ibv_query_device_ex(dev
, NULL
, &attr
);
1152 if (attr
.odp_caps
.general_caps
& IBV_ODP_SUPPORT
) {
1160 * ibv_advise_mr to avoid RNR NAK error as far as possible.
1161 * The responder mr registering with ODP will sent RNR NAK back to
1162 * the requester in the face of the page fault.
1164 static void qemu_rdma_advise_prefetch_mr(struct ibv_pd
*pd
, uint64_t addr
,
1165 uint32_t len
, uint32_t lkey
,
1166 const char *name
, bool wr
)
1168 #ifdef HAVE_IBV_ADVISE_MR
1170 int advice
= wr
? IBV_ADVISE_MR_ADVICE_PREFETCH_WRITE
:
1171 IBV_ADVISE_MR_ADVICE_PREFETCH
;
1172 struct ibv_sge sg_list
= {.lkey
= lkey
, .addr
= addr
, .length
= len
};
1174 ret
= ibv_advise_mr(pd
, advice
,
1175 IBV_ADVISE_MR_FLAG_FLUSH
, &sg_list
, 1);
1176 /* ignore the error */
1177 trace_qemu_rdma_advise_mr(name
, len
, addr
, strerror(ret
));
1181 static int qemu_rdma_reg_whole_ram_blocks(RDMAContext
*rdma
, Error
**errp
)
1184 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
1186 for (i
= 0; i
< local
->nb_blocks
; i
++) {
1187 int access
= IBV_ACCESS_LOCAL_WRITE
| IBV_ACCESS_REMOTE_WRITE
;
1189 local
->block
[i
].mr
=
1190 ibv_reg_mr(rdma
->pd
,
1191 local
->block
[i
].local_host_addr
,
1192 local
->block
[i
].length
, access
1195 * ibv_reg_mr() is not documented to set errno. If it does,
1196 * it's somebody else's doc bug. If it doesn't, the use of
1197 * errno below is wrong.
1198 * TODO Find out whether ibv_reg_mr() sets errno.
1200 if (!local
->block
[i
].mr
&&
1201 errno
== ENOTSUP
&& rdma_support_odp(rdma
->verbs
)) {
1202 access
|= IBV_ACCESS_ON_DEMAND
;
1203 /* register ODP mr */
1204 local
->block
[i
].mr
=
1205 ibv_reg_mr(rdma
->pd
,
1206 local
->block
[i
].local_host_addr
,
1207 local
->block
[i
].length
, access
);
1208 trace_qemu_rdma_register_odp_mr(local
->block
[i
].block_name
);
1210 if (local
->block
[i
].mr
) {
1211 qemu_rdma_advise_prefetch_mr(rdma
->pd
,
1212 (uintptr_t)local
->block
[i
].local_host_addr
,
1213 local
->block
[i
].length
,
1214 local
->block
[i
].mr
->lkey
,
1215 local
->block
[i
].block_name
,
1220 if (!local
->block
[i
].mr
) {
1221 error_setg_errno(errp
, errno
,
1222 "Failed to register local dest ram block!");
1225 rdma
->total_registrations
++;
1231 for (i
--; i
>= 0; i
--) {
1232 ibv_dereg_mr(local
->block
[i
].mr
);
1233 local
->block
[i
].mr
= NULL
;
1234 rdma
->total_registrations
--;
1242 * Find the ram block that corresponds to the page requested to be
1243 * transmitted by QEMU.
1245 * Once the block is found, also identify which 'chunk' within that
1246 * block that the page belongs to.
1248 static void qemu_rdma_search_ram_block(RDMAContext
*rdma
,
1249 uintptr_t block_offset
,
1252 uint64_t *block_index
,
1253 uint64_t *chunk_index
)
1255 uint64_t current_addr
= block_offset
+ offset
;
1256 RDMALocalBlock
*block
= g_hash_table_lookup(rdma
->blockmap
,
1257 (void *) block_offset
);
1259 assert(current_addr
>= block
->offset
);
1260 assert((current_addr
+ length
) <= (block
->offset
+ block
->length
));
1262 *block_index
= block
->index
;
1263 *chunk_index
= ram_chunk_index(block
->local_host_addr
,
1264 block
->local_host_addr
+ (current_addr
- block
->offset
));
1268 * Register a chunk with IB. If the chunk was already registered
1269 * previously, then skip.
1271 * Also return the keys associated with the registration needed
1272 * to perform the actual RDMA operation.
1274 static int qemu_rdma_register_and_get_keys(RDMAContext
*rdma
,
1275 RDMALocalBlock
*block
, uintptr_t host_addr
,
1276 uint32_t *lkey
, uint32_t *rkey
, int chunk
,
1277 uint8_t *chunk_start
, uint8_t *chunk_end
)
1281 *lkey
= block
->mr
->lkey
;
1284 *rkey
= block
->mr
->rkey
;
1289 /* allocate memory to store chunk MRs */
1291 block
->pmr
= g_new0(struct ibv_mr
*, block
->nb_chunks
);
1295 * If 'rkey', then we're the destination, so grant access to the source.
1297 * If 'lkey', then we're the source VM, so grant access only to ourselves.
1299 if (!block
->pmr
[chunk
]) {
1300 uint64_t len
= chunk_end
- chunk_start
;
1301 int access
= rkey
? IBV_ACCESS_LOCAL_WRITE
| IBV_ACCESS_REMOTE_WRITE
:
1304 trace_qemu_rdma_register_and_get_keys(len
, chunk_start
);
1306 block
->pmr
[chunk
] = ibv_reg_mr(rdma
->pd
, chunk_start
, len
, access
);
1308 * ibv_reg_mr() is not documented to set errno. If it does,
1309 * it's somebody else's doc bug. If it doesn't, the use of
1310 * errno below is wrong.
1311 * TODO Find out whether ibv_reg_mr() sets errno.
1313 if (!block
->pmr
[chunk
] &&
1314 errno
== ENOTSUP
&& rdma_support_odp(rdma
->verbs
)) {
1315 access
|= IBV_ACCESS_ON_DEMAND
;
1316 /* register ODP mr */
1317 block
->pmr
[chunk
] = ibv_reg_mr(rdma
->pd
, chunk_start
, len
, access
);
1318 trace_qemu_rdma_register_odp_mr(block
->block_name
);
1320 if (block
->pmr
[chunk
]) {
1321 qemu_rdma_advise_prefetch_mr(rdma
->pd
, (uintptr_t)chunk_start
,
1322 len
, block
->pmr
[chunk
]->lkey
,
1323 block
->block_name
, rkey
);
1328 if (!block
->pmr
[chunk
]) {
1329 perror("Failed to register chunk!");
1330 fprintf(stderr
, "Chunk details: block: %d chunk index %d"
1331 " start %" PRIuPTR
" end %" PRIuPTR
1333 " local %" PRIuPTR
" registrations: %d\n",
1334 block
->index
, chunk
, (uintptr_t)chunk_start
,
1335 (uintptr_t)chunk_end
, host_addr
,
1336 (uintptr_t)block
->local_host_addr
,
1337 rdma
->total_registrations
);
1340 rdma
->total_registrations
++;
1343 *lkey
= block
->pmr
[chunk
]->lkey
;
1346 *rkey
= block
->pmr
[chunk
]->rkey
;
1352 * Register (at connection time) the memory used for control
1355 static int qemu_rdma_reg_control(RDMAContext
*rdma
, int idx
)
1357 rdma
->wr_data
[idx
].control_mr
= ibv_reg_mr(rdma
->pd
,
1358 rdma
->wr_data
[idx
].control
, RDMA_CONTROL_MAX_BUFFER
,
1359 IBV_ACCESS_LOCAL_WRITE
| IBV_ACCESS_REMOTE_WRITE
);
1360 if (rdma
->wr_data
[idx
].control_mr
) {
1361 rdma
->total_registrations
++;
1364 error_report("qemu_rdma_reg_control failed");
1369 * Perform a non-optimized memory unregistration after every transfer
1370 * for demonstration purposes, only if pin-all is not requested.
1372 * Potential optimizations:
1373 * 1. Start a new thread to run this function continuously
1375 - and for receipt of unregister messages
1377 * 3. Use workload hints.
1379 static int qemu_rdma_unregister_waiting(RDMAContext
*rdma
)
1383 while (rdma
->unregistrations
[rdma
->unregister_current
]) {
1385 uint64_t wr_id
= rdma
->unregistrations
[rdma
->unregister_current
];
1387 (wr_id
& RDMA_WRID_CHUNK_MASK
) >> RDMA_WRID_CHUNK_SHIFT
;
1389 (wr_id
& RDMA_WRID_BLOCK_MASK
) >> RDMA_WRID_BLOCK_SHIFT
;
1390 RDMALocalBlock
*block
=
1391 &(rdma
->local_ram_blocks
.block
[index
]);
1392 RDMARegister reg
= { .current_index
= index
};
1393 RDMAControlHeader resp
= { .type
= RDMA_CONTROL_UNREGISTER_FINISHED
,
1395 RDMAControlHeader head
= { .len
= sizeof(RDMARegister
),
1396 .type
= RDMA_CONTROL_UNREGISTER_REQUEST
,
1400 trace_qemu_rdma_unregister_waiting_proc(chunk
,
1401 rdma
->unregister_current
);
1403 rdma
->unregistrations
[rdma
->unregister_current
] = 0;
1404 rdma
->unregister_current
++;
1406 if (rdma
->unregister_current
== RDMA_SIGNALED_SEND_MAX
) {
1407 rdma
->unregister_current
= 0;
1412 * Unregistration is speculative (because migration is single-threaded
1413 * and we cannot break the protocol's inifinband message ordering).
1414 * Thus, if the memory is currently being used for transmission,
1415 * then abort the attempt to unregister and try again
1416 * later the next time a completion is received for this memory.
1418 clear_bit(chunk
, block
->unregister_bitmap
);
1420 if (test_bit(chunk
, block
->transit_bitmap
)) {
1421 trace_qemu_rdma_unregister_waiting_inflight(chunk
);
1425 trace_qemu_rdma_unregister_waiting_send(chunk
);
1427 ret
= ibv_dereg_mr(block
->pmr
[chunk
]);
1428 block
->pmr
[chunk
] = NULL
;
1429 block
->remote_keys
[chunk
] = 0;
1433 * FIXME perror() is problematic, bcause ibv_dereg_mr() is
1434 * not documented to set errno. Will go away later in
1437 perror("unregistration chunk failed");
1440 rdma
->total_registrations
--;
1442 reg
.key
.chunk
= chunk
;
1443 register_to_network(rdma
, ®
);
1444 ret
= qemu_rdma_exchange_send(rdma
, &head
, (uint8_t *) ®
,
1445 &resp
, NULL
, NULL
, &err
);
1447 error_report_err(err
);
1451 trace_qemu_rdma_unregister_waiting_complete(chunk
);
1457 static uint64_t qemu_rdma_make_wrid(uint64_t wr_id
, uint64_t index
,
1460 uint64_t result
= wr_id
& RDMA_WRID_TYPE_MASK
;
1462 result
|= (index
<< RDMA_WRID_BLOCK_SHIFT
);
1463 result
|= (chunk
<< RDMA_WRID_CHUNK_SHIFT
);
1469 * Consult the connection manager to see a work request
1470 * (of any kind) has completed.
1471 * Return the work request ID that completed.
1473 static int qemu_rdma_poll(RDMAContext
*rdma
, struct ibv_cq
*cq
,
1474 uint64_t *wr_id_out
, uint32_t *byte_len
)
1480 ret
= ibv_poll_cq(cq
, 1, &wc
);
1483 *wr_id_out
= RDMA_WRID_NONE
;
1488 error_report("ibv_poll_cq failed");
1492 wr_id
= wc
.wr_id
& RDMA_WRID_TYPE_MASK
;
1494 if (wc
.status
!= IBV_WC_SUCCESS
) {
1495 fprintf(stderr
, "ibv_poll_cq wc.status=%d %s!\n",
1496 wc
.status
, ibv_wc_status_str(wc
.status
));
1497 fprintf(stderr
, "ibv_poll_cq wrid=%" PRIu64
"!\n", wr_id
);
1502 if (rdma
->control_ready_expected
&&
1503 (wr_id
>= RDMA_WRID_RECV_CONTROL
)) {
1504 trace_qemu_rdma_poll_recv(wr_id
- RDMA_WRID_RECV_CONTROL
, wr_id
,
1506 rdma
->control_ready_expected
= 0;
1509 if (wr_id
== RDMA_WRID_RDMA_WRITE
) {
1511 (wc
.wr_id
& RDMA_WRID_CHUNK_MASK
) >> RDMA_WRID_CHUNK_SHIFT
;
1513 (wc
.wr_id
& RDMA_WRID_BLOCK_MASK
) >> RDMA_WRID_BLOCK_SHIFT
;
1514 RDMALocalBlock
*block
= &(rdma
->local_ram_blocks
.block
[index
]);
1516 trace_qemu_rdma_poll_write(wr_id
, rdma
->nb_sent
,
1517 index
, chunk
, block
->local_host_addr
,
1518 (void *)(uintptr_t)block
->remote_host_addr
);
1520 clear_bit(chunk
, block
->transit_bitmap
);
1522 if (rdma
->nb_sent
> 0) {
1526 trace_qemu_rdma_poll_other(wr_id
, rdma
->nb_sent
);
1529 *wr_id_out
= wc
.wr_id
;
1531 *byte_len
= wc
.byte_len
;
1537 /* Wait for activity on the completion channel.
1538 * Returns 0 on success, none-0 on error.
1540 static int qemu_rdma_wait_comp_channel(RDMAContext
*rdma
,
1541 struct ibv_comp_channel
*comp_channel
)
1543 struct rdma_cm_event
*cm_event
;
1547 * Coroutine doesn't start until migration_fd_process_incoming()
1548 * so don't yield unless we know we're running inside of a coroutine.
1550 if (rdma
->migration_started_on_destination
&&
1551 migration_incoming_get_current()->state
== MIGRATION_STATUS_ACTIVE
) {
1552 yield_until_fd_readable(comp_channel
->fd
);
1554 /* This is the source side, we're in a separate thread
1555 * or destination prior to migration_fd_process_incoming()
1556 * after postcopy, the destination also in a separate thread.
1557 * we can't yield; so we have to poll the fd.
1558 * But we need to be able to handle 'cancel' or an error
1559 * without hanging forever.
1561 while (!rdma
->errored
&& !rdma
->received_error
) {
1563 pfds
[0].fd
= comp_channel
->fd
;
1564 pfds
[0].events
= G_IO_IN
| G_IO_HUP
| G_IO_ERR
;
1565 pfds
[0].revents
= 0;
1567 pfds
[1].fd
= rdma
->channel
->fd
;
1568 pfds
[1].events
= G_IO_IN
| G_IO_HUP
| G_IO_ERR
;
1569 pfds
[1].revents
= 0;
1571 /* 0.1s timeout, should be fine for a 'cancel' */
1572 switch (qemu_poll_ns(pfds
, 2, 100 * 1000 * 1000)) {
1574 case 1: /* fd active */
1575 if (pfds
[0].revents
) {
1579 if (pfds
[1].revents
) {
1580 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
1582 error_report("failed to get cm event while wait "
1583 "completion channel");
1587 error_report("receive cm event while wait comp channel,"
1588 "cm event is %d", cm_event
->event
);
1589 if (cm_event
->event
== RDMA_CM_EVENT_DISCONNECTED
||
1590 cm_event
->event
== RDMA_CM_EVENT_DEVICE_REMOVAL
) {
1591 rdma_ack_cm_event(cm_event
);
1594 rdma_ack_cm_event(cm_event
);
1598 case 0: /* Timeout, go around again */
1601 default: /* Error of some type -
1602 * I don't trust errno from qemu_poll_ns
1604 error_report("%s: poll failed", __func__
);
1608 if (migrate_get_current()->state
== MIGRATION_STATUS_CANCELLING
) {
1609 /* Bail out and let the cancellation happen */
1615 if (rdma
->received_error
) {
1618 return -rdma
->errored
;
1621 static struct ibv_comp_channel
*to_channel(RDMAContext
*rdma
, uint64_t wrid
)
1623 return wrid
< RDMA_WRID_RECV_CONTROL
? rdma
->send_comp_channel
:
1624 rdma
->recv_comp_channel
;
1627 static struct ibv_cq
*to_cq(RDMAContext
*rdma
, uint64_t wrid
)
1629 return wrid
< RDMA_WRID_RECV_CONTROL
? rdma
->send_cq
: rdma
->recv_cq
;
1633 * Block until the next work request has completed.
1635 * First poll to see if a work request has already completed,
1638 * If we encounter completed work requests for IDs other than
1639 * the one we're interested in, then that's generally an error.
1641 * The only exception is actual RDMA Write completions. These
1642 * completions only need to be recorded, but do not actually
1643 * need further processing.
1645 static int qemu_rdma_block_for_wrid(RDMAContext
*rdma
,
1646 uint64_t wrid_requested
,
1649 int num_cq_events
= 0, ret
;
1652 uint64_t wr_id
= RDMA_WRID_NONE
, wr_id_in
;
1653 struct ibv_comp_channel
*ch
= to_channel(rdma
, wrid_requested
);
1654 struct ibv_cq
*poll_cq
= to_cq(rdma
, wrid_requested
);
1656 if (ibv_req_notify_cq(poll_cq
, 0)) {
1660 while (wr_id
!= wrid_requested
) {
1661 ret
= qemu_rdma_poll(rdma
, poll_cq
, &wr_id_in
, byte_len
);
1666 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
1668 if (wr_id
== RDMA_WRID_NONE
) {
1671 if (wr_id
!= wrid_requested
) {
1672 trace_qemu_rdma_block_for_wrid_miss(wrid_requested
, wr_id
);
1676 if (wr_id
== wrid_requested
) {
1681 ret
= qemu_rdma_wait_comp_channel(rdma
, ch
);
1683 goto err_block_for_wrid
;
1686 ret
= ibv_get_cq_event(ch
, &cq
, &cq_ctx
);
1689 * FIXME perror() is problematic, because ibv_reg_mr() is
1690 * not documented to set errno. Will go away later in
1693 perror("ibv_get_cq_event");
1694 goto err_block_for_wrid
;
1699 if (ibv_req_notify_cq(cq
, 0)) {
1700 goto err_block_for_wrid
;
1703 while (wr_id
!= wrid_requested
) {
1704 ret
= qemu_rdma_poll(rdma
, poll_cq
, &wr_id_in
, byte_len
);
1706 goto err_block_for_wrid
;
1709 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
1711 if (wr_id
== RDMA_WRID_NONE
) {
1714 if (wr_id
!= wrid_requested
) {
1715 trace_qemu_rdma_block_for_wrid_miss(wrid_requested
, wr_id
);
1719 if (wr_id
== wrid_requested
) {
1720 goto success_block_for_wrid
;
1724 success_block_for_wrid
:
1725 if (num_cq_events
) {
1726 ibv_ack_cq_events(cq
, num_cq_events
);
1731 if (num_cq_events
) {
1732 ibv_ack_cq_events(cq
, num_cq_events
);
1735 rdma
->errored
= true;
1740 * Post a SEND message work request for the control channel
1741 * containing some data and block until the post completes.
1743 static int qemu_rdma_post_send_control(RDMAContext
*rdma
, uint8_t *buf
,
1744 RDMAControlHeader
*head
)
1747 RDMAWorkRequestData
*wr
= &rdma
->wr_data
[RDMA_WRID_CONTROL
];
1748 struct ibv_send_wr
*bad_wr
;
1749 struct ibv_sge sge
= {
1750 .addr
= (uintptr_t)(wr
->control
),
1751 .length
= head
->len
+ sizeof(RDMAControlHeader
),
1752 .lkey
= wr
->control_mr
->lkey
,
1754 struct ibv_send_wr send_wr
= {
1755 .wr_id
= RDMA_WRID_SEND_CONTROL
,
1756 .opcode
= IBV_WR_SEND
,
1757 .send_flags
= IBV_SEND_SIGNALED
,
1762 trace_qemu_rdma_post_send_control(control_desc(head
->type
));
1765 * We don't actually need to do a memcpy() in here if we used
1766 * the "sge" properly, but since we're only sending control messages
1767 * (not RAM in a performance-critical path), then its OK for now.
1769 * The copy makes the RDMAControlHeader simpler to manipulate
1770 * for the time being.
1772 assert(head
->len
<= RDMA_CONTROL_MAX_BUFFER
- sizeof(*head
));
1773 memcpy(wr
->control
, head
, sizeof(RDMAControlHeader
));
1774 control_to_network((void *) wr
->control
);
1777 memcpy(wr
->control
+ sizeof(RDMAControlHeader
), buf
, head
->len
);
1781 ret
= ibv_post_send(rdma
->qp
, &send_wr
, &bad_wr
);
1784 error_report("Failed to use post IB SEND for control");
1788 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_SEND_CONTROL
, NULL
);
1790 error_report("rdma migration: send polling control error");
1798 * Post a RECV work request in anticipation of some future receipt
1799 * of data on the control channel.
1801 static int qemu_rdma_post_recv_control(RDMAContext
*rdma
, int idx
)
1803 struct ibv_recv_wr
*bad_wr
;
1804 struct ibv_sge sge
= {
1805 .addr
= (uintptr_t)(rdma
->wr_data
[idx
].control
),
1806 .length
= RDMA_CONTROL_MAX_BUFFER
,
1807 .lkey
= rdma
->wr_data
[idx
].control_mr
->lkey
,
1810 struct ibv_recv_wr recv_wr
= {
1811 .wr_id
= RDMA_WRID_RECV_CONTROL
+ idx
,
1817 if (ibv_post_recv(rdma
->qp
, &recv_wr
, &bad_wr
)) {
1825 * Block and wait for a RECV control channel message to arrive.
1827 static int qemu_rdma_exchange_get_response(RDMAContext
*rdma
,
1828 RDMAControlHeader
*head
, uint32_t expecting
, int idx
,
1832 int ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RECV_CONTROL
+ idx
,
1836 error_setg(errp
, "rdma migration: recv polling control error!");
1840 network_to_control((void *) rdma
->wr_data
[idx
].control
);
1841 memcpy(head
, rdma
->wr_data
[idx
].control
, sizeof(RDMAControlHeader
));
1843 trace_qemu_rdma_exchange_get_response_start(control_desc(expecting
));
1845 if (expecting
== RDMA_CONTROL_NONE
) {
1846 trace_qemu_rdma_exchange_get_response_none(control_desc(head
->type
),
1848 } else if (head
->type
!= expecting
|| head
->type
== RDMA_CONTROL_ERROR
) {
1849 error_setg(errp
, "Was expecting a %s (%d) control message"
1850 ", but got: %s (%d), length: %d",
1851 control_desc(expecting
), expecting
,
1852 control_desc(head
->type
), head
->type
, head
->len
);
1853 if (head
->type
== RDMA_CONTROL_ERROR
) {
1854 rdma
->received_error
= true;
1858 if (head
->len
> RDMA_CONTROL_MAX_BUFFER
- sizeof(*head
)) {
1859 error_setg(errp
, "too long length: %d", head
->len
);
1862 if (sizeof(*head
) + head
->len
!= byte_len
) {
1863 error_setg(errp
, "Malformed length: %d byte_len %d",
1864 head
->len
, byte_len
);
1872 * When a RECV work request has completed, the work request's
1873 * buffer is pointed at the header.
1875 * This will advance the pointer to the data portion
1876 * of the control message of the work request's buffer that
1877 * was populated after the work request finished.
1879 static void qemu_rdma_move_header(RDMAContext
*rdma
, int idx
,
1880 RDMAControlHeader
*head
)
1882 rdma
->wr_data
[idx
].control_len
= head
->len
;
1883 rdma
->wr_data
[idx
].control_curr
=
1884 rdma
->wr_data
[idx
].control
+ sizeof(RDMAControlHeader
);
1888 * This is an 'atomic' high-level operation to deliver a single, unified
1889 * control-channel message.
1891 * Additionally, if the user is expecting some kind of reply to this message,
1892 * they can request a 'resp' response message be filled in by posting an
1893 * additional work request on behalf of the user and waiting for an additional
1896 * The extra (optional) response is used during registration to us from having
1897 * to perform an *additional* exchange of message just to provide a response by
1898 * instead piggy-backing on the acknowledgement.
1900 static int qemu_rdma_exchange_send(RDMAContext
*rdma
, RDMAControlHeader
*head
,
1901 uint8_t *data
, RDMAControlHeader
*resp
,
1903 int (*callback
)(RDMAContext
*rdma
,
1910 * Wait until the dest is ready before attempting to deliver the message
1911 * by waiting for a READY message.
1913 if (rdma
->control_ready_expected
) {
1914 RDMAControlHeader resp_ignored
;
1916 ret
= qemu_rdma_exchange_get_response(rdma
, &resp_ignored
,
1918 RDMA_WRID_READY
, errp
);
1925 * If the user is expecting a response, post a WR in anticipation of it.
1928 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_DATA
);
1930 error_setg(errp
, "rdma migration: error posting"
1931 " extra control recv for anticipated result!");
1937 * Post a WR to replace the one we just consumed for the READY message.
1939 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
1941 error_setg(errp
, "rdma migration: error posting first control recv!");
1946 * Deliver the control message that was requested.
1948 ret
= qemu_rdma_post_send_control(rdma
, data
, head
);
1951 error_setg(errp
, "Failed to send control buffer!");
1956 * If we're expecting a response, block and wait for it.
1960 trace_qemu_rdma_exchange_send_issue_callback();
1961 ret
= callback(rdma
, errp
);
1967 trace_qemu_rdma_exchange_send_waiting(control_desc(resp
->type
));
1968 ret
= qemu_rdma_exchange_get_response(rdma
, resp
,
1969 resp
->type
, RDMA_WRID_DATA
,
1976 qemu_rdma_move_header(rdma
, RDMA_WRID_DATA
, resp
);
1978 *resp_idx
= RDMA_WRID_DATA
;
1980 trace_qemu_rdma_exchange_send_received(control_desc(resp
->type
));
1983 rdma
->control_ready_expected
= 1;
1989 * This is an 'atomic' high-level operation to receive a single, unified
1990 * control-channel message.
1992 static int qemu_rdma_exchange_recv(RDMAContext
*rdma
, RDMAControlHeader
*head
,
1993 uint32_t expecting
, Error
**errp
)
1995 RDMAControlHeader ready
= {
1997 .type
= RDMA_CONTROL_READY
,
2003 * Inform the source that we're ready to receive a message.
2005 ret
= qemu_rdma_post_send_control(rdma
, NULL
, &ready
);
2008 error_setg(errp
, "Failed to send control buffer!");
2013 * Block and wait for the message.
2015 ret
= qemu_rdma_exchange_get_response(rdma
, head
,
2016 expecting
, RDMA_WRID_READY
, errp
);
2022 qemu_rdma_move_header(rdma
, RDMA_WRID_READY
, head
);
2025 * Post a new RECV work request to replace the one we just consumed.
2027 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
2029 error_setg(errp
, "rdma migration: error posting second control recv!");
2037 * Write an actual chunk of memory using RDMA.
2039 * If we're using dynamic registration on the dest-side, we have to
2040 * send a registration command first.
2042 static int qemu_rdma_write_one(RDMAContext
*rdma
,
2043 int current_index
, uint64_t current_addr
,
2048 struct ibv_send_wr send_wr
= { 0 };
2049 struct ibv_send_wr
*bad_wr
;
2050 int reg_result_idx
, ret
, count
= 0;
2051 uint64_t chunk
, chunks
;
2052 uint8_t *chunk_start
, *chunk_end
;
2053 RDMALocalBlock
*block
= &(rdma
->local_ram_blocks
.block
[current_index
]);
2055 RDMARegisterResult
*reg_result
;
2056 RDMAControlHeader resp
= { .type
= RDMA_CONTROL_REGISTER_RESULT
};
2057 RDMAControlHeader head
= { .len
= sizeof(RDMARegister
),
2058 .type
= RDMA_CONTROL_REGISTER_REQUEST
,
2063 sge
.addr
= (uintptr_t)(block
->local_host_addr
+
2064 (current_addr
- block
->offset
));
2065 sge
.length
= length
;
2067 chunk
= ram_chunk_index(block
->local_host_addr
,
2068 (uint8_t *)(uintptr_t)sge
.addr
);
2069 chunk_start
= ram_chunk_start(block
, chunk
);
2071 if (block
->is_ram_block
) {
2072 chunks
= length
/ (1UL << RDMA_REG_CHUNK_SHIFT
);
2074 if (chunks
&& ((length
% (1UL << RDMA_REG_CHUNK_SHIFT
)) == 0)) {
2078 chunks
= block
->length
/ (1UL << RDMA_REG_CHUNK_SHIFT
);
2080 if (chunks
&& ((block
->length
% (1UL << RDMA_REG_CHUNK_SHIFT
)) == 0)) {
2085 trace_qemu_rdma_write_one_top(chunks
+ 1,
2087 (1UL << RDMA_REG_CHUNK_SHIFT
) / 1024 / 1024);
2089 chunk_end
= ram_chunk_end(block
, chunk
+ chunks
);
2092 while (test_bit(chunk
, block
->transit_bitmap
)) {
2094 trace_qemu_rdma_write_one_block(count
++, current_index
, chunk
,
2095 sge
.addr
, length
, rdma
->nb_sent
, block
->nb_chunks
);
2097 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RDMA_WRITE
, NULL
);
2100 error_report("Failed to Wait for previous write to complete "
2101 "block %d chunk %" PRIu64
2102 " current %" PRIu64
" len %" PRIu64
" %d",
2103 current_index
, chunk
, sge
.addr
, length
, rdma
->nb_sent
);
2108 if (!rdma
->pin_all
|| !block
->is_ram_block
) {
2109 if (!block
->remote_keys
[chunk
]) {
2111 * This chunk has not yet been registered, so first check to see
2112 * if the entire chunk is zero. If so, tell the other size to
2113 * memset() + madvise() the entire chunk without RDMA.
2116 if (buffer_is_zero((void *)(uintptr_t)sge
.addr
, length
)) {
2117 RDMACompress comp
= {
2118 .offset
= current_addr
,
2120 .block_idx
= current_index
,
2124 head
.len
= sizeof(comp
);
2125 head
.type
= RDMA_CONTROL_COMPRESS
;
2127 trace_qemu_rdma_write_one_zero(chunk
, sge
.length
,
2128 current_index
, current_addr
);
2130 compress_to_network(rdma
, &comp
);
2131 ret
= qemu_rdma_exchange_send(rdma
, &head
,
2132 (uint8_t *) &comp
, NULL
, NULL
, NULL
, &err
);
2135 error_report_err(err
);
2140 * TODO: Here we are sending something, but we are not
2141 * accounting for anything transferred. The following is wrong:
2143 * stat64_add(&mig_stats.rdma_bytes, sge.length);
2145 * because we are using some kind of compression. I
2146 * would think that head.len would be the more similar
2147 * thing to a correct value.
2149 stat64_add(&mig_stats
.zero_pages
,
2150 sge
.length
/ qemu_target_page_size());
2155 * Otherwise, tell other side to register.
2157 reg
.current_index
= current_index
;
2158 if (block
->is_ram_block
) {
2159 reg
.key
.current_addr
= current_addr
;
2161 reg
.key
.chunk
= chunk
;
2163 reg
.chunks
= chunks
;
2165 trace_qemu_rdma_write_one_sendreg(chunk
, sge
.length
, current_index
,
2168 register_to_network(rdma
, ®
);
2169 ret
= qemu_rdma_exchange_send(rdma
, &head
, (uint8_t *) ®
,
2170 &resp
, ®_result_idx
, NULL
, &err
);
2172 error_report_err(err
);
2176 /* try to overlap this single registration with the one we sent. */
2177 if (qemu_rdma_register_and_get_keys(rdma
, block
, sge
.addr
,
2178 &sge
.lkey
, NULL
, chunk
,
2179 chunk_start
, chunk_end
)) {
2180 error_report("cannot get lkey");
2184 reg_result
= (RDMARegisterResult
*)
2185 rdma
->wr_data
[reg_result_idx
].control_curr
;
2187 network_to_result(reg_result
);
2189 trace_qemu_rdma_write_one_recvregres(block
->remote_keys
[chunk
],
2190 reg_result
->rkey
, chunk
);
2192 block
->remote_keys
[chunk
] = reg_result
->rkey
;
2193 block
->remote_host_addr
= reg_result
->host_addr
;
2195 /* already registered before */
2196 if (qemu_rdma_register_and_get_keys(rdma
, block
, sge
.addr
,
2197 &sge
.lkey
, NULL
, chunk
,
2198 chunk_start
, chunk_end
)) {
2199 error_report("cannot get lkey!");
2204 send_wr
.wr
.rdma
.rkey
= block
->remote_keys
[chunk
];
2206 send_wr
.wr
.rdma
.rkey
= block
->remote_rkey
;
2208 if (qemu_rdma_register_and_get_keys(rdma
, block
, sge
.addr
,
2209 &sge
.lkey
, NULL
, chunk
,
2210 chunk_start
, chunk_end
)) {
2211 error_report("cannot get lkey!");
2217 * Encode the ram block index and chunk within this wrid.
2218 * We will use this information at the time of completion
2219 * to figure out which bitmap to check against and then which
2220 * chunk in the bitmap to look for.
2222 send_wr
.wr_id
= qemu_rdma_make_wrid(RDMA_WRID_RDMA_WRITE
,
2223 current_index
, chunk
);
2225 send_wr
.opcode
= IBV_WR_RDMA_WRITE
;
2226 send_wr
.send_flags
= IBV_SEND_SIGNALED
;
2227 send_wr
.sg_list
= &sge
;
2228 send_wr
.num_sge
= 1;
2229 send_wr
.wr
.rdma
.remote_addr
= block
->remote_host_addr
+
2230 (current_addr
- block
->offset
);
2232 trace_qemu_rdma_write_one_post(chunk
, sge
.addr
, send_wr
.wr
.rdma
.remote_addr
,
2236 * ibv_post_send() does not return negative error numbers,
2237 * per the specification they are positive - no idea why.
2239 ret
= ibv_post_send(rdma
->qp
, &send_wr
, &bad_wr
);
2241 if (ret
== ENOMEM
) {
2242 trace_qemu_rdma_write_one_queue_full();
2243 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RDMA_WRITE
, NULL
);
2245 error_report("rdma migration: failed to make "
2246 "room in full send queue!");
2252 } else if (ret
> 0) {
2254 * FIXME perror() is problematic, because whether
2255 * ibv_post_send() sets errno is unclear. Will go away later
2258 perror("rdma migration: post rdma write failed");
2262 set_bit(chunk
, block
->transit_bitmap
);
2263 stat64_add(&mig_stats
.normal_pages
, sge
.length
/ qemu_target_page_size());
2265 * We are adding to transferred the amount of data written, but no
2266 * overhead at all. I will asume that RDMA is magicaly and don't
2267 * need to transfer (at least) the addresses where it wants to
2268 * write the pages. Here it looks like it should be something
2270 * sizeof(send_wr) + sge.length
2271 * but this being RDMA, who knows.
2273 stat64_add(&mig_stats
.rdma_bytes
, sge
.length
);
2274 ram_transferred_add(sge
.length
);
2275 rdma
->total_writes
++;
2281 * Push out any unwritten RDMA operations.
2283 * We support sending out multiple chunks at the same time.
2284 * Not all of them need to get signaled in the completion queue.
2286 static int qemu_rdma_write_flush(RDMAContext
*rdma
)
2290 if (!rdma
->current_length
) {
2294 ret
= qemu_rdma_write_one(rdma
,
2295 rdma
->current_index
, rdma
->current_addr
, rdma
->current_length
);
2303 trace_qemu_rdma_write_flush(rdma
->nb_sent
);
2306 rdma
->current_length
= 0;
2307 rdma
->current_addr
= 0;
2312 static inline bool qemu_rdma_buffer_mergeable(RDMAContext
*rdma
,
2313 uint64_t offset
, uint64_t len
)
2315 RDMALocalBlock
*block
;
2319 if (rdma
->current_index
< 0) {
2323 if (rdma
->current_chunk
< 0) {
2327 block
= &(rdma
->local_ram_blocks
.block
[rdma
->current_index
]);
2328 host_addr
= block
->local_host_addr
+ (offset
- block
->offset
);
2329 chunk_end
= ram_chunk_end(block
, rdma
->current_chunk
);
2331 if (rdma
->current_length
== 0) {
2336 * Only merge into chunk sequentially.
2338 if (offset
!= (rdma
->current_addr
+ rdma
->current_length
)) {
2342 if (offset
< block
->offset
) {
2346 if ((offset
+ len
) > (block
->offset
+ block
->length
)) {
2350 if ((host_addr
+ len
) > chunk_end
) {
2358 * We're not actually writing here, but doing three things:
2360 * 1. Identify the chunk the buffer belongs to.
2361 * 2. If the chunk is full or the buffer doesn't belong to the current
2362 * chunk, then start a new chunk and flush() the old chunk.
2363 * 3. To keep the hardware busy, we also group chunks into batches
2364 * and only require that a batch gets acknowledged in the completion
2365 * queue instead of each individual chunk.
2367 static int qemu_rdma_write(RDMAContext
*rdma
,
2368 uint64_t block_offset
, uint64_t offset
,
2371 uint64_t current_addr
= block_offset
+ offset
;
2372 uint64_t index
= rdma
->current_index
;
2373 uint64_t chunk
= rdma
->current_chunk
;
2376 /* If we cannot merge it, we flush the current buffer first. */
2377 if (!qemu_rdma_buffer_mergeable(rdma
, current_addr
, len
)) {
2378 ret
= qemu_rdma_write_flush(rdma
);
2382 rdma
->current_length
= 0;
2383 rdma
->current_addr
= current_addr
;
2385 qemu_rdma_search_ram_block(rdma
, block_offset
,
2386 offset
, len
, &index
, &chunk
);
2387 rdma
->current_index
= index
;
2388 rdma
->current_chunk
= chunk
;
2392 rdma
->current_length
+= len
;
2394 /* flush it if buffer is too large */
2395 if (rdma
->current_length
>= RDMA_MERGE_MAX
) {
2396 return qemu_rdma_write_flush(rdma
);
2402 static void qemu_rdma_cleanup(RDMAContext
*rdma
)
2406 if (rdma
->cm_id
&& rdma
->connected
) {
2407 if ((rdma
->errored
||
2408 migrate_get_current()->state
== MIGRATION_STATUS_CANCELLING
) &&
2409 !rdma
->received_error
) {
2410 RDMAControlHeader head
= { .len
= 0,
2411 .type
= RDMA_CONTROL_ERROR
,
2414 error_report("Early error. Sending error.");
2415 qemu_rdma_post_send_control(rdma
, NULL
, &head
);
2418 rdma_disconnect(rdma
->cm_id
);
2419 trace_qemu_rdma_cleanup_disconnect();
2420 rdma
->connected
= false;
2423 if (rdma
->channel
) {
2424 qemu_set_fd_handler(rdma
->channel
->fd
, NULL
, NULL
, NULL
);
2426 g_free(rdma
->dest_blocks
);
2427 rdma
->dest_blocks
= NULL
;
2429 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2430 if (rdma
->wr_data
[idx
].control_mr
) {
2431 rdma
->total_registrations
--;
2432 ibv_dereg_mr(rdma
->wr_data
[idx
].control_mr
);
2434 rdma
->wr_data
[idx
].control_mr
= NULL
;
2437 if (rdma
->local_ram_blocks
.block
) {
2438 while (rdma
->local_ram_blocks
.nb_blocks
) {
2439 rdma_delete_block(rdma
, &rdma
->local_ram_blocks
.block
[0]);
2444 rdma_destroy_qp(rdma
->cm_id
);
2447 if (rdma
->recv_cq
) {
2448 ibv_destroy_cq(rdma
->recv_cq
);
2449 rdma
->recv_cq
= NULL
;
2451 if (rdma
->send_cq
) {
2452 ibv_destroy_cq(rdma
->send_cq
);
2453 rdma
->send_cq
= NULL
;
2455 if (rdma
->recv_comp_channel
) {
2456 ibv_destroy_comp_channel(rdma
->recv_comp_channel
);
2457 rdma
->recv_comp_channel
= NULL
;
2459 if (rdma
->send_comp_channel
) {
2460 ibv_destroy_comp_channel(rdma
->send_comp_channel
);
2461 rdma
->send_comp_channel
= NULL
;
2464 ibv_dealloc_pd(rdma
->pd
);
2468 rdma_destroy_id(rdma
->cm_id
);
2472 /* the destination side, listen_id and channel is shared */
2473 if (rdma
->listen_id
) {
2474 if (!rdma
->is_return_path
) {
2475 rdma_destroy_id(rdma
->listen_id
);
2477 rdma
->listen_id
= NULL
;
2479 if (rdma
->channel
) {
2480 if (!rdma
->is_return_path
) {
2481 rdma_destroy_event_channel(rdma
->channel
);
2483 rdma
->channel
= NULL
;
2487 if (rdma
->channel
) {
2488 rdma_destroy_event_channel(rdma
->channel
);
2489 rdma
->channel
= NULL
;
2492 g_free(rdma
->host_port
);
2494 rdma
->host_port
= NULL
;
2498 static int qemu_rdma_source_init(RDMAContext
*rdma
, bool pin_all
, Error
**errp
)
2503 * Will be validated against destination's actual capabilities
2504 * after the connect() completes.
2506 rdma
->pin_all
= pin_all
;
2508 ret
= qemu_rdma_resolve_host(rdma
, errp
);
2510 goto err_rdma_source_init
;
2513 ret
= qemu_rdma_alloc_pd_cq(rdma
);
2515 error_setg(errp
, "RDMA ERROR: "
2516 "rdma migration: error allocating pd and cq! Your mlock()"
2517 " limits may be too low. Please check $ ulimit -a # and "
2518 "search for 'ulimit -l' in the output");
2519 goto err_rdma_source_init
;
2522 ret
= qemu_rdma_alloc_qp(rdma
);
2524 error_setg(errp
, "RDMA ERROR: rdma migration: error allocating qp!");
2525 goto err_rdma_source_init
;
2528 qemu_rdma_init_ram_blocks(rdma
);
2530 /* Build the hash that maps from offset to RAMBlock */
2531 rdma
->blockmap
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
2532 for (idx
= 0; idx
< rdma
->local_ram_blocks
.nb_blocks
; idx
++) {
2533 g_hash_table_insert(rdma
->blockmap
,
2534 (void *)(uintptr_t)rdma
->local_ram_blocks
.block
[idx
].offset
,
2535 &rdma
->local_ram_blocks
.block
[idx
]);
2538 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2539 ret
= qemu_rdma_reg_control(rdma
, idx
);
2542 "RDMA ERROR: rdma migration: error registering %d control!",
2544 goto err_rdma_source_init
;
2550 err_rdma_source_init
:
2551 qemu_rdma_cleanup(rdma
);
2555 static int qemu_get_cm_event_timeout(RDMAContext
*rdma
,
2556 struct rdma_cm_event
**cm_event
,
2557 long msec
, Error
**errp
)
2560 struct pollfd poll_fd
= {
2561 .fd
= rdma
->channel
->fd
,
2567 ret
= poll(&poll_fd
, 1, msec
);
2568 } while (ret
< 0 && errno
== EINTR
);
2571 error_setg(errp
, "RDMA ERROR: poll cm event timeout");
2573 } else if (ret
< 0) {
2574 error_setg(errp
, "RDMA ERROR: failed to poll cm event, errno=%i",
2577 } else if (poll_fd
.revents
& POLLIN
) {
2578 if (rdma_get_cm_event(rdma
->channel
, cm_event
) < 0) {
2579 error_setg(errp
, "RDMA ERROR: failed to get cm event");
2584 error_setg(errp
, "RDMA ERROR: no POLLIN event, revent=%x",
2590 static int qemu_rdma_connect(RDMAContext
*rdma
, bool return_path
,
2593 RDMACapabilities cap
= {
2594 .version
= RDMA_CONTROL_VERSION_CURRENT
,
2597 struct rdma_conn_param conn_param
= { .initiator_depth
= 2,
2599 .private_data
= &cap
,
2600 .private_data_len
= sizeof(cap
),
2602 struct rdma_cm_event
*cm_event
;
2606 * Only negotiate the capability with destination if the user
2607 * on the source first requested the capability.
2609 if (rdma
->pin_all
) {
2610 trace_qemu_rdma_connect_pin_all_requested();
2611 cap
.flags
|= RDMA_CAPABILITY_PIN_ALL
;
2614 caps_to_network(&cap
);
2616 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
2618 error_setg(errp
, "RDMA ERROR: posting second control recv");
2619 goto err_rdma_source_connect
;
2622 ret
= rdma_connect(rdma
->cm_id
, &conn_param
);
2624 perror("rdma_connect");
2625 error_setg(errp
, "RDMA ERROR: connecting to destination!");
2626 goto err_rdma_source_connect
;
2630 ret
= qemu_get_cm_event_timeout(rdma
, &cm_event
, 5000, errp
);
2632 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
2634 error_setg(errp
, "RDMA ERROR: failed to get cm event");
2639 * FIXME perror() is wrong, because
2640 * qemu_get_cm_event_timeout() can fail without setting errno.
2641 * Will go away later in this series.
2643 perror("rdma_get_cm_event after rdma_connect");
2644 goto err_rdma_source_connect
;
2647 if (cm_event
->event
!= RDMA_CM_EVENT_ESTABLISHED
) {
2648 error_report("rdma_get_cm_event != EVENT_ESTABLISHED after rdma_connect");
2649 error_setg(errp
, "RDMA ERROR: connecting to destination!");
2650 rdma_ack_cm_event(cm_event
);
2651 goto err_rdma_source_connect
;
2653 rdma
->connected
= true;
2655 memcpy(&cap
, cm_event
->param
.conn
.private_data
, sizeof(cap
));
2656 network_to_caps(&cap
);
2659 * Verify that the *requested* capabilities are supported by the destination
2660 * and disable them otherwise.
2662 if (rdma
->pin_all
&& !(cap
.flags
& RDMA_CAPABILITY_PIN_ALL
)) {
2663 warn_report("RDMA: Server cannot support pinning all memory. "
2664 "Will register memory dynamically.");
2665 rdma
->pin_all
= false;
2668 trace_qemu_rdma_connect_pin_all_outcome(rdma
->pin_all
);
2670 rdma_ack_cm_event(cm_event
);
2672 rdma
->control_ready_expected
= 1;
2676 err_rdma_source_connect
:
2677 qemu_rdma_cleanup(rdma
);
2681 static int qemu_rdma_dest_init(RDMAContext
*rdma
, Error
**errp
)
2685 struct rdma_cm_id
*listen_id
;
2686 char ip
[40] = "unknown";
2687 struct rdma_addrinfo
*res
, *e
;
2691 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2692 rdma
->wr_data
[idx
].control_len
= 0;
2693 rdma
->wr_data
[idx
].control_curr
= NULL
;
2696 if (!rdma
->host
|| !rdma
->host
[0]) {
2697 error_setg(errp
, "RDMA ERROR: RDMA host is not set!");
2698 rdma
->errored
= true;
2701 /* create CM channel */
2702 rdma
->channel
= rdma_create_event_channel();
2703 if (!rdma
->channel
) {
2704 error_setg(errp
, "RDMA ERROR: could not create rdma event channel");
2705 rdma
->errored
= true;
2710 ret
= rdma_create_id(rdma
->channel
, &listen_id
, NULL
, RDMA_PS_TCP
);
2712 error_setg(errp
, "RDMA ERROR: could not create cm_id!");
2713 goto err_dest_init_create_listen_id
;
2716 snprintf(port_str
, 16, "%d", rdma
->port
);
2717 port_str
[15] = '\0';
2719 ret
= rdma_getaddrinfo(rdma
->host
, port_str
, NULL
, &res
);
2721 error_setg(errp
, "RDMA ERROR: could not rdma_getaddrinfo address %s",
2723 goto err_dest_init_bind_addr
;
2726 ret
= rdma_set_option(listen_id
, RDMA_OPTION_ID
, RDMA_OPTION_ID_REUSEADDR
,
2727 &reuse
, sizeof reuse
);
2729 error_setg(errp
, "RDMA ERROR: Error: could not set REUSEADDR option");
2730 goto err_dest_init_bind_addr
;
2733 /* Try all addresses, saving the first error in @err */
2734 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
2735 Error
**local_errp
= err
? NULL
: &err
;
2737 inet_ntop(e
->ai_family
,
2738 &((struct sockaddr_in
*) e
->ai_dst_addr
)->sin_addr
, ip
, sizeof ip
);
2739 trace_qemu_rdma_dest_init_trying(rdma
->host
, ip
);
2740 ret
= rdma_bind_addr(listen_id
, e
->ai_dst_addr
);
2744 if (e
->ai_family
== AF_INET6
) {
2745 ret
= qemu_rdma_broken_ipv6_kernel(listen_id
->verbs
,
2755 rdma_freeaddrinfo(res
);
2758 error_propagate(errp
, err
);
2760 error_setg(errp
, "RDMA ERROR: Error: could not rdma_bind_addr!");
2762 goto err_dest_init_bind_addr
;
2765 rdma
->listen_id
= listen_id
;
2766 qemu_rdma_dump_gid("dest_init", listen_id
);
2769 err_dest_init_bind_addr
:
2770 rdma_destroy_id(listen_id
);
2771 err_dest_init_create_listen_id
:
2772 rdma_destroy_event_channel(rdma
->channel
);
2773 rdma
->channel
= NULL
;
2774 rdma
->errored
= true;
2779 static void qemu_rdma_return_path_dest_init(RDMAContext
*rdma_return_path
,
2784 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2785 rdma_return_path
->wr_data
[idx
].control_len
= 0;
2786 rdma_return_path
->wr_data
[idx
].control_curr
= NULL
;
2789 /*the CM channel and CM id is shared*/
2790 rdma_return_path
->channel
= rdma
->channel
;
2791 rdma_return_path
->listen_id
= rdma
->listen_id
;
2793 rdma
->return_path
= rdma_return_path
;
2794 rdma_return_path
->return_path
= rdma
;
2795 rdma_return_path
->is_return_path
= true;
2798 static RDMAContext
*qemu_rdma_data_init(const char *host_port
, Error
**errp
)
2800 RDMAContext
*rdma
= NULL
;
2801 InetSocketAddress
*addr
;
2803 rdma
= g_new0(RDMAContext
, 1);
2804 rdma
->current_index
= -1;
2805 rdma
->current_chunk
= -1;
2807 addr
= g_new(InetSocketAddress
, 1);
2808 if (!inet_parse(addr
, host_port
, NULL
)) {
2809 rdma
->port
= atoi(addr
->port
);
2810 rdma
->host
= g_strdup(addr
->host
);
2811 rdma
->host_port
= g_strdup(host_port
);
2813 error_setg(errp
, "RDMA ERROR: bad RDMA migration address '%s'",
2819 qapi_free_InetSocketAddress(addr
);
2824 * QEMUFile interface to the control channel.
2825 * SEND messages for control only.
2826 * VM's ram is handled with regular RDMA messages.
2828 static ssize_t
qio_channel_rdma_writev(QIOChannel
*ioc
,
2829 const struct iovec
*iov
,
2836 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
2842 RCU_READ_LOCK_GUARD();
2843 rdma
= qatomic_rcu_read(&rioc
->rdmaout
);
2846 error_setg(errp
, "RDMA control channel output is not set");
2850 if (rdma
->errored
) {
2852 "RDMA is in an error state waiting migration to abort!");
2857 * Push out any writes that
2858 * we're queued up for VM's ram.
2860 ret
= qemu_rdma_write_flush(rdma
);
2862 rdma
->errored
= true;
2863 error_setg(errp
, "qemu_rdma_write_flush failed");
2867 for (i
= 0; i
< niov
; i
++) {
2868 size_t remaining
= iov
[i
].iov_len
;
2869 uint8_t * data
= (void *)iov
[i
].iov_base
;
2871 RDMAControlHeader head
= {};
2873 len
= MIN(remaining
, RDMA_SEND_INCREMENT
);
2877 head
.type
= RDMA_CONTROL_QEMU_FILE
;
2879 ret
= qemu_rdma_exchange_send(rdma
, &head
,
2880 data
, NULL
, NULL
, NULL
, errp
);
2883 rdma
->errored
= true;
2895 static size_t qemu_rdma_fill(RDMAContext
*rdma
, uint8_t *buf
,
2896 size_t size
, int idx
)
2900 if (rdma
->wr_data
[idx
].control_len
) {
2901 trace_qemu_rdma_fill(rdma
->wr_data
[idx
].control_len
, size
);
2903 len
= MIN(size
, rdma
->wr_data
[idx
].control_len
);
2904 memcpy(buf
, rdma
->wr_data
[idx
].control_curr
, len
);
2905 rdma
->wr_data
[idx
].control_curr
+= len
;
2906 rdma
->wr_data
[idx
].control_len
-= len
;
2913 * QEMUFile interface to the control channel.
2914 * RDMA links don't use bytestreams, so we have to
2915 * return bytes to QEMUFile opportunistically.
2917 static ssize_t
qio_channel_rdma_readv(QIOChannel
*ioc
,
2918 const struct iovec
*iov
,
2925 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
2927 RDMAControlHeader head
;
2932 RCU_READ_LOCK_GUARD();
2933 rdma
= qatomic_rcu_read(&rioc
->rdmain
);
2936 error_setg(errp
, "RDMA control channel input is not set");
2940 if (rdma
->errored
) {
2942 "RDMA is in an error state waiting migration to abort!");
2946 for (i
= 0; i
< niov
; i
++) {
2947 size_t want
= iov
[i
].iov_len
;
2948 uint8_t *data
= (void *)iov
[i
].iov_base
;
2951 * First, we hold on to the last SEND message we
2952 * were given and dish out the bytes until we run
2955 len
= qemu_rdma_fill(rdma
, data
, want
, 0);
2958 /* Got what we needed, so go to next iovec */
2963 /* If we got any data so far, then don't wait
2964 * for more, just return what we have */
2970 /* We've got nothing at all, so lets wait for
2973 ret
= qemu_rdma_exchange_recv(rdma
, &head
, RDMA_CONTROL_QEMU_FILE
,
2977 rdma
->errored
= true;
2982 * SEND was received with new bytes, now try again.
2984 len
= qemu_rdma_fill(rdma
, data
, want
, 0);
2988 /* Still didn't get enough, so lets just return */
2991 return QIO_CHANNEL_ERR_BLOCK
;
3001 * Block until all the outstanding chunks have been delivered by the hardware.
3003 static int qemu_rdma_drain_cq(RDMAContext
*rdma
)
3007 if (qemu_rdma_write_flush(rdma
) < 0) {
3011 while (rdma
->nb_sent
) {
3012 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RDMA_WRITE
, NULL
);
3014 error_report("rdma migration: complete polling error!");
3019 qemu_rdma_unregister_waiting(rdma
);
3025 static int qio_channel_rdma_set_blocking(QIOChannel
*ioc
,
3029 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
3030 /* XXX we should make readv/writev actually honour this :-) */
3031 rioc
->blocking
= blocking
;
3036 typedef struct QIOChannelRDMASource QIOChannelRDMASource
;
3037 struct QIOChannelRDMASource
{
3039 QIOChannelRDMA
*rioc
;
3040 GIOCondition condition
;
3044 qio_channel_rdma_source_prepare(GSource
*source
,
3047 QIOChannelRDMASource
*rsource
= (QIOChannelRDMASource
*)source
;
3049 GIOCondition cond
= 0;
3052 RCU_READ_LOCK_GUARD();
3053 if (rsource
->condition
== G_IO_IN
) {
3054 rdma
= qatomic_rcu_read(&rsource
->rioc
->rdmain
);
3056 rdma
= qatomic_rcu_read(&rsource
->rioc
->rdmaout
);
3060 error_report("RDMAContext is NULL when prepare Gsource");
3064 if (rdma
->wr_data
[0].control_len
) {
3069 return cond
& rsource
->condition
;
3073 qio_channel_rdma_source_check(GSource
*source
)
3075 QIOChannelRDMASource
*rsource
= (QIOChannelRDMASource
*)source
;
3077 GIOCondition cond
= 0;
3079 RCU_READ_LOCK_GUARD();
3080 if (rsource
->condition
== G_IO_IN
) {
3081 rdma
= qatomic_rcu_read(&rsource
->rioc
->rdmain
);
3083 rdma
= qatomic_rcu_read(&rsource
->rioc
->rdmaout
);
3087 error_report("RDMAContext is NULL when check Gsource");
3091 if (rdma
->wr_data
[0].control_len
) {
3096 return cond
& rsource
->condition
;
3100 qio_channel_rdma_source_dispatch(GSource
*source
,
3101 GSourceFunc callback
,
3104 QIOChannelFunc func
= (QIOChannelFunc
)callback
;
3105 QIOChannelRDMASource
*rsource
= (QIOChannelRDMASource
*)source
;
3107 GIOCondition cond
= 0;
3109 RCU_READ_LOCK_GUARD();
3110 if (rsource
->condition
== G_IO_IN
) {
3111 rdma
= qatomic_rcu_read(&rsource
->rioc
->rdmain
);
3113 rdma
= qatomic_rcu_read(&rsource
->rioc
->rdmaout
);
3117 error_report("RDMAContext is NULL when dispatch Gsource");
3121 if (rdma
->wr_data
[0].control_len
) {
3126 return (*func
)(QIO_CHANNEL(rsource
->rioc
),
3127 (cond
& rsource
->condition
),
3132 qio_channel_rdma_source_finalize(GSource
*source
)
3134 QIOChannelRDMASource
*ssource
= (QIOChannelRDMASource
*)source
;
3136 object_unref(OBJECT(ssource
->rioc
));
3139 static GSourceFuncs qio_channel_rdma_source_funcs
= {
3140 qio_channel_rdma_source_prepare
,
3141 qio_channel_rdma_source_check
,
3142 qio_channel_rdma_source_dispatch
,
3143 qio_channel_rdma_source_finalize
3146 static GSource
*qio_channel_rdma_create_watch(QIOChannel
*ioc
,
3147 GIOCondition condition
)
3149 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
3150 QIOChannelRDMASource
*ssource
;
3153 source
= g_source_new(&qio_channel_rdma_source_funcs
,
3154 sizeof(QIOChannelRDMASource
));
3155 ssource
= (QIOChannelRDMASource
*)source
;
3157 ssource
->rioc
= rioc
;
3158 object_ref(OBJECT(rioc
));
3160 ssource
->condition
= condition
;
3165 static void qio_channel_rdma_set_aio_fd_handler(QIOChannel
*ioc
,
3166 AioContext
*read_ctx
,
3168 AioContext
*write_ctx
,
3169 IOHandler
*io_write
,
3172 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
3174 aio_set_fd_handler(read_ctx
, rioc
->rdmain
->recv_comp_channel
->fd
,
3175 io_read
, io_write
, NULL
, NULL
, opaque
);
3176 aio_set_fd_handler(read_ctx
, rioc
->rdmain
->send_comp_channel
->fd
,
3177 io_read
, io_write
, NULL
, NULL
, opaque
);
3179 aio_set_fd_handler(write_ctx
, rioc
->rdmaout
->recv_comp_channel
->fd
,
3180 io_read
, io_write
, NULL
, NULL
, opaque
);
3181 aio_set_fd_handler(write_ctx
, rioc
->rdmaout
->send_comp_channel
->fd
,
3182 io_read
, io_write
, NULL
, NULL
, opaque
);
3186 struct rdma_close_rcu
{
3187 struct rcu_head rcu
;
3188 RDMAContext
*rdmain
;
3189 RDMAContext
*rdmaout
;
3192 /* callback from qio_channel_rdma_close via call_rcu */
3193 static void qio_channel_rdma_close_rcu(struct rdma_close_rcu
*rcu
)
3196 qemu_rdma_cleanup(rcu
->rdmain
);
3200 qemu_rdma_cleanup(rcu
->rdmaout
);
3203 g_free(rcu
->rdmain
);
3204 g_free(rcu
->rdmaout
);
3208 static int qio_channel_rdma_close(QIOChannel
*ioc
,
3211 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
3212 RDMAContext
*rdmain
, *rdmaout
;
3213 struct rdma_close_rcu
*rcu
= g_new(struct rdma_close_rcu
, 1);
3215 trace_qemu_rdma_close();
3217 rdmain
= rioc
->rdmain
;
3219 qatomic_rcu_set(&rioc
->rdmain
, NULL
);
3222 rdmaout
= rioc
->rdmaout
;
3224 qatomic_rcu_set(&rioc
->rdmaout
, NULL
);
3227 rcu
->rdmain
= rdmain
;
3228 rcu
->rdmaout
= rdmaout
;
3229 call_rcu(rcu
, qio_channel_rdma_close_rcu
, rcu
);
3235 qio_channel_rdma_shutdown(QIOChannel
*ioc
,
3236 QIOChannelShutdown how
,
3239 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
3240 RDMAContext
*rdmain
, *rdmaout
;
3242 RCU_READ_LOCK_GUARD();
3244 rdmain
= qatomic_rcu_read(&rioc
->rdmain
);
3245 rdmaout
= qatomic_rcu_read(&rioc
->rdmain
);
3248 case QIO_CHANNEL_SHUTDOWN_READ
:
3250 rdmain
->errored
= true;
3253 case QIO_CHANNEL_SHUTDOWN_WRITE
:
3255 rdmaout
->errored
= true;
3258 case QIO_CHANNEL_SHUTDOWN_BOTH
:
3261 rdmain
->errored
= true;
3264 rdmaout
->errored
= true;
3275 * This means that 'block_offset' is a full virtual address that does not
3276 * belong to a RAMBlock of the virtual machine and instead
3277 * represents a private malloc'd memory area that the caller wishes to
3281 * Offset is an offset to be added to block_offset and used
3282 * to also lookup the corresponding RAMBlock.
3284 * @size : Number of bytes to transfer
3286 * @pages_sent : User-specificed pointer to indicate how many pages were
3287 * sent. Usually, this will not be more than a few bytes of
3288 * the protocol because most transfers are sent asynchronously.
3290 static int qemu_rdma_save_page(QEMUFile
*f
, ram_addr_t block_offset
,
3291 ram_addr_t offset
, size_t size
)
3293 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(qemu_file_get_ioc(f
));
3297 if (migration_in_postcopy()) {
3298 return RAM_SAVE_CONTROL_NOT_SUPP
;
3301 RCU_READ_LOCK_GUARD();
3302 rdma
= qatomic_rcu_read(&rioc
->rdmaout
);
3308 if (rdma_errored(rdma
)) {
3315 * Add this page to the current 'chunk'. If the chunk
3316 * is full, or the page doesn't belong to the current chunk,
3317 * an actual RDMA write will occur and a new chunk will be formed.
3319 ret
= qemu_rdma_write(rdma
, block_offset
, offset
, size
);
3321 error_report("rdma migration: write error");
3326 * Drain the Completion Queue if possible, but do not block,
3329 * If nothing to poll, the end of the iteration will do this
3330 * again to make sure we don't overflow the request queue.
3333 uint64_t wr_id
, wr_id_in
;
3334 ret
= qemu_rdma_poll(rdma
, rdma
->recv_cq
, &wr_id_in
, NULL
);
3337 error_report("rdma migration: polling error");
3341 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
3343 if (wr_id
== RDMA_WRID_NONE
) {
3349 uint64_t wr_id
, wr_id_in
;
3350 ret
= qemu_rdma_poll(rdma
, rdma
->send_cq
, &wr_id_in
, NULL
);
3353 error_report("rdma migration: polling error");
3357 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
3359 if (wr_id
== RDMA_WRID_NONE
) {
3364 return RAM_SAVE_CONTROL_DELAYED
;
3367 rdma
->errored
= true;
3371 static void rdma_accept_incoming_migration(void *opaque
);
3373 static void rdma_cm_poll_handler(void *opaque
)
3375 RDMAContext
*rdma
= opaque
;
3377 struct rdma_cm_event
*cm_event
;
3378 MigrationIncomingState
*mis
= migration_incoming_get_current();
3380 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
3382 error_report("get_cm_event failed %d", errno
);
3386 if (cm_event
->event
== RDMA_CM_EVENT_DISCONNECTED
||
3387 cm_event
->event
== RDMA_CM_EVENT_DEVICE_REMOVAL
) {
3388 if (!rdma
->errored
&&
3389 migration_incoming_get_current()->state
!=
3390 MIGRATION_STATUS_COMPLETED
) {
3391 error_report("receive cm event, cm event is %d", cm_event
->event
);
3392 rdma
->errored
= true;
3393 if (rdma
->return_path
) {
3394 rdma
->return_path
->errored
= true;
3397 rdma_ack_cm_event(cm_event
);
3398 if (mis
->loadvm_co
) {
3399 qemu_coroutine_enter(mis
->loadvm_co
);
3403 rdma_ack_cm_event(cm_event
);
3406 static int qemu_rdma_accept(RDMAContext
*rdma
)
3408 RDMACapabilities cap
;
3409 struct rdma_conn_param conn_param
= {
3410 .responder_resources
= 2,
3411 .private_data
= &cap
,
3412 .private_data_len
= sizeof(cap
),
3414 RDMAContext
*rdma_return_path
= NULL
;
3415 struct rdma_cm_event
*cm_event
;
3416 struct ibv_context
*verbs
;
3420 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
3422 goto err_rdma_dest_wait
;
3425 if (cm_event
->event
!= RDMA_CM_EVENT_CONNECT_REQUEST
) {
3426 rdma_ack_cm_event(cm_event
);
3427 goto err_rdma_dest_wait
;
3431 * initialize the RDMAContext for return path for postcopy after first
3432 * connection request reached.
3434 if ((migrate_postcopy() || migrate_return_path())
3435 && !rdma
->is_return_path
) {
3436 rdma_return_path
= qemu_rdma_data_init(rdma
->host_port
, NULL
);
3437 if (rdma_return_path
== NULL
) {
3438 rdma_ack_cm_event(cm_event
);
3439 goto err_rdma_dest_wait
;
3442 qemu_rdma_return_path_dest_init(rdma_return_path
, rdma
);
3445 memcpy(&cap
, cm_event
->param
.conn
.private_data
, sizeof(cap
));
3447 network_to_caps(&cap
);
3449 if (cap
.version
< 1 || cap
.version
> RDMA_CONTROL_VERSION_CURRENT
) {
3450 error_report("Unknown source RDMA version: %d, bailing...",
3452 rdma_ack_cm_event(cm_event
);
3453 goto err_rdma_dest_wait
;
3457 * Respond with only the capabilities this version of QEMU knows about.
3459 cap
.flags
&= known_capabilities
;
3462 * Enable the ones that we do know about.
3463 * Add other checks here as new ones are introduced.
3465 if (cap
.flags
& RDMA_CAPABILITY_PIN_ALL
) {
3466 rdma
->pin_all
= true;
3469 rdma
->cm_id
= cm_event
->id
;
3470 verbs
= cm_event
->id
->verbs
;
3472 rdma_ack_cm_event(cm_event
);
3474 trace_qemu_rdma_accept_pin_state(rdma
->pin_all
);
3476 caps_to_network(&cap
);
3478 trace_qemu_rdma_accept_pin_verbsc(verbs
);
3481 rdma
->verbs
= verbs
;
3482 } else if (rdma
->verbs
!= verbs
) {
3483 error_report("ibv context not matching %p, %p!", rdma
->verbs
,
3485 goto err_rdma_dest_wait
;
3488 qemu_rdma_dump_id("dest_init", verbs
);
3490 ret
= qemu_rdma_alloc_pd_cq(rdma
);
3492 error_report("rdma migration: error allocating pd and cq!");
3493 goto err_rdma_dest_wait
;
3496 ret
= qemu_rdma_alloc_qp(rdma
);
3498 error_report("rdma migration: error allocating qp!");
3499 goto err_rdma_dest_wait
;
3502 qemu_rdma_init_ram_blocks(rdma
);
3504 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
3505 ret
= qemu_rdma_reg_control(rdma
, idx
);
3507 error_report("rdma: error registering %d control", idx
);
3508 goto err_rdma_dest_wait
;
3512 /* Accept the second connection request for return path */
3513 if ((migrate_postcopy() || migrate_return_path())
3514 && !rdma
->is_return_path
) {
3515 qemu_set_fd_handler(rdma
->channel
->fd
, rdma_accept_incoming_migration
,
3517 (void *)(intptr_t)rdma
->return_path
);
3519 qemu_set_fd_handler(rdma
->channel
->fd
, rdma_cm_poll_handler
,
3523 ret
= rdma_accept(rdma
->cm_id
, &conn_param
);
3525 error_report("rdma_accept failed");
3526 goto err_rdma_dest_wait
;
3529 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
3531 error_report("rdma_accept get_cm_event failed");
3532 goto err_rdma_dest_wait
;
3535 if (cm_event
->event
!= RDMA_CM_EVENT_ESTABLISHED
) {
3536 error_report("rdma_accept not event established");
3537 rdma_ack_cm_event(cm_event
);
3538 goto err_rdma_dest_wait
;
3541 rdma_ack_cm_event(cm_event
);
3542 rdma
->connected
= true;
3544 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
3546 error_report("rdma migration: error posting second control recv");
3547 goto err_rdma_dest_wait
;
3550 qemu_rdma_dump_gid("dest_connect", rdma
->cm_id
);
3555 rdma
->errored
= true;
3556 qemu_rdma_cleanup(rdma
);
3557 g_free(rdma_return_path
);
3561 static int dest_ram_sort_func(const void *a
, const void *b
)
3563 unsigned int a_index
= ((const RDMALocalBlock
*)a
)->src_index
;
3564 unsigned int b_index
= ((const RDMALocalBlock
*)b
)->src_index
;
3566 return (a_index
< b_index
) ? -1 : (a_index
!= b_index
);
3570 * During each iteration of the migration, we listen for instructions
3571 * by the source VM to perform dynamic page registrations before they
3572 * can perform RDMA operations.
3574 * We respond with the 'rkey'.
3576 * Keep doing this until the source tells us to stop.
3578 static int qemu_rdma_registration_handle(QEMUFile
*f
)
3580 RDMAControlHeader reg_resp
= { .len
= sizeof(RDMARegisterResult
),
3581 .type
= RDMA_CONTROL_REGISTER_RESULT
,
3584 RDMAControlHeader unreg_resp
= { .len
= 0,
3585 .type
= RDMA_CONTROL_UNREGISTER_FINISHED
,
3588 RDMAControlHeader blocks
= { .type
= RDMA_CONTROL_RAM_BLOCKS_RESULT
,
3590 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(qemu_file_get_ioc(f
));
3593 RDMALocalBlocks
*local
;
3594 RDMAControlHeader head
;
3595 RDMARegister
*reg
, *registers
;
3597 RDMARegisterResult
*reg_result
;
3598 static RDMARegisterResult results
[RDMA_CONTROL_MAX_COMMANDS_PER_MESSAGE
];
3599 RDMALocalBlock
*block
;
3606 RCU_READ_LOCK_GUARD();
3607 rdma
= qatomic_rcu_read(&rioc
->rdmain
);
3613 if (rdma_errored(rdma
)) {
3617 local
= &rdma
->local_ram_blocks
;
3619 trace_qemu_rdma_registration_handle_wait();
3621 ret
= qemu_rdma_exchange_recv(rdma
, &head
, RDMA_CONTROL_NONE
, &err
);
3624 error_report_err(err
);
3628 if (head
.repeat
> RDMA_CONTROL_MAX_COMMANDS_PER_MESSAGE
) {
3629 error_report("rdma: Too many requests in this message (%d)."
3630 "Bailing.", head
.repeat
);
3634 switch (head
.type
) {
3635 case RDMA_CONTROL_COMPRESS
:
3636 comp
= (RDMACompress
*) rdma
->wr_data
[idx
].control_curr
;
3637 network_to_compress(comp
);
3639 trace_qemu_rdma_registration_handle_compress(comp
->length
,
3642 if (comp
->block_idx
>= rdma
->local_ram_blocks
.nb_blocks
) {
3643 error_report("rdma: 'compress' bad block index %u (vs %d)",
3644 (unsigned int)comp
->block_idx
,
3645 rdma
->local_ram_blocks
.nb_blocks
);
3648 block
= &(rdma
->local_ram_blocks
.block
[comp
->block_idx
]);
3650 host_addr
= block
->local_host_addr
+
3651 (comp
->offset
- block
->offset
);
3653 ram_handle_compressed(host_addr
, comp
->value
, comp
->length
);
3656 case RDMA_CONTROL_REGISTER_FINISHED
:
3657 trace_qemu_rdma_registration_handle_finished();
3660 case RDMA_CONTROL_RAM_BLOCKS_REQUEST
:
3661 trace_qemu_rdma_registration_handle_ram_blocks();
3663 /* Sort our local RAM Block list so it's the same as the source,
3664 * we can do this since we've filled in a src_index in the list
3665 * as we received the RAMBlock list earlier.
3667 qsort(rdma
->local_ram_blocks
.block
,
3668 rdma
->local_ram_blocks
.nb_blocks
,
3669 sizeof(RDMALocalBlock
), dest_ram_sort_func
);
3670 for (i
= 0; i
< local
->nb_blocks
; i
++) {
3671 local
->block
[i
].index
= i
;
3674 if (rdma
->pin_all
) {
3675 ret
= qemu_rdma_reg_whole_ram_blocks(rdma
, &err
);
3677 error_report_err(err
);
3683 * Dest uses this to prepare to transmit the RAMBlock descriptions
3684 * to the source VM after connection setup.
3685 * Both sides use the "remote" structure to communicate and update
3686 * their "local" descriptions with what was sent.
3688 for (i
= 0; i
< local
->nb_blocks
; i
++) {
3689 rdma
->dest_blocks
[i
].remote_host_addr
=
3690 (uintptr_t)(local
->block
[i
].local_host_addr
);
3692 if (rdma
->pin_all
) {
3693 rdma
->dest_blocks
[i
].remote_rkey
= local
->block
[i
].mr
->rkey
;
3696 rdma
->dest_blocks
[i
].offset
= local
->block
[i
].offset
;
3697 rdma
->dest_blocks
[i
].length
= local
->block
[i
].length
;
3699 dest_block_to_network(&rdma
->dest_blocks
[i
]);
3700 trace_qemu_rdma_registration_handle_ram_blocks_loop(
3701 local
->block
[i
].block_name
,
3702 local
->block
[i
].offset
,
3703 local
->block
[i
].length
,
3704 local
->block
[i
].local_host_addr
,
3705 local
->block
[i
].src_index
);
3708 blocks
.len
= rdma
->local_ram_blocks
.nb_blocks
3709 * sizeof(RDMADestBlock
);
3712 ret
= qemu_rdma_post_send_control(rdma
,
3713 (uint8_t *) rdma
->dest_blocks
, &blocks
);
3716 error_report("rdma migration: error sending remote info");
3721 case RDMA_CONTROL_REGISTER_REQUEST
:
3722 trace_qemu_rdma_registration_handle_register(head
.repeat
);
3724 reg_resp
.repeat
= head
.repeat
;
3725 registers
= (RDMARegister
*) rdma
->wr_data
[idx
].control_curr
;
3727 for (count
= 0; count
< head
.repeat
; count
++) {
3729 uint8_t *chunk_start
, *chunk_end
;
3731 reg
= ®isters
[count
];
3732 network_to_register(reg
);
3734 reg_result
= &results
[count
];
3736 trace_qemu_rdma_registration_handle_register_loop(count
,
3737 reg
->current_index
, reg
->key
.current_addr
, reg
->chunks
);
3739 if (reg
->current_index
>= rdma
->local_ram_blocks
.nb_blocks
) {
3740 error_report("rdma: 'register' bad block index %u (vs %d)",
3741 (unsigned int)reg
->current_index
,
3742 rdma
->local_ram_blocks
.nb_blocks
);
3745 block
= &(rdma
->local_ram_blocks
.block
[reg
->current_index
]);
3746 if (block
->is_ram_block
) {
3747 if (block
->offset
> reg
->key
.current_addr
) {
3748 error_report("rdma: bad register address for block %s"
3749 " offset: %" PRIx64
" current_addr: %" PRIx64
,
3750 block
->block_name
, block
->offset
,
3751 reg
->key
.current_addr
);
3754 host_addr
= (block
->local_host_addr
+
3755 (reg
->key
.current_addr
- block
->offset
));
3756 chunk
= ram_chunk_index(block
->local_host_addr
,
3757 (uint8_t *) host_addr
);
3759 chunk
= reg
->key
.chunk
;
3760 host_addr
= block
->local_host_addr
+
3761 (reg
->key
.chunk
* (1UL << RDMA_REG_CHUNK_SHIFT
));
3762 /* Check for particularly bad chunk value */
3763 if (host_addr
< (void *)block
->local_host_addr
) {
3764 error_report("rdma: bad chunk for block %s"
3766 block
->block_name
, reg
->key
.chunk
);
3770 chunk_start
= ram_chunk_start(block
, chunk
);
3771 chunk_end
= ram_chunk_end(block
, chunk
+ reg
->chunks
);
3772 /* avoid "-Waddress-of-packed-member" warning */
3773 uint32_t tmp_rkey
= 0;
3774 if (qemu_rdma_register_and_get_keys(rdma
, block
,
3775 (uintptr_t)host_addr
, NULL
, &tmp_rkey
,
3776 chunk
, chunk_start
, chunk_end
)) {
3777 error_report("cannot get rkey");
3780 reg_result
->rkey
= tmp_rkey
;
3782 reg_result
->host_addr
= (uintptr_t)block
->local_host_addr
;
3784 trace_qemu_rdma_registration_handle_register_rkey(
3787 result_to_network(reg_result
);
3790 ret
= qemu_rdma_post_send_control(rdma
,
3791 (uint8_t *) results
, ®_resp
);
3794 error_report("Failed to send control buffer");
3798 case RDMA_CONTROL_UNREGISTER_REQUEST
:
3799 trace_qemu_rdma_registration_handle_unregister(head
.repeat
);
3800 unreg_resp
.repeat
= head
.repeat
;
3801 registers
= (RDMARegister
*) rdma
->wr_data
[idx
].control_curr
;
3803 for (count
= 0; count
< head
.repeat
; count
++) {
3804 reg
= ®isters
[count
];
3805 network_to_register(reg
);
3807 trace_qemu_rdma_registration_handle_unregister_loop(count
,
3808 reg
->current_index
, reg
->key
.chunk
);
3810 block
= &(rdma
->local_ram_blocks
.block
[reg
->current_index
]);
3812 ret
= ibv_dereg_mr(block
->pmr
[reg
->key
.chunk
]);
3813 block
->pmr
[reg
->key
.chunk
] = NULL
;
3816 perror("rdma unregistration chunk failed");
3820 rdma
->total_registrations
--;
3822 trace_qemu_rdma_registration_handle_unregister_success(
3826 ret
= qemu_rdma_post_send_control(rdma
, NULL
, &unreg_resp
);
3829 error_report("Failed to send control buffer");
3833 case RDMA_CONTROL_REGISTER_RESULT
:
3834 error_report("Invalid RESULT message at dest.");
3837 error_report("Unknown control message %s", control_desc(head
.type
));
3843 rdma
->errored
= true;
3848 * Called via a ram_control_load_hook during the initial RAM load section which
3849 * lists the RAMBlocks by name. This lets us know the order of the RAMBlocks
3851 * We've already built our local RAMBlock list, but not yet sent the list to
3855 rdma_block_notification_handle(QEMUFile
*f
, const char *name
)
3858 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(qemu_file_get_ioc(f
));
3862 RCU_READ_LOCK_GUARD();
3863 rdma
= qatomic_rcu_read(&rioc
->rdmain
);
3869 /* Find the matching RAMBlock in our local list */
3870 for (curr
= 0; curr
< rdma
->local_ram_blocks
.nb_blocks
; curr
++) {
3871 if (!strcmp(rdma
->local_ram_blocks
.block
[curr
].block_name
, name
)) {
3878 error_report("RAMBlock '%s' not found on destination", name
);
3882 rdma
->local_ram_blocks
.block
[curr
].src_index
= rdma
->next_src_index
;
3883 trace_rdma_block_notification_handle(name
, rdma
->next_src_index
);
3884 rdma
->next_src_index
++;
3889 static int rdma_load_hook(QEMUFile
*f
, uint64_t flags
, void *data
)
3892 case RAM_CONTROL_BLOCK_REG
:
3893 return rdma_block_notification_handle(f
, data
);
3895 case RAM_CONTROL_HOOK
:
3896 return qemu_rdma_registration_handle(f
);
3899 /* Shouldn't be called with any other values */
3904 static int qemu_rdma_registration_start(QEMUFile
*f
,
3905 uint64_t flags
, void *data
)
3907 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(qemu_file_get_ioc(f
));
3910 if (migration_in_postcopy()) {
3914 RCU_READ_LOCK_GUARD();
3915 rdma
= qatomic_rcu_read(&rioc
->rdmaout
);
3920 if (rdma_errored(rdma
)) {
3924 trace_qemu_rdma_registration_start(flags
);
3925 qemu_put_be64(f
, RAM_SAVE_FLAG_HOOK
);
3932 * Inform dest that dynamic registrations are done for now.
3933 * First, flush writes, if any.
3935 static int qemu_rdma_registration_stop(QEMUFile
*f
,
3936 uint64_t flags
, void *data
)
3938 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(qemu_file_get_ioc(f
));
3941 RDMAControlHeader head
= { .len
= 0, .repeat
= 1 };
3944 if (migration_in_postcopy()) {
3948 RCU_READ_LOCK_GUARD();
3949 rdma
= qatomic_rcu_read(&rioc
->rdmaout
);
3954 if (rdma_errored(rdma
)) {
3959 ret
= qemu_rdma_drain_cq(rdma
);
3965 if (flags
== RAM_CONTROL_SETUP
) {
3966 RDMAControlHeader resp
= {.type
= RDMA_CONTROL_RAM_BLOCKS_RESULT
};
3967 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
3968 int reg_result_idx
, i
, nb_dest_blocks
;
3970 head
.type
= RDMA_CONTROL_RAM_BLOCKS_REQUEST
;
3971 trace_qemu_rdma_registration_stop_ram();
3974 * Make sure that we parallelize the pinning on both sides.
3975 * For very large guests, doing this serially takes a really
3976 * long time, so we have to 'interleave' the pinning locally
3977 * with the control messages by performing the pinning on this
3978 * side before we receive the control response from the other
3979 * side that the pinning has completed.
3981 ret
= qemu_rdma_exchange_send(rdma
, &head
, NULL
, &resp
,
3982 ®_result_idx
, rdma
->pin_all
?
3983 qemu_rdma_reg_whole_ram_blocks
: NULL
,
3986 error_report_err(err
);
3990 nb_dest_blocks
= resp
.len
/ sizeof(RDMADestBlock
);
3993 * The protocol uses two different sets of rkeys (mutually exclusive):
3994 * 1. One key to represent the virtual address of the entire ram block.
3995 * (dynamic chunk registration disabled - pin everything with one rkey.)
3996 * 2. One to represent individual chunks within a ram block.
3997 * (dynamic chunk registration enabled - pin individual chunks.)
3999 * Once the capability is successfully negotiated, the destination transmits
4000 * the keys to use (or sends them later) including the virtual addresses
4001 * and then propagates the remote ram block descriptions to his local copy.
4004 if (local
->nb_blocks
!= nb_dest_blocks
) {
4005 fprintf(stderr
, "ram blocks mismatch (Number of blocks %d vs %d) "
4006 "Your QEMU command line parameters are probably "
4007 "not identical on both the source and destination.",
4008 local
->nb_blocks
, nb_dest_blocks
);
4009 rdma
->errored
= true;
4013 qemu_rdma_move_header(rdma
, reg_result_idx
, &resp
);
4014 memcpy(rdma
->dest_blocks
,
4015 rdma
->wr_data
[reg_result_idx
].control_curr
, resp
.len
);
4016 for (i
= 0; i
< nb_dest_blocks
; i
++) {
4017 network_to_dest_block(&rdma
->dest_blocks
[i
]);
4019 /* We require that the blocks are in the same order */
4020 if (rdma
->dest_blocks
[i
].length
!= local
->block
[i
].length
) {
4021 fprintf(stderr
, "Block %s/%d has a different length %" PRIu64
4022 "vs %" PRIu64
, local
->block
[i
].block_name
, i
,
4023 local
->block
[i
].length
,
4024 rdma
->dest_blocks
[i
].length
);
4025 rdma
->errored
= true;
4028 local
->block
[i
].remote_host_addr
=
4029 rdma
->dest_blocks
[i
].remote_host_addr
;
4030 local
->block
[i
].remote_rkey
= rdma
->dest_blocks
[i
].remote_rkey
;
4034 trace_qemu_rdma_registration_stop(flags
);
4036 head
.type
= RDMA_CONTROL_REGISTER_FINISHED
;
4037 ret
= qemu_rdma_exchange_send(rdma
, &head
, NULL
, NULL
, NULL
, NULL
, &err
);
4040 error_report_err(err
);
4046 rdma
->errored
= true;
4050 static const QEMUFileHooks rdma_read_hooks
= {
4051 .hook_ram_load
= rdma_load_hook
,
4054 static const QEMUFileHooks rdma_write_hooks
= {
4055 .before_ram_iterate
= qemu_rdma_registration_start
,
4056 .after_ram_iterate
= qemu_rdma_registration_stop
,
4057 .save_page
= qemu_rdma_save_page
,
4061 static void qio_channel_rdma_finalize(Object
*obj
)
4063 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(obj
);
4065 qemu_rdma_cleanup(rioc
->rdmain
);
4066 g_free(rioc
->rdmain
);
4067 rioc
->rdmain
= NULL
;
4069 if (rioc
->rdmaout
) {
4070 qemu_rdma_cleanup(rioc
->rdmaout
);
4071 g_free(rioc
->rdmaout
);
4072 rioc
->rdmaout
= NULL
;
4076 static void qio_channel_rdma_class_init(ObjectClass
*klass
,
4077 void *class_data G_GNUC_UNUSED
)
4079 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
4081 ioc_klass
->io_writev
= qio_channel_rdma_writev
;
4082 ioc_klass
->io_readv
= qio_channel_rdma_readv
;
4083 ioc_klass
->io_set_blocking
= qio_channel_rdma_set_blocking
;
4084 ioc_klass
->io_close
= qio_channel_rdma_close
;
4085 ioc_klass
->io_create_watch
= qio_channel_rdma_create_watch
;
4086 ioc_klass
->io_set_aio_fd_handler
= qio_channel_rdma_set_aio_fd_handler
;
4087 ioc_klass
->io_shutdown
= qio_channel_rdma_shutdown
;
4090 static const TypeInfo qio_channel_rdma_info
= {
4091 .parent
= TYPE_QIO_CHANNEL
,
4092 .name
= TYPE_QIO_CHANNEL_RDMA
,
4093 .instance_size
= sizeof(QIOChannelRDMA
),
4094 .instance_finalize
= qio_channel_rdma_finalize
,
4095 .class_init
= qio_channel_rdma_class_init
,
4098 static void qio_channel_rdma_register_types(void)
4100 type_register_static(&qio_channel_rdma_info
);
4103 type_init(qio_channel_rdma_register_types
);
4105 static QEMUFile
*rdma_new_input(RDMAContext
*rdma
)
4107 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(object_new(TYPE_QIO_CHANNEL_RDMA
));
4109 rioc
->file
= qemu_file_new_input(QIO_CHANNEL(rioc
));
4110 rioc
->rdmain
= rdma
;
4111 rioc
->rdmaout
= rdma
->return_path
;
4112 qemu_file_set_hooks(rioc
->file
, &rdma_read_hooks
);
4117 static QEMUFile
*rdma_new_output(RDMAContext
*rdma
)
4119 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(object_new(TYPE_QIO_CHANNEL_RDMA
));
4121 rioc
->file
= qemu_file_new_output(QIO_CHANNEL(rioc
));
4122 rioc
->rdmaout
= rdma
;
4123 rioc
->rdmain
= rdma
->return_path
;
4124 qemu_file_set_hooks(rioc
->file
, &rdma_write_hooks
);
4129 static void rdma_accept_incoming_migration(void *opaque
)
4131 RDMAContext
*rdma
= opaque
;
4134 Error
*local_err
= NULL
;
4136 trace_qemu_rdma_accept_incoming_migration();
4137 ret
= qemu_rdma_accept(rdma
);
4140 fprintf(stderr
, "RDMA ERROR: Migration initialization failed\n");
4144 trace_qemu_rdma_accept_incoming_migration_accepted();
4146 if (rdma
->is_return_path
) {
4150 f
= rdma_new_input(rdma
);
4152 fprintf(stderr
, "RDMA ERROR: could not open RDMA for input\n");
4153 qemu_rdma_cleanup(rdma
);
4157 rdma
->migration_started_on_destination
= 1;
4158 migration_fd_process_incoming(f
, &local_err
);
4160 error_reportf_err(local_err
, "RDMA ERROR:");
4164 void rdma_start_incoming_migration(const char *host_port
, Error
**errp
)
4169 trace_rdma_start_incoming_migration();
4171 /* Avoid ram_block_discard_disable(), cannot change during migration. */
4172 if (ram_block_discard_is_required()) {
4173 error_setg(errp
, "RDMA: cannot disable RAM discard");
4177 rdma
= qemu_rdma_data_init(host_port
, errp
);
4182 ret
= qemu_rdma_dest_init(rdma
, errp
);
4187 trace_rdma_start_incoming_migration_after_dest_init();
4189 ret
= rdma_listen(rdma
->listen_id
, 5);
4192 error_setg(errp
, "RDMA ERROR: listening on socket!");
4196 trace_rdma_start_incoming_migration_after_rdma_listen();
4198 qemu_set_fd_handler(rdma
->channel
->fd
, rdma_accept_incoming_migration
,
4199 NULL
, (void *)(intptr_t)rdma
);
4203 qemu_rdma_cleanup(rdma
);
4207 g_free(rdma
->host_port
);
4212 void rdma_start_outgoing_migration(void *opaque
,
4213 const char *host_port
, Error
**errp
)
4215 MigrationState
*s
= opaque
;
4216 RDMAContext
*rdma_return_path
= NULL
;
4220 /* Avoid ram_block_discard_disable(), cannot change during migration. */
4221 if (ram_block_discard_is_required()) {
4222 error_setg(errp
, "RDMA: cannot disable RAM discard");
4226 rdma
= qemu_rdma_data_init(host_port
, errp
);
4231 ret
= qemu_rdma_source_init(rdma
, migrate_rdma_pin_all(), errp
);
4237 trace_rdma_start_outgoing_migration_after_rdma_source_init();
4238 ret
= qemu_rdma_connect(rdma
, false, errp
);
4244 /* RDMA postcopy need a separate queue pair for return path */
4245 if (migrate_postcopy() || migrate_return_path()) {
4246 rdma_return_path
= qemu_rdma_data_init(host_port
, errp
);
4248 if (rdma_return_path
== NULL
) {
4249 goto return_path_err
;
4252 ret
= qemu_rdma_source_init(rdma_return_path
,
4253 migrate_rdma_pin_all(), errp
);
4256 goto return_path_err
;
4259 ret
= qemu_rdma_connect(rdma_return_path
, true, errp
);
4262 goto return_path_err
;
4265 rdma
->return_path
= rdma_return_path
;
4266 rdma_return_path
->return_path
= rdma
;
4267 rdma_return_path
->is_return_path
= true;
4270 trace_rdma_start_outgoing_migration_after_rdma_connect();
4272 s
->to_dst_file
= rdma_new_output(rdma
);
4273 migrate_fd_connect(s
, NULL
);
4276 qemu_rdma_cleanup(rdma
);
4279 g_free(rdma_return_path
);