2 * RDMA protocol and interfaces
4 * Copyright IBM, Corp. 2010-2013
7 * Michael R. Hines <mrhines@us.ibm.com>
8 * Jiuxing Liu <jl@us.ibm.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or
11 * later. See the COPYING file in the top-level directory.
14 #include "qemu-common.h"
15 #include "migration/migration.h"
16 #include "migration/qemu-file.h"
17 #include "exec/cpu-common.h"
18 #include "qemu/main-loop.h"
19 #include "qemu/sockets.h"
20 #include "qemu/bitmap.h"
21 #include "block/coroutine.h"
23 #include <sys/types.h>
24 #include <sys/socket.h>
26 #include <arpa/inet.h>
28 #include <rdma/rdma_cma.h>
31 //#define DEBUG_RDMA_VERBOSE
32 //#define DEBUG_RDMA_REALLY_VERBOSE
35 #define DPRINTF(fmt, ...) \
36 do { printf("rdma: " fmt, ## __VA_ARGS__); } while (0)
38 #define DPRINTF(fmt, ...) \
42 #ifdef DEBUG_RDMA_VERBOSE
43 #define DDPRINTF(fmt, ...) \
44 do { printf("rdma: " fmt, ## __VA_ARGS__); } while (0)
46 #define DDPRINTF(fmt, ...) \
50 #ifdef DEBUG_RDMA_REALLY_VERBOSE
51 #define DDDPRINTF(fmt, ...) \
52 do { printf("rdma: " fmt, ## __VA_ARGS__); } while (0)
54 #define DDDPRINTF(fmt, ...) \
59 * Print and error on both the Monitor and the Log file.
61 #define ERROR(errp, fmt, ...) \
63 fprintf(stderr, "RDMA ERROR: " fmt "\n", ## __VA_ARGS__); \
64 if (errp && (*(errp) == NULL)) { \
65 error_setg(errp, "RDMA ERROR: " fmt, ## __VA_ARGS__); \
69 #define RDMA_RESOLVE_TIMEOUT_MS 10000
71 /* Do not merge data if larger than this. */
72 #define RDMA_MERGE_MAX (2 * 1024 * 1024)
73 #define RDMA_SIGNALED_SEND_MAX (RDMA_MERGE_MAX / 4096)
75 #define RDMA_REG_CHUNK_SHIFT 20 /* 1 MB */
78 * This is only for non-live state being migrated.
79 * Instead of RDMA_WRITE messages, we use RDMA_SEND
80 * messages for that state, which requires a different
81 * delivery design than main memory.
83 #define RDMA_SEND_INCREMENT 32768
86 * Maximum size infiniband SEND message
88 #define RDMA_CONTROL_MAX_BUFFER (512 * 1024)
89 #define RDMA_CONTROL_MAX_COMMANDS_PER_MESSAGE 4096
91 #define RDMA_CONTROL_VERSION_CURRENT 1
93 * Capabilities for negotiation.
95 #define RDMA_CAPABILITY_PIN_ALL 0x01
98 * Add the other flags above to this list of known capabilities
99 * as they are introduced.
101 static uint32_t known_capabilities
= RDMA_CAPABILITY_PIN_ALL
;
103 #define CHECK_ERROR_STATE() \
105 if (rdma->error_state) { \
106 if (!rdma->error_reported) { \
107 fprintf(stderr, "RDMA is in an error state waiting migration" \
109 rdma->error_reported = 1; \
111 return rdma->error_state; \
116 * A work request ID is 64-bits and we split up these bits
119 * bits 0-15 : type of control message, 2^16
120 * bits 16-29: ram block index, 2^14
121 * bits 30-63: ram block chunk number, 2^34
123 * The last two bit ranges are only used for RDMA writes,
124 * in order to track their completion and potentially
125 * also track unregistration status of the message.
127 #define RDMA_WRID_TYPE_SHIFT 0UL
128 #define RDMA_WRID_BLOCK_SHIFT 16UL
129 #define RDMA_WRID_CHUNK_SHIFT 30UL
131 #define RDMA_WRID_TYPE_MASK \
132 ((1UL << RDMA_WRID_BLOCK_SHIFT) - 1UL)
134 #define RDMA_WRID_BLOCK_MASK \
135 (~RDMA_WRID_TYPE_MASK & ((1UL << RDMA_WRID_CHUNK_SHIFT) - 1UL))
137 #define RDMA_WRID_CHUNK_MASK (~RDMA_WRID_BLOCK_MASK & ~RDMA_WRID_TYPE_MASK)
140 * RDMA migration protocol:
141 * 1. RDMA Writes (data messages, i.e. RAM)
142 * 2. IB Send/Recv (control channel messages)
146 RDMA_WRID_RDMA_WRITE
= 1,
147 RDMA_WRID_SEND_CONTROL
= 2000,
148 RDMA_WRID_RECV_CONTROL
= 4000,
151 const char *wrid_desc
[] = {
152 [RDMA_WRID_NONE
] = "NONE",
153 [RDMA_WRID_RDMA_WRITE
] = "WRITE RDMA",
154 [RDMA_WRID_SEND_CONTROL
] = "CONTROL SEND",
155 [RDMA_WRID_RECV_CONTROL
] = "CONTROL RECV",
159 * Work request IDs for IB SEND messages only (not RDMA writes).
160 * This is used by the migration protocol to transmit
161 * control messages (such as device state and registration commands)
163 * We could use more WRs, but we have enough for now.
173 * SEND/RECV IB Control Messages.
176 RDMA_CONTROL_NONE
= 0,
178 RDMA_CONTROL_READY
, /* ready to receive */
179 RDMA_CONTROL_QEMU_FILE
, /* QEMUFile-transmitted bytes */
180 RDMA_CONTROL_RAM_BLOCKS_REQUEST
, /* RAMBlock synchronization */
181 RDMA_CONTROL_RAM_BLOCKS_RESULT
, /* RAMBlock synchronization */
182 RDMA_CONTROL_COMPRESS
, /* page contains repeat values */
183 RDMA_CONTROL_REGISTER_REQUEST
, /* dynamic page registration */
184 RDMA_CONTROL_REGISTER_RESULT
, /* key to use after registration */
185 RDMA_CONTROL_REGISTER_FINISHED
, /* current iteration finished */
186 RDMA_CONTROL_UNREGISTER_REQUEST
, /* dynamic UN-registration */
187 RDMA_CONTROL_UNREGISTER_FINISHED
, /* unpinning finished */
190 const char *control_desc
[] = {
191 [RDMA_CONTROL_NONE
] = "NONE",
192 [RDMA_CONTROL_ERROR
] = "ERROR",
193 [RDMA_CONTROL_READY
] = "READY",
194 [RDMA_CONTROL_QEMU_FILE
] = "QEMU FILE",
195 [RDMA_CONTROL_RAM_BLOCKS_REQUEST
] = "RAM BLOCKS REQUEST",
196 [RDMA_CONTROL_RAM_BLOCKS_RESULT
] = "RAM BLOCKS RESULT",
197 [RDMA_CONTROL_COMPRESS
] = "COMPRESS",
198 [RDMA_CONTROL_REGISTER_REQUEST
] = "REGISTER REQUEST",
199 [RDMA_CONTROL_REGISTER_RESULT
] = "REGISTER RESULT",
200 [RDMA_CONTROL_REGISTER_FINISHED
] = "REGISTER FINISHED",
201 [RDMA_CONTROL_UNREGISTER_REQUEST
] = "UNREGISTER REQUEST",
202 [RDMA_CONTROL_UNREGISTER_FINISHED
] = "UNREGISTER FINISHED",
206 * Memory and MR structures used to represent an IB Send/Recv work request.
207 * This is *not* used for RDMA writes, only IB Send/Recv.
210 uint8_t control
[RDMA_CONTROL_MAX_BUFFER
]; /* actual buffer to register */
211 struct ibv_mr
*control_mr
; /* registration metadata */
212 size_t control_len
; /* length of the message */
213 uint8_t *control_curr
; /* start of unconsumed bytes */
214 } RDMAWorkRequestData
;
217 * Negotiate RDMA capabilities during connection-setup time.
224 static void caps_to_network(RDMACapabilities
*cap
)
226 cap
->version
= htonl(cap
->version
);
227 cap
->flags
= htonl(cap
->flags
);
230 static void network_to_caps(RDMACapabilities
*cap
)
232 cap
->version
= ntohl(cap
->version
);
233 cap
->flags
= ntohl(cap
->flags
);
237 * Representation of a RAMBlock from an RDMA perspective.
238 * This is not transmitted, only local.
239 * This and subsequent structures cannot be linked lists
240 * because we're using a single IB message to transmit
241 * the information. It's small anyway, so a list is overkill.
243 typedef struct RDMALocalBlock
{
244 uint8_t *local_host_addr
; /* local virtual address */
245 uint64_t remote_host_addr
; /* remote virtual address */
248 struct ibv_mr
**pmr
; /* MRs for chunk-level registration */
249 struct ibv_mr
*mr
; /* MR for non-chunk-level registration */
250 uint32_t *remote_keys
; /* rkeys for chunk-level registration */
251 uint32_t remote_rkey
; /* rkeys for non-chunk-level registration */
252 int index
; /* which block are we */
255 unsigned long *transit_bitmap
;
256 unsigned long *unregister_bitmap
;
260 * Also represents a RAMblock, but only on the dest.
261 * This gets transmitted by the dest during connection-time
262 * to the source VM and then is used to populate the
263 * corresponding RDMALocalBlock with
264 * the information needed to perform the actual RDMA.
266 typedef struct QEMU_PACKED RDMARemoteBlock
{
267 uint64_t remote_host_addr
;
270 uint32_t remote_rkey
;
274 static uint64_t htonll(uint64_t v
)
276 union { uint32_t lv
[2]; uint64_t llv
; } u
;
277 u
.lv
[0] = htonl(v
>> 32);
278 u
.lv
[1] = htonl(v
& 0xFFFFFFFFULL
);
282 static uint64_t ntohll(uint64_t v
) {
283 union { uint32_t lv
[2]; uint64_t llv
; } u
;
285 return ((uint64_t)ntohl(u
.lv
[0]) << 32) | (uint64_t) ntohl(u
.lv
[1]);
288 static void remote_block_to_network(RDMARemoteBlock
*rb
)
290 rb
->remote_host_addr
= htonll(rb
->remote_host_addr
);
291 rb
->offset
= htonll(rb
->offset
);
292 rb
->length
= htonll(rb
->length
);
293 rb
->remote_rkey
= htonl(rb
->remote_rkey
);
296 static void network_to_remote_block(RDMARemoteBlock
*rb
)
298 rb
->remote_host_addr
= ntohll(rb
->remote_host_addr
);
299 rb
->offset
= ntohll(rb
->offset
);
300 rb
->length
= ntohll(rb
->length
);
301 rb
->remote_rkey
= ntohl(rb
->remote_rkey
);
305 * Virtual address of the above structures used for transmitting
306 * the RAMBlock descriptions at connection-time.
307 * This structure is *not* transmitted.
309 typedef struct RDMALocalBlocks
{
311 bool init
; /* main memory init complete */
312 RDMALocalBlock
*block
;
316 * Main data structure for RDMA state.
317 * While there is only one copy of this structure being allocated right now,
318 * this is the place where one would start if you wanted to consider
319 * having more than one RDMA connection open at the same time.
321 typedef struct RDMAContext
{
325 RDMAWorkRequestData wr_data
[RDMA_WRID_MAX
];
328 * This is used by *_exchange_send() to figure out whether or not
329 * the initial "READY" message has already been received or not.
330 * This is because other functions may potentially poll() and detect
331 * the READY message before send() does, in which case we need to
332 * know if it completed.
334 int control_ready_expected
;
336 /* number of outstanding writes */
339 /* store info about current buffer so that we can
340 merge it with future sends */
341 uint64_t current_addr
;
342 uint64_t current_length
;
343 /* index of ram block the current buffer belongs to */
345 /* index of the chunk in the current ram block */
351 * infiniband-specific variables for opening the device
352 * and maintaining connection state and so forth.
354 * cm_id also has ibv_context, rdma_event_channel, and ibv_qp in
355 * cm_id->verbs, cm_id->channel, and cm_id->qp.
357 struct rdma_cm_id
*cm_id
; /* connection manager ID */
358 struct rdma_cm_id
*listen_id
;
360 struct ibv_context
*verbs
;
361 struct rdma_event_channel
*channel
;
362 struct ibv_qp
*qp
; /* queue pair */
363 struct ibv_comp_channel
*comp_channel
; /* completion channel */
364 struct ibv_pd
*pd
; /* protection domain */
365 struct ibv_cq
*cq
; /* completion queue */
368 * If a previous write failed (perhaps because of a failed
369 * memory registration, then do not attempt any future work
370 * and remember the error state.
376 * Description of ram blocks used throughout the code.
378 RDMALocalBlocks local_ram_blocks
;
379 RDMARemoteBlock
*block
;
382 * Migration on *destination* started.
383 * Then use coroutine yield function.
384 * Source runs in a thread, so we don't care.
386 int migration_started_on_destination
;
388 int total_registrations
;
391 int unregister_current
, unregister_next
;
392 uint64_t unregistrations
[RDMA_SIGNALED_SEND_MAX
];
394 GHashTable
*blockmap
;
399 * Interface to the rest of the migration call stack.
401 typedef struct QEMUFileRDMA
{
408 * Main structure for IB Send/Recv control messages.
409 * This gets prepended at the beginning of every Send/Recv.
411 typedef struct QEMU_PACKED
{
412 uint32_t len
; /* Total length of data portion */
413 uint32_t type
; /* which control command to perform */
414 uint32_t repeat
; /* number of commands in data portion of same type */
418 static void control_to_network(RDMAControlHeader
*control
)
420 control
->type
= htonl(control
->type
);
421 control
->len
= htonl(control
->len
);
422 control
->repeat
= htonl(control
->repeat
);
425 static void network_to_control(RDMAControlHeader
*control
)
427 control
->type
= ntohl(control
->type
);
428 control
->len
= ntohl(control
->len
);
429 control
->repeat
= ntohl(control
->repeat
);
433 * Register a single Chunk.
434 * Information sent by the source VM to inform the dest
435 * to register an single chunk of memory before we can perform
436 * the actual RDMA operation.
438 typedef struct QEMU_PACKED
{
440 uint64_t current_addr
; /* offset into the ramblock of the chunk */
441 uint64_t chunk
; /* chunk to lookup if unregistering */
443 uint32_t current_index
; /* which ramblock the chunk belongs to */
445 uint64_t chunks
; /* how many sequential chunks to register */
448 static void register_to_network(RDMARegister
*reg
)
450 reg
->key
.current_addr
= htonll(reg
->key
.current_addr
);
451 reg
->current_index
= htonl(reg
->current_index
);
452 reg
->chunks
= htonll(reg
->chunks
);
455 static void network_to_register(RDMARegister
*reg
)
457 reg
->key
.current_addr
= ntohll(reg
->key
.current_addr
);
458 reg
->current_index
= ntohl(reg
->current_index
);
459 reg
->chunks
= ntohll(reg
->chunks
);
462 typedef struct QEMU_PACKED
{
463 uint32_t value
; /* if zero, we will madvise() */
464 uint32_t block_idx
; /* which ram block index */
465 uint64_t offset
; /* where in the remote ramblock this chunk */
466 uint64_t length
; /* length of the chunk */
469 static void compress_to_network(RDMACompress
*comp
)
471 comp
->value
= htonl(comp
->value
);
472 comp
->block_idx
= htonl(comp
->block_idx
);
473 comp
->offset
= htonll(comp
->offset
);
474 comp
->length
= htonll(comp
->length
);
477 static void network_to_compress(RDMACompress
*comp
)
479 comp
->value
= ntohl(comp
->value
);
480 comp
->block_idx
= ntohl(comp
->block_idx
);
481 comp
->offset
= ntohll(comp
->offset
);
482 comp
->length
= ntohll(comp
->length
);
486 * The result of the dest's memory registration produces an "rkey"
487 * which the source VM must reference in order to perform
488 * the RDMA operation.
490 typedef struct QEMU_PACKED
{
494 } RDMARegisterResult
;
496 static void result_to_network(RDMARegisterResult
*result
)
498 result
->rkey
= htonl(result
->rkey
);
499 result
->host_addr
= htonll(result
->host_addr
);
502 static void network_to_result(RDMARegisterResult
*result
)
504 result
->rkey
= ntohl(result
->rkey
);
505 result
->host_addr
= ntohll(result
->host_addr
);
508 const char *print_wrid(int wrid
);
509 static int qemu_rdma_exchange_send(RDMAContext
*rdma
, RDMAControlHeader
*head
,
510 uint8_t *data
, RDMAControlHeader
*resp
,
512 int (*callback
)(RDMAContext
*rdma
));
514 static inline uint64_t ram_chunk_index(uint8_t *start
, uint8_t *host
)
516 return ((uintptr_t) host
- (uintptr_t) start
) >> RDMA_REG_CHUNK_SHIFT
;
519 static inline uint8_t *ram_chunk_start(RDMALocalBlock
*rdma_ram_block
,
522 return (uint8_t *) (((uintptr_t) rdma_ram_block
->local_host_addr
)
523 + (i
<< RDMA_REG_CHUNK_SHIFT
));
526 static inline uint8_t *ram_chunk_end(RDMALocalBlock
*rdma_ram_block
, uint64_t i
)
528 uint8_t *result
= ram_chunk_start(rdma_ram_block
, i
) +
529 (1UL << RDMA_REG_CHUNK_SHIFT
);
531 if (result
> (rdma_ram_block
->local_host_addr
+ rdma_ram_block
->length
)) {
532 result
= rdma_ram_block
->local_host_addr
+ rdma_ram_block
->length
;
538 static int __qemu_rdma_add_block(RDMAContext
*rdma
, void *host_addr
,
539 ram_addr_t block_offset
, uint64_t length
)
541 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
542 RDMALocalBlock
*block
= g_hash_table_lookup(rdma
->blockmap
,
543 (void *) block_offset
);
544 RDMALocalBlock
*old
= local
->block
;
546 assert(block
== NULL
);
548 local
->block
= g_malloc0(sizeof(RDMALocalBlock
) * (local
->nb_blocks
+ 1));
550 if (local
->nb_blocks
) {
553 for (x
= 0; x
< local
->nb_blocks
; x
++) {
554 g_hash_table_remove(rdma
->blockmap
, (void *)old
[x
].offset
);
555 g_hash_table_insert(rdma
->blockmap
, (void *)old
[x
].offset
,
558 memcpy(local
->block
, old
, sizeof(RDMALocalBlock
) * local
->nb_blocks
);
562 block
= &local
->block
[local
->nb_blocks
];
564 block
->local_host_addr
= host_addr
;
565 block
->offset
= block_offset
;
566 block
->length
= length
;
567 block
->index
= local
->nb_blocks
;
568 block
->nb_chunks
= ram_chunk_index(host_addr
, host_addr
+ length
) + 1UL;
569 block
->transit_bitmap
= bitmap_new(block
->nb_chunks
);
570 bitmap_clear(block
->transit_bitmap
, 0, block
->nb_chunks
);
571 block
->unregister_bitmap
= bitmap_new(block
->nb_chunks
);
572 bitmap_clear(block
->unregister_bitmap
, 0, block
->nb_chunks
);
573 block
->remote_keys
= g_malloc0(block
->nb_chunks
* sizeof(uint32_t));
575 block
->is_ram_block
= local
->init
? false : true;
577 g_hash_table_insert(rdma
->blockmap
, (void *) block_offset
, block
);
579 DDPRINTF("Added Block: %d, addr: %" PRIu64
", offset: %" PRIu64
580 " length: %" PRIu64
" end: %" PRIu64
" bits %" PRIu64
" chunks %d\n",
581 local
->nb_blocks
, (uint64_t) block
->local_host_addr
, block
->offset
,
582 block
->length
, (uint64_t) (block
->local_host_addr
+ block
->length
),
583 BITS_TO_LONGS(block
->nb_chunks
) *
584 sizeof(unsigned long) * 8, block
->nb_chunks
);
592 * Memory regions need to be registered with the device and queue pairs setup
593 * in advanced before the migration starts. This tells us where the RAM blocks
594 * are so that we can register them individually.
596 static void qemu_rdma_init_one_block(void *host_addr
,
597 ram_addr_t block_offset
, ram_addr_t length
, void *opaque
)
599 __qemu_rdma_add_block(opaque
, host_addr
, block_offset
, length
);
603 * Identify the RAMBlocks and their quantity. They will be references to
604 * identify chunk boundaries inside each RAMBlock and also be referenced
605 * during dynamic page registration.
607 static int qemu_rdma_init_ram_blocks(RDMAContext
*rdma
)
609 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
611 assert(rdma
->blockmap
== NULL
);
612 rdma
->blockmap
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
613 memset(local
, 0, sizeof *local
);
614 qemu_ram_foreach_block(qemu_rdma_init_one_block
, rdma
);
615 DPRINTF("Allocated %d local ram block structures\n", local
->nb_blocks
);
616 rdma
->block
= (RDMARemoteBlock
*) g_malloc0(sizeof(RDMARemoteBlock
) *
617 rdma
->local_ram_blocks
.nb_blocks
);
622 static int __qemu_rdma_delete_block(RDMAContext
*rdma
, ram_addr_t block_offset
)
624 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
625 RDMALocalBlock
*block
= g_hash_table_lookup(rdma
->blockmap
,
626 (void *) block_offset
);
627 RDMALocalBlock
*old
= local
->block
;
635 for (j
= 0; j
< block
->nb_chunks
; j
++) {
636 if (!block
->pmr
[j
]) {
639 ibv_dereg_mr(block
->pmr
[j
]);
640 rdma
->total_registrations
--;
647 ibv_dereg_mr(block
->mr
);
648 rdma
->total_registrations
--;
652 g_free(block
->transit_bitmap
);
653 block
->transit_bitmap
= NULL
;
655 g_free(block
->unregister_bitmap
);
656 block
->unregister_bitmap
= NULL
;
658 g_free(block
->remote_keys
);
659 block
->remote_keys
= NULL
;
661 for (x
= 0; x
< local
->nb_blocks
; x
++) {
662 g_hash_table_remove(rdma
->blockmap
, (void *)old
[x
].offset
);
665 if (local
->nb_blocks
> 1) {
667 local
->block
= g_malloc0(sizeof(RDMALocalBlock
) *
668 (local
->nb_blocks
- 1));
671 memcpy(local
->block
, old
, sizeof(RDMALocalBlock
) * block
->index
);
674 if (block
->index
< (local
->nb_blocks
- 1)) {
675 memcpy(local
->block
+ block
->index
, old
+ (block
->index
+ 1),
676 sizeof(RDMALocalBlock
) *
677 (local
->nb_blocks
- (block
->index
+ 1)));
680 assert(block
== local
->block
);
684 DDPRINTF("Deleted Block: %d, addr: %" PRIu64
", offset: %" PRIu64
685 " length: %" PRIu64
" end: %" PRIu64
" bits %" PRIu64
" chunks %d\n",
686 local
->nb_blocks
, (uint64_t) block
->local_host_addr
, block
->offset
,
687 block
->length
, (uint64_t) (block
->local_host_addr
+ block
->length
),
688 BITS_TO_LONGS(block
->nb_chunks
) *
689 sizeof(unsigned long) * 8, block
->nb_chunks
);
695 if (local
->nb_blocks
) {
696 for (x
= 0; x
< local
->nb_blocks
; x
++) {
697 g_hash_table_insert(rdma
->blockmap
, (void *)local
->block
[x
].offset
,
706 * Put in the log file which RDMA device was opened and the details
707 * associated with that device.
709 static void qemu_rdma_dump_id(const char *who
, struct ibv_context
*verbs
)
711 printf("%s RDMA Device opened: kernel name %s "
712 "uverbs device name %s, "
713 "infiniband_verbs class device path %s,"
714 " infiniband class device path %s\n",
717 verbs
->device
->dev_name
,
718 verbs
->device
->dev_path
,
719 verbs
->device
->ibdev_path
);
723 * Put in the log file the RDMA gid addressing information,
724 * useful for folks who have trouble understanding the
725 * RDMA device hierarchy in the kernel.
727 static void qemu_rdma_dump_gid(const char *who
, struct rdma_cm_id
*id
)
731 inet_ntop(AF_INET6
, &id
->route
.addr
.addr
.ibaddr
.sgid
, sgid
, sizeof sgid
);
732 inet_ntop(AF_INET6
, &id
->route
.addr
.addr
.ibaddr
.dgid
, dgid
, sizeof dgid
);
733 DPRINTF("%s Source GID: %s, Dest GID: %s\n", who
, sgid
, dgid
);
737 * Figure out which RDMA device corresponds to the requested IP hostname
738 * Also create the initial connection manager identifiers for opening
741 static int qemu_rdma_resolve_host(RDMAContext
*rdma
, Error
**errp
)
744 struct addrinfo
*res
;
746 struct rdma_cm_event
*cm_event
;
747 char ip
[40] = "unknown";
748 int af
= rdma
->ipv6
? PF_INET6
: PF_INET
;
750 if (rdma
->host
== NULL
|| !strcmp(rdma
->host
, "")) {
751 ERROR(errp
, "RDMA hostname has not been set");
755 /* create CM channel */
756 rdma
->channel
= rdma_create_event_channel();
757 if (!rdma
->channel
) {
758 ERROR(errp
, "could not create CM channel");
763 ret
= rdma_create_id(rdma
->channel
, &rdma
->cm_id
, NULL
, RDMA_PS_TCP
);
765 ERROR(errp
, "could not create channel id");
766 goto err_resolve_create_id
;
769 snprintf(port_str
, 16, "%d", rdma
->port
);
772 ret
= getaddrinfo(rdma
->host
, port_str
, NULL
, &res
);
774 ERROR(errp
, "could not getaddrinfo address %s", rdma
->host
);
775 goto err_resolve_get_addr
;
778 inet_ntop(af
, &((struct sockaddr_in
*) res
->ai_addr
)->sin_addr
,
780 DPRINTF("%s => %s\n", rdma
->host
, ip
);
782 /* resolve the first address */
783 ret
= rdma_resolve_addr(rdma
->cm_id
, NULL
, res
->ai_addr
,
784 RDMA_RESOLVE_TIMEOUT_MS
);
786 ERROR(errp
, "could not resolve address %s", rdma
->host
);
787 goto err_resolve_get_addr
;
790 qemu_rdma_dump_gid("source_resolve_addr", rdma
->cm_id
);
792 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
794 ERROR(errp
, "could not perform event_addr_resolved");
795 goto err_resolve_get_addr
;
798 if (cm_event
->event
!= RDMA_CM_EVENT_ADDR_RESOLVED
) {
799 ERROR(errp
, "result not equal to event_addr_resolved %s",
800 rdma_event_str(cm_event
->event
));
801 perror("rdma_resolve_addr");
802 goto err_resolve_get_addr
;
804 rdma_ack_cm_event(cm_event
);
807 ret
= rdma_resolve_route(rdma
->cm_id
, RDMA_RESOLVE_TIMEOUT_MS
);
809 ERROR(errp
, "could not resolve rdma route");
810 goto err_resolve_get_addr
;
813 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
815 ERROR(errp
, "could not perform event_route_resolved");
816 goto err_resolve_get_addr
;
818 if (cm_event
->event
!= RDMA_CM_EVENT_ROUTE_RESOLVED
) {
819 ERROR(errp
, "result not equal to event_route_resolved: %s",
820 rdma_event_str(cm_event
->event
));
821 rdma_ack_cm_event(cm_event
);
822 goto err_resolve_get_addr
;
824 rdma_ack_cm_event(cm_event
);
825 rdma
->verbs
= rdma
->cm_id
->verbs
;
826 qemu_rdma_dump_id("source_resolve_host", rdma
->cm_id
->verbs
);
827 qemu_rdma_dump_gid("source_resolve_host", rdma
->cm_id
);
830 err_resolve_get_addr
:
831 rdma_destroy_id(rdma
->cm_id
);
833 err_resolve_create_id
:
834 rdma_destroy_event_channel(rdma
->channel
);
835 rdma
->channel
= NULL
;
841 * Create protection domain and completion queues
843 static int qemu_rdma_alloc_pd_cq(RDMAContext
*rdma
)
846 rdma
->pd
= ibv_alloc_pd(rdma
->verbs
);
848 fprintf(stderr
, "failed to allocate protection domain\n");
852 /* create completion channel */
853 rdma
->comp_channel
= ibv_create_comp_channel(rdma
->verbs
);
854 if (!rdma
->comp_channel
) {
855 fprintf(stderr
, "failed to allocate completion channel\n");
856 goto err_alloc_pd_cq
;
860 * Completion queue can be filled by both read and write work requests,
861 * so must reflect the sum of both possible queue sizes.
863 rdma
->cq
= ibv_create_cq(rdma
->verbs
, (RDMA_SIGNALED_SEND_MAX
* 3),
864 NULL
, rdma
->comp_channel
, 0);
866 fprintf(stderr
, "failed to allocate completion queue\n");
867 goto err_alloc_pd_cq
;
874 ibv_dealloc_pd(rdma
->pd
);
876 if (rdma
->comp_channel
) {
877 ibv_destroy_comp_channel(rdma
->comp_channel
);
880 rdma
->comp_channel
= NULL
;
886 * Create queue pairs.
888 static int qemu_rdma_alloc_qp(RDMAContext
*rdma
)
890 struct ibv_qp_init_attr attr
= { 0 };
893 attr
.cap
.max_send_wr
= RDMA_SIGNALED_SEND_MAX
;
894 attr
.cap
.max_recv_wr
= 3;
895 attr
.cap
.max_send_sge
= 1;
896 attr
.cap
.max_recv_sge
= 1;
897 attr
.send_cq
= rdma
->cq
;
898 attr
.recv_cq
= rdma
->cq
;
899 attr
.qp_type
= IBV_QPT_RC
;
901 ret
= rdma_create_qp(rdma
->cm_id
, rdma
->pd
, &attr
);
906 rdma
->qp
= rdma
->cm_id
->qp
;
910 static int qemu_rdma_reg_whole_ram_blocks(RDMAContext
*rdma
)
913 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
915 for (i
= 0; i
< local
->nb_blocks
; i
++) {
918 local
->block
[i
].local_host_addr
,
919 local
->block
[i
].length
,
920 IBV_ACCESS_LOCAL_WRITE
|
921 IBV_ACCESS_REMOTE_WRITE
923 if (!local
->block
[i
].mr
) {
924 perror("Failed to register local dest ram block!\n");
927 rdma
->total_registrations
++;
930 if (i
>= local
->nb_blocks
) {
934 for (i
--; i
>= 0; i
--) {
935 ibv_dereg_mr(local
->block
[i
].mr
);
936 rdma
->total_registrations
--;
944 * Find the ram block that corresponds to the page requested to be
945 * transmitted by QEMU.
947 * Once the block is found, also identify which 'chunk' within that
948 * block that the page belongs to.
950 * This search cannot fail or the migration will fail.
952 static int qemu_rdma_search_ram_block(RDMAContext
*rdma
,
953 uint64_t block_offset
,
956 uint64_t *block_index
,
957 uint64_t *chunk_index
)
959 uint64_t current_addr
= block_offset
+ offset
;
960 RDMALocalBlock
*block
= g_hash_table_lookup(rdma
->blockmap
,
961 (void *) block_offset
);
963 assert(current_addr
>= block
->offset
);
964 assert((current_addr
+ length
) <= (block
->offset
+ block
->length
));
966 *block_index
= block
->index
;
967 *chunk_index
= ram_chunk_index(block
->local_host_addr
,
968 block
->local_host_addr
+ (current_addr
- block
->offset
));
974 * Register a chunk with IB. If the chunk was already registered
975 * previously, then skip.
977 * Also return the keys associated with the registration needed
978 * to perform the actual RDMA operation.
980 static int qemu_rdma_register_and_get_keys(RDMAContext
*rdma
,
981 RDMALocalBlock
*block
, uint8_t *host_addr
,
982 uint32_t *lkey
, uint32_t *rkey
, int chunk
,
983 uint8_t *chunk_start
, uint8_t *chunk_end
)
987 *lkey
= block
->mr
->lkey
;
990 *rkey
= block
->mr
->rkey
;
995 /* allocate memory to store chunk MRs */
997 block
->pmr
= g_malloc0(block
->nb_chunks
* sizeof(struct ibv_mr
*));
1004 * If 'rkey', then we're the destination, so grant access to the source.
1006 * If 'lkey', then we're the source VM, so grant access only to ourselves.
1008 if (!block
->pmr
[chunk
]) {
1009 uint64_t len
= chunk_end
- chunk_start
;
1011 DDPRINTF("Registering %" PRIu64
" bytes @ %p\n",
1014 block
->pmr
[chunk
] = ibv_reg_mr(rdma
->pd
,
1016 (rkey
? (IBV_ACCESS_LOCAL_WRITE
|
1017 IBV_ACCESS_REMOTE_WRITE
) : 0));
1019 if (!block
->pmr
[chunk
]) {
1020 perror("Failed to register chunk!");
1021 fprintf(stderr
, "Chunk details: block: %d chunk index %d"
1022 " start %" PRIu64
" end %" PRIu64
" host %" PRIu64
1023 " local %" PRIu64
" registrations: %d\n",
1024 block
->index
, chunk
, (uint64_t) chunk_start
,
1025 (uint64_t) chunk_end
, (uint64_t) host_addr
,
1026 (uint64_t) block
->local_host_addr
,
1027 rdma
->total_registrations
);
1030 rdma
->total_registrations
++;
1034 *lkey
= block
->pmr
[chunk
]->lkey
;
1037 *rkey
= block
->pmr
[chunk
]->rkey
;
1043 * Register (at connection time) the memory used for control
1046 static int qemu_rdma_reg_control(RDMAContext
*rdma
, int idx
)
1048 rdma
->wr_data
[idx
].control_mr
= ibv_reg_mr(rdma
->pd
,
1049 rdma
->wr_data
[idx
].control
, RDMA_CONTROL_MAX_BUFFER
,
1050 IBV_ACCESS_LOCAL_WRITE
| IBV_ACCESS_REMOTE_WRITE
);
1051 if (rdma
->wr_data
[idx
].control_mr
) {
1052 rdma
->total_registrations
++;
1055 fprintf(stderr
, "qemu_rdma_reg_control failed!\n");
1059 const char *print_wrid(int wrid
)
1061 if (wrid
>= RDMA_WRID_RECV_CONTROL
) {
1062 return wrid_desc
[RDMA_WRID_RECV_CONTROL
];
1064 return wrid_desc
[wrid
];
1068 * RDMA requires memory registration (mlock/pinning), but this is not good for
1071 * In preparation for the future where LRU information or workload-specific
1072 * writable writable working set memory access behavior is available to QEMU
1073 * it would be nice to have in place the ability to UN-register/UN-pin
1074 * particular memory regions from the RDMA hardware when it is determine that
1075 * those regions of memory will likely not be accessed again in the near future.
1077 * While we do not yet have such information right now, the following
1078 * compile-time option allows us to perform a non-optimized version of this
1081 * By uncommenting this option, you will cause *all* RDMA transfers to be
1082 * unregistered immediately after the transfer completes on both sides of the
1083 * connection. This has no effect in 'rdma-pin-all' mode, only regular mode.
1085 * This will have a terrible impact on migration performance, so until future
1086 * workload information or LRU information is available, do not attempt to use
1087 * this feature except for basic testing.
1089 //#define RDMA_UNREGISTRATION_EXAMPLE
1092 * Perform a non-optimized memory unregistration after every transfer
1093 * for demonsration purposes, only if pin-all is not requested.
1095 * Potential optimizations:
1096 * 1. Start a new thread to run this function continuously
1098 - and for receipt of unregister messages
1100 * 3. Use workload hints.
1102 static int qemu_rdma_unregister_waiting(RDMAContext
*rdma
)
1104 while (rdma
->unregistrations
[rdma
->unregister_current
]) {
1106 uint64_t wr_id
= rdma
->unregistrations
[rdma
->unregister_current
];
1108 (wr_id
& RDMA_WRID_CHUNK_MASK
) >> RDMA_WRID_CHUNK_SHIFT
;
1110 (wr_id
& RDMA_WRID_BLOCK_MASK
) >> RDMA_WRID_BLOCK_SHIFT
;
1111 RDMALocalBlock
*block
=
1112 &(rdma
->local_ram_blocks
.block
[index
]);
1113 RDMARegister reg
= { .current_index
= index
};
1114 RDMAControlHeader resp
= { .type
= RDMA_CONTROL_UNREGISTER_FINISHED
,
1116 RDMAControlHeader head
= { .len
= sizeof(RDMARegister
),
1117 .type
= RDMA_CONTROL_UNREGISTER_REQUEST
,
1121 DDPRINTF("Processing unregister for chunk: %" PRIu64
1122 " at position %d\n", chunk
, rdma
->unregister_current
);
1124 rdma
->unregistrations
[rdma
->unregister_current
] = 0;
1125 rdma
->unregister_current
++;
1127 if (rdma
->unregister_current
== RDMA_SIGNALED_SEND_MAX
) {
1128 rdma
->unregister_current
= 0;
1133 * Unregistration is speculative (because migration is single-threaded
1134 * and we cannot break the protocol's inifinband message ordering).
1135 * Thus, if the memory is currently being used for transmission,
1136 * then abort the attempt to unregister and try again
1137 * later the next time a completion is received for this memory.
1139 clear_bit(chunk
, block
->unregister_bitmap
);
1141 if (test_bit(chunk
, block
->transit_bitmap
)) {
1142 DDPRINTF("Cannot unregister inflight chunk: %" PRIu64
"\n", chunk
);
1146 DDPRINTF("Sending unregister for chunk: %" PRIu64
"\n", chunk
);
1148 ret
= ibv_dereg_mr(block
->pmr
[chunk
]);
1149 block
->pmr
[chunk
] = NULL
;
1150 block
->remote_keys
[chunk
] = 0;
1153 perror("unregistration chunk failed");
1156 rdma
->total_registrations
--;
1158 reg
.key
.chunk
= chunk
;
1159 register_to_network(®
);
1160 ret
= qemu_rdma_exchange_send(rdma
, &head
, (uint8_t *) ®
,
1166 DDPRINTF("Unregister for chunk: %" PRIu64
" complete.\n", chunk
);
1172 static uint64_t qemu_rdma_make_wrid(uint64_t wr_id
, uint64_t index
,
1175 uint64_t result
= wr_id
& RDMA_WRID_TYPE_MASK
;
1177 result
|= (index
<< RDMA_WRID_BLOCK_SHIFT
);
1178 result
|= (chunk
<< RDMA_WRID_CHUNK_SHIFT
);
1184 * Set bit for unregistration in the next iteration.
1185 * We cannot transmit right here, but will unpin later.
1187 static void qemu_rdma_signal_unregister(RDMAContext
*rdma
, uint64_t index
,
1188 uint64_t chunk
, uint64_t wr_id
)
1190 if (rdma
->unregistrations
[rdma
->unregister_next
] != 0) {
1191 fprintf(stderr
, "rdma migration: queue is full!\n");
1193 RDMALocalBlock
*block
= &(rdma
->local_ram_blocks
.block
[index
]);
1195 if (!test_and_set_bit(chunk
, block
->unregister_bitmap
)) {
1196 DDPRINTF("Appending unregister chunk %" PRIu64
1197 " at position %d\n", chunk
, rdma
->unregister_next
);
1199 rdma
->unregistrations
[rdma
->unregister_next
++] =
1200 qemu_rdma_make_wrid(wr_id
, index
, chunk
);
1202 if (rdma
->unregister_next
== RDMA_SIGNALED_SEND_MAX
) {
1203 rdma
->unregister_next
= 0;
1206 DDPRINTF("Unregister chunk %" PRIu64
" already in queue.\n",
1213 * Consult the connection manager to see a work request
1214 * (of any kind) has completed.
1215 * Return the work request ID that completed.
1217 static uint64_t qemu_rdma_poll(RDMAContext
*rdma
, uint64_t *wr_id_out
,
1224 ret
= ibv_poll_cq(rdma
->cq
, 1, &wc
);
1227 *wr_id_out
= RDMA_WRID_NONE
;
1232 fprintf(stderr
, "ibv_poll_cq return %d!\n", ret
);
1236 wr_id
= wc
.wr_id
& RDMA_WRID_TYPE_MASK
;
1238 if (wc
.status
!= IBV_WC_SUCCESS
) {
1239 fprintf(stderr
, "ibv_poll_cq wc.status=%d %s!\n",
1240 wc
.status
, ibv_wc_status_str(wc
.status
));
1241 fprintf(stderr
, "ibv_poll_cq wrid=%s!\n", wrid_desc
[wr_id
]);
1246 if (rdma
->control_ready_expected
&&
1247 (wr_id
>= RDMA_WRID_RECV_CONTROL
)) {
1248 DDDPRINTF("completion %s #%" PRId64
" received (%" PRId64
")"
1249 " left %d\n", wrid_desc
[RDMA_WRID_RECV_CONTROL
],
1250 wr_id
- RDMA_WRID_RECV_CONTROL
, wr_id
, rdma
->nb_sent
);
1251 rdma
->control_ready_expected
= 0;
1254 if (wr_id
== RDMA_WRID_RDMA_WRITE
) {
1256 (wc
.wr_id
& RDMA_WRID_CHUNK_MASK
) >> RDMA_WRID_CHUNK_SHIFT
;
1258 (wc
.wr_id
& RDMA_WRID_BLOCK_MASK
) >> RDMA_WRID_BLOCK_SHIFT
;
1259 RDMALocalBlock
*block
= &(rdma
->local_ram_blocks
.block
[index
]);
1261 DDDPRINTF("completions %s (%" PRId64
") left %d, "
1262 "block %" PRIu64
", chunk: %" PRIu64
" %p %p\n",
1263 print_wrid(wr_id
), wr_id
, rdma
->nb_sent
, index
, chunk
,
1264 block
->local_host_addr
, (void *)block
->remote_host_addr
);
1266 clear_bit(chunk
, block
->transit_bitmap
);
1268 if (rdma
->nb_sent
> 0) {
1272 if (!rdma
->pin_all
) {
1274 * FYI: If one wanted to signal a specific chunk to be unregistered
1275 * using LRU or workload-specific information, this is the function
1276 * you would call to do so. That chunk would then get asynchronously
1277 * unregistered later.
1279 #ifdef RDMA_UNREGISTRATION_EXAMPLE
1280 qemu_rdma_signal_unregister(rdma
, index
, chunk
, wc
.wr_id
);
1284 DDDPRINTF("other completion %s (%" PRId64
") received left %d\n",
1285 print_wrid(wr_id
), wr_id
, rdma
->nb_sent
);
1288 *wr_id_out
= wc
.wr_id
;
1290 *byte_len
= wc
.byte_len
;
1297 * Block until the next work request has completed.
1299 * First poll to see if a work request has already completed,
1302 * If we encounter completed work requests for IDs other than
1303 * the one we're interested in, then that's generally an error.
1305 * The only exception is actual RDMA Write completions. These
1306 * completions only need to be recorded, but do not actually
1307 * need further processing.
1309 static int qemu_rdma_block_for_wrid(RDMAContext
*rdma
, int wrid_requested
,
1312 int num_cq_events
= 0, ret
= 0;
1315 uint64_t wr_id
= RDMA_WRID_NONE
, wr_id_in
;
1317 if (ibv_req_notify_cq(rdma
->cq
, 0)) {
1321 while (wr_id
!= wrid_requested
) {
1322 ret
= qemu_rdma_poll(rdma
, &wr_id_in
, byte_len
);
1327 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
1329 if (wr_id
== RDMA_WRID_NONE
) {
1332 if (wr_id
!= wrid_requested
) {
1333 DDDPRINTF("A Wanted wrid %s (%d) but got %s (%" PRIu64
")\n",
1334 print_wrid(wrid_requested
),
1335 wrid_requested
, print_wrid(wr_id
), wr_id
);
1339 if (wr_id
== wrid_requested
) {
1345 * Coroutine doesn't start until process_incoming_migration()
1346 * so don't yield unless we know we're running inside of a coroutine.
1348 if (rdma
->migration_started_on_destination
) {
1349 yield_until_fd_readable(rdma
->comp_channel
->fd
);
1352 if (ibv_get_cq_event(rdma
->comp_channel
, &cq
, &cq_ctx
)) {
1353 perror("ibv_get_cq_event");
1354 goto err_block_for_wrid
;
1359 if (ibv_req_notify_cq(cq
, 0)) {
1360 goto err_block_for_wrid
;
1363 while (wr_id
!= wrid_requested
) {
1364 ret
= qemu_rdma_poll(rdma
, &wr_id_in
, byte_len
);
1366 goto err_block_for_wrid
;
1369 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
1371 if (wr_id
== RDMA_WRID_NONE
) {
1374 if (wr_id
!= wrid_requested
) {
1375 DDDPRINTF("B Wanted wrid %s (%d) but got %s (%" PRIu64
")\n",
1376 print_wrid(wrid_requested
), wrid_requested
,
1377 print_wrid(wr_id
), wr_id
);
1381 if (wr_id
== wrid_requested
) {
1382 goto success_block_for_wrid
;
1386 success_block_for_wrid
:
1387 if (num_cq_events
) {
1388 ibv_ack_cq_events(cq
, num_cq_events
);
1393 if (num_cq_events
) {
1394 ibv_ack_cq_events(cq
, num_cq_events
);
1400 * Post a SEND message work request for the control channel
1401 * containing some data and block until the post completes.
1403 static int qemu_rdma_post_send_control(RDMAContext
*rdma
, uint8_t *buf
,
1404 RDMAControlHeader
*head
)
1407 RDMAWorkRequestData
*wr
= &rdma
->wr_data
[RDMA_WRID_CONTROL
];
1408 struct ibv_send_wr
*bad_wr
;
1409 struct ibv_sge sge
= {
1410 .addr
= (uint64_t)(wr
->control
),
1411 .length
= head
->len
+ sizeof(RDMAControlHeader
),
1412 .lkey
= wr
->control_mr
->lkey
,
1414 struct ibv_send_wr send_wr
= {
1415 .wr_id
= RDMA_WRID_SEND_CONTROL
,
1416 .opcode
= IBV_WR_SEND
,
1417 .send_flags
= IBV_SEND_SIGNALED
,
1422 DDDPRINTF("CONTROL: sending %s..\n", control_desc
[head
->type
]);
1425 * We don't actually need to do a memcpy() in here if we used
1426 * the "sge" properly, but since we're only sending control messages
1427 * (not RAM in a performance-critical path), then its OK for now.
1429 * The copy makes the RDMAControlHeader simpler to manipulate
1430 * for the time being.
1432 assert(head
->len
<= RDMA_CONTROL_MAX_BUFFER
- sizeof(*head
));
1433 memcpy(wr
->control
, head
, sizeof(RDMAControlHeader
));
1434 control_to_network((void *) wr
->control
);
1437 memcpy(wr
->control
+ sizeof(RDMAControlHeader
), buf
, head
->len
);
1441 if (ibv_post_send(rdma
->qp
, &send_wr
, &bad_wr
)) {
1446 fprintf(stderr
, "Failed to use post IB SEND for control!\n");
1450 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_SEND_CONTROL
, NULL
);
1452 fprintf(stderr
, "rdma migration: send polling control error!\n");
1459 * Post a RECV work request in anticipation of some future receipt
1460 * of data on the control channel.
1462 static int qemu_rdma_post_recv_control(RDMAContext
*rdma
, int idx
)
1464 struct ibv_recv_wr
*bad_wr
;
1465 struct ibv_sge sge
= {
1466 .addr
= (uint64_t)(rdma
->wr_data
[idx
].control
),
1467 .length
= RDMA_CONTROL_MAX_BUFFER
,
1468 .lkey
= rdma
->wr_data
[idx
].control_mr
->lkey
,
1471 struct ibv_recv_wr recv_wr
= {
1472 .wr_id
= RDMA_WRID_RECV_CONTROL
+ idx
,
1478 if (ibv_post_recv(rdma
->qp
, &recv_wr
, &bad_wr
)) {
1486 * Block and wait for a RECV control channel message to arrive.
1488 static int qemu_rdma_exchange_get_response(RDMAContext
*rdma
,
1489 RDMAControlHeader
*head
, int expecting
, int idx
)
1492 int ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RECV_CONTROL
+ idx
,
1496 fprintf(stderr
, "rdma migration: recv polling control error!\n");
1500 network_to_control((void *) rdma
->wr_data
[idx
].control
);
1501 memcpy(head
, rdma
->wr_data
[idx
].control
, sizeof(RDMAControlHeader
));
1503 DDDPRINTF("CONTROL: %s receiving...\n", control_desc
[expecting
]);
1505 if (expecting
== RDMA_CONTROL_NONE
) {
1506 DDDPRINTF("Surprise: got %s (%d)\n",
1507 control_desc
[head
->type
], head
->type
);
1508 } else if (head
->type
!= expecting
|| head
->type
== RDMA_CONTROL_ERROR
) {
1509 fprintf(stderr
, "Was expecting a %s (%d) control message"
1510 ", but got: %s (%d), length: %d\n",
1511 control_desc
[expecting
], expecting
,
1512 control_desc
[head
->type
], head
->type
, head
->len
);
1515 if (head
->len
> RDMA_CONTROL_MAX_BUFFER
- sizeof(*head
)) {
1516 fprintf(stderr
, "too long length: %d\n", head
->len
);
1519 if (sizeof(*head
) + head
->len
!= byte_len
) {
1520 fprintf(stderr
, "Malformed length: %d byte_len %d\n",
1521 head
->len
, byte_len
);
1529 * When a RECV work request has completed, the work request's
1530 * buffer is pointed at the header.
1532 * This will advance the pointer to the data portion
1533 * of the control message of the work request's buffer that
1534 * was populated after the work request finished.
1536 static void qemu_rdma_move_header(RDMAContext
*rdma
, int idx
,
1537 RDMAControlHeader
*head
)
1539 rdma
->wr_data
[idx
].control_len
= head
->len
;
1540 rdma
->wr_data
[idx
].control_curr
=
1541 rdma
->wr_data
[idx
].control
+ sizeof(RDMAControlHeader
);
1545 * This is an 'atomic' high-level operation to deliver a single, unified
1546 * control-channel message.
1548 * Additionally, if the user is expecting some kind of reply to this message,
1549 * they can request a 'resp' response message be filled in by posting an
1550 * additional work request on behalf of the user and waiting for an additional
1553 * The extra (optional) response is used during registration to us from having
1554 * to perform an *additional* exchange of message just to provide a response by
1555 * instead piggy-backing on the acknowledgement.
1557 static int qemu_rdma_exchange_send(RDMAContext
*rdma
, RDMAControlHeader
*head
,
1558 uint8_t *data
, RDMAControlHeader
*resp
,
1560 int (*callback
)(RDMAContext
*rdma
))
1565 * Wait until the dest is ready before attempting to deliver the message
1566 * by waiting for a READY message.
1568 if (rdma
->control_ready_expected
) {
1569 RDMAControlHeader resp
;
1570 ret
= qemu_rdma_exchange_get_response(rdma
,
1571 &resp
, RDMA_CONTROL_READY
, RDMA_WRID_READY
);
1578 * If the user is expecting a response, post a WR in anticipation of it.
1581 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_DATA
);
1583 fprintf(stderr
, "rdma migration: error posting"
1584 " extra control recv for anticipated result!");
1590 * Post a WR to replace the one we just consumed for the READY message.
1592 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
1594 fprintf(stderr
, "rdma migration: error posting first control recv!");
1599 * Deliver the control message that was requested.
1601 ret
= qemu_rdma_post_send_control(rdma
, data
, head
);
1604 fprintf(stderr
, "Failed to send control buffer!\n");
1609 * If we're expecting a response, block and wait for it.
1613 DDPRINTF("Issuing callback before receiving response...\n");
1614 ret
= callback(rdma
);
1620 DDPRINTF("Waiting for response %s\n", control_desc
[resp
->type
]);
1621 ret
= qemu_rdma_exchange_get_response(rdma
, resp
,
1622 resp
->type
, RDMA_WRID_DATA
);
1628 qemu_rdma_move_header(rdma
, RDMA_WRID_DATA
, resp
);
1630 *resp_idx
= RDMA_WRID_DATA
;
1632 DDPRINTF("Response %s received.\n", control_desc
[resp
->type
]);
1635 rdma
->control_ready_expected
= 1;
1641 * This is an 'atomic' high-level operation to receive a single, unified
1642 * control-channel message.
1644 static int qemu_rdma_exchange_recv(RDMAContext
*rdma
, RDMAControlHeader
*head
,
1647 RDMAControlHeader ready
= {
1649 .type
= RDMA_CONTROL_READY
,
1655 * Inform the source that we're ready to receive a message.
1657 ret
= qemu_rdma_post_send_control(rdma
, NULL
, &ready
);
1660 fprintf(stderr
, "Failed to send control buffer!\n");
1665 * Block and wait for the message.
1667 ret
= qemu_rdma_exchange_get_response(rdma
, head
,
1668 expecting
, RDMA_WRID_READY
);
1674 qemu_rdma_move_header(rdma
, RDMA_WRID_READY
, head
);
1677 * Post a new RECV work request to replace the one we just consumed.
1679 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
1681 fprintf(stderr
, "rdma migration: error posting second control recv!");
1689 * Write an actual chunk of memory using RDMA.
1691 * If we're using dynamic registration on the dest-side, we have to
1692 * send a registration command first.
1694 static int qemu_rdma_write_one(QEMUFile
*f
, RDMAContext
*rdma
,
1695 int current_index
, uint64_t current_addr
,
1699 struct ibv_send_wr send_wr
= { 0 };
1700 struct ibv_send_wr
*bad_wr
;
1701 int reg_result_idx
, ret
, count
= 0;
1702 uint64_t chunk
, chunks
;
1703 uint8_t *chunk_start
, *chunk_end
;
1704 RDMALocalBlock
*block
= &(rdma
->local_ram_blocks
.block
[current_index
]);
1706 RDMARegisterResult
*reg_result
;
1707 RDMAControlHeader resp
= { .type
= RDMA_CONTROL_REGISTER_RESULT
};
1708 RDMAControlHeader head
= { .len
= sizeof(RDMARegister
),
1709 .type
= RDMA_CONTROL_REGISTER_REQUEST
,
1714 sge
.addr
= (uint64_t)(block
->local_host_addr
+
1715 (current_addr
- block
->offset
));
1716 sge
.length
= length
;
1718 chunk
= ram_chunk_index(block
->local_host_addr
, (uint8_t *) sge
.addr
);
1719 chunk_start
= ram_chunk_start(block
, chunk
);
1721 if (block
->is_ram_block
) {
1722 chunks
= length
/ (1UL << RDMA_REG_CHUNK_SHIFT
);
1724 if (chunks
&& ((length
% (1UL << RDMA_REG_CHUNK_SHIFT
)) == 0)) {
1728 chunks
= block
->length
/ (1UL << RDMA_REG_CHUNK_SHIFT
);
1730 if (chunks
&& ((block
->length
% (1UL << RDMA_REG_CHUNK_SHIFT
)) == 0)) {
1735 DDPRINTF("Writing %" PRIu64
" chunks, (%" PRIu64
" MB)\n",
1736 chunks
+ 1, (chunks
+ 1) * (1UL << RDMA_REG_CHUNK_SHIFT
) / 1024 / 1024);
1738 chunk_end
= ram_chunk_end(block
, chunk
+ chunks
);
1740 if (!rdma
->pin_all
) {
1741 #ifdef RDMA_UNREGISTRATION_EXAMPLE
1742 qemu_rdma_unregister_waiting(rdma
);
1746 while (test_bit(chunk
, block
->transit_bitmap
)) {
1748 DDPRINTF("(%d) Not clobbering: block: %d chunk %" PRIu64
1749 " current %" PRIu64
" len %" PRIu64
" %d %d\n",
1750 count
++, current_index
, chunk
,
1751 sge
.addr
, length
, rdma
->nb_sent
, block
->nb_chunks
);
1753 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RDMA_WRITE
, NULL
);
1756 fprintf(stderr
, "Failed to Wait for previous write to complete "
1757 "block %d chunk %" PRIu64
1758 " current %" PRIu64
" len %" PRIu64
" %d\n",
1759 current_index
, chunk
, sge
.addr
, length
, rdma
->nb_sent
);
1764 if (!rdma
->pin_all
|| !block
->is_ram_block
) {
1765 if (!block
->remote_keys
[chunk
]) {
1767 * This chunk has not yet been registered, so first check to see
1768 * if the entire chunk is zero. If so, tell the other size to
1769 * memset() + madvise() the entire chunk without RDMA.
1772 if (can_use_buffer_find_nonzero_offset((void *)sge
.addr
, length
)
1773 && buffer_find_nonzero_offset((void *)sge
.addr
,
1774 length
) == length
) {
1775 RDMACompress comp
= {
1776 .offset
= current_addr
,
1778 .block_idx
= current_index
,
1782 head
.len
= sizeof(comp
);
1783 head
.type
= RDMA_CONTROL_COMPRESS
;
1785 DDPRINTF("Entire chunk is zero, sending compress: %"
1787 "bytes, index: %d, offset: %" PRId64
"...\n",
1788 chunk
, sge
.length
, current_index
, current_addr
);
1790 compress_to_network(&comp
);
1791 ret
= qemu_rdma_exchange_send(rdma
, &head
,
1792 (uint8_t *) &comp
, NULL
, NULL
, NULL
);
1798 acct_update_position(f
, sge
.length
, true);
1804 * Otherwise, tell other side to register.
1806 reg
.current_index
= current_index
;
1807 if (block
->is_ram_block
) {
1808 reg
.key
.current_addr
= current_addr
;
1810 reg
.key
.chunk
= chunk
;
1812 reg
.chunks
= chunks
;
1814 DDPRINTF("Sending registration request chunk %" PRIu64
" for %d "
1815 "bytes, index: %d, offset: %" PRId64
"...\n",
1816 chunk
, sge
.length
, current_index
, current_addr
);
1818 register_to_network(®
);
1819 ret
= qemu_rdma_exchange_send(rdma
, &head
, (uint8_t *) ®
,
1820 &resp
, ®_result_idx
, NULL
);
1825 /* try to overlap this single registration with the one we sent. */
1826 if (qemu_rdma_register_and_get_keys(rdma
, block
,
1827 (uint8_t *) sge
.addr
,
1828 &sge
.lkey
, NULL
, chunk
,
1829 chunk_start
, chunk_end
)) {
1830 fprintf(stderr
, "cannot get lkey!\n");
1834 reg_result
= (RDMARegisterResult
*)
1835 rdma
->wr_data
[reg_result_idx
].control_curr
;
1837 network_to_result(reg_result
);
1839 DDPRINTF("Received registration result:"
1840 " my key: %x their key %x, chunk %" PRIu64
"\n",
1841 block
->remote_keys
[chunk
], reg_result
->rkey
, chunk
);
1843 block
->remote_keys
[chunk
] = reg_result
->rkey
;
1844 block
->remote_host_addr
= reg_result
->host_addr
;
1846 /* already registered before */
1847 if (qemu_rdma_register_and_get_keys(rdma
, block
,
1848 (uint8_t *)sge
.addr
,
1849 &sge
.lkey
, NULL
, chunk
,
1850 chunk_start
, chunk_end
)) {
1851 fprintf(stderr
, "cannot get lkey!\n");
1856 send_wr
.wr
.rdma
.rkey
= block
->remote_keys
[chunk
];
1858 send_wr
.wr
.rdma
.rkey
= block
->remote_rkey
;
1860 if (qemu_rdma_register_and_get_keys(rdma
, block
, (uint8_t *)sge
.addr
,
1861 &sge
.lkey
, NULL
, chunk
,
1862 chunk_start
, chunk_end
)) {
1863 fprintf(stderr
, "cannot get lkey!\n");
1869 * Encode the ram block index and chunk within this wrid.
1870 * We will use this information at the time of completion
1871 * to figure out which bitmap to check against and then which
1872 * chunk in the bitmap to look for.
1874 send_wr
.wr_id
= qemu_rdma_make_wrid(RDMA_WRID_RDMA_WRITE
,
1875 current_index
, chunk
);
1877 send_wr
.opcode
= IBV_WR_RDMA_WRITE
;
1878 send_wr
.send_flags
= IBV_SEND_SIGNALED
;
1879 send_wr
.sg_list
= &sge
;
1880 send_wr
.num_sge
= 1;
1881 send_wr
.wr
.rdma
.remote_addr
= block
->remote_host_addr
+
1882 (current_addr
- block
->offset
);
1884 DDDPRINTF("Posting chunk: %" PRIu64
", addr: %lx"
1885 " remote: %lx, bytes %" PRIu32
"\n",
1886 chunk
, sge
.addr
, send_wr
.wr
.rdma
.remote_addr
,
1890 * ibv_post_send() does not return negative error numbers,
1891 * per the specification they are positive - no idea why.
1893 ret
= ibv_post_send(rdma
->qp
, &send_wr
, &bad_wr
);
1895 if (ret
== ENOMEM
) {
1896 DDPRINTF("send queue is full. wait a little....\n");
1897 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RDMA_WRITE
, NULL
);
1899 fprintf(stderr
, "rdma migration: failed to make "
1900 "room in full send queue! %d\n", ret
);
1906 } else if (ret
> 0) {
1907 perror("rdma migration: post rdma write failed");
1911 set_bit(chunk
, block
->transit_bitmap
);
1912 acct_update_position(f
, sge
.length
, false);
1913 rdma
->total_writes
++;
1919 * Push out any unwritten RDMA operations.
1921 * We support sending out multiple chunks at the same time.
1922 * Not all of them need to get signaled in the completion queue.
1924 static int qemu_rdma_write_flush(QEMUFile
*f
, RDMAContext
*rdma
)
1928 if (!rdma
->current_length
) {
1932 ret
= qemu_rdma_write_one(f
, rdma
,
1933 rdma
->current_index
, rdma
->current_addr
, rdma
->current_length
);
1941 DDDPRINTF("sent total: %d\n", rdma
->nb_sent
);
1944 rdma
->current_length
= 0;
1945 rdma
->current_addr
= 0;
1950 static inline int qemu_rdma_buffer_mergable(RDMAContext
*rdma
,
1951 uint64_t offset
, uint64_t len
)
1953 RDMALocalBlock
*block
;
1957 if (rdma
->current_index
< 0) {
1961 if (rdma
->current_chunk
< 0) {
1965 block
= &(rdma
->local_ram_blocks
.block
[rdma
->current_index
]);
1966 host_addr
= block
->local_host_addr
+ (offset
- block
->offset
);
1967 chunk_end
= ram_chunk_end(block
, rdma
->current_chunk
);
1969 if (rdma
->current_length
== 0) {
1974 * Only merge into chunk sequentially.
1976 if (offset
!= (rdma
->current_addr
+ rdma
->current_length
)) {
1980 if (offset
< block
->offset
) {
1984 if ((offset
+ len
) > (block
->offset
+ block
->length
)) {
1988 if ((host_addr
+ len
) > chunk_end
) {
1996 * We're not actually writing here, but doing three things:
1998 * 1. Identify the chunk the buffer belongs to.
1999 * 2. If the chunk is full or the buffer doesn't belong to the current
2000 * chunk, then start a new chunk and flush() the old chunk.
2001 * 3. To keep the hardware busy, we also group chunks into batches
2002 * and only require that a batch gets acknowledged in the completion
2003 * qeueue instead of each individual chunk.
2005 static int qemu_rdma_write(QEMUFile
*f
, RDMAContext
*rdma
,
2006 uint64_t block_offset
, uint64_t offset
,
2009 uint64_t current_addr
= block_offset
+ offset
;
2010 uint64_t index
= rdma
->current_index
;
2011 uint64_t chunk
= rdma
->current_chunk
;
2014 /* If we cannot merge it, we flush the current buffer first. */
2015 if (!qemu_rdma_buffer_mergable(rdma
, current_addr
, len
)) {
2016 ret
= qemu_rdma_write_flush(f
, rdma
);
2020 rdma
->current_length
= 0;
2021 rdma
->current_addr
= current_addr
;
2023 ret
= qemu_rdma_search_ram_block(rdma
, block_offset
,
2024 offset
, len
, &index
, &chunk
);
2026 fprintf(stderr
, "ram block search failed\n");
2029 rdma
->current_index
= index
;
2030 rdma
->current_chunk
= chunk
;
2034 rdma
->current_length
+= len
;
2036 /* flush it if buffer is too large */
2037 if (rdma
->current_length
>= RDMA_MERGE_MAX
) {
2038 return qemu_rdma_write_flush(f
, rdma
);
2044 static void qemu_rdma_cleanup(RDMAContext
*rdma
)
2046 struct rdma_cm_event
*cm_event
;
2050 if (rdma
->error_state
) {
2051 RDMAControlHeader head
= { .len
= 0,
2052 .type
= RDMA_CONTROL_ERROR
,
2055 fprintf(stderr
, "Early error. Sending error.\n");
2056 qemu_rdma_post_send_control(rdma
, NULL
, &head
);
2059 ret
= rdma_disconnect(rdma
->cm_id
);
2061 DDPRINTF("waiting for disconnect\n");
2062 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
2064 rdma_ack_cm_event(cm_event
);
2067 DDPRINTF("Disconnected.\n");
2071 g_free(rdma
->block
);
2074 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2075 if (rdma
->wr_data
[idx
].control_mr
) {
2076 rdma
->total_registrations
--;
2077 ibv_dereg_mr(rdma
->wr_data
[idx
].control_mr
);
2079 rdma
->wr_data
[idx
].control_mr
= NULL
;
2082 if (rdma
->local_ram_blocks
.block
) {
2083 while (rdma
->local_ram_blocks
.nb_blocks
) {
2084 __qemu_rdma_delete_block(rdma
,
2085 rdma
->local_ram_blocks
.block
->offset
);
2090 ibv_destroy_qp(rdma
->qp
);
2094 ibv_destroy_cq(rdma
->cq
);
2097 if (rdma
->comp_channel
) {
2098 ibv_destroy_comp_channel(rdma
->comp_channel
);
2099 rdma
->comp_channel
= NULL
;
2102 ibv_dealloc_pd(rdma
->pd
);
2105 if (rdma
->listen_id
) {
2106 rdma_destroy_id(rdma
->listen_id
);
2107 rdma
->listen_id
= NULL
;
2110 rdma_destroy_id(rdma
->cm_id
);
2113 if (rdma
->channel
) {
2114 rdma_destroy_event_channel(rdma
->channel
);
2115 rdma
->channel
= NULL
;
2122 static int qemu_rdma_source_init(RDMAContext
*rdma
, Error
**errp
, bool pin_all
)
2125 Error
*local_err
= NULL
, **temp
= &local_err
;
2128 * Will be validated against destination's actual capabilities
2129 * after the connect() completes.
2131 rdma
->pin_all
= pin_all
;
2133 ret
= qemu_rdma_resolve_host(rdma
, temp
);
2135 goto err_rdma_source_init
;
2138 ret
= qemu_rdma_alloc_pd_cq(rdma
);
2140 ERROR(temp
, "rdma migration: error allocating pd and cq! Your mlock()"
2141 " limits may be too low. Please check $ ulimit -a # and "
2142 "search for 'ulimit -l' in the output");
2143 goto err_rdma_source_init
;
2146 ret
= qemu_rdma_alloc_qp(rdma
);
2148 ERROR(temp
, "rdma migration: error allocating qp!");
2149 goto err_rdma_source_init
;
2152 ret
= qemu_rdma_init_ram_blocks(rdma
);
2154 ERROR(temp
, "rdma migration: error initializing ram blocks!");
2155 goto err_rdma_source_init
;
2158 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2159 ret
= qemu_rdma_reg_control(rdma
, idx
);
2161 ERROR(temp
, "rdma migration: error registering %d control!",
2163 goto err_rdma_source_init
;
2169 err_rdma_source_init
:
2170 error_propagate(errp
, local_err
);
2171 qemu_rdma_cleanup(rdma
);
2175 static int qemu_rdma_connect(RDMAContext
*rdma
, Error
**errp
)
2177 RDMACapabilities cap
= {
2178 .version
= RDMA_CONTROL_VERSION_CURRENT
,
2181 struct rdma_conn_param conn_param
= { .initiator_depth
= 2,
2183 .private_data
= &cap
,
2184 .private_data_len
= sizeof(cap
),
2186 struct rdma_cm_event
*cm_event
;
2190 * Only negotiate the capability with destination if the user
2191 * on the source first requested the capability.
2193 if (rdma
->pin_all
) {
2194 DPRINTF("Server pin-all memory requested.\n");
2195 cap
.flags
|= RDMA_CAPABILITY_PIN_ALL
;
2198 caps_to_network(&cap
);
2200 ret
= rdma_connect(rdma
->cm_id
, &conn_param
);
2202 perror("rdma_connect");
2203 ERROR(errp
, "connecting to destination!");
2204 rdma_destroy_id(rdma
->cm_id
);
2206 goto err_rdma_source_connect
;
2209 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
2211 perror("rdma_get_cm_event after rdma_connect");
2212 ERROR(errp
, "connecting to destination!");
2213 rdma_ack_cm_event(cm_event
);
2214 rdma_destroy_id(rdma
->cm_id
);
2216 goto err_rdma_source_connect
;
2219 if (cm_event
->event
!= RDMA_CM_EVENT_ESTABLISHED
) {
2220 perror("rdma_get_cm_event != EVENT_ESTABLISHED after rdma_connect");
2221 ERROR(errp
, "connecting to destination!");
2222 rdma_ack_cm_event(cm_event
);
2223 rdma_destroy_id(rdma
->cm_id
);
2225 goto err_rdma_source_connect
;
2228 memcpy(&cap
, cm_event
->param
.conn
.private_data
, sizeof(cap
));
2229 network_to_caps(&cap
);
2232 * Verify that the *requested* capabilities are supported by the destination
2233 * and disable them otherwise.
2235 if (rdma
->pin_all
&& !(cap
.flags
& RDMA_CAPABILITY_PIN_ALL
)) {
2236 ERROR(errp
, "Server cannot support pinning all memory. "
2237 "Will register memory dynamically.");
2238 rdma
->pin_all
= false;
2241 DPRINTF("Pin all memory: %s\n", rdma
->pin_all
? "enabled" : "disabled");
2243 rdma_ack_cm_event(cm_event
);
2245 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
2247 ERROR(errp
, "posting second control recv!");
2248 goto err_rdma_source_connect
;
2251 rdma
->control_ready_expected
= 1;
2255 err_rdma_source_connect
:
2256 qemu_rdma_cleanup(rdma
);
2260 static int qemu_rdma_dest_init(RDMAContext
*rdma
, Error
**errp
)
2262 int ret
= -EINVAL
, idx
;
2263 int af
= rdma
->ipv6
? PF_INET6
: PF_INET
;
2264 struct sockaddr_in sin
;
2265 struct rdma_cm_id
*listen_id
;
2266 char ip
[40] = "unknown";
2267 struct addrinfo
*res
;
2270 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2271 rdma
->wr_data
[idx
].control_len
= 0;
2272 rdma
->wr_data
[idx
].control_curr
= NULL
;
2275 if (rdma
->host
== NULL
) {
2276 ERROR(errp
, "RDMA host is not set!");
2277 rdma
->error_state
= -EINVAL
;
2280 /* create CM channel */
2281 rdma
->channel
= rdma_create_event_channel();
2282 if (!rdma
->channel
) {
2283 ERROR(errp
, "could not create rdma event channel");
2284 rdma
->error_state
= -EINVAL
;
2289 ret
= rdma_create_id(rdma
->channel
, &listen_id
, NULL
, RDMA_PS_TCP
);
2291 ERROR(errp
, "could not create cm_id!");
2292 goto err_dest_init_create_listen_id
;
2295 memset(&sin
, 0, sizeof(sin
));
2296 sin
.sin_family
= af
;
2297 sin
.sin_port
= htons(rdma
->port
);
2298 snprintf(port_str
, 16, "%d", rdma
->port
);
2299 port_str
[15] = '\0';
2301 if (rdma
->host
&& strcmp("", rdma
->host
)) {
2302 ret
= getaddrinfo(rdma
->host
, port_str
, NULL
, &res
);
2304 ERROR(errp
, "could not getaddrinfo address %s", rdma
->host
);
2305 goto err_dest_init_bind_addr
;
2309 inet_ntop(af
, &((struct sockaddr_in
*) res
->ai_addr
)->sin_addr
,
2312 ERROR(errp
, "migration host and port not specified!");
2314 goto err_dest_init_bind_addr
;
2317 DPRINTF("%s => %s\n", rdma
->host
, ip
);
2319 ret
= rdma_bind_addr(listen_id
, res
->ai_addr
);
2321 ERROR(errp
, "Error: could not rdma_bind_addr!");
2322 goto err_dest_init_bind_addr
;
2325 rdma
->listen_id
= listen_id
;
2326 qemu_rdma_dump_gid("dest_init", listen_id
);
2329 err_dest_init_bind_addr
:
2330 rdma_destroy_id(listen_id
);
2331 err_dest_init_create_listen_id
:
2332 rdma_destroy_event_channel(rdma
->channel
);
2333 rdma
->channel
= NULL
;
2334 rdma
->error_state
= ret
;
2339 static void *qemu_rdma_data_init(const char *host_port
, Error
**errp
)
2341 RDMAContext
*rdma
= NULL
;
2342 InetSocketAddress
*addr
;
2345 rdma
= g_malloc0(sizeof(RDMAContext
));
2346 memset(rdma
, 0, sizeof(RDMAContext
));
2347 rdma
->current_index
= -1;
2348 rdma
->current_chunk
= -1;
2350 addr
= inet_parse(host_port
, NULL
);
2352 rdma
->port
= atoi(addr
->port
);
2353 rdma
->host
= g_strdup(addr
->host
);
2354 rdma
->ipv6
= addr
->ipv6
;
2356 ERROR(errp
, "bad RDMA migration address '%s'", host_port
);
2366 * QEMUFile interface to the control channel.
2367 * SEND messages for control only.
2368 * pc.ram is handled with regular RDMA messages.
2370 static int qemu_rdma_put_buffer(void *opaque
, const uint8_t *buf
,
2371 int64_t pos
, int size
)
2373 QEMUFileRDMA
*r
= opaque
;
2374 QEMUFile
*f
= r
->file
;
2375 RDMAContext
*rdma
= r
->rdma
;
2376 size_t remaining
= size
;
2377 uint8_t * data
= (void *) buf
;
2380 CHECK_ERROR_STATE();
2383 * Push out any writes that
2384 * we're queued up for pc.ram.
2386 ret
= qemu_rdma_write_flush(f
, rdma
);
2388 rdma
->error_state
= ret
;
2393 RDMAControlHeader head
;
2395 r
->len
= MIN(remaining
, RDMA_SEND_INCREMENT
);
2396 remaining
-= r
->len
;
2399 head
.type
= RDMA_CONTROL_QEMU_FILE
;
2401 ret
= qemu_rdma_exchange_send(rdma
, &head
, data
, NULL
, NULL
, NULL
);
2404 rdma
->error_state
= ret
;
2414 static size_t qemu_rdma_fill(RDMAContext
*rdma
, uint8_t *buf
,
2419 if (rdma
->wr_data
[idx
].control_len
) {
2420 DDDPRINTF("RDMA %" PRId64
" of %d bytes already in buffer\n",
2421 rdma
->wr_data
[idx
].control_len
, size
);
2423 len
= MIN(size
, rdma
->wr_data
[idx
].control_len
);
2424 memcpy(buf
, rdma
->wr_data
[idx
].control_curr
, len
);
2425 rdma
->wr_data
[idx
].control_curr
+= len
;
2426 rdma
->wr_data
[idx
].control_len
-= len
;
2433 * QEMUFile interface to the control channel.
2434 * RDMA links don't use bytestreams, so we have to
2435 * return bytes to QEMUFile opportunistically.
2437 static int qemu_rdma_get_buffer(void *opaque
, uint8_t *buf
,
2438 int64_t pos
, int size
)
2440 QEMUFileRDMA
*r
= opaque
;
2441 RDMAContext
*rdma
= r
->rdma
;
2442 RDMAControlHeader head
;
2445 CHECK_ERROR_STATE();
2448 * First, we hold on to the last SEND message we
2449 * were given and dish out the bytes until we run
2452 r
->len
= qemu_rdma_fill(r
->rdma
, buf
, size
, 0);
2458 * Once we run out, we block and wait for another
2459 * SEND message to arrive.
2461 ret
= qemu_rdma_exchange_recv(rdma
, &head
, RDMA_CONTROL_QEMU_FILE
);
2464 rdma
->error_state
= ret
;
2469 * SEND was received with new bytes, now try again.
2471 return qemu_rdma_fill(r
->rdma
, buf
, size
, 0);
2475 * Block until all the outstanding chunks have been delivered by the hardware.
2477 static int qemu_rdma_drain_cq(QEMUFile
*f
, RDMAContext
*rdma
)
2481 if (qemu_rdma_write_flush(f
, rdma
) < 0) {
2485 while (rdma
->nb_sent
) {
2486 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RDMA_WRITE
, NULL
);
2488 fprintf(stderr
, "rdma migration: complete polling error!\n");
2493 qemu_rdma_unregister_waiting(rdma
);
2498 static int qemu_rdma_close(void *opaque
)
2500 DPRINTF("Shutting down connection.\n");
2501 QEMUFileRDMA
*r
= opaque
;
2503 qemu_rdma_cleanup(r
->rdma
);
2513 * This means that 'block_offset' is a full virtual address that does not
2514 * belong to a RAMBlock of the virtual machine and instead
2515 * represents a private malloc'd memory area that the caller wishes to
2519 * Offset is an offset to be added to block_offset and used
2520 * to also lookup the corresponding RAMBlock.
2523 * Initiate an transfer this size.
2526 * A 'hint' or 'advice' that means that we wish to speculatively
2527 * and asynchronously unregister this memory. In this case, there is no
2528 * guarantee that the unregister will actually happen, for example,
2529 * if the memory is being actively transmitted. Additionally, the memory
2530 * may be re-registered at any future time if a write within the same
2531 * chunk was requested again, even if you attempted to unregister it
2534 * @size < 0 : TODO, not yet supported
2535 * Unregister the memory NOW. This means that the caller does not
2536 * expect there to be any future RDMA transfers and we just want to clean
2537 * things up. This is used in case the upper layer owns the memory and
2538 * cannot wait for qemu_fclose() to occur.
2540 * @bytes_sent : User-specificed pointer to indicate how many bytes were
2541 * sent. Usually, this will not be more than a few bytes of
2542 * the protocol because most transfers are sent asynchronously.
2544 static size_t qemu_rdma_save_page(QEMUFile
*f
, void *opaque
,
2545 ram_addr_t block_offset
, ram_addr_t offset
,
2546 size_t size
, int *bytes_sent
)
2548 QEMUFileRDMA
*rfile
= opaque
;
2549 RDMAContext
*rdma
= rfile
->rdma
;
2552 CHECK_ERROR_STATE();
2558 * Add this page to the current 'chunk'. If the chunk
2559 * is full, or the page doen't belong to the current chunk,
2560 * an actual RDMA write will occur and a new chunk will be formed.
2562 ret
= qemu_rdma_write(f
, rdma
, block_offset
, offset
, size
);
2564 fprintf(stderr
, "rdma migration: write error! %d\n", ret
);
2569 * We always return 1 bytes because the RDMA
2570 * protocol is completely asynchronous. We do not yet know
2571 * whether an identified chunk is zero or not because we're
2572 * waiting for other pages to potentially be merged with
2573 * the current chunk. So, we have to call qemu_update_position()
2574 * later on when the actual write occurs.
2580 uint64_t index
, chunk
;
2582 /* TODO: Change QEMUFileOps prototype to be signed: size_t => long
2584 ret = qemu_rdma_drain_cq(f, rdma);
2586 fprintf(stderr, "rdma: failed to synchronously drain"
2587 " completion queue before unregistration.\n");
2593 ret
= qemu_rdma_search_ram_block(rdma
, block_offset
,
2594 offset
, size
, &index
, &chunk
);
2597 fprintf(stderr
, "ram block search failed\n");
2601 qemu_rdma_signal_unregister(rdma
, index
, chunk
, 0);
2604 * TODO: Synchronous, guaranteed unregistration (should not occur during
2605 * fast-path). Otherwise, unregisters will process on the next call to
2606 * qemu_rdma_drain_cq()
2608 qemu_rdma_unregister_waiting(rdma);
2614 * Drain the Completion Queue if possible, but do not block,
2617 * If nothing to poll, the end of the iteration will do this
2618 * again to make sure we don't overflow the request queue.
2621 uint64_t wr_id
, wr_id_in
;
2622 int ret
= qemu_rdma_poll(rdma
, &wr_id_in
, NULL
);
2624 fprintf(stderr
, "rdma migration: polling error! %d\n", ret
);
2628 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
2630 if (wr_id
== RDMA_WRID_NONE
) {
2635 return RAM_SAVE_CONTROL_DELAYED
;
2637 rdma
->error_state
= ret
;
2641 static int qemu_rdma_accept(RDMAContext
*rdma
)
2643 RDMACapabilities cap
;
2644 struct rdma_conn_param conn_param
= {
2645 .responder_resources
= 2,
2646 .private_data
= &cap
,
2647 .private_data_len
= sizeof(cap
),
2649 struct rdma_cm_event
*cm_event
;
2650 struct ibv_context
*verbs
;
2654 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
2656 goto err_rdma_dest_wait
;
2659 if (cm_event
->event
!= RDMA_CM_EVENT_CONNECT_REQUEST
) {
2660 rdma_ack_cm_event(cm_event
);
2661 goto err_rdma_dest_wait
;
2664 memcpy(&cap
, cm_event
->param
.conn
.private_data
, sizeof(cap
));
2666 network_to_caps(&cap
);
2668 if (cap
.version
< 1 || cap
.version
> RDMA_CONTROL_VERSION_CURRENT
) {
2669 fprintf(stderr
, "Unknown source RDMA version: %d, bailing...\n",
2671 rdma_ack_cm_event(cm_event
);
2672 goto err_rdma_dest_wait
;
2676 * Respond with only the capabilities this version of QEMU knows about.
2678 cap
.flags
&= known_capabilities
;
2681 * Enable the ones that we do know about.
2682 * Add other checks here as new ones are introduced.
2684 if (cap
.flags
& RDMA_CAPABILITY_PIN_ALL
) {
2685 rdma
->pin_all
= true;
2688 rdma
->cm_id
= cm_event
->id
;
2689 verbs
= cm_event
->id
->verbs
;
2691 rdma_ack_cm_event(cm_event
);
2693 DPRINTF("Memory pin all: %s\n", rdma
->pin_all
? "enabled" : "disabled");
2695 caps_to_network(&cap
);
2697 DPRINTF("verbs context after listen: %p\n", verbs
);
2700 rdma
->verbs
= verbs
;
2701 } else if (rdma
->verbs
!= verbs
) {
2702 fprintf(stderr
, "ibv context not matching %p, %p!\n",
2703 rdma
->verbs
, verbs
);
2704 goto err_rdma_dest_wait
;
2707 qemu_rdma_dump_id("dest_init", verbs
);
2709 ret
= qemu_rdma_alloc_pd_cq(rdma
);
2711 fprintf(stderr
, "rdma migration: error allocating pd and cq!\n");
2712 goto err_rdma_dest_wait
;
2715 ret
= qemu_rdma_alloc_qp(rdma
);
2717 fprintf(stderr
, "rdma migration: error allocating qp!\n");
2718 goto err_rdma_dest_wait
;
2721 ret
= qemu_rdma_init_ram_blocks(rdma
);
2723 fprintf(stderr
, "rdma migration: error initializing ram blocks!\n");
2724 goto err_rdma_dest_wait
;
2727 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2728 ret
= qemu_rdma_reg_control(rdma
, idx
);
2730 fprintf(stderr
, "rdma: error registering %d control!\n", idx
);
2731 goto err_rdma_dest_wait
;
2735 qemu_set_fd_handler2(rdma
->channel
->fd
, NULL
, NULL
, NULL
, NULL
);
2737 ret
= rdma_accept(rdma
->cm_id
, &conn_param
);
2739 fprintf(stderr
, "rdma_accept returns %d!\n", ret
);
2740 goto err_rdma_dest_wait
;
2743 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
2745 fprintf(stderr
, "rdma_accept get_cm_event failed %d!\n", ret
);
2746 goto err_rdma_dest_wait
;
2749 if (cm_event
->event
!= RDMA_CM_EVENT_ESTABLISHED
) {
2750 fprintf(stderr
, "rdma_accept not event established!\n");
2751 rdma_ack_cm_event(cm_event
);
2752 goto err_rdma_dest_wait
;
2755 rdma_ack_cm_event(cm_event
);
2757 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
2759 fprintf(stderr
, "rdma migration: error posting second control recv!\n");
2760 goto err_rdma_dest_wait
;
2763 qemu_rdma_dump_gid("dest_connect", rdma
->cm_id
);
2768 rdma
->error_state
= ret
;
2769 qemu_rdma_cleanup(rdma
);
2774 * During each iteration of the migration, we listen for instructions
2775 * by the source VM to perform dynamic page registrations before they
2776 * can perform RDMA operations.
2778 * We respond with the 'rkey'.
2780 * Keep doing this until the source tells us to stop.
2782 static int qemu_rdma_registration_handle(QEMUFile
*f
, void *opaque
,
2785 RDMAControlHeader reg_resp
= { .len
= sizeof(RDMARegisterResult
),
2786 .type
= RDMA_CONTROL_REGISTER_RESULT
,
2789 RDMAControlHeader unreg_resp
= { .len
= 0,
2790 .type
= RDMA_CONTROL_UNREGISTER_FINISHED
,
2793 RDMAControlHeader blocks
= { .type
= RDMA_CONTROL_RAM_BLOCKS_RESULT
,
2795 QEMUFileRDMA
*rfile
= opaque
;
2796 RDMAContext
*rdma
= rfile
->rdma
;
2797 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
2798 RDMAControlHeader head
;
2799 RDMARegister
*reg
, *registers
;
2801 RDMARegisterResult
*reg_result
;
2802 static RDMARegisterResult results
[RDMA_CONTROL_MAX_COMMANDS_PER_MESSAGE
];
2803 RDMALocalBlock
*block
;
2810 CHECK_ERROR_STATE();
2813 DDDPRINTF("Waiting for next request %" PRIu64
"...\n", flags
);
2815 ret
= qemu_rdma_exchange_recv(rdma
, &head
, RDMA_CONTROL_NONE
);
2821 if (head
.repeat
> RDMA_CONTROL_MAX_COMMANDS_PER_MESSAGE
) {
2822 fprintf(stderr
, "rdma: Too many requests in this message (%d)."
2823 "Bailing.\n", head
.repeat
);
2828 switch (head
.type
) {
2829 case RDMA_CONTROL_COMPRESS
:
2830 comp
= (RDMACompress
*) rdma
->wr_data
[idx
].control_curr
;
2831 network_to_compress(comp
);
2833 DDPRINTF("Zapping zero chunk: %" PRId64
2834 " bytes, index %d, offset %" PRId64
"\n",
2835 comp
->length
, comp
->block_idx
, comp
->offset
);
2836 block
= &(rdma
->local_ram_blocks
.block
[comp
->block_idx
]);
2838 host_addr
= block
->local_host_addr
+
2839 (comp
->offset
- block
->offset
);
2841 ram_handle_compressed(host_addr
, comp
->value
, comp
->length
);
2844 case RDMA_CONTROL_REGISTER_FINISHED
:
2845 DDDPRINTF("Current registrations complete.\n");
2848 case RDMA_CONTROL_RAM_BLOCKS_REQUEST
:
2849 DPRINTF("Initial setup info requested.\n");
2851 if (rdma
->pin_all
) {
2852 ret
= qemu_rdma_reg_whole_ram_blocks(rdma
);
2854 fprintf(stderr
, "rdma migration: error dest "
2855 "registering ram blocks!\n");
2861 * Dest uses this to prepare to transmit the RAMBlock descriptions
2862 * to the source VM after connection setup.
2863 * Both sides use the "remote" structure to communicate and update
2864 * their "local" descriptions with what was sent.
2866 for (i
= 0; i
< local
->nb_blocks
; i
++) {
2867 rdma
->block
[i
].remote_host_addr
=
2868 (uint64_t)(local
->block
[i
].local_host_addr
);
2870 if (rdma
->pin_all
) {
2871 rdma
->block
[i
].remote_rkey
= local
->block
[i
].mr
->rkey
;
2874 rdma
->block
[i
].offset
= local
->block
[i
].offset
;
2875 rdma
->block
[i
].length
= local
->block
[i
].length
;
2877 remote_block_to_network(&rdma
->block
[i
]);
2880 blocks
.len
= rdma
->local_ram_blocks
.nb_blocks
2881 * sizeof(RDMARemoteBlock
);
2884 ret
= qemu_rdma_post_send_control(rdma
,
2885 (uint8_t *) rdma
->block
, &blocks
);
2888 fprintf(stderr
, "rdma migration: error sending remote info!\n");
2893 case RDMA_CONTROL_REGISTER_REQUEST
:
2894 DDPRINTF("There are %d registration requests\n", head
.repeat
);
2896 reg_resp
.repeat
= head
.repeat
;
2897 registers
= (RDMARegister
*) rdma
->wr_data
[idx
].control_curr
;
2899 for (count
= 0; count
< head
.repeat
; count
++) {
2901 uint8_t *chunk_start
, *chunk_end
;
2903 reg
= ®isters
[count
];
2904 network_to_register(reg
);
2906 reg_result
= &results
[count
];
2908 DDPRINTF("Registration request (%d): index %d, current_addr %"
2909 PRIu64
" chunks: %" PRIu64
"\n", count
,
2910 reg
->current_index
, reg
->key
.current_addr
, reg
->chunks
);
2912 block
= &(rdma
->local_ram_blocks
.block
[reg
->current_index
]);
2913 if (block
->is_ram_block
) {
2914 host_addr
= (block
->local_host_addr
+
2915 (reg
->key
.current_addr
- block
->offset
));
2916 chunk
= ram_chunk_index(block
->local_host_addr
,
2917 (uint8_t *) host_addr
);
2919 chunk
= reg
->key
.chunk
;
2920 host_addr
= block
->local_host_addr
+
2921 (reg
->key
.chunk
* (1UL << RDMA_REG_CHUNK_SHIFT
));
2923 chunk_start
= ram_chunk_start(block
, chunk
);
2924 chunk_end
= ram_chunk_end(block
, chunk
+ reg
->chunks
);
2925 if (qemu_rdma_register_and_get_keys(rdma
, block
,
2926 (uint8_t *)host_addr
, NULL
, ®_result
->rkey
,
2927 chunk
, chunk_start
, chunk_end
)) {
2928 fprintf(stderr
, "cannot get rkey!\n");
2933 reg_result
->host_addr
= (uint64_t) block
->local_host_addr
;
2935 DDPRINTF("Registered rkey for this request: %x\n",
2938 result_to_network(reg_result
);
2941 ret
= qemu_rdma_post_send_control(rdma
,
2942 (uint8_t *) results
, ®_resp
);
2945 fprintf(stderr
, "Failed to send control buffer!\n");
2949 case RDMA_CONTROL_UNREGISTER_REQUEST
:
2950 DDPRINTF("There are %d unregistration requests\n", head
.repeat
);
2951 unreg_resp
.repeat
= head
.repeat
;
2952 registers
= (RDMARegister
*) rdma
->wr_data
[idx
].control_curr
;
2954 for (count
= 0; count
< head
.repeat
; count
++) {
2955 reg
= ®isters
[count
];
2956 network_to_register(reg
);
2958 DDPRINTF("Unregistration request (%d): "
2959 " index %d, chunk %" PRIu64
"\n",
2960 count
, reg
->current_index
, reg
->key
.chunk
);
2962 block
= &(rdma
->local_ram_blocks
.block
[reg
->current_index
]);
2964 ret
= ibv_dereg_mr(block
->pmr
[reg
->key
.chunk
]);
2965 block
->pmr
[reg
->key
.chunk
] = NULL
;
2968 perror("rdma unregistration chunk failed");
2973 rdma
->total_registrations
--;
2975 DDPRINTF("Unregistered chunk %" PRIu64
" successfully.\n",
2979 ret
= qemu_rdma_post_send_control(rdma
, NULL
, &unreg_resp
);
2982 fprintf(stderr
, "Failed to send control buffer!\n");
2986 case RDMA_CONTROL_REGISTER_RESULT
:
2987 fprintf(stderr
, "Invalid RESULT message at dest.\n");
2991 fprintf(stderr
, "Unknown control message %s\n",
2992 control_desc
[head
.type
]);
2999 rdma
->error_state
= ret
;
3004 static int qemu_rdma_registration_start(QEMUFile
*f
, void *opaque
,
3007 QEMUFileRDMA
*rfile
= opaque
;
3008 RDMAContext
*rdma
= rfile
->rdma
;
3010 CHECK_ERROR_STATE();
3012 DDDPRINTF("start section: %" PRIu64
"\n", flags
);
3013 qemu_put_be64(f
, RAM_SAVE_FLAG_HOOK
);
3020 * Inform dest that dynamic registrations are done for now.
3021 * First, flush writes, if any.
3023 static int qemu_rdma_registration_stop(QEMUFile
*f
, void *opaque
,
3026 Error
*local_err
= NULL
, **errp
= &local_err
;
3027 QEMUFileRDMA
*rfile
= opaque
;
3028 RDMAContext
*rdma
= rfile
->rdma
;
3029 RDMAControlHeader head
= { .len
= 0, .repeat
= 1 };
3032 CHECK_ERROR_STATE();
3035 ret
= qemu_rdma_drain_cq(f
, rdma
);
3041 if (flags
== RAM_CONTROL_SETUP
) {
3042 RDMAControlHeader resp
= {.type
= RDMA_CONTROL_RAM_BLOCKS_RESULT
};
3043 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
3044 int reg_result_idx
, i
, j
, nb_remote_blocks
;
3046 head
.type
= RDMA_CONTROL_RAM_BLOCKS_REQUEST
;
3047 DPRINTF("Sending registration setup for ram blocks...\n");
3050 * Make sure that we parallelize the pinning on both sides.
3051 * For very large guests, doing this serially takes a really
3052 * long time, so we have to 'interleave' the pinning locally
3053 * with the control messages by performing the pinning on this
3054 * side before we receive the control response from the other
3055 * side that the pinning has completed.
3057 ret
= qemu_rdma_exchange_send(rdma
, &head
, NULL
, &resp
,
3058 ®_result_idx
, rdma
->pin_all
?
3059 qemu_rdma_reg_whole_ram_blocks
: NULL
);
3061 ERROR(errp
, "receiving remote info!");
3065 nb_remote_blocks
= resp
.len
/ sizeof(RDMARemoteBlock
);
3068 * The protocol uses two different sets of rkeys (mutually exclusive):
3069 * 1. One key to represent the virtual address of the entire ram block.
3070 * (dynamic chunk registration disabled - pin everything with one rkey.)
3071 * 2. One to represent individual chunks within a ram block.
3072 * (dynamic chunk registration enabled - pin individual chunks.)
3074 * Once the capability is successfully negotiated, the destination transmits
3075 * the keys to use (or sends them later) including the virtual addresses
3076 * and then propagates the remote ram block descriptions to his local copy.
3079 if (local
->nb_blocks
!= nb_remote_blocks
) {
3080 ERROR(errp
, "ram blocks mismatch #1! "
3081 "Your QEMU command line parameters are probably "
3082 "not identical on both the source and destination.");
3086 qemu_rdma_move_header(rdma
, reg_result_idx
, &resp
);
3088 rdma
->wr_data
[reg_result_idx
].control_curr
, resp
.len
);
3089 for (i
= 0; i
< nb_remote_blocks
; i
++) {
3090 network_to_remote_block(&rdma
->block
[i
]);
3092 /* search local ram blocks */
3093 for (j
= 0; j
< local
->nb_blocks
; j
++) {
3094 if (rdma
->block
[i
].offset
!= local
->block
[j
].offset
) {
3098 if (rdma
->block
[i
].length
!= local
->block
[j
].length
) {
3099 ERROR(errp
, "ram blocks mismatch #2! "
3100 "Your QEMU command line parameters are probably "
3101 "not identical on both the source and destination.");
3104 local
->block
[j
].remote_host_addr
=
3105 rdma
->block
[i
].remote_host_addr
;
3106 local
->block
[j
].remote_rkey
= rdma
->block
[i
].remote_rkey
;
3110 if (j
>= local
->nb_blocks
) {
3111 ERROR(errp
, "ram blocks mismatch #3! "
3112 "Your QEMU command line parameters are probably "
3113 "not identical on both the source and destination.");
3119 DDDPRINTF("Sending registration finish %" PRIu64
"...\n", flags
);
3121 head
.type
= RDMA_CONTROL_REGISTER_FINISHED
;
3122 ret
= qemu_rdma_exchange_send(rdma
, &head
, NULL
, NULL
, NULL
, NULL
);
3130 rdma
->error_state
= ret
;
3134 static int qemu_rdma_get_fd(void *opaque
)
3136 QEMUFileRDMA
*rfile
= opaque
;
3137 RDMAContext
*rdma
= rfile
->rdma
;
3139 return rdma
->comp_channel
->fd
;
3142 const QEMUFileOps rdma_read_ops
= {
3143 .get_buffer
= qemu_rdma_get_buffer
,
3144 .get_fd
= qemu_rdma_get_fd
,
3145 .close
= qemu_rdma_close
,
3146 .hook_ram_load
= qemu_rdma_registration_handle
,
3149 const QEMUFileOps rdma_write_ops
= {
3150 .put_buffer
= qemu_rdma_put_buffer
,
3151 .close
= qemu_rdma_close
,
3152 .before_ram_iterate
= qemu_rdma_registration_start
,
3153 .after_ram_iterate
= qemu_rdma_registration_stop
,
3154 .save_page
= qemu_rdma_save_page
,
3157 static void *qemu_fopen_rdma(RDMAContext
*rdma
, const char *mode
)
3159 QEMUFileRDMA
*r
= g_malloc0(sizeof(QEMUFileRDMA
));
3161 if (qemu_file_mode_is_not_valid(mode
)) {
3167 if (mode
[0] == 'w') {
3168 r
->file
= qemu_fopen_ops(r
, &rdma_write_ops
);
3170 r
->file
= qemu_fopen_ops(r
, &rdma_read_ops
);
3176 static void rdma_accept_incoming_migration(void *opaque
)
3178 RDMAContext
*rdma
= opaque
;
3181 Error
*local_err
= NULL
, **errp
= &local_err
;
3183 DPRINTF("Accepting rdma connection...\n");
3184 ret
= qemu_rdma_accept(rdma
);
3187 ERROR(errp
, "RDMA Migration initialization failed!");
3191 DPRINTF("Accepted migration\n");
3193 f
= qemu_fopen_rdma(rdma
, "rb");
3195 ERROR(errp
, "could not qemu_fopen_rdma!");
3196 qemu_rdma_cleanup(rdma
);
3200 rdma
->migration_started_on_destination
= 1;
3201 process_incoming_migration(f
);
3204 void rdma_start_incoming_migration(const char *host_port
, Error
**errp
)
3208 Error
*local_err
= NULL
;
3210 DPRINTF("Starting RDMA-based incoming migration\n");
3211 rdma
= qemu_rdma_data_init(host_port
, &local_err
);
3217 ret
= qemu_rdma_dest_init(rdma
, &local_err
);
3223 DPRINTF("qemu_rdma_dest_init success\n");
3225 ret
= rdma_listen(rdma
->listen_id
, 5);
3228 ERROR(errp
, "listening on socket!");
3232 DPRINTF("rdma_listen success\n");
3234 qemu_set_fd_handler2(rdma
->channel
->fd
, NULL
,
3235 rdma_accept_incoming_migration
, NULL
,
3236 (void *)(intptr_t) rdma
);
3239 error_propagate(errp
, local_err
);
3243 void rdma_start_outgoing_migration(void *opaque
,
3244 const char *host_port
, Error
**errp
)
3246 MigrationState
*s
= opaque
;
3247 Error
*local_err
= NULL
, **temp
= &local_err
;
3248 RDMAContext
*rdma
= qemu_rdma_data_init(host_port
, &local_err
);
3252 ERROR(temp
, "Failed to initialize RDMA data structures! %d", ret
);
3256 ret
= qemu_rdma_source_init(rdma
, &local_err
,
3257 s
->enabled_capabilities
[MIGRATION_CAPABILITY_X_RDMA_PIN_ALL
]);
3263 DPRINTF("qemu_rdma_source_init success\n");
3264 ret
= qemu_rdma_connect(rdma
, &local_err
);
3270 DPRINTF("qemu_rdma_source_connect success\n");
3272 s
->file
= qemu_fopen_rdma(rdma
, "wb");
3273 migrate_fd_connect(s
);
3276 error_propagate(errp
, local_err
);
3278 migrate_fd_error(s
);