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.
16 #include "qemu/osdep.h"
17 #include "qapi/error.h"
18 #include "qemu-common.h"
19 #include "qemu/cutils.h"
20 #include "migration/migration.h"
21 #include "migration/qemu-file.h"
22 #include "exec/cpu-common.h"
23 #include "qemu/error-report.h"
24 #include "qemu/main-loop.h"
25 #include "qemu/sockets.h"
26 #include "qemu/bitmap.h"
27 #include "qemu/coroutine.h"
28 #include <sys/socket.h>
30 #include <arpa/inet.h>
31 #include <rdma/rdma_cma.h>
35 * Print and error on both the Monitor and the Log file.
37 #define ERROR(errp, fmt, ...) \
39 fprintf(stderr, "RDMA ERROR: " fmt "\n", ## __VA_ARGS__); \
40 if (errp && (*(errp) == NULL)) { \
41 error_setg(errp, "RDMA ERROR: " fmt, ## __VA_ARGS__); \
45 #define RDMA_RESOLVE_TIMEOUT_MS 10000
47 /* Do not merge data if larger than this. */
48 #define RDMA_MERGE_MAX (2 * 1024 * 1024)
49 #define RDMA_SIGNALED_SEND_MAX (RDMA_MERGE_MAX / 4096)
51 #define RDMA_REG_CHUNK_SHIFT 20 /* 1 MB */
54 * This is only for non-live state being migrated.
55 * Instead of RDMA_WRITE messages, we use RDMA_SEND
56 * messages for that state, which requires a different
57 * delivery design than main memory.
59 #define RDMA_SEND_INCREMENT 32768
62 * Maximum size infiniband SEND message
64 #define RDMA_CONTROL_MAX_BUFFER (512 * 1024)
65 #define RDMA_CONTROL_MAX_COMMANDS_PER_MESSAGE 4096
67 #define RDMA_CONTROL_VERSION_CURRENT 1
69 * Capabilities for negotiation.
71 #define RDMA_CAPABILITY_PIN_ALL 0x01
74 * Add the other flags above to this list of known capabilities
75 * as they are introduced.
77 static uint32_t known_capabilities
= RDMA_CAPABILITY_PIN_ALL
;
79 #define CHECK_ERROR_STATE() \
81 if (rdma->error_state) { \
82 if (!rdma->error_reported) { \
83 error_report("RDMA is in an error state waiting migration" \
85 rdma->error_reported = 1; \
87 return rdma->error_state; \
92 * A work request ID is 64-bits and we split up these bits
95 * bits 0-15 : type of control message, 2^16
96 * bits 16-29: ram block index, 2^14
97 * bits 30-63: ram block chunk number, 2^34
99 * The last two bit ranges are only used for RDMA writes,
100 * in order to track their completion and potentially
101 * also track unregistration status of the message.
103 #define RDMA_WRID_TYPE_SHIFT 0UL
104 #define RDMA_WRID_BLOCK_SHIFT 16UL
105 #define RDMA_WRID_CHUNK_SHIFT 30UL
107 #define RDMA_WRID_TYPE_MASK \
108 ((1UL << RDMA_WRID_BLOCK_SHIFT) - 1UL)
110 #define RDMA_WRID_BLOCK_MASK \
111 (~RDMA_WRID_TYPE_MASK & ((1UL << RDMA_WRID_CHUNK_SHIFT) - 1UL))
113 #define RDMA_WRID_CHUNK_MASK (~RDMA_WRID_BLOCK_MASK & ~RDMA_WRID_TYPE_MASK)
116 * RDMA migration protocol:
117 * 1. RDMA Writes (data messages, i.e. RAM)
118 * 2. IB Send/Recv (control channel messages)
122 RDMA_WRID_RDMA_WRITE
= 1,
123 RDMA_WRID_SEND_CONTROL
= 2000,
124 RDMA_WRID_RECV_CONTROL
= 4000,
127 static const char *wrid_desc
[] = {
128 [RDMA_WRID_NONE
] = "NONE",
129 [RDMA_WRID_RDMA_WRITE
] = "WRITE RDMA",
130 [RDMA_WRID_SEND_CONTROL
] = "CONTROL SEND",
131 [RDMA_WRID_RECV_CONTROL
] = "CONTROL RECV",
135 * Work request IDs for IB SEND messages only (not RDMA writes).
136 * This is used by the migration protocol to transmit
137 * control messages (such as device state and registration commands)
139 * We could use more WRs, but we have enough for now.
149 * SEND/RECV IB Control Messages.
152 RDMA_CONTROL_NONE
= 0,
154 RDMA_CONTROL_READY
, /* ready to receive */
155 RDMA_CONTROL_QEMU_FILE
, /* QEMUFile-transmitted bytes */
156 RDMA_CONTROL_RAM_BLOCKS_REQUEST
, /* RAMBlock synchronization */
157 RDMA_CONTROL_RAM_BLOCKS_RESULT
, /* RAMBlock synchronization */
158 RDMA_CONTROL_COMPRESS
, /* page contains repeat values */
159 RDMA_CONTROL_REGISTER_REQUEST
, /* dynamic page registration */
160 RDMA_CONTROL_REGISTER_RESULT
, /* key to use after registration */
161 RDMA_CONTROL_REGISTER_FINISHED
, /* current iteration finished */
162 RDMA_CONTROL_UNREGISTER_REQUEST
, /* dynamic UN-registration */
163 RDMA_CONTROL_UNREGISTER_FINISHED
, /* unpinning finished */
166 static const char *control_desc
[] = {
167 [RDMA_CONTROL_NONE
] = "NONE",
168 [RDMA_CONTROL_ERROR
] = "ERROR",
169 [RDMA_CONTROL_READY
] = "READY",
170 [RDMA_CONTROL_QEMU_FILE
] = "QEMU FILE",
171 [RDMA_CONTROL_RAM_BLOCKS_REQUEST
] = "RAM BLOCKS REQUEST",
172 [RDMA_CONTROL_RAM_BLOCKS_RESULT
] = "RAM BLOCKS RESULT",
173 [RDMA_CONTROL_COMPRESS
] = "COMPRESS",
174 [RDMA_CONTROL_REGISTER_REQUEST
] = "REGISTER REQUEST",
175 [RDMA_CONTROL_REGISTER_RESULT
] = "REGISTER RESULT",
176 [RDMA_CONTROL_REGISTER_FINISHED
] = "REGISTER FINISHED",
177 [RDMA_CONTROL_UNREGISTER_REQUEST
] = "UNREGISTER REQUEST",
178 [RDMA_CONTROL_UNREGISTER_FINISHED
] = "UNREGISTER FINISHED",
182 * Memory and MR structures used to represent an IB Send/Recv work request.
183 * This is *not* used for RDMA writes, only IB Send/Recv.
186 uint8_t control
[RDMA_CONTROL_MAX_BUFFER
]; /* actual buffer to register */
187 struct ibv_mr
*control_mr
; /* registration metadata */
188 size_t control_len
; /* length of the message */
189 uint8_t *control_curr
; /* start of unconsumed bytes */
190 } RDMAWorkRequestData
;
193 * Negotiate RDMA capabilities during connection-setup time.
200 static void caps_to_network(RDMACapabilities
*cap
)
202 cap
->version
= htonl(cap
->version
);
203 cap
->flags
= htonl(cap
->flags
);
206 static void network_to_caps(RDMACapabilities
*cap
)
208 cap
->version
= ntohl(cap
->version
);
209 cap
->flags
= ntohl(cap
->flags
);
213 * Representation of a RAMBlock from an RDMA perspective.
214 * This is not transmitted, only local.
215 * This and subsequent structures cannot be linked lists
216 * because we're using a single IB message to transmit
217 * the information. It's small anyway, so a list is overkill.
219 typedef struct RDMALocalBlock
{
221 uint8_t *local_host_addr
; /* local virtual address */
222 uint64_t remote_host_addr
; /* remote virtual address */
225 struct ibv_mr
**pmr
; /* MRs for chunk-level registration */
226 struct ibv_mr
*mr
; /* MR for non-chunk-level registration */
227 uint32_t *remote_keys
; /* rkeys for chunk-level registration */
228 uint32_t remote_rkey
; /* rkeys for non-chunk-level registration */
229 int index
; /* which block are we */
230 unsigned int src_index
; /* (Only used on dest) */
233 unsigned long *transit_bitmap
;
234 unsigned long *unregister_bitmap
;
238 * Also represents a RAMblock, but only on the dest.
239 * This gets transmitted by the dest during connection-time
240 * to the source VM and then is used to populate the
241 * corresponding RDMALocalBlock with
242 * the information needed to perform the actual RDMA.
244 typedef struct QEMU_PACKED RDMADestBlock
{
245 uint64_t remote_host_addr
;
248 uint32_t remote_rkey
;
252 static uint64_t htonll(uint64_t v
)
254 union { uint32_t lv
[2]; uint64_t llv
; } u
;
255 u
.lv
[0] = htonl(v
>> 32);
256 u
.lv
[1] = htonl(v
& 0xFFFFFFFFULL
);
260 static uint64_t ntohll(uint64_t v
) {
261 union { uint32_t lv
[2]; uint64_t llv
; } u
;
263 return ((uint64_t)ntohl(u
.lv
[0]) << 32) | (uint64_t) ntohl(u
.lv
[1]);
266 static void dest_block_to_network(RDMADestBlock
*db
)
268 db
->remote_host_addr
= htonll(db
->remote_host_addr
);
269 db
->offset
= htonll(db
->offset
);
270 db
->length
= htonll(db
->length
);
271 db
->remote_rkey
= htonl(db
->remote_rkey
);
274 static void network_to_dest_block(RDMADestBlock
*db
)
276 db
->remote_host_addr
= ntohll(db
->remote_host_addr
);
277 db
->offset
= ntohll(db
->offset
);
278 db
->length
= ntohll(db
->length
);
279 db
->remote_rkey
= ntohl(db
->remote_rkey
);
283 * Virtual address of the above structures used for transmitting
284 * the RAMBlock descriptions at connection-time.
285 * This structure is *not* transmitted.
287 typedef struct RDMALocalBlocks
{
289 bool init
; /* main memory init complete */
290 RDMALocalBlock
*block
;
294 * Main data structure for RDMA state.
295 * While there is only one copy of this structure being allocated right now,
296 * this is the place where one would start if you wanted to consider
297 * having more than one RDMA connection open at the same time.
299 typedef struct RDMAContext
{
303 RDMAWorkRequestData wr_data
[RDMA_WRID_MAX
];
306 * This is used by *_exchange_send() to figure out whether or not
307 * the initial "READY" message has already been received or not.
308 * This is because other functions may potentially poll() and detect
309 * the READY message before send() does, in which case we need to
310 * know if it completed.
312 int control_ready_expected
;
314 /* number of outstanding writes */
317 /* store info about current buffer so that we can
318 merge it with future sends */
319 uint64_t current_addr
;
320 uint64_t current_length
;
321 /* index of ram block the current buffer belongs to */
323 /* index of the chunk in the current ram block */
329 * infiniband-specific variables for opening the device
330 * and maintaining connection state and so forth.
332 * cm_id also has ibv_context, rdma_event_channel, and ibv_qp in
333 * cm_id->verbs, cm_id->channel, and cm_id->qp.
335 struct rdma_cm_id
*cm_id
; /* connection manager ID */
336 struct rdma_cm_id
*listen_id
;
339 struct ibv_context
*verbs
;
340 struct rdma_event_channel
*channel
;
341 struct ibv_qp
*qp
; /* queue pair */
342 struct ibv_comp_channel
*comp_channel
; /* completion channel */
343 struct ibv_pd
*pd
; /* protection domain */
344 struct ibv_cq
*cq
; /* completion queue */
347 * If a previous write failed (perhaps because of a failed
348 * memory registration, then do not attempt any future work
349 * and remember the error state.
356 * Description of ram blocks used throughout the code.
358 RDMALocalBlocks local_ram_blocks
;
359 RDMADestBlock
*dest_blocks
;
361 /* Index of the next RAMBlock received during block registration */
362 unsigned int next_src_index
;
365 * Migration on *destination* started.
366 * Then use coroutine yield function.
367 * Source runs in a thread, so we don't care.
369 int migration_started_on_destination
;
371 int total_registrations
;
374 int unregister_current
, unregister_next
;
375 uint64_t unregistrations
[RDMA_SIGNALED_SEND_MAX
];
377 GHashTable
*blockmap
;
380 #define TYPE_QIO_CHANNEL_RDMA "qio-channel-rdma"
381 #define QIO_CHANNEL_RDMA(obj) \
382 OBJECT_CHECK(QIOChannelRDMA, (obj), TYPE_QIO_CHANNEL_RDMA)
384 typedef struct QIOChannelRDMA QIOChannelRDMA
;
387 struct QIOChannelRDMA
{
392 bool blocking
; /* XXX we don't actually honour this yet */
396 * Main structure for IB Send/Recv control messages.
397 * This gets prepended at the beginning of every Send/Recv.
399 typedef struct QEMU_PACKED
{
400 uint32_t len
; /* Total length of data portion */
401 uint32_t type
; /* which control command to perform */
402 uint32_t repeat
; /* number of commands in data portion of same type */
406 static void control_to_network(RDMAControlHeader
*control
)
408 control
->type
= htonl(control
->type
);
409 control
->len
= htonl(control
->len
);
410 control
->repeat
= htonl(control
->repeat
);
413 static void network_to_control(RDMAControlHeader
*control
)
415 control
->type
= ntohl(control
->type
);
416 control
->len
= ntohl(control
->len
);
417 control
->repeat
= ntohl(control
->repeat
);
421 * Register a single Chunk.
422 * Information sent by the source VM to inform the dest
423 * to register an single chunk of memory before we can perform
424 * the actual RDMA operation.
426 typedef struct QEMU_PACKED
{
428 uint64_t current_addr
; /* offset into the ram_addr_t space */
429 uint64_t chunk
; /* chunk to lookup if unregistering */
431 uint32_t current_index
; /* which ramblock the chunk belongs to */
433 uint64_t chunks
; /* how many sequential chunks to register */
436 static void register_to_network(RDMAContext
*rdma
, RDMARegister
*reg
)
438 RDMALocalBlock
*local_block
;
439 local_block
= &rdma
->local_ram_blocks
.block
[reg
->current_index
];
441 if (local_block
->is_ram_block
) {
443 * current_addr as passed in is an address in the local ram_addr_t
444 * space, we need to translate this for the destination
446 reg
->key
.current_addr
-= local_block
->offset
;
447 reg
->key
.current_addr
+= rdma
->dest_blocks
[reg
->current_index
].offset
;
449 reg
->key
.current_addr
= htonll(reg
->key
.current_addr
);
450 reg
->current_index
= htonl(reg
->current_index
);
451 reg
->chunks
= htonll(reg
->chunks
);
454 static void network_to_register(RDMARegister
*reg
)
456 reg
->key
.current_addr
= ntohll(reg
->key
.current_addr
);
457 reg
->current_index
= ntohl(reg
->current_index
);
458 reg
->chunks
= ntohll(reg
->chunks
);
461 typedef struct QEMU_PACKED
{
462 uint32_t value
; /* if zero, we will madvise() */
463 uint32_t block_idx
; /* which ram block index */
464 uint64_t offset
; /* Address in remote ram_addr_t space */
465 uint64_t length
; /* length of the chunk */
468 static void compress_to_network(RDMAContext
*rdma
, RDMACompress
*comp
)
470 comp
->value
= htonl(comp
->value
);
472 * comp->offset as passed in is an address in the local ram_addr_t
473 * space, we need to translate this for the destination
475 comp
->offset
-= rdma
->local_ram_blocks
.block
[comp
->block_idx
].offset
;
476 comp
->offset
+= rdma
->dest_blocks
[comp
->block_idx
].offset
;
477 comp
->block_idx
= htonl(comp
->block_idx
);
478 comp
->offset
= htonll(comp
->offset
);
479 comp
->length
= htonll(comp
->length
);
482 static void network_to_compress(RDMACompress
*comp
)
484 comp
->value
= ntohl(comp
->value
);
485 comp
->block_idx
= ntohl(comp
->block_idx
);
486 comp
->offset
= ntohll(comp
->offset
);
487 comp
->length
= ntohll(comp
->length
);
491 * The result of the dest's memory registration produces an "rkey"
492 * which the source VM must reference in order to perform
493 * the RDMA operation.
495 typedef struct QEMU_PACKED
{
499 } RDMARegisterResult
;
501 static void result_to_network(RDMARegisterResult
*result
)
503 result
->rkey
= htonl(result
->rkey
);
504 result
->host_addr
= htonll(result
->host_addr
);
507 static void network_to_result(RDMARegisterResult
*result
)
509 result
->rkey
= ntohl(result
->rkey
);
510 result
->host_addr
= ntohll(result
->host_addr
);
513 const char *print_wrid(int wrid
);
514 static int qemu_rdma_exchange_send(RDMAContext
*rdma
, RDMAControlHeader
*head
,
515 uint8_t *data
, RDMAControlHeader
*resp
,
517 int (*callback
)(RDMAContext
*rdma
));
519 static inline uint64_t ram_chunk_index(const uint8_t *start
,
522 return ((uintptr_t) host
- (uintptr_t) start
) >> RDMA_REG_CHUNK_SHIFT
;
525 static inline uint8_t *ram_chunk_start(const RDMALocalBlock
*rdma_ram_block
,
528 return (uint8_t *)(uintptr_t)(rdma_ram_block
->local_host_addr
+
529 (i
<< RDMA_REG_CHUNK_SHIFT
));
532 static inline uint8_t *ram_chunk_end(const RDMALocalBlock
*rdma_ram_block
,
535 uint8_t *result
= ram_chunk_start(rdma_ram_block
, i
) +
536 (1UL << RDMA_REG_CHUNK_SHIFT
);
538 if (result
> (rdma_ram_block
->local_host_addr
+ rdma_ram_block
->length
)) {
539 result
= rdma_ram_block
->local_host_addr
+ rdma_ram_block
->length
;
545 static int rdma_add_block(RDMAContext
*rdma
, const char *block_name
,
547 ram_addr_t block_offset
, uint64_t length
)
549 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
550 RDMALocalBlock
*block
;
551 RDMALocalBlock
*old
= local
->block
;
553 local
->block
= g_new0(RDMALocalBlock
, local
->nb_blocks
+ 1);
555 if (local
->nb_blocks
) {
558 if (rdma
->blockmap
) {
559 for (x
= 0; x
< local
->nb_blocks
; x
++) {
560 g_hash_table_remove(rdma
->blockmap
,
561 (void *)(uintptr_t)old
[x
].offset
);
562 g_hash_table_insert(rdma
->blockmap
,
563 (void *)(uintptr_t)old
[x
].offset
,
567 memcpy(local
->block
, old
, sizeof(RDMALocalBlock
) * local
->nb_blocks
);
571 block
= &local
->block
[local
->nb_blocks
];
573 block
->block_name
= g_strdup(block_name
);
574 block
->local_host_addr
= host_addr
;
575 block
->offset
= block_offset
;
576 block
->length
= length
;
577 block
->index
= local
->nb_blocks
;
578 block
->src_index
= ~0U; /* Filled in by the receipt of the block list */
579 block
->nb_chunks
= ram_chunk_index(host_addr
, host_addr
+ length
) + 1UL;
580 block
->transit_bitmap
= bitmap_new(block
->nb_chunks
);
581 bitmap_clear(block
->transit_bitmap
, 0, block
->nb_chunks
);
582 block
->unregister_bitmap
= bitmap_new(block
->nb_chunks
);
583 bitmap_clear(block
->unregister_bitmap
, 0, block
->nb_chunks
);
584 block
->remote_keys
= g_new0(uint32_t, block
->nb_chunks
);
586 block
->is_ram_block
= local
->init
? false : true;
588 if (rdma
->blockmap
) {
589 g_hash_table_insert(rdma
->blockmap
, (void *)(uintptr_t)block_offset
, block
);
592 trace_rdma_add_block(block_name
, local
->nb_blocks
,
593 (uintptr_t) block
->local_host_addr
,
594 block
->offset
, block
->length
,
595 (uintptr_t) (block
->local_host_addr
+ block
->length
),
596 BITS_TO_LONGS(block
->nb_chunks
) *
597 sizeof(unsigned long) * 8,
606 * Memory regions need to be registered with the device and queue pairs setup
607 * in advanced before the migration starts. This tells us where the RAM blocks
608 * are so that we can register them individually.
610 static int qemu_rdma_init_one_block(const char *block_name
, void *host_addr
,
611 ram_addr_t block_offset
, ram_addr_t length
, void *opaque
)
613 return rdma_add_block(opaque
, block_name
, host_addr
, block_offset
, length
);
617 * Identify the RAMBlocks and their quantity. They will be references to
618 * identify chunk boundaries inside each RAMBlock and also be referenced
619 * during dynamic page registration.
621 static int qemu_rdma_init_ram_blocks(RDMAContext
*rdma
)
623 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
625 assert(rdma
->blockmap
== NULL
);
626 memset(local
, 0, sizeof *local
);
627 qemu_ram_foreach_block(qemu_rdma_init_one_block
, rdma
);
628 trace_qemu_rdma_init_ram_blocks(local
->nb_blocks
);
629 rdma
->dest_blocks
= g_new0(RDMADestBlock
,
630 rdma
->local_ram_blocks
.nb_blocks
);
636 * Note: If used outside of cleanup, the caller must ensure that the destination
637 * block structures are also updated
639 static int rdma_delete_block(RDMAContext
*rdma
, RDMALocalBlock
*block
)
641 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
642 RDMALocalBlock
*old
= local
->block
;
645 if (rdma
->blockmap
) {
646 g_hash_table_remove(rdma
->blockmap
, (void *)(uintptr_t)block
->offset
);
651 for (j
= 0; j
< block
->nb_chunks
; j
++) {
652 if (!block
->pmr
[j
]) {
655 ibv_dereg_mr(block
->pmr
[j
]);
656 rdma
->total_registrations
--;
663 ibv_dereg_mr(block
->mr
);
664 rdma
->total_registrations
--;
668 g_free(block
->transit_bitmap
);
669 block
->transit_bitmap
= NULL
;
671 g_free(block
->unregister_bitmap
);
672 block
->unregister_bitmap
= NULL
;
674 g_free(block
->remote_keys
);
675 block
->remote_keys
= NULL
;
677 g_free(block
->block_name
);
678 block
->block_name
= NULL
;
680 if (rdma
->blockmap
) {
681 for (x
= 0; x
< local
->nb_blocks
; x
++) {
682 g_hash_table_remove(rdma
->blockmap
,
683 (void *)(uintptr_t)old
[x
].offset
);
687 if (local
->nb_blocks
> 1) {
689 local
->block
= g_new0(RDMALocalBlock
, local
->nb_blocks
- 1);
692 memcpy(local
->block
, old
, sizeof(RDMALocalBlock
) * block
->index
);
695 if (block
->index
< (local
->nb_blocks
- 1)) {
696 memcpy(local
->block
+ block
->index
, old
+ (block
->index
+ 1),
697 sizeof(RDMALocalBlock
) *
698 (local
->nb_blocks
- (block
->index
+ 1)));
701 assert(block
== local
->block
);
705 trace_rdma_delete_block(block
, (uintptr_t)block
->local_host_addr
,
706 block
->offset
, block
->length
,
707 (uintptr_t)(block
->local_host_addr
+ block
->length
),
708 BITS_TO_LONGS(block
->nb_chunks
) *
709 sizeof(unsigned long) * 8, block
->nb_chunks
);
715 if (local
->nb_blocks
&& rdma
->blockmap
) {
716 for (x
= 0; x
< local
->nb_blocks
; x
++) {
717 g_hash_table_insert(rdma
->blockmap
,
718 (void *)(uintptr_t)local
->block
[x
].offset
,
727 * Put in the log file which RDMA device was opened and the details
728 * associated with that device.
730 static void qemu_rdma_dump_id(const char *who
, struct ibv_context
*verbs
)
732 struct ibv_port_attr port
;
734 if (ibv_query_port(verbs
, 1, &port
)) {
735 error_report("Failed to query port information");
739 printf("%s RDMA Device opened: kernel name %s "
740 "uverbs device name %s, "
741 "infiniband_verbs class device path %s, "
742 "infiniband class device path %s, "
743 "transport: (%d) %s\n",
746 verbs
->device
->dev_name
,
747 verbs
->device
->dev_path
,
748 verbs
->device
->ibdev_path
,
750 (port
.link_layer
== IBV_LINK_LAYER_INFINIBAND
) ? "Infiniband" :
751 ((port
.link_layer
== IBV_LINK_LAYER_ETHERNET
)
752 ? "Ethernet" : "Unknown"));
756 * Put in the log file the RDMA gid addressing information,
757 * useful for folks who have trouble understanding the
758 * RDMA device hierarchy in the kernel.
760 static void qemu_rdma_dump_gid(const char *who
, struct rdma_cm_id
*id
)
764 inet_ntop(AF_INET6
, &id
->route
.addr
.addr
.ibaddr
.sgid
, sgid
, sizeof sgid
);
765 inet_ntop(AF_INET6
, &id
->route
.addr
.addr
.ibaddr
.dgid
, dgid
, sizeof dgid
);
766 trace_qemu_rdma_dump_gid(who
, sgid
, dgid
);
770 * As of now, IPv6 over RoCE / iWARP is not supported by linux.
771 * We will try the next addrinfo struct, and fail if there are
772 * no other valid addresses to bind against.
774 * If user is listening on '[::]', then we will not have a opened a device
775 * yet and have no way of verifying if the device is RoCE or not.
777 * In this case, the source VM will throw an error for ALL types of
778 * connections (both IPv4 and IPv6) if the destination machine does not have
779 * a regular infiniband network available for use.
781 * The only way to guarantee that an error is thrown for broken kernels is
782 * for the management software to choose a *specific* interface at bind time
783 * and validate what time of hardware it is.
785 * Unfortunately, this puts the user in a fix:
787 * If the source VM connects with an IPv4 address without knowing that the
788 * destination has bound to '[::]' the migration will unconditionally fail
789 * unless the management software is explicitly listening on the IPv4
790 * address while using a RoCE-based device.
792 * If the source VM connects with an IPv6 address, then we're OK because we can
793 * throw an error on the source (and similarly on the destination).
795 * But in mixed environments, this will be broken for a while until it is fixed
798 * We do provide a *tiny* bit of help in this function: We can list all of the
799 * devices in the system and check to see if all the devices are RoCE or
802 * If we detect that we have a *pure* RoCE environment, then we can safely
803 * thrown an error even if the management software has specified '[::]' as the
806 * However, if there is are multiple hetergeneous devices, then we cannot make
807 * this assumption and the user just has to be sure they know what they are
810 * Patches are being reviewed on linux-rdma.
812 static int qemu_rdma_broken_ipv6_kernel(Error
**errp
, struct ibv_context
*verbs
)
814 struct ibv_port_attr port_attr
;
816 /* This bug only exists in linux, to our knowledge. */
820 * Verbs are only NULL if management has bound to '[::]'.
822 * Let's iterate through all the devices and see if there any pure IB
823 * devices (non-ethernet).
825 * If not, then we can safely proceed with the migration.
826 * Otherwise, there are no guarantees until the bug is fixed in linux.
830 struct ibv_device
** dev_list
= ibv_get_device_list(&num_devices
);
831 bool roce_found
= false;
832 bool ib_found
= false;
834 for (x
= 0; x
< num_devices
; x
++) {
835 verbs
= ibv_open_device(dev_list
[x
]);
837 if (errno
== EPERM
) {
844 if (ibv_query_port(verbs
, 1, &port_attr
)) {
845 ibv_close_device(verbs
);
846 ERROR(errp
, "Could not query initial IB port");
850 if (port_attr
.link_layer
== IBV_LINK_LAYER_INFINIBAND
) {
852 } else if (port_attr
.link_layer
== IBV_LINK_LAYER_ETHERNET
) {
856 ibv_close_device(verbs
);
862 fprintf(stderr
, "WARN: migrations may fail:"
863 " IPv6 over RoCE / iWARP in linux"
864 " is broken. But since you appear to have a"
865 " mixed RoCE / IB environment, be sure to only"
866 " migrate over the IB fabric until the kernel "
867 " fixes the bug.\n");
869 ERROR(errp
, "You only have RoCE / iWARP devices in your systems"
870 " and your management software has specified '[::]'"
871 ", but IPv6 over RoCE / iWARP is not supported in Linux.");
880 * If we have a verbs context, that means that some other than '[::]' was
881 * used by the management software for binding. In which case we can
882 * actually warn the user about a potentially broken kernel.
885 /* IB ports start with 1, not 0 */
886 if (ibv_query_port(verbs
, 1, &port_attr
)) {
887 ERROR(errp
, "Could not query initial IB port");
891 if (port_attr
.link_layer
== IBV_LINK_LAYER_ETHERNET
) {
892 ERROR(errp
, "Linux kernel's RoCE / iWARP does not support IPv6 "
893 "(but patches on linux-rdma in progress)");
903 * Figure out which RDMA device corresponds to the requested IP hostname
904 * Also create the initial connection manager identifiers for opening
907 static int qemu_rdma_resolve_host(RDMAContext
*rdma
, Error
**errp
)
910 struct rdma_addrinfo
*res
;
912 struct rdma_cm_event
*cm_event
;
913 char ip
[40] = "unknown";
914 struct rdma_addrinfo
*e
;
916 if (rdma
->host
== NULL
|| !strcmp(rdma
->host
, "")) {
917 ERROR(errp
, "RDMA hostname has not been set");
921 /* create CM channel */
922 rdma
->channel
= rdma_create_event_channel();
923 if (!rdma
->channel
) {
924 ERROR(errp
, "could not create CM channel");
929 ret
= rdma_create_id(rdma
->channel
, &rdma
->cm_id
, NULL
, RDMA_PS_TCP
);
931 ERROR(errp
, "could not create channel id");
932 goto err_resolve_create_id
;
935 snprintf(port_str
, 16, "%d", rdma
->port
);
938 ret
= rdma_getaddrinfo(rdma
->host
, port_str
, NULL
, &res
);
940 ERROR(errp
, "could not rdma_getaddrinfo address %s", rdma
->host
);
941 goto err_resolve_get_addr
;
944 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
945 inet_ntop(e
->ai_family
,
946 &((struct sockaddr_in
*) e
->ai_dst_addr
)->sin_addr
, ip
, sizeof ip
);
947 trace_qemu_rdma_resolve_host_trying(rdma
->host
, ip
);
949 ret
= rdma_resolve_addr(rdma
->cm_id
, NULL
, e
->ai_dst_addr
,
950 RDMA_RESOLVE_TIMEOUT_MS
);
952 if (e
->ai_family
== AF_INET6
) {
953 ret
= qemu_rdma_broken_ipv6_kernel(errp
, rdma
->cm_id
->verbs
);
962 ERROR(errp
, "could not resolve address %s", rdma
->host
);
963 goto err_resolve_get_addr
;
966 qemu_rdma_dump_gid("source_resolve_addr", rdma
->cm_id
);
968 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
970 ERROR(errp
, "could not perform event_addr_resolved");
971 goto err_resolve_get_addr
;
974 if (cm_event
->event
!= RDMA_CM_EVENT_ADDR_RESOLVED
) {
975 ERROR(errp
, "result not equal to event_addr_resolved %s",
976 rdma_event_str(cm_event
->event
));
977 perror("rdma_resolve_addr");
978 rdma_ack_cm_event(cm_event
);
980 goto err_resolve_get_addr
;
982 rdma_ack_cm_event(cm_event
);
985 ret
= rdma_resolve_route(rdma
->cm_id
, RDMA_RESOLVE_TIMEOUT_MS
);
987 ERROR(errp
, "could not resolve rdma route");
988 goto err_resolve_get_addr
;
991 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
993 ERROR(errp
, "could not perform event_route_resolved");
994 goto err_resolve_get_addr
;
996 if (cm_event
->event
!= RDMA_CM_EVENT_ROUTE_RESOLVED
) {
997 ERROR(errp
, "result not equal to event_route_resolved: %s",
998 rdma_event_str(cm_event
->event
));
999 rdma_ack_cm_event(cm_event
);
1001 goto err_resolve_get_addr
;
1003 rdma_ack_cm_event(cm_event
);
1004 rdma
->verbs
= rdma
->cm_id
->verbs
;
1005 qemu_rdma_dump_id("source_resolve_host", rdma
->cm_id
->verbs
);
1006 qemu_rdma_dump_gid("source_resolve_host", rdma
->cm_id
);
1009 err_resolve_get_addr
:
1010 rdma_destroy_id(rdma
->cm_id
);
1012 err_resolve_create_id
:
1013 rdma_destroy_event_channel(rdma
->channel
);
1014 rdma
->channel
= NULL
;
1019 * Create protection domain and completion queues
1021 static int qemu_rdma_alloc_pd_cq(RDMAContext
*rdma
)
1024 rdma
->pd
= ibv_alloc_pd(rdma
->verbs
);
1026 error_report("failed to allocate protection domain");
1030 /* create completion channel */
1031 rdma
->comp_channel
= ibv_create_comp_channel(rdma
->verbs
);
1032 if (!rdma
->comp_channel
) {
1033 error_report("failed to allocate completion channel");
1034 goto err_alloc_pd_cq
;
1038 * Completion queue can be filled by both read and write work requests,
1039 * so must reflect the sum of both possible queue sizes.
1041 rdma
->cq
= ibv_create_cq(rdma
->verbs
, (RDMA_SIGNALED_SEND_MAX
* 3),
1042 NULL
, rdma
->comp_channel
, 0);
1044 error_report("failed to allocate completion queue");
1045 goto err_alloc_pd_cq
;
1052 ibv_dealloc_pd(rdma
->pd
);
1054 if (rdma
->comp_channel
) {
1055 ibv_destroy_comp_channel(rdma
->comp_channel
);
1058 rdma
->comp_channel
= NULL
;
1064 * Create queue pairs.
1066 static int qemu_rdma_alloc_qp(RDMAContext
*rdma
)
1068 struct ibv_qp_init_attr attr
= { 0 };
1071 attr
.cap
.max_send_wr
= RDMA_SIGNALED_SEND_MAX
;
1072 attr
.cap
.max_recv_wr
= 3;
1073 attr
.cap
.max_send_sge
= 1;
1074 attr
.cap
.max_recv_sge
= 1;
1075 attr
.send_cq
= rdma
->cq
;
1076 attr
.recv_cq
= rdma
->cq
;
1077 attr
.qp_type
= IBV_QPT_RC
;
1079 ret
= rdma_create_qp(rdma
->cm_id
, rdma
->pd
, &attr
);
1084 rdma
->qp
= rdma
->cm_id
->qp
;
1088 static int qemu_rdma_reg_whole_ram_blocks(RDMAContext
*rdma
)
1091 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
1093 for (i
= 0; i
< local
->nb_blocks
; i
++) {
1094 local
->block
[i
].mr
=
1095 ibv_reg_mr(rdma
->pd
,
1096 local
->block
[i
].local_host_addr
,
1097 local
->block
[i
].length
,
1098 IBV_ACCESS_LOCAL_WRITE
|
1099 IBV_ACCESS_REMOTE_WRITE
1101 if (!local
->block
[i
].mr
) {
1102 perror("Failed to register local dest ram block!\n");
1105 rdma
->total_registrations
++;
1108 if (i
>= local
->nb_blocks
) {
1112 for (i
--; i
>= 0; i
--) {
1113 ibv_dereg_mr(local
->block
[i
].mr
);
1114 rdma
->total_registrations
--;
1122 * Find the ram block that corresponds to the page requested to be
1123 * transmitted by QEMU.
1125 * Once the block is found, also identify which 'chunk' within that
1126 * block that the page belongs to.
1128 * This search cannot fail or the migration will fail.
1130 static int qemu_rdma_search_ram_block(RDMAContext
*rdma
,
1131 uintptr_t block_offset
,
1134 uint64_t *block_index
,
1135 uint64_t *chunk_index
)
1137 uint64_t current_addr
= block_offset
+ offset
;
1138 RDMALocalBlock
*block
= g_hash_table_lookup(rdma
->blockmap
,
1139 (void *) block_offset
);
1141 assert(current_addr
>= block
->offset
);
1142 assert((current_addr
+ length
) <= (block
->offset
+ block
->length
));
1144 *block_index
= block
->index
;
1145 *chunk_index
= ram_chunk_index(block
->local_host_addr
,
1146 block
->local_host_addr
+ (current_addr
- block
->offset
));
1152 * Register a chunk with IB. If the chunk was already registered
1153 * previously, then skip.
1155 * Also return the keys associated with the registration needed
1156 * to perform the actual RDMA operation.
1158 static int qemu_rdma_register_and_get_keys(RDMAContext
*rdma
,
1159 RDMALocalBlock
*block
, uintptr_t host_addr
,
1160 uint32_t *lkey
, uint32_t *rkey
, int chunk
,
1161 uint8_t *chunk_start
, uint8_t *chunk_end
)
1165 *lkey
= block
->mr
->lkey
;
1168 *rkey
= block
->mr
->rkey
;
1173 /* allocate memory to store chunk MRs */
1175 block
->pmr
= g_new0(struct ibv_mr
*, block
->nb_chunks
);
1179 * If 'rkey', then we're the destination, so grant access to the source.
1181 * If 'lkey', then we're the source VM, so grant access only to ourselves.
1183 if (!block
->pmr
[chunk
]) {
1184 uint64_t len
= chunk_end
- chunk_start
;
1186 trace_qemu_rdma_register_and_get_keys(len
, chunk_start
);
1188 block
->pmr
[chunk
] = ibv_reg_mr(rdma
->pd
,
1190 (rkey
? (IBV_ACCESS_LOCAL_WRITE
|
1191 IBV_ACCESS_REMOTE_WRITE
) : 0));
1193 if (!block
->pmr
[chunk
]) {
1194 perror("Failed to register chunk!");
1195 fprintf(stderr
, "Chunk details: block: %d chunk index %d"
1196 " start %" PRIuPTR
" end %" PRIuPTR
1198 " local %" PRIuPTR
" registrations: %d\n",
1199 block
->index
, chunk
, (uintptr_t)chunk_start
,
1200 (uintptr_t)chunk_end
, host_addr
,
1201 (uintptr_t)block
->local_host_addr
,
1202 rdma
->total_registrations
);
1205 rdma
->total_registrations
++;
1209 *lkey
= block
->pmr
[chunk
]->lkey
;
1212 *rkey
= block
->pmr
[chunk
]->rkey
;
1218 * Register (at connection time) the memory used for control
1221 static int qemu_rdma_reg_control(RDMAContext
*rdma
, int idx
)
1223 rdma
->wr_data
[idx
].control_mr
= ibv_reg_mr(rdma
->pd
,
1224 rdma
->wr_data
[idx
].control
, RDMA_CONTROL_MAX_BUFFER
,
1225 IBV_ACCESS_LOCAL_WRITE
| IBV_ACCESS_REMOTE_WRITE
);
1226 if (rdma
->wr_data
[idx
].control_mr
) {
1227 rdma
->total_registrations
++;
1230 error_report("qemu_rdma_reg_control failed");
1234 const char *print_wrid(int wrid
)
1236 if (wrid
>= RDMA_WRID_RECV_CONTROL
) {
1237 return wrid_desc
[RDMA_WRID_RECV_CONTROL
];
1239 return wrid_desc
[wrid
];
1243 * RDMA requires memory registration (mlock/pinning), but this is not good for
1246 * In preparation for the future where LRU information or workload-specific
1247 * writable writable working set memory access behavior is available to QEMU
1248 * it would be nice to have in place the ability to UN-register/UN-pin
1249 * particular memory regions from the RDMA hardware when it is determine that
1250 * those regions of memory will likely not be accessed again in the near future.
1252 * While we do not yet have such information right now, the following
1253 * compile-time option allows us to perform a non-optimized version of this
1256 * By uncommenting this option, you will cause *all* RDMA transfers to be
1257 * unregistered immediately after the transfer completes on both sides of the
1258 * connection. This has no effect in 'rdma-pin-all' mode, only regular mode.
1260 * This will have a terrible impact on migration performance, so until future
1261 * workload information or LRU information is available, do not attempt to use
1262 * this feature except for basic testing.
1264 //#define RDMA_UNREGISTRATION_EXAMPLE
1267 * Perform a non-optimized memory unregistration after every transfer
1268 * for demonstration purposes, only if pin-all is not requested.
1270 * Potential optimizations:
1271 * 1. Start a new thread to run this function continuously
1273 - and for receipt of unregister messages
1275 * 3. Use workload hints.
1277 static int qemu_rdma_unregister_waiting(RDMAContext
*rdma
)
1279 while (rdma
->unregistrations
[rdma
->unregister_current
]) {
1281 uint64_t wr_id
= rdma
->unregistrations
[rdma
->unregister_current
];
1283 (wr_id
& RDMA_WRID_CHUNK_MASK
) >> RDMA_WRID_CHUNK_SHIFT
;
1285 (wr_id
& RDMA_WRID_BLOCK_MASK
) >> RDMA_WRID_BLOCK_SHIFT
;
1286 RDMALocalBlock
*block
=
1287 &(rdma
->local_ram_blocks
.block
[index
]);
1288 RDMARegister reg
= { .current_index
= index
};
1289 RDMAControlHeader resp
= { .type
= RDMA_CONTROL_UNREGISTER_FINISHED
,
1291 RDMAControlHeader head
= { .len
= sizeof(RDMARegister
),
1292 .type
= RDMA_CONTROL_UNREGISTER_REQUEST
,
1296 trace_qemu_rdma_unregister_waiting_proc(chunk
,
1297 rdma
->unregister_current
);
1299 rdma
->unregistrations
[rdma
->unregister_current
] = 0;
1300 rdma
->unregister_current
++;
1302 if (rdma
->unregister_current
== RDMA_SIGNALED_SEND_MAX
) {
1303 rdma
->unregister_current
= 0;
1308 * Unregistration is speculative (because migration is single-threaded
1309 * and we cannot break the protocol's inifinband message ordering).
1310 * Thus, if the memory is currently being used for transmission,
1311 * then abort the attempt to unregister and try again
1312 * later the next time a completion is received for this memory.
1314 clear_bit(chunk
, block
->unregister_bitmap
);
1316 if (test_bit(chunk
, block
->transit_bitmap
)) {
1317 trace_qemu_rdma_unregister_waiting_inflight(chunk
);
1321 trace_qemu_rdma_unregister_waiting_send(chunk
);
1323 ret
= ibv_dereg_mr(block
->pmr
[chunk
]);
1324 block
->pmr
[chunk
] = NULL
;
1325 block
->remote_keys
[chunk
] = 0;
1328 perror("unregistration chunk failed");
1331 rdma
->total_registrations
--;
1333 reg
.key
.chunk
= chunk
;
1334 register_to_network(rdma
, ®
);
1335 ret
= qemu_rdma_exchange_send(rdma
, &head
, (uint8_t *) ®
,
1341 trace_qemu_rdma_unregister_waiting_complete(chunk
);
1347 static uint64_t qemu_rdma_make_wrid(uint64_t wr_id
, uint64_t index
,
1350 uint64_t result
= wr_id
& RDMA_WRID_TYPE_MASK
;
1352 result
|= (index
<< RDMA_WRID_BLOCK_SHIFT
);
1353 result
|= (chunk
<< RDMA_WRID_CHUNK_SHIFT
);
1359 * Set bit for unregistration in the next iteration.
1360 * We cannot transmit right here, but will unpin later.
1362 static void qemu_rdma_signal_unregister(RDMAContext
*rdma
, uint64_t index
,
1363 uint64_t chunk
, uint64_t wr_id
)
1365 if (rdma
->unregistrations
[rdma
->unregister_next
] != 0) {
1366 error_report("rdma migration: queue is full");
1368 RDMALocalBlock
*block
= &(rdma
->local_ram_blocks
.block
[index
]);
1370 if (!test_and_set_bit(chunk
, block
->unregister_bitmap
)) {
1371 trace_qemu_rdma_signal_unregister_append(chunk
,
1372 rdma
->unregister_next
);
1374 rdma
->unregistrations
[rdma
->unregister_next
++] =
1375 qemu_rdma_make_wrid(wr_id
, index
, chunk
);
1377 if (rdma
->unregister_next
== RDMA_SIGNALED_SEND_MAX
) {
1378 rdma
->unregister_next
= 0;
1381 trace_qemu_rdma_signal_unregister_already(chunk
);
1387 * Consult the connection manager to see a work request
1388 * (of any kind) has completed.
1389 * Return the work request ID that completed.
1391 static uint64_t qemu_rdma_poll(RDMAContext
*rdma
, uint64_t *wr_id_out
,
1398 ret
= ibv_poll_cq(rdma
->cq
, 1, &wc
);
1401 *wr_id_out
= RDMA_WRID_NONE
;
1406 error_report("ibv_poll_cq return %d", ret
);
1410 wr_id
= wc
.wr_id
& RDMA_WRID_TYPE_MASK
;
1412 if (wc
.status
!= IBV_WC_SUCCESS
) {
1413 fprintf(stderr
, "ibv_poll_cq wc.status=%d %s!\n",
1414 wc
.status
, ibv_wc_status_str(wc
.status
));
1415 fprintf(stderr
, "ibv_poll_cq wrid=%s!\n", wrid_desc
[wr_id
]);
1420 if (rdma
->control_ready_expected
&&
1421 (wr_id
>= RDMA_WRID_RECV_CONTROL
)) {
1422 trace_qemu_rdma_poll_recv(wrid_desc
[RDMA_WRID_RECV_CONTROL
],
1423 wr_id
- RDMA_WRID_RECV_CONTROL
, wr_id
, rdma
->nb_sent
);
1424 rdma
->control_ready_expected
= 0;
1427 if (wr_id
== RDMA_WRID_RDMA_WRITE
) {
1429 (wc
.wr_id
& RDMA_WRID_CHUNK_MASK
) >> RDMA_WRID_CHUNK_SHIFT
;
1431 (wc
.wr_id
& RDMA_WRID_BLOCK_MASK
) >> RDMA_WRID_BLOCK_SHIFT
;
1432 RDMALocalBlock
*block
= &(rdma
->local_ram_blocks
.block
[index
]);
1434 trace_qemu_rdma_poll_write(print_wrid(wr_id
), wr_id
, rdma
->nb_sent
,
1435 index
, chunk
, block
->local_host_addr
,
1436 (void *)(uintptr_t)block
->remote_host_addr
);
1438 clear_bit(chunk
, block
->transit_bitmap
);
1440 if (rdma
->nb_sent
> 0) {
1444 if (!rdma
->pin_all
) {
1446 * FYI: If one wanted to signal a specific chunk to be unregistered
1447 * using LRU or workload-specific information, this is the function
1448 * you would call to do so. That chunk would then get asynchronously
1449 * unregistered later.
1451 #ifdef RDMA_UNREGISTRATION_EXAMPLE
1452 qemu_rdma_signal_unregister(rdma
, index
, chunk
, wc
.wr_id
);
1456 trace_qemu_rdma_poll_other(print_wrid(wr_id
), wr_id
, rdma
->nb_sent
);
1459 *wr_id_out
= wc
.wr_id
;
1461 *byte_len
= wc
.byte_len
;
1468 * Block until the next work request has completed.
1470 * First poll to see if a work request has already completed,
1473 * If we encounter completed work requests for IDs other than
1474 * the one we're interested in, then that's generally an error.
1476 * The only exception is actual RDMA Write completions. These
1477 * completions only need to be recorded, but do not actually
1478 * need further processing.
1480 static int qemu_rdma_block_for_wrid(RDMAContext
*rdma
, int wrid_requested
,
1483 int num_cq_events
= 0, ret
= 0;
1486 uint64_t wr_id
= RDMA_WRID_NONE
, wr_id_in
;
1488 if (ibv_req_notify_cq(rdma
->cq
, 0)) {
1492 while (wr_id
!= wrid_requested
) {
1493 ret
= qemu_rdma_poll(rdma
, &wr_id_in
, byte_len
);
1498 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
1500 if (wr_id
== RDMA_WRID_NONE
) {
1503 if (wr_id
!= wrid_requested
) {
1504 trace_qemu_rdma_block_for_wrid_miss(print_wrid(wrid_requested
),
1505 wrid_requested
, print_wrid(wr_id
), wr_id
);
1509 if (wr_id
== wrid_requested
) {
1515 * Coroutine doesn't start until migration_fd_process_incoming()
1516 * so don't yield unless we know we're running inside of a coroutine.
1518 if (rdma
->migration_started_on_destination
) {
1519 yield_until_fd_readable(rdma
->comp_channel
->fd
);
1522 if (ibv_get_cq_event(rdma
->comp_channel
, &cq
, &cq_ctx
)) {
1523 perror("ibv_get_cq_event");
1524 goto err_block_for_wrid
;
1529 if (ibv_req_notify_cq(cq
, 0)) {
1530 goto err_block_for_wrid
;
1533 while (wr_id
!= wrid_requested
) {
1534 ret
= qemu_rdma_poll(rdma
, &wr_id_in
, byte_len
);
1536 goto err_block_for_wrid
;
1539 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
1541 if (wr_id
== RDMA_WRID_NONE
) {
1544 if (wr_id
!= wrid_requested
) {
1545 trace_qemu_rdma_block_for_wrid_miss(print_wrid(wrid_requested
),
1546 wrid_requested
, print_wrid(wr_id
), wr_id
);
1550 if (wr_id
== wrid_requested
) {
1551 goto success_block_for_wrid
;
1555 success_block_for_wrid
:
1556 if (num_cq_events
) {
1557 ibv_ack_cq_events(cq
, num_cq_events
);
1562 if (num_cq_events
) {
1563 ibv_ack_cq_events(cq
, num_cq_events
);
1569 * Post a SEND message work request for the control channel
1570 * containing some data and block until the post completes.
1572 static int qemu_rdma_post_send_control(RDMAContext
*rdma
, uint8_t *buf
,
1573 RDMAControlHeader
*head
)
1576 RDMAWorkRequestData
*wr
= &rdma
->wr_data
[RDMA_WRID_CONTROL
];
1577 struct ibv_send_wr
*bad_wr
;
1578 struct ibv_sge sge
= {
1579 .addr
= (uintptr_t)(wr
->control
),
1580 .length
= head
->len
+ sizeof(RDMAControlHeader
),
1581 .lkey
= wr
->control_mr
->lkey
,
1583 struct ibv_send_wr send_wr
= {
1584 .wr_id
= RDMA_WRID_SEND_CONTROL
,
1585 .opcode
= IBV_WR_SEND
,
1586 .send_flags
= IBV_SEND_SIGNALED
,
1591 trace_qemu_rdma_post_send_control(control_desc
[head
->type
]);
1594 * We don't actually need to do a memcpy() in here if we used
1595 * the "sge" properly, but since we're only sending control messages
1596 * (not RAM in a performance-critical path), then its OK for now.
1598 * The copy makes the RDMAControlHeader simpler to manipulate
1599 * for the time being.
1601 assert(head
->len
<= RDMA_CONTROL_MAX_BUFFER
- sizeof(*head
));
1602 memcpy(wr
->control
, head
, sizeof(RDMAControlHeader
));
1603 control_to_network((void *) wr
->control
);
1606 memcpy(wr
->control
+ sizeof(RDMAControlHeader
), buf
, head
->len
);
1610 ret
= ibv_post_send(rdma
->qp
, &send_wr
, &bad_wr
);
1613 error_report("Failed to use post IB SEND for control");
1617 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_SEND_CONTROL
, NULL
);
1619 error_report("rdma migration: send polling control error");
1626 * Post a RECV work request in anticipation of some future receipt
1627 * of data on the control channel.
1629 static int qemu_rdma_post_recv_control(RDMAContext
*rdma
, int idx
)
1631 struct ibv_recv_wr
*bad_wr
;
1632 struct ibv_sge sge
= {
1633 .addr
= (uintptr_t)(rdma
->wr_data
[idx
].control
),
1634 .length
= RDMA_CONTROL_MAX_BUFFER
,
1635 .lkey
= rdma
->wr_data
[idx
].control_mr
->lkey
,
1638 struct ibv_recv_wr recv_wr
= {
1639 .wr_id
= RDMA_WRID_RECV_CONTROL
+ idx
,
1645 if (ibv_post_recv(rdma
->qp
, &recv_wr
, &bad_wr
)) {
1653 * Block and wait for a RECV control channel message to arrive.
1655 static int qemu_rdma_exchange_get_response(RDMAContext
*rdma
,
1656 RDMAControlHeader
*head
, int expecting
, int idx
)
1659 int ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RECV_CONTROL
+ idx
,
1663 error_report("rdma migration: recv polling control error!");
1667 network_to_control((void *) rdma
->wr_data
[idx
].control
);
1668 memcpy(head
, rdma
->wr_data
[idx
].control
, sizeof(RDMAControlHeader
));
1670 trace_qemu_rdma_exchange_get_response_start(control_desc
[expecting
]);
1672 if (expecting
== RDMA_CONTROL_NONE
) {
1673 trace_qemu_rdma_exchange_get_response_none(control_desc
[head
->type
],
1675 } else if (head
->type
!= expecting
|| head
->type
== RDMA_CONTROL_ERROR
) {
1676 error_report("Was expecting a %s (%d) control message"
1677 ", but got: %s (%d), length: %d",
1678 control_desc
[expecting
], expecting
,
1679 control_desc
[head
->type
], head
->type
, head
->len
);
1680 if (head
->type
== RDMA_CONTROL_ERROR
) {
1681 rdma
->received_error
= true;
1685 if (head
->len
> RDMA_CONTROL_MAX_BUFFER
- sizeof(*head
)) {
1686 error_report("too long length: %d", head
->len
);
1689 if (sizeof(*head
) + head
->len
!= byte_len
) {
1690 error_report("Malformed length: %d byte_len %d", head
->len
, byte_len
);
1698 * When a RECV work request has completed, the work request's
1699 * buffer is pointed at the header.
1701 * This will advance the pointer to the data portion
1702 * of the control message of the work request's buffer that
1703 * was populated after the work request finished.
1705 static void qemu_rdma_move_header(RDMAContext
*rdma
, int idx
,
1706 RDMAControlHeader
*head
)
1708 rdma
->wr_data
[idx
].control_len
= head
->len
;
1709 rdma
->wr_data
[idx
].control_curr
=
1710 rdma
->wr_data
[idx
].control
+ sizeof(RDMAControlHeader
);
1714 * This is an 'atomic' high-level operation to deliver a single, unified
1715 * control-channel message.
1717 * Additionally, if the user is expecting some kind of reply to this message,
1718 * they can request a 'resp' response message be filled in by posting an
1719 * additional work request on behalf of the user and waiting for an additional
1722 * The extra (optional) response is used during registration to us from having
1723 * to perform an *additional* exchange of message just to provide a response by
1724 * instead piggy-backing on the acknowledgement.
1726 static int qemu_rdma_exchange_send(RDMAContext
*rdma
, RDMAControlHeader
*head
,
1727 uint8_t *data
, RDMAControlHeader
*resp
,
1729 int (*callback
)(RDMAContext
*rdma
))
1734 * Wait until the dest is ready before attempting to deliver the message
1735 * by waiting for a READY message.
1737 if (rdma
->control_ready_expected
) {
1738 RDMAControlHeader resp
;
1739 ret
= qemu_rdma_exchange_get_response(rdma
,
1740 &resp
, RDMA_CONTROL_READY
, RDMA_WRID_READY
);
1747 * If the user is expecting a response, post a WR in anticipation of it.
1750 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_DATA
);
1752 error_report("rdma migration: error posting"
1753 " extra control recv for anticipated result!");
1759 * Post a WR to replace the one we just consumed for the READY message.
1761 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
1763 error_report("rdma migration: error posting first control recv!");
1768 * Deliver the control message that was requested.
1770 ret
= qemu_rdma_post_send_control(rdma
, data
, head
);
1773 error_report("Failed to send control buffer!");
1778 * If we're expecting a response, block and wait for it.
1782 trace_qemu_rdma_exchange_send_issue_callback();
1783 ret
= callback(rdma
);
1789 trace_qemu_rdma_exchange_send_waiting(control_desc
[resp
->type
]);
1790 ret
= qemu_rdma_exchange_get_response(rdma
, resp
,
1791 resp
->type
, RDMA_WRID_DATA
);
1797 qemu_rdma_move_header(rdma
, RDMA_WRID_DATA
, resp
);
1799 *resp_idx
= RDMA_WRID_DATA
;
1801 trace_qemu_rdma_exchange_send_received(control_desc
[resp
->type
]);
1804 rdma
->control_ready_expected
= 1;
1810 * This is an 'atomic' high-level operation to receive a single, unified
1811 * control-channel message.
1813 static int qemu_rdma_exchange_recv(RDMAContext
*rdma
, RDMAControlHeader
*head
,
1816 RDMAControlHeader ready
= {
1818 .type
= RDMA_CONTROL_READY
,
1824 * Inform the source that we're ready to receive a message.
1826 ret
= qemu_rdma_post_send_control(rdma
, NULL
, &ready
);
1829 error_report("Failed to send control buffer!");
1834 * Block and wait for the message.
1836 ret
= qemu_rdma_exchange_get_response(rdma
, head
,
1837 expecting
, RDMA_WRID_READY
);
1843 qemu_rdma_move_header(rdma
, RDMA_WRID_READY
, head
);
1846 * Post a new RECV work request to replace the one we just consumed.
1848 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
1850 error_report("rdma migration: error posting second control recv!");
1858 * Write an actual chunk of memory using RDMA.
1860 * If we're using dynamic registration on the dest-side, we have to
1861 * send a registration command first.
1863 static int qemu_rdma_write_one(QEMUFile
*f
, RDMAContext
*rdma
,
1864 int current_index
, uint64_t current_addr
,
1868 struct ibv_send_wr send_wr
= { 0 };
1869 struct ibv_send_wr
*bad_wr
;
1870 int reg_result_idx
, ret
, count
= 0;
1871 uint64_t chunk
, chunks
;
1872 uint8_t *chunk_start
, *chunk_end
;
1873 RDMALocalBlock
*block
= &(rdma
->local_ram_blocks
.block
[current_index
]);
1875 RDMARegisterResult
*reg_result
;
1876 RDMAControlHeader resp
= { .type
= RDMA_CONTROL_REGISTER_RESULT
};
1877 RDMAControlHeader head
= { .len
= sizeof(RDMARegister
),
1878 .type
= RDMA_CONTROL_REGISTER_REQUEST
,
1883 sge
.addr
= (uintptr_t)(block
->local_host_addr
+
1884 (current_addr
- block
->offset
));
1885 sge
.length
= length
;
1887 chunk
= ram_chunk_index(block
->local_host_addr
,
1888 (uint8_t *)(uintptr_t)sge
.addr
);
1889 chunk_start
= ram_chunk_start(block
, chunk
);
1891 if (block
->is_ram_block
) {
1892 chunks
= length
/ (1UL << RDMA_REG_CHUNK_SHIFT
);
1894 if (chunks
&& ((length
% (1UL << RDMA_REG_CHUNK_SHIFT
)) == 0)) {
1898 chunks
= block
->length
/ (1UL << RDMA_REG_CHUNK_SHIFT
);
1900 if (chunks
&& ((block
->length
% (1UL << RDMA_REG_CHUNK_SHIFT
)) == 0)) {
1905 trace_qemu_rdma_write_one_top(chunks
+ 1,
1907 (1UL << RDMA_REG_CHUNK_SHIFT
) / 1024 / 1024);
1909 chunk_end
= ram_chunk_end(block
, chunk
+ chunks
);
1911 if (!rdma
->pin_all
) {
1912 #ifdef RDMA_UNREGISTRATION_EXAMPLE
1913 qemu_rdma_unregister_waiting(rdma
);
1917 while (test_bit(chunk
, block
->transit_bitmap
)) {
1919 trace_qemu_rdma_write_one_block(count
++, current_index
, chunk
,
1920 sge
.addr
, length
, rdma
->nb_sent
, block
->nb_chunks
);
1922 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RDMA_WRITE
, NULL
);
1925 error_report("Failed to Wait for previous write to complete "
1926 "block %d chunk %" PRIu64
1927 " current %" PRIu64
" len %" PRIu64
" %d",
1928 current_index
, chunk
, sge
.addr
, length
, rdma
->nb_sent
);
1933 if (!rdma
->pin_all
|| !block
->is_ram_block
) {
1934 if (!block
->remote_keys
[chunk
]) {
1936 * This chunk has not yet been registered, so first check to see
1937 * if the entire chunk is zero. If so, tell the other size to
1938 * memset() + madvise() the entire chunk without RDMA.
1941 if (buffer_is_zero((void *)(uintptr_t)sge
.addr
, length
)) {
1942 RDMACompress comp
= {
1943 .offset
= current_addr
,
1945 .block_idx
= current_index
,
1949 head
.len
= sizeof(comp
);
1950 head
.type
= RDMA_CONTROL_COMPRESS
;
1952 trace_qemu_rdma_write_one_zero(chunk
, sge
.length
,
1953 current_index
, current_addr
);
1955 compress_to_network(rdma
, &comp
);
1956 ret
= qemu_rdma_exchange_send(rdma
, &head
,
1957 (uint8_t *) &comp
, NULL
, NULL
, NULL
);
1963 acct_update_position(f
, sge
.length
, true);
1969 * Otherwise, tell other side to register.
1971 reg
.current_index
= current_index
;
1972 if (block
->is_ram_block
) {
1973 reg
.key
.current_addr
= current_addr
;
1975 reg
.key
.chunk
= chunk
;
1977 reg
.chunks
= chunks
;
1979 trace_qemu_rdma_write_one_sendreg(chunk
, sge
.length
, current_index
,
1982 register_to_network(rdma
, ®
);
1983 ret
= qemu_rdma_exchange_send(rdma
, &head
, (uint8_t *) ®
,
1984 &resp
, ®_result_idx
, NULL
);
1989 /* try to overlap this single registration with the one we sent. */
1990 if (qemu_rdma_register_and_get_keys(rdma
, block
, sge
.addr
,
1991 &sge
.lkey
, NULL
, chunk
,
1992 chunk_start
, chunk_end
)) {
1993 error_report("cannot get lkey");
1997 reg_result
= (RDMARegisterResult
*)
1998 rdma
->wr_data
[reg_result_idx
].control_curr
;
2000 network_to_result(reg_result
);
2002 trace_qemu_rdma_write_one_recvregres(block
->remote_keys
[chunk
],
2003 reg_result
->rkey
, chunk
);
2005 block
->remote_keys
[chunk
] = reg_result
->rkey
;
2006 block
->remote_host_addr
= reg_result
->host_addr
;
2008 /* already registered before */
2009 if (qemu_rdma_register_and_get_keys(rdma
, block
, sge
.addr
,
2010 &sge
.lkey
, NULL
, chunk
,
2011 chunk_start
, chunk_end
)) {
2012 error_report("cannot get lkey!");
2017 send_wr
.wr
.rdma
.rkey
= block
->remote_keys
[chunk
];
2019 send_wr
.wr
.rdma
.rkey
= block
->remote_rkey
;
2021 if (qemu_rdma_register_and_get_keys(rdma
, block
, sge
.addr
,
2022 &sge
.lkey
, NULL
, chunk
,
2023 chunk_start
, chunk_end
)) {
2024 error_report("cannot get lkey!");
2030 * Encode the ram block index and chunk within this wrid.
2031 * We will use this information at the time of completion
2032 * to figure out which bitmap to check against and then which
2033 * chunk in the bitmap to look for.
2035 send_wr
.wr_id
= qemu_rdma_make_wrid(RDMA_WRID_RDMA_WRITE
,
2036 current_index
, chunk
);
2038 send_wr
.opcode
= IBV_WR_RDMA_WRITE
;
2039 send_wr
.send_flags
= IBV_SEND_SIGNALED
;
2040 send_wr
.sg_list
= &sge
;
2041 send_wr
.num_sge
= 1;
2042 send_wr
.wr
.rdma
.remote_addr
= block
->remote_host_addr
+
2043 (current_addr
- block
->offset
);
2045 trace_qemu_rdma_write_one_post(chunk
, sge
.addr
, send_wr
.wr
.rdma
.remote_addr
,
2049 * ibv_post_send() does not return negative error numbers,
2050 * per the specification they are positive - no idea why.
2052 ret
= ibv_post_send(rdma
->qp
, &send_wr
, &bad_wr
);
2054 if (ret
== ENOMEM
) {
2055 trace_qemu_rdma_write_one_queue_full();
2056 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RDMA_WRITE
, NULL
);
2058 error_report("rdma migration: failed to make "
2059 "room in full send queue! %d", ret
);
2065 } else if (ret
> 0) {
2066 perror("rdma migration: post rdma write failed");
2070 set_bit(chunk
, block
->transit_bitmap
);
2071 acct_update_position(f
, sge
.length
, false);
2072 rdma
->total_writes
++;
2078 * Push out any unwritten RDMA operations.
2080 * We support sending out multiple chunks at the same time.
2081 * Not all of them need to get signaled in the completion queue.
2083 static int qemu_rdma_write_flush(QEMUFile
*f
, RDMAContext
*rdma
)
2087 if (!rdma
->current_length
) {
2091 ret
= qemu_rdma_write_one(f
, rdma
,
2092 rdma
->current_index
, rdma
->current_addr
, rdma
->current_length
);
2100 trace_qemu_rdma_write_flush(rdma
->nb_sent
);
2103 rdma
->current_length
= 0;
2104 rdma
->current_addr
= 0;
2109 static inline int qemu_rdma_buffer_mergable(RDMAContext
*rdma
,
2110 uint64_t offset
, uint64_t len
)
2112 RDMALocalBlock
*block
;
2116 if (rdma
->current_index
< 0) {
2120 if (rdma
->current_chunk
< 0) {
2124 block
= &(rdma
->local_ram_blocks
.block
[rdma
->current_index
]);
2125 host_addr
= block
->local_host_addr
+ (offset
- block
->offset
);
2126 chunk_end
= ram_chunk_end(block
, rdma
->current_chunk
);
2128 if (rdma
->current_length
== 0) {
2133 * Only merge into chunk sequentially.
2135 if (offset
!= (rdma
->current_addr
+ rdma
->current_length
)) {
2139 if (offset
< block
->offset
) {
2143 if ((offset
+ len
) > (block
->offset
+ block
->length
)) {
2147 if ((host_addr
+ len
) > chunk_end
) {
2155 * We're not actually writing here, but doing three things:
2157 * 1. Identify the chunk the buffer belongs to.
2158 * 2. If the chunk is full or the buffer doesn't belong to the current
2159 * chunk, then start a new chunk and flush() the old chunk.
2160 * 3. To keep the hardware busy, we also group chunks into batches
2161 * and only require that a batch gets acknowledged in the completion
2162 * qeueue instead of each individual chunk.
2164 static int qemu_rdma_write(QEMUFile
*f
, RDMAContext
*rdma
,
2165 uint64_t block_offset
, uint64_t offset
,
2168 uint64_t current_addr
= block_offset
+ offset
;
2169 uint64_t index
= rdma
->current_index
;
2170 uint64_t chunk
= rdma
->current_chunk
;
2173 /* If we cannot merge it, we flush the current buffer first. */
2174 if (!qemu_rdma_buffer_mergable(rdma
, current_addr
, len
)) {
2175 ret
= qemu_rdma_write_flush(f
, rdma
);
2179 rdma
->current_length
= 0;
2180 rdma
->current_addr
= current_addr
;
2182 ret
= qemu_rdma_search_ram_block(rdma
, block_offset
,
2183 offset
, len
, &index
, &chunk
);
2185 error_report("ram block search failed");
2188 rdma
->current_index
= index
;
2189 rdma
->current_chunk
= chunk
;
2193 rdma
->current_length
+= len
;
2195 /* flush it if buffer is too large */
2196 if (rdma
->current_length
>= RDMA_MERGE_MAX
) {
2197 return qemu_rdma_write_flush(f
, rdma
);
2203 static void qemu_rdma_cleanup(RDMAContext
*rdma
)
2205 struct rdma_cm_event
*cm_event
;
2208 if (rdma
->cm_id
&& rdma
->connected
) {
2209 if (rdma
->error_state
&& !rdma
->received_error
) {
2210 RDMAControlHeader head
= { .len
= 0,
2211 .type
= RDMA_CONTROL_ERROR
,
2214 error_report("Early error. Sending error.");
2215 qemu_rdma_post_send_control(rdma
, NULL
, &head
);
2218 ret
= rdma_disconnect(rdma
->cm_id
);
2220 trace_qemu_rdma_cleanup_waiting_for_disconnect();
2221 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
2223 rdma_ack_cm_event(cm_event
);
2226 trace_qemu_rdma_cleanup_disconnect();
2227 rdma
->connected
= false;
2230 g_free(rdma
->dest_blocks
);
2231 rdma
->dest_blocks
= NULL
;
2233 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2234 if (rdma
->wr_data
[idx
].control_mr
) {
2235 rdma
->total_registrations
--;
2236 ibv_dereg_mr(rdma
->wr_data
[idx
].control_mr
);
2238 rdma
->wr_data
[idx
].control_mr
= NULL
;
2241 if (rdma
->local_ram_blocks
.block
) {
2242 while (rdma
->local_ram_blocks
.nb_blocks
) {
2243 rdma_delete_block(rdma
, &rdma
->local_ram_blocks
.block
[0]);
2248 rdma_destroy_qp(rdma
->cm_id
);
2252 ibv_destroy_cq(rdma
->cq
);
2255 if (rdma
->comp_channel
) {
2256 ibv_destroy_comp_channel(rdma
->comp_channel
);
2257 rdma
->comp_channel
= NULL
;
2260 ibv_dealloc_pd(rdma
->pd
);
2264 rdma_destroy_id(rdma
->cm_id
);
2267 if (rdma
->listen_id
) {
2268 rdma_destroy_id(rdma
->listen_id
);
2269 rdma
->listen_id
= NULL
;
2271 if (rdma
->channel
) {
2272 rdma_destroy_event_channel(rdma
->channel
);
2273 rdma
->channel
= NULL
;
2280 static int qemu_rdma_source_init(RDMAContext
*rdma
, Error
**errp
, bool pin_all
)
2283 Error
*local_err
= NULL
, **temp
= &local_err
;
2286 * Will be validated against destination's actual capabilities
2287 * after the connect() completes.
2289 rdma
->pin_all
= pin_all
;
2291 ret
= qemu_rdma_resolve_host(rdma
, temp
);
2293 goto err_rdma_source_init
;
2296 ret
= qemu_rdma_alloc_pd_cq(rdma
);
2298 ERROR(temp
, "rdma migration: error allocating pd and cq! Your mlock()"
2299 " limits may be too low. Please check $ ulimit -a # and "
2300 "search for 'ulimit -l' in the output");
2301 goto err_rdma_source_init
;
2304 ret
= qemu_rdma_alloc_qp(rdma
);
2306 ERROR(temp
, "rdma migration: error allocating qp!");
2307 goto err_rdma_source_init
;
2310 ret
= qemu_rdma_init_ram_blocks(rdma
);
2312 ERROR(temp
, "rdma migration: error initializing ram blocks!");
2313 goto err_rdma_source_init
;
2316 /* Build the hash that maps from offset to RAMBlock */
2317 rdma
->blockmap
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
2318 for (idx
= 0; idx
< rdma
->local_ram_blocks
.nb_blocks
; idx
++) {
2319 g_hash_table_insert(rdma
->blockmap
,
2320 (void *)(uintptr_t)rdma
->local_ram_blocks
.block
[idx
].offset
,
2321 &rdma
->local_ram_blocks
.block
[idx
]);
2324 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2325 ret
= qemu_rdma_reg_control(rdma
, idx
);
2327 ERROR(temp
, "rdma migration: error registering %d control!",
2329 goto err_rdma_source_init
;
2335 err_rdma_source_init
:
2336 error_propagate(errp
, local_err
);
2337 qemu_rdma_cleanup(rdma
);
2341 static int qemu_rdma_connect(RDMAContext
*rdma
, Error
**errp
)
2343 RDMACapabilities cap
= {
2344 .version
= RDMA_CONTROL_VERSION_CURRENT
,
2347 struct rdma_conn_param conn_param
= { .initiator_depth
= 2,
2349 .private_data
= &cap
,
2350 .private_data_len
= sizeof(cap
),
2352 struct rdma_cm_event
*cm_event
;
2356 * Only negotiate the capability with destination if the user
2357 * on the source first requested the capability.
2359 if (rdma
->pin_all
) {
2360 trace_qemu_rdma_connect_pin_all_requested();
2361 cap
.flags
|= RDMA_CAPABILITY_PIN_ALL
;
2364 caps_to_network(&cap
);
2366 ret
= rdma_connect(rdma
->cm_id
, &conn_param
);
2368 perror("rdma_connect");
2369 ERROR(errp
, "connecting to destination!");
2370 goto err_rdma_source_connect
;
2373 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
2375 perror("rdma_get_cm_event after rdma_connect");
2376 ERROR(errp
, "connecting to destination!");
2377 rdma_ack_cm_event(cm_event
);
2378 goto err_rdma_source_connect
;
2381 if (cm_event
->event
!= RDMA_CM_EVENT_ESTABLISHED
) {
2382 perror("rdma_get_cm_event != EVENT_ESTABLISHED after rdma_connect");
2383 ERROR(errp
, "connecting to destination!");
2384 rdma_ack_cm_event(cm_event
);
2385 goto err_rdma_source_connect
;
2387 rdma
->connected
= true;
2389 memcpy(&cap
, cm_event
->param
.conn
.private_data
, sizeof(cap
));
2390 network_to_caps(&cap
);
2393 * Verify that the *requested* capabilities are supported by the destination
2394 * and disable them otherwise.
2396 if (rdma
->pin_all
&& !(cap
.flags
& RDMA_CAPABILITY_PIN_ALL
)) {
2397 ERROR(errp
, "Server cannot support pinning all memory. "
2398 "Will register memory dynamically.");
2399 rdma
->pin_all
= false;
2402 trace_qemu_rdma_connect_pin_all_outcome(rdma
->pin_all
);
2404 rdma_ack_cm_event(cm_event
);
2406 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
2408 ERROR(errp
, "posting second control recv!");
2409 goto err_rdma_source_connect
;
2412 rdma
->control_ready_expected
= 1;
2416 err_rdma_source_connect
:
2417 qemu_rdma_cleanup(rdma
);
2421 static int qemu_rdma_dest_init(RDMAContext
*rdma
, Error
**errp
)
2424 struct rdma_cm_id
*listen_id
;
2425 char ip
[40] = "unknown";
2426 struct rdma_addrinfo
*res
, *e
;
2429 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2430 rdma
->wr_data
[idx
].control_len
= 0;
2431 rdma
->wr_data
[idx
].control_curr
= NULL
;
2434 if (!rdma
->host
|| !rdma
->host
[0]) {
2435 ERROR(errp
, "RDMA host is not set!");
2436 rdma
->error_state
= -EINVAL
;
2439 /* create CM channel */
2440 rdma
->channel
= rdma_create_event_channel();
2441 if (!rdma
->channel
) {
2442 ERROR(errp
, "could not create rdma event channel");
2443 rdma
->error_state
= -EINVAL
;
2448 ret
= rdma_create_id(rdma
->channel
, &listen_id
, NULL
, RDMA_PS_TCP
);
2450 ERROR(errp
, "could not create cm_id!");
2451 goto err_dest_init_create_listen_id
;
2454 snprintf(port_str
, 16, "%d", rdma
->port
);
2455 port_str
[15] = '\0';
2457 ret
= rdma_getaddrinfo(rdma
->host
, port_str
, NULL
, &res
);
2459 ERROR(errp
, "could not rdma_getaddrinfo address %s", rdma
->host
);
2460 goto err_dest_init_bind_addr
;
2463 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
2464 inet_ntop(e
->ai_family
,
2465 &((struct sockaddr_in
*) e
->ai_dst_addr
)->sin_addr
, ip
, sizeof ip
);
2466 trace_qemu_rdma_dest_init_trying(rdma
->host
, ip
);
2467 ret
= rdma_bind_addr(listen_id
, e
->ai_dst_addr
);
2471 if (e
->ai_family
== AF_INET6
) {
2472 ret
= qemu_rdma_broken_ipv6_kernel(errp
, listen_id
->verbs
);
2481 ERROR(errp
, "Error: could not rdma_bind_addr!");
2482 goto err_dest_init_bind_addr
;
2485 rdma
->listen_id
= listen_id
;
2486 qemu_rdma_dump_gid("dest_init", listen_id
);
2489 err_dest_init_bind_addr
:
2490 rdma_destroy_id(listen_id
);
2491 err_dest_init_create_listen_id
:
2492 rdma_destroy_event_channel(rdma
->channel
);
2493 rdma
->channel
= NULL
;
2494 rdma
->error_state
= ret
;
2499 static void *qemu_rdma_data_init(const char *host_port
, Error
**errp
)
2501 RDMAContext
*rdma
= NULL
;
2502 InetSocketAddress
*addr
;
2505 rdma
= g_new0(RDMAContext
, 1);
2506 rdma
->current_index
= -1;
2507 rdma
->current_chunk
= -1;
2509 addr
= inet_parse(host_port
, NULL
);
2511 rdma
->port
= atoi(addr
->port
);
2512 rdma
->host
= g_strdup(addr
->host
);
2514 ERROR(errp
, "bad RDMA migration address '%s'", host_port
);
2519 qapi_free_InetSocketAddress(addr
);
2526 * QEMUFile interface to the control channel.
2527 * SEND messages for control only.
2528 * VM's ram is handled with regular RDMA messages.
2530 static ssize_t
qio_channel_rdma_writev(QIOChannel
*ioc
,
2531 const struct iovec
*iov
,
2537 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
2538 QEMUFile
*f
= rioc
->file
;
2539 RDMAContext
*rdma
= rioc
->rdma
;
2544 CHECK_ERROR_STATE();
2547 * Push out any writes that
2548 * we're queued up for VM's ram.
2550 ret
= qemu_rdma_write_flush(f
, rdma
);
2552 rdma
->error_state
= ret
;
2556 for (i
= 0; i
< niov
; i
++) {
2557 size_t remaining
= iov
[i
].iov_len
;
2558 uint8_t * data
= (void *)iov
[i
].iov_base
;
2560 RDMAControlHeader head
;
2562 rioc
->len
= MIN(remaining
, RDMA_SEND_INCREMENT
);
2563 remaining
-= rioc
->len
;
2565 head
.len
= rioc
->len
;
2566 head
.type
= RDMA_CONTROL_QEMU_FILE
;
2568 ret
= qemu_rdma_exchange_send(rdma
, &head
, data
, NULL
, NULL
, NULL
);
2571 rdma
->error_state
= ret
;
2583 static size_t qemu_rdma_fill(RDMAContext
*rdma
, uint8_t *buf
,
2584 size_t size
, int idx
)
2588 if (rdma
->wr_data
[idx
].control_len
) {
2589 trace_qemu_rdma_fill(rdma
->wr_data
[idx
].control_len
, size
);
2591 len
= MIN(size
, rdma
->wr_data
[idx
].control_len
);
2592 memcpy(buf
, rdma
->wr_data
[idx
].control_curr
, len
);
2593 rdma
->wr_data
[idx
].control_curr
+= len
;
2594 rdma
->wr_data
[idx
].control_len
-= len
;
2601 * QEMUFile interface to the control channel.
2602 * RDMA links don't use bytestreams, so we have to
2603 * return bytes to QEMUFile opportunistically.
2605 static ssize_t
qio_channel_rdma_readv(QIOChannel
*ioc
,
2606 const struct iovec
*iov
,
2612 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
2613 RDMAContext
*rdma
= rioc
->rdma
;
2614 RDMAControlHeader head
;
2619 CHECK_ERROR_STATE();
2621 for (i
= 0; i
< niov
; i
++) {
2622 size_t want
= iov
[i
].iov_len
;
2623 uint8_t *data
= (void *)iov
[i
].iov_base
;
2626 * First, we hold on to the last SEND message we
2627 * were given and dish out the bytes until we run
2630 ret
= qemu_rdma_fill(rioc
->rdma
, data
, want
, 0);
2633 /* Got what we needed, so go to next iovec */
2638 /* If we got any data so far, then don't wait
2639 * for more, just return what we have */
2645 /* We've got nothing at all, so lets wait for
2648 ret
= qemu_rdma_exchange_recv(rdma
, &head
, RDMA_CONTROL_QEMU_FILE
);
2651 rdma
->error_state
= ret
;
2656 * SEND was received with new bytes, now try again.
2658 ret
= qemu_rdma_fill(rioc
->rdma
, data
, want
, 0);
2662 /* Still didn't get enough, so lets just return */
2665 return QIO_CHANNEL_ERR_BLOCK
;
2676 * Block until all the outstanding chunks have been delivered by the hardware.
2678 static int qemu_rdma_drain_cq(QEMUFile
*f
, RDMAContext
*rdma
)
2682 if (qemu_rdma_write_flush(f
, rdma
) < 0) {
2686 while (rdma
->nb_sent
) {
2687 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RDMA_WRITE
, NULL
);
2689 error_report("rdma migration: complete polling error!");
2694 qemu_rdma_unregister_waiting(rdma
);
2700 static int qio_channel_rdma_set_blocking(QIOChannel
*ioc
,
2704 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
2705 /* XXX we should make readv/writev actually honour this :-) */
2706 rioc
->blocking
= blocking
;
2711 typedef struct QIOChannelRDMASource QIOChannelRDMASource
;
2712 struct QIOChannelRDMASource
{
2714 QIOChannelRDMA
*rioc
;
2715 GIOCondition condition
;
2719 qio_channel_rdma_source_prepare(GSource
*source
,
2722 QIOChannelRDMASource
*rsource
= (QIOChannelRDMASource
*)source
;
2723 RDMAContext
*rdma
= rsource
->rioc
->rdma
;
2724 GIOCondition cond
= 0;
2727 if (rdma
->wr_data
[0].control_len
) {
2732 return cond
& rsource
->condition
;
2736 qio_channel_rdma_source_check(GSource
*source
)
2738 QIOChannelRDMASource
*rsource
= (QIOChannelRDMASource
*)source
;
2739 RDMAContext
*rdma
= rsource
->rioc
->rdma
;
2740 GIOCondition cond
= 0;
2742 if (rdma
->wr_data
[0].control_len
) {
2747 return cond
& rsource
->condition
;
2751 qio_channel_rdma_source_dispatch(GSource
*source
,
2752 GSourceFunc callback
,
2755 QIOChannelFunc func
= (QIOChannelFunc
)callback
;
2756 QIOChannelRDMASource
*rsource
= (QIOChannelRDMASource
*)source
;
2757 RDMAContext
*rdma
= rsource
->rioc
->rdma
;
2758 GIOCondition cond
= 0;
2760 if (rdma
->wr_data
[0].control_len
) {
2765 return (*func
)(QIO_CHANNEL(rsource
->rioc
),
2766 (cond
& rsource
->condition
),
2771 qio_channel_rdma_source_finalize(GSource
*source
)
2773 QIOChannelRDMASource
*ssource
= (QIOChannelRDMASource
*)source
;
2775 object_unref(OBJECT(ssource
->rioc
));
2778 GSourceFuncs qio_channel_rdma_source_funcs
= {
2779 qio_channel_rdma_source_prepare
,
2780 qio_channel_rdma_source_check
,
2781 qio_channel_rdma_source_dispatch
,
2782 qio_channel_rdma_source_finalize
2785 static GSource
*qio_channel_rdma_create_watch(QIOChannel
*ioc
,
2786 GIOCondition condition
)
2788 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
2789 QIOChannelRDMASource
*ssource
;
2792 source
= g_source_new(&qio_channel_rdma_source_funcs
,
2793 sizeof(QIOChannelRDMASource
));
2794 ssource
= (QIOChannelRDMASource
*)source
;
2796 ssource
->rioc
= rioc
;
2797 object_ref(OBJECT(rioc
));
2799 ssource
->condition
= condition
;
2805 static int qio_channel_rdma_close(QIOChannel
*ioc
,
2808 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(ioc
);
2809 trace_qemu_rdma_close();
2811 if (!rioc
->rdma
->error_state
) {
2812 rioc
->rdma
->error_state
= qemu_file_get_error(rioc
->file
);
2814 qemu_rdma_cleanup(rioc
->rdma
);
2824 * This means that 'block_offset' is a full virtual address that does not
2825 * belong to a RAMBlock of the virtual machine and instead
2826 * represents a private malloc'd memory area that the caller wishes to
2830 * Offset is an offset to be added to block_offset and used
2831 * to also lookup the corresponding RAMBlock.
2834 * Initiate an transfer this size.
2837 * A 'hint' or 'advice' that means that we wish to speculatively
2838 * and asynchronously unregister this memory. In this case, there is no
2839 * guarantee that the unregister will actually happen, for example,
2840 * if the memory is being actively transmitted. Additionally, the memory
2841 * may be re-registered at any future time if a write within the same
2842 * chunk was requested again, even if you attempted to unregister it
2845 * @size < 0 : TODO, not yet supported
2846 * Unregister the memory NOW. This means that the caller does not
2847 * expect there to be any future RDMA transfers and we just want to clean
2848 * things up. This is used in case the upper layer owns the memory and
2849 * cannot wait for qemu_fclose() to occur.
2851 * @bytes_sent : User-specificed pointer to indicate how many bytes were
2852 * sent. Usually, this will not be more than a few bytes of
2853 * the protocol because most transfers are sent asynchronously.
2855 static size_t qemu_rdma_save_page(QEMUFile
*f
, void *opaque
,
2856 ram_addr_t block_offset
, ram_addr_t offset
,
2857 size_t size
, uint64_t *bytes_sent
)
2859 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(opaque
);
2860 RDMAContext
*rdma
= rioc
->rdma
;
2863 CHECK_ERROR_STATE();
2869 * Add this page to the current 'chunk'. If the chunk
2870 * is full, or the page doen't belong to the current chunk,
2871 * an actual RDMA write will occur and a new chunk will be formed.
2873 ret
= qemu_rdma_write(f
, rdma
, block_offset
, offset
, size
);
2875 error_report("rdma migration: write error! %d", ret
);
2880 * We always return 1 bytes because the RDMA
2881 * protocol is completely asynchronous. We do not yet know
2882 * whether an identified chunk is zero or not because we're
2883 * waiting for other pages to potentially be merged with
2884 * the current chunk. So, we have to call qemu_update_position()
2885 * later on when the actual write occurs.
2891 uint64_t index
, chunk
;
2893 /* TODO: Change QEMUFileOps prototype to be signed: size_t => long
2895 ret = qemu_rdma_drain_cq(f, rdma);
2897 fprintf(stderr, "rdma: failed to synchronously drain"
2898 " completion queue before unregistration.\n");
2904 ret
= qemu_rdma_search_ram_block(rdma
, block_offset
,
2905 offset
, size
, &index
, &chunk
);
2908 error_report("ram block search failed");
2912 qemu_rdma_signal_unregister(rdma
, index
, chunk
, 0);
2915 * TODO: Synchronous, guaranteed unregistration (should not occur during
2916 * fast-path). Otherwise, unregisters will process on the next call to
2917 * qemu_rdma_drain_cq()
2919 qemu_rdma_unregister_waiting(rdma);
2925 * Drain the Completion Queue if possible, but do not block,
2928 * If nothing to poll, the end of the iteration will do this
2929 * again to make sure we don't overflow the request queue.
2932 uint64_t wr_id
, wr_id_in
;
2933 int ret
= qemu_rdma_poll(rdma
, &wr_id_in
, NULL
);
2935 error_report("rdma migration: polling error! %d", ret
);
2939 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
2941 if (wr_id
== RDMA_WRID_NONE
) {
2946 return RAM_SAVE_CONTROL_DELAYED
;
2948 rdma
->error_state
= ret
;
2952 static int qemu_rdma_accept(RDMAContext
*rdma
)
2954 RDMACapabilities cap
;
2955 struct rdma_conn_param conn_param
= {
2956 .responder_resources
= 2,
2957 .private_data
= &cap
,
2958 .private_data_len
= sizeof(cap
),
2960 struct rdma_cm_event
*cm_event
;
2961 struct ibv_context
*verbs
;
2965 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
2967 goto err_rdma_dest_wait
;
2970 if (cm_event
->event
!= RDMA_CM_EVENT_CONNECT_REQUEST
) {
2971 rdma_ack_cm_event(cm_event
);
2972 goto err_rdma_dest_wait
;
2975 memcpy(&cap
, cm_event
->param
.conn
.private_data
, sizeof(cap
));
2977 network_to_caps(&cap
);
2979 if (cap
.version
< 1 || cap
.version
> RDMA_CONTROL_VERSION_CURRENT
) {
2980 error_report("Unknown source RDMA version: %d, bailing...",
2982 rdma_ack_cm_event(cm_event
);
2983 goto err_rdma_dest_wait
;
2987 * Respond with only the capabilities this version of QEMU knows about.
2989 cap
.flags
&= known_capabilities
;
2992 * Enable the ones that we do know about.
2993 * Add other checks here as new ones are introduced.
2995 if (cap
.flags
& RDMA_CAPABILITY_PIN_ALL
) {
2996 rdma
->pin_all
= true;
2999 rdma
->cm_id
= cm_event
->id
;
3000 verbs
= cm_event
->id
->verbs
;
3002 rdma_ack_cm_event(cm_event
);
3004 trace_qemu_rdma_accept_pin_state(rdma
->pin_all
);
3006 caps_to_network(&cap
);
3008 trace_qemu_rdma_accept_pin_verbsc(verbs
);
3011 rdma
->verbs
= verbs
;
3012 } else if (rdma
->verbs
!= verbs
) {
3013 error_report("ibv context not matching %p, %p!", rdma
->verbs
,
3015 goto err_rdma_dest_wait
;
3018 qemu_rdma_dump_id("dest_init", verbs
);
3020 ret
= qemu_rdma_alloc_pd_cq(rdma
);
3022 error_report("rdma migration: error allocating pd and cq!");
3023 goto err_rdma_dest_wait
;
3026 ret
= qemu_rdma_alloc_qp(rdma
);
3028 error_report("rdma migration: error allocating qp!");
3029 goto err_rdma_dest_wait
;
3032 ret
= qemu_rdma_init_ram_blocks(rdma
);
3034 error_report("rdma migration: error initializing ram blocks!");
3035 goto err_rdma_dest_wait
;
3038 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
3039 ret
= qemu_rdma_reg_control(rdma
, idx
);
3041 error_report("rdma: error registering %d control", idx
);
3042 goto err_rdma_dest_wait
;
3046 qemu_set_fd_handler(rdma
->channel
->fd
, NULL
, NULL
, NULL
);
3048 ret
= rdma_accept(rdma
->cm_id
, &conn_param
);
3050 error_report("rdma_accept returns %d", ret
);
3051 goto err_rdma_dest_wait
;
3054 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
3056 error_report("rdma_accept get_cm_event failed %d", ret
);
3057 goto err_rdma_dest_wait
;
3060 if (cm_event
->event
!= RDMA_CM_EVENT_ESTABLISHED
) {
3061 error_report("rdma_accept not event established");
3062 rdma_ack_cm_event(cm_event
);
3063 goto err_rdma_dest_wait
;
3066 rdma_ack_cm_event(cm_event
);
3067 rdma
->connected
= true;
3069 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
3071 error_report("rdma migration: error posting second control recv");
3072 goto err_rdma_dest_wait
;
3075 qemu_rdma_dump_gid("dest_connect", rdma
->cm_id
);
3080 rdma
->error_state
= ret
;
3081 qemu_rdma_cleanup(rdma
);
3085 static int dest_ram_sort_func(const void *a
, const void *b
)
3087 unsigned int a_index
= ((const RDMALocalBlock
*)a
)->src_index
;
3088 unsigned int b_index
= ((const RDMALocalBlock
*)b
)->src_index
;
3090 return (a_index
< b_index
) ? -1 : (a_index
!= b_index
);
3094 * During each iteration of the migration, we listen for instructions
3095 * by the source VM to perform dynamic page registrations before they
3096 * can perform RDMA operations.
3098 * We respond with the 'rkey'.
3100 * Keep doing this until the source tells us to stop.
3102 static int qemu_rdma_registration_handle(QEMUFile
*f
, void *opaque
)
3104 RDMAControlHeader reg_resp
= { .len
= sizeof(RDMARegisterResult
),
3105 .type
= RDMA_CONTROL_REGISTER_RESULT
,
3108 RDMAControlHeader unreg_resp
= { .len
= 0,
3109 .type
= RDMA_CONTROL_UNREGISTER_FINISHED
,
3112 RDMAControlHeader blocks
= { .type
= RDMA_CONTROL_RAM_BLOCKS_RESULT
,
3114 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(opaque
);
3115 RDMAContext
*rdma
= rioc
->rdma
;
3116 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
3117 RDMAControlHeader head
;
3118 RDMARegister
*reg
, *registers
;
3120 RDMARegisterResult
*reg_result
;
3121 static RDMARegisterResult results
[RDMA_CONTROL_MAX_COMMANDS_PER_MESSAGE
];
3122 RDMALocalBlock
*block
;
3129 CHECK_ERROR_STATE();
3132 trace_qemu_rdma_registration_handle_wait();
3134 ret
= qemu_rdma_exchange_recv(rdma
, &head
, RDMA_CONTROL_NONE
);
3140 if (head
.repeat
> RDMA_CONTROL_MAX_COMMANDS_PER_MESSAGE
) {
3141 error_report("rdma: Too many requests in this message (%d)."
3142 "Bailing.", head
.repeat
);
3147 switch (head
.type
) {
3148 case RDMA_CONTROL_COMPRESS
:
3149 comp
= (RDMACompress
*) rdma
->wr_data
[idx
].control_curr
;
3150 network_to_compress(comp
);
3152 trace_qemu_rdma_registration_handle_compress(comp
->length
,
3155 if (comp
->block_idx
>= rdma
->local_ram_blocks
.nb_blocks
) {
3156 error_report("rdma: 'compress' bad block index %u (vs %d)",
3157 (unsigned int)comp
->block_idx
,
3158 rdma
->local_ram_blocks
.nb_blocks
);
3162 block
= &(rdma
->local_ram_blocks
.block
[comp
->block_idx
]);
3164 host_addr
= block
->local_host_addr
+
3165 (comp
->offset
- block
->offset
);
3167 ram_handle_compressed(host_addr
, comp
->value
, comp
->length
);
3170 case RDMA_CONTROL_REGISTER_FINISHED
:
3171 trace_qemu_rdma_registration_handle_finished();
3174 case RDMA_CONTROL_RAM_BLOCKS_REQUEST
:
3175 trace_qemu_rdma_registration_handle_ram_blocks();
3177 /* Sort our local RAM Block list so it's the same as the source,
3178 * we can do this since we've filled in a src_index in the list
3179 * as we received the RAMBlock list earlier.
3181 qsort(rdma
->local_ram_blocks
.block
,
3182 rdma
->local_ram_blocks
.nb_blocks
,
3183 sizeof(RDMALocalBlock
), dest_ram_sort_func
);
3184 if (rdma
->pin_all
) {
3185 ret
= qemu_rdma_reg_whole_ram_blocks(rdma
);
3187 error_report("rdma migration: error dest "
3188 "registering ram blocks");
3194 * Dest uses this to prepare to transmit the RAMBlock descriptions
3195 * to the source VM after connection setup.
3196 * Both sides use the "remote" structure to communicate and update
3197 * their "local" descriptions with what was sent.
3199 for (i
= 0; i
< local
->nb_blocks
; i
++) {
3200 rdma
->dest_blocks
[i
].remote_host_addr
=
3201 (uintptr_t)(local
->block
[i
].local_host_addr
);
3203 if (rdma
->pin_all
) {
3204 rdma
->dest_blocks
[i
].remote_rkey
= local
->block
[i
].mr
->rkey
;
3207 rdma
->dest_blocks
[i
].offset
= local
->block
[i
].offset
;
3208 rdma
->dest_blocks
[i
].length
= local
->block
[i
].length
;
3210 dest_block_to_network(&rdma
->dest_blocks
[i
]);
3211 trace_qemu_rdma_registration_handle_ram_blocks_loop(
3212 local
->block
[i
].block_name
,
3213 local
->block
[i
].offset
,
3214 local
->block
[i
].length
,
3215 local
->block
[i
].local_host_addr
,
3216 local
->block
[i
].src_index
);
3219 blocks
.len
= rdma
->local_ram_blocks
.nb_blocks
3220 * sizeof(RDMADestBlock
);
3223 ret
= qemu_rdma_post_send_control(rdma
,
3224 (uint8_t *) rdma
->dest_blocks
, &blocks
);
3227 error_report("rdma migration: error sending remote info");
3232 case RDMA_CONTROL_REGISTER_REQUEST
:
3233 trace_qemu_rdma_registration_handle_register(head
.repeat
);
3235 reg_resp
.repeat
= head
.repeat
;
3236 registers
= (RDMARegister
*) rdma
->wr_data
[idx
].control_curr
;
3238 for (count
= 0; count
< head
.repeat
; count
++) {
3240 uint8_t *chunk_start
, *chunk_end
;
3242 reg
= ®isters
[count
];
3243 network_to_register(reg
);
3245 reg_result
= &results
[count
];
3247 trace_qemu_rdma_registration_handle_register_loop(count
,
3248 reg
->current_index
, reg
->key
.current_addr
, reg
->chunks
);
3250 if (reg
->current_index
>= rdma
->local_ram_blocks
.nb_blocks
) {
3251 error_report("rdma: 'register' bad block index %u (vs %d)",
3252 (unsigned int)reg
->current_index
,
3253 rdma
->local_ram_blocks
.nb_blocks
);
3257 block
= &(rdma
->local_ram_blocks
.block
[reg
->current_index
]);
3258 if (block
->is_ram_block
) {
3259 if (block
->offset
> reg
->key
.current_addr
) {
3260 error_report("rdma: bad register address for block %s"
3261 " offset: %" PRIx64
" current_addr: %" PRIx64
,
3262 block
->block_name
, block
->offset
,
3263 reg
->key
.current_addr
);
3267 host_addr
= (block
->local_host_addr
+
3268 (reg
->key
.current_addr
- block
->offset
));
3269 chunk
= ram_chunk_index(block
->local_host_addr
,
3270 (uint8_t *) host_addr
);
3272 chunk
= reg
->key
.chunk
;
3273 host_addr
= block
->local_host_addr
+
3274 (reg
->key
.chunk
* (1UL << RDMA_REG_CHUNK_SHIFT
));
3275 /* Check for particularly bad chunk value */
3276 if (host_addr
< (void *)block
->local_host_addr
) {
3277 error_report("rdma: bad chunk for block %s"
3279 block
->block_name
, reg
->key
.chunk
);
3284 chunk_start
= ram_chunk_start(block
, chunk
);
3285 chunk_end
= ram_chunk_end(block
, chunk
+ reg
->chunks
);
3286 if (qemu_rdma_register_and_get_keys(rdma
, block
,
3287 (uintptr_t)host_addr
, NULL
, ®_result
->rkey
,
3288 chunk
, chunk_start
, chunk_end
)) {
3289 error_report("cannot get rkey");
3294 reg_result
->host_addr
= (uintptr_t)block
->local_host_addr
;
3296 trace_qemu_rdma_registration_handle_register_rkey(
3299 result_to_network(reg_result
);
3302 ret
= qemu_rdma_post_send_control(rdma
,
3303 (uint8_t *) results
, ®_resp
);
3306 error_report("Failed to send control buffer");
3310 case RDMA_CONTROL_UNREGISTER_REQUEST
:
3311 trace_qemu_rdma_registration_handle_unregister(head
.repeat
);
3312 unreg_resp
.repeat
= head
.repeat
;
3313 registers
= (RDMARegister
*) rdma
->wr_data
[idx
].control_curr
;
3315 for (count
= 0; count
< head
.repeat
; count
++) {
3316 reg
= ®isters
[count
];
3317 network_to_register(reg
);
3319 trace_qemu_rdma_registration_handle_unregister_loop(count
,
3320 reg
->current_index
, reg
->key
.chunk
);
3322 block
= &(rdma
->local_ram_blocks
.block
[reg
->current_index
]);
3324 ret
= ibv_dereg_mr(block
->pmr
[reg
->key
.chunk
]);
3325 block
->pmr
[reg
->key
.chunk
] = NULL
;
3328 perror("rdma unregistration chunk failed");
3333 rdma
->total_registrations
--;
3335 trace_qemu_rdma_registration_handle_unregister_success(
3339 ret
= qemu_rdma_post_send_control(rdma
, NULL
, &unreg_resp
);
3342 error_report("Failed to send control buffer");
3346 case RDMA_CONTROL_REGISTER_RESULT
:
3347 error_report("Invalid RESULT message at dest.");
3351 error_report("Unknown control message %s", control_desc
[head
.type
]);
3358 rdma
->error_state
= ret
;
3364 * Called via a ram_control_load_hook during the initial RAM load section which
3365 * lists the RAMBlocks by name. This lets us know the order of the RAMBlocks
3367 * We've already built our local RAMBlock list, but not yet sent the list to
3371 rdma_block_notification_handle(QIOChannelRDMA
*rioc
, const char *name
)
3373 RDMAContext
*rdma
= rioc
->rdma
;
3377 /* Find the matching RAMBlock in our local list */
3378 for (curr
= 0; curr
< rdma
->local_ram_blocks
.nb_blocks
; curr
++) {
3379 if (!strcmp(rdma
->local_ram_blocks
.block
[curr
].block_name
, name
)) {
3386 error_report("RAMBlock '%s' not found on destination", name
);
3390 rdma
->local_ram_blocks
.block
[curr
].src_index
= rdma
->next_src_index
;
3391 trace_rdma_block_notification_handle(name
, rdma
->next_src_index
);
3392 rdma
->next_src_index
++;
3397 static int rdma_load_hook(QEMUFile
*f
, void *opaque
, uint64_t flags
, void *data
)
3400 case RAM_CONTROL_BLOCK_REG
:
3401 return rdma_block_notification_handle(opaque
, data
);
3403 case RAM_CONTROL_HOOK
:
3404 return qemu_rdma_registration_handle(f
, opaque
);
3407 /* Shouldn't be called with any other values */
3412 static int qemu_rdma_registration_start(QEMUFile
*f
, void *opaque
,
3413 uint64_t flags
, void *data
)
3415 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(opaque
);
3416 RDMAContext
*rdma
= rioc
->rdma
;
3418 CHECK_ERROR_STATE();
3420 trace_qemu_rdma_registration_start(flags
);
3421 qemu_put_be64(f
, RAM_SAVE_FLAG_HOOK
);
3428 * Inform dest that dynamic registrations are done for now.
3429 * First, flush writes, if any.
3431 static int qemu_rdma_registration_stop(QEMUFile
*f
, void *opaque
,
3432 uint64_t flags
, void *data
)
3434 Error
*local_err
= NULL
, **errp
= &local_err
;
3435 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(opaque
);
3436 RDMAContext
*rdma
= rioc
->rdma
;
3437 RDMAControlHeader head
= { .len
= 0, .repeat
= 1 };
3440 CHECK_ERROR_STATE();
3443 ret
= qemu_rdma_drain_cq(f
, rdma
);
3449 if (flags
== RAM_CONTROL_SETUP
) {
3450 RDMAControlHeader resp
= {.type
= RDMA_CONTROL_RAM_BLOCKS_RESULT
};
3451 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
3452 int reg_result_idx
, i
, nb_dest_blocks
;
3454 head
.type
= RDMA_CONTROL_RAM_BLOCKS_REQUEST
;
3455 trace_qemu_rdma_registration_stop_ram();
3458 * Make sure that we parallelize the pinning on both sides.
3459 * For very large guests, doing this serially takes a really
3460 * long time, so we have to 'interleave' the pinning locally
3461 * with the control messages by performing the pinning on this
3462 * side before we receive the control response from the other
3463 * side that the pinning has completed.
3465 ret
= qemu_rdma_exchange_send(rdma
, &head
, NULL
, &resp
,
3466 ®_result_idx
, rdma
->pin_all
?
3467 qemu_rdma_reg_whole_ram_blocks
: NULL
);
3469 ERROR(errp
, "receiving remote info!");
3473 nb_dest_blocks
= resp
.len
/ sizeof(RDMADestBlock
);
3476 * The protocol uses two different sets of rkeys (mutually exclusive):
3477 * 1. One key to represent the virtual address of the entire ram block.
3478 * (dynamic chunk registration disabled - pin everything with one rkey.)
3479 * 2. One to represent individual chunks within a ram block.
3480 * (dynamic chunk registration enabled - pin individual chunks.)
3482 * Once the capability is successfully negotiated, the destination transmits
3483 * the keys to use (or sends them later) including the virtual addresses
3484 * and then propagates the remote ram block descriptions to his local copy.
3487 if (local
->nb_blocks
!= nb_dest_blocks
) {
3488 ERROR(errp
, "ram blocks mismatch (Number of blocks %d vs %d) "
3489 "Your QEMU command line parameters are probably "
3490 "not identical on both the source and destination.",
3491 local
->nb_blocks
, nb_dest_blocks
);
3492 rdma
->error_state
= -EINVAL
;
3496 qemu_rdma_move_header(rdma
, reg_result_idx
, &resp
);
3497 memcpy(rdma
->dest_blocks
,
3498 rdma
->wr_data
[reg_result_idx
].control_curr
, resp
.len
);
3499 for (i
= 0; i
< nb_dest_blocks
; i
++) {
3500 network_to_dest_block(&rdma
->dest_blocks
[i
]);
3502 /* We require that the blocks are in the same order */
3503 if (rdma
->dest_blocks
[i
].length
!= local
->block
[i
].length
) {
3504 ERROR(errp
, "Block %s/%d has a different length %" PRIu64
3505 "vs %" PRIu64
, local
->block
[i
].block_name
, i
,
3506 local
->block
[i
].length
,
3507 rdma
->dest_blocks
[i
].length
);
3508 rdma
->error_state
= -EINVAL
;
3511 local
->block
[i
].remote_host_addr
=
3512 rdma
->dest_blocks
[i
].remote_host_addr
;
3513 local
->block
[i
].remote_rkey
= rdma
->dest_blocks
[i
].remote_rkey
;
3517 trace_qemu_rdma_registration_stop(flags
);
3519 head
.type
= RDMA_CONTROL_REGISTER_FINISHED
;
3520 ret
= qemu_rdma_exchange_send(rdma
, &head
, NULL
, NULL
, NULL
, NULL
);
3528 rdma
->error_state
= ret
;
3532 static const QEMUFileHooks rdma_read_hooks
= {
3533 .hook_ram_load
= rdma_load_hook
,
3536 static const QEMUFileHooks rdma_write_hooks
= {
3537 .before_ram_iterate
= qemu_rdma_registration_start
,
3538 .after_ram_iterate
= qemu_rdma_registration_stop
,
3539 .save_page
= qemu_rdma_save_page
,
3543 static void qio_channel_rdma_finalize(Object
*obj
)
3545 QIOChannelRDMA
*rioc
= QIO_CHANNEL_RDMA(obj
);
3547 qemu_rdma_cleanup(rioc
->rdma
);
3553 static void qio_channel_rdma_class_init(ObjectClass
*klass
,
3554 void *class_data G_GNUC_UNUSED
)
3556 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
3558 ioc_klass
->io_writev
= qio_channel_rdma_writev
;
3559 ioc_klass
->io_readv
= qio_channel_rdma_readv
;
3560 ioc_klass
->io_set_blocking
= qio_channel_rdma_set_blocking
;
3561 ioc_klass
->io_close
= qio_channel_rdma_close
;
3562 ioc_klass
->io_create_watch
= qio_channel_rdma_create_watch
;
3565 static const TypeInfo qio_channel_rdma_info
= {
3566 .parent
= TYPE_QIO_CHANNEL
,
3567 .name
= TYPE_QIO_CHANNEL_RDMA
,
3568 .instance_size
= sizeof(QIOChannelRDMA
),
3569 .instance_finalize
= qio_channel_rdma_finalize
,
3570 .class_init
= qio_channel_rdma_class_init
,
3573 static void qio_channel_rdma_register_types(void)
3575 type_register_static(&qio_channel_rdma_info
);
3578 type_init(qio_channel_rdma_register_types
);
3580 static QEMUFile
*qemu_fopen_rdma(RDMAContext
*rdma
, const char *mode
)
3582 QIOChannelRDMA
*rioc
;
3584 if (qemu_file_mode_is_not_valid(mode
)) {
3588 rioc
= QIO_CHANNEL_RDMA(object_new(TYPE_QIO_CHANNEL_RDMA
));
3591 if (mode
[0] == 'w') {
3592 rioc
->file
= qemu_fopen_channel_output(QIO_CHANNEL(rioc
));
3593 qemu_file_set_hooks(rioc
->file
, &rdma_write_hooks
);
3595 rioc
->file
= qemu_fopen_channel_input(QIO_CHANNEL(rioc
));
3596 qemu_file_set_hooks(rioc
->file
, &rdma_read_hooks
);
3602 static void rdma_accept_incoming_migration(void *opaque
)
3604 RDMAContext
*rdma
= opaque
;
3607 Error
*local_err
= NULL
, **errp
= &local_err
;
3609 trace_qemu_rdma_accept_incoming_migration();
3610 ret
= qemu_rdma_accept(rdma
);
3613 ERROR(errp
, "RDMA Migration initialization failed!");
3617 trace_qemu_rdma_accept_incoming_migration_accepted();
3619 f
= qemu_fopen_rdma(rdma
, "rb");
3621 ERROR(errp
, "could not qemu_fopen_rdma!");
3622 qemu_rdma_cleanup(rdma
);
3626 rdma
->migration_started_on_destination
= 1;
3627 migration_fd_process_incoming(f
);
3630 void rdma_start_incoming_migration(const char *host_port
, Error
**errp
)
3634 Error
*local_err
= NULL
;
3636 trace_rdma_start_incoming_migration();
3637 rdma
= qemu_rdma_data_init(host_port
, &local_err
);
3643 ret
= qemu_rdma_dest_init(rdma
, &local_err
);
3649 trace_rdma_start_incoming_migration_after_dest_init();
3651 ret
= rdma_listen(rdma
->listen_id
, 5);
3654 ERROR(errp
, "listening on socket!");
3658 trace_rdma_start_incoming_migration_after_rdma_listen();
3660 qemu_set_fd_handler(rdma
->channel
->fd
, rdma_accept_incoming_migration
,
3661 NULL
, (void *)(intptr_t)rdma
);
3664 error_propagate(errp
, local_err
);
3668 void rdma_start_outgoing_migration(void *opaque
,
3669 const char *host_port
, Error
**errp
)
3671 MigrationState
*s
= opaque
;
3672 RDMAContext
*rdma
= qemu_rdma_data_init(host_port
, errp
);
3679 ret
= qemu_rdma_source_init(rdma
, errp
,
3680 s
->enabled_capabilities
[MIGRATION_CAPABILITY_RDMA_PIN_ALL
]);
3686 trace_rdma_start_outgoing_migration_after_rdma_source_init();
3687 ret
= qemu_rdma_connect(rdma
, errp
);
3693 trace_rdma_start_outgoing_migration_after_rdma_connect();
3695 s
->to_dst_file
= qemu_fopen_rdma(rdma
, "wb");
3696 migrate_fd_connect(s
);