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>
32 * Print and error on both the Monitor and the Log file.
34 #define ERROR(errp, fmt, ...) \
36 fprintf(stderr, "RDMA ERROR: " fmt "\n", ## __VA_ARGS__); \
37 if (errp && (*(errp) == NULL)) { \
38 error_setg(errp, "RDMA ERROR: " fmt, ## __VA_ARGS__); \
42 #define RDMA_RESOLVE_TIMEOUT_MS 10000
44 /* Do not merge data if larger than this. */
45 #define RDMA_MERGE_MAX (2 * 1024 * 1024)
46 #define RDMA_SIGNALED_SEND_MAX (RDMA_MERGE_MAX / 4096)
48 #define RDMA_REG_CHUNK_SHIFT 20 /* 1 MB */
51 * This is only for non-live state being migrated.
52 * Instead of RDMA_WRITE messages, we use RDMA_SEND
53 * messages for that state, which requires a different
54 * delivery design than main memory.
56 #define RDMA_SEND_INCREMENT 32768
59 * Maximum size infiniband SEND message
61 #define RDMA_CONTROL_MAX_BUFFER (512 * 1024)
62 #define RDMA_CONTROL_MAX_COMMANDS_PER_MESSAGE 4096
64 #define RDMA_CONTROL_VERSION_CURRENT 1
66 * Capabilities for negotiation.
68 #define RDMA_CAPABILITY_PIN_ALL 0x01
71 * Add the other flags above to this list of known capabilities
72 * as they are introduced.
74 static uint32_t known_capabilities
= RDMA_CAPABILITY_PIN_ALL
;
76 #define CHECK_ERROR_STATE() \
78 if (rdma->error_state) { \
79 if (!rdma->error_reported) { \
80 error_report("RDMA is in an error state waiting migration" \
82 rdma->error_reported = 1; \
84 return rdma->error_state; \
89 * A work request ID is 64-bits and we split up these bits
92 * bits 0-15 : type of control message, 2^16
93 * bits 16-29: ram block index, 2^14
94 * bits 30-63: ram block chunk number, 2^34
96 * The last two bit ranges are only used for RDMA writes,
97 * in order to track their completion and potentially
98 * also track unregistration status of the message.
100 #define RDMA_WRID_TYPE_SHIFT 0UL
101 #define RDMA_WRID_BLOCK_SHIFT 16UL
102 #define RDMA_WRID_CHUNK_SHIFT 30UL
104 #define RDMA_WRID_TYPE_MASK \
105 ((1UL << RDMA_WRID_BLOCK_SHIFT) - 1UL)
107 #define RDMA_WRID_BLOCK_MASK \
108 (~RDMA_WRID_TYPE_MASK & ((1UL << RDMA_WRID_CHUNK_SHIFT) - 1UL))
110 #define RDMA_WRID_CHUNK_MASK (~RDMA_WRID_BLOCK_MASK & ~RDMA_WRID_TYPE_MASK)
113 * RDMA migration protocol:
114 * 1. RDMA Writes (data messages, i.e. RAM)
115 * 2. IB Send/Recv (control channel messages)
119 RDMA_WRID_RDMA_WRITE
= 1,
120 RDMA_WRID_SEND_CONTROL
= 2000,
121 RDMA_WRID_RECV_CONTROL
= 4000,
124 static const char *wrid_desc
[] = {
125 [RDMA_WRID_NONE
] = "NONE",
126 [RDMA_WRID_RDMA_WRITE
] = "WRITE RDMA",
127 [RDMA_WRID_SEND_CONTROL
] = "CONTROL SEND",
128 [RDMA_WRID_RECV_CONTROL
] = "CONTROL RECV",
132 * Work request IDs for IB SEND messages only (not RDMA writes).
133 * This is used by the migration protocol to transmit
134 * control messages (such as device state and registration commands)
136 * We could use more WRs, but we have enough for now.
146 * SEND/RECV IB Control Messages.
149 RDMA_CONTROL_NONE
= 0,
151 RDMA_CONTROL_READY
, /* ready to receive */
152 RDMA_CONTROL_QEMU_FILE
, /* QEMUFile-transmitted bytes */
153 RDMA_CONTROL_RAM_BLOCKS_REQUEST
, /* RAMBlock synchronization */
154 RDMA_CONTROL_RAM_BLOCKS_RESULT
, /* RAMBlock synchronization */
155 RDMA_CONTROL_COMPRESS
, /* page contains repeat values */
156 RDMA_CONTROL_REGISTER_REQUEST
, /* dynamic page registration */
157 RDMA_CONTROL_REGISTER_RESULT
, /* key to use after registration */
158 RDMA_CONTROL_REGISTER_FINISHED
, /* current iteration finished */
159 RDMA_CONTROL_UNREGISTER_REQUEST
, /* dynamic UN-registration */
160 RDMA_CONTROL_UNREGISTER_FINISHED
, /* unpinning finished */
163 static const char *control_desc
[] = {
164 [RDMA_CONTROL_NONE
] = "NONE",
165 [RDMA_CONTROL_ERROR
] = "ERROR",
166 [RDMA_CONTROL_READY
] = "READY",
167 [RDMA_CONTROL_QEMU_FILE
] = "QEMU FILE",
168 [RDMA_CONTROL_RAM_BLOCKS_REQUEST
] = "RAM BLOCKS REQUEST",
169 [RDMA_CONTROL_RAM_BLOCKS_RESULT
] = "RAM BLOCKS RESULT",
170 [RDMA_CONTROL_COMPRESS
] = "COMPRESS",
171 [RDMA_CONTROL_REGISTER_REQUEST
] = "REGISTER REQUEST",
172 [RDMA_CONTROL_REGISTER_RESULT
] = "REGISTER RESULT",
173 [RDMA_CONTROL_REGISTER_FINISHED
] = "REGISTER FINISHED",
174 [RDMA_CONTROL_UNREGISTER_REQUEST
] = "UNREGISTER REQUEST",
175 [RDMA_CONTROL_UNREGISTER_FINISHED
] = "UNREGISTER FINISHED",
179 * Memory and MR structures used to represent an IB Send/Recv work request.
180 * This is *not* used for RDMA writes, only IB Send/Recv.
183 uint8_t control
[RDMA_CONTROL_MAX_BUFFER
]; /* actual buffer to register */
184 struct ibv_mr
*control_mr
; /* registration metadata */
185 size_t control_len
; /* length of the message */
186 uint8_t *control_curr
; /* start of unconsumed bytes */
187 } RDMAWorkRequestData
;
190 * Negotiate RDMA capabilities during connection-setup time.
197 static void caps_to_network(RDMACapabilities
*cap
)
199 cap
->version
= htonl(cap
->version
);
200 cap
->flags
= htonl(cap
->flags
);
203 static void network_to_caps(RDMACapabilities
*cap
)
205 cap
->version
= ntohl(cap
->version
);
206 cap
->flags
= ntohl(cap
->flags
);
210 * Representation of a RAMBlock from an RDMA perspective.
211 * This is not transmitted, only local.
212 * This and subsequent structures cannot be linked lists
213 * because we're using a single IB message to transmit
214 * the information. It's small anyway, so a list is overkill.
216 typedef struct RDMALocalBlock
{
217 uint8_t *local_host_addr
; /* local virtual address */
218 uint64_t remote_host_addr
; /* remote virtual address */
221 struct ibv_mr
**pmr
; /* MRs for chunk-level registration */
222 struct ibv_mr
*mr
; /* MR for non-chunk-level registration */
223 uint32_t *remote_keys
; /* rkeys for chunk-level registration */
224 uint32_t remote_rkey
; /* rkeys for non-chunk-level registration */
225 int index
; /* which block are we */
228 unsigned long *transit_bitmap
;
229 unsigned long *unregister_bitmap
;
233 * Also represents a RAMblock, but only on the dest.
234 * This gets transmitted by the dest during connection-time
235 * to the source VM and then is used to populate the
236 * corresponding RDMALocalBlock with
237 * the information needed to perform the actual RDMA.
239 typedef struct QEMU_PACKED RDMARemoteBlock
{
240 uint64_t remote_host_addr
;
243 uint32_t remote_rkey
;
247 static uint64_t htonll(uint64_t v
)
249 union { uint32_t lv
[2]; uint64_t llv
; } u
;
250 u
.lv
[0] = htonl(v
>> 32);
251 u
.lv
[1] = htonl(v
& 0xFFFFFFFFULL
);
255 static uint64_t ntohll(uint64_t v
) {
256 union { uint32_t lv
[2]; uint64_t llv
; } u
;
258 return ((uint64_t)ntohl(u
.lv
[0]) << 32) | (uint64_t) ntohl(u
.lv
[1]);
261 static void remote_block_to_network(RDMARemoteBlock
*rb
)
263 rb
->remote_host_addr
= htonll(rb
->remote_host_addr
);
264 rb
->offset
= htonll(rb
->offset
);
265 rb
->length
= htonll(rb
->length
);
266 rb
->remote_rkey
= htonl(rb
->remote_rkey
);
269 static void network_to_remote_block(RDMARemoteBlock
*rb
)
271 rb
->remote_host_addr
= ntohll(rb
->remote_host_addr
);
272 rb
->offset
= ntohll(rb
->offset
);
273 rb
->length
= ntohll(rb
->length
);
274 rb
->remote_rkey
= ntohl(rb
->remote_rkey
);
278 * Virtual address of the above structures used for transmitting
279 * the RAMBlock descriptions at connection-time.
280 * This structure is *not* transmitted.
282 typedef struct RDMALocalBlocks
{
284 bool init
; /* main memory init complete */
285 RDMALocalBlock
*block
;
289 * Main data structure for RDMA state.
290 * While there is only one copy of this structure being allocated right now,
291 * this is the place where one would start if you wanted to consider
292 * having more than one RDMA connection open at the same time.
294 typedef struct RDMAContext
{
298 RDMAWorkRequestData wr_data
[RDMA_WRID_MAX
];
301 * This is used by *_exchange_send() to figure out whether or not
302 * the initial "READY" message has already been received or not.
303 * This is because other functions may potentially poll() and detect
304 * the READY message before send() does, in which case we need to
305 * know if it completed.
307 int control_ready_expected
;
309 /* number of outstanding writes */
312 /* store info about current buffer so that we can
313 merge it with future sends */
314 uint64_t current_addr
;
315 uint64_t current_length
;
316 /* index of ram block the current buffer belongs to */
318 /* index of the chunk in the current ram block */
324 * infiniband-specific variables for opening the device
325 * and maintaining connection state and so forth.
327 * cm_id also has ibv_context, rdma_event_channel, and ibv_qp in
328 * cm_id->verbs, cm_id->channel, and cm_id->qp.
330 struct rdma_cm_id
*cm_id
; /* connection manager ID */
331 struct rdma_cm_id
*listen_id
;
334 struct ibv_context
*verbs
;
335 struct rdma_event_channel
*channel
;
336 struct ibv_qp
*qp
; /* queue pair */
337 struct ibv_comp_channel
*comp_channel
; /* completion channel */
338 struct ibv_pd
*pd
; /* protection domain */
339 struct ibv_cq
*cq
; /* completion queue */
342 * If a previous write failed (perhaps because of a failed
343 * memory registration, then do not attempt any future work
344 * and remember the error state.
350 * Description of ram blocks used throughout the code.
352 RDMALocalBlocks local_ram_blocks
;
353 RDMARemoteBlock
*block
;
356 * Migration on *destination* started.
357 * Then use coroutine yield function.
358 * Source runs in a thread, so we don't care.
360 int migration_started_on_destination
;
362 int total_registrations
;
365 int unregister_current
, unregister_next
;
366 uint64_t unregistrations
[RDMA_SIGNALED_SEND_MAX
];
368 GHashTable
*blockmap
;
372 * Interface to the rest of the migration call stack.
374 typedef struct QEMUFileRDMA
{
381 * Main structure for IB Send/Recv control messages.
382 * This gets prepended at the beginning of every Send/Recv.
384 typedef struct QEMU_PACKED
{
385 uint32_t len
; /* Total length of data portion */
386 uint32_t type
; /* which control command to perform */
387 uint32_t repeat
; /* number of commands in data portion of same type */
391 static void control_to_network(RDMAControlHeader
*control
)
393 control
->type
= htonl(control
->type
);
394 control
->len
= htonl(control
->len
);
395 control
->repeat
= htonl(control
->repeat
);
398 static void network_to_control(RDMAControlHeader
*control
)
400 control
->type
= ntohl(control
->type
);
401 control
->len
= ntohl(control
->len
);
402 control
->repeat
= ntohl(control
->repeat
);
406 * Register a single Chunk.
407 * Information sent by the source VM to inform the dest
408 * to register an single chunk of memory before we can perform
409 * the actual RDMA operation.
411 typedef struct QEMU_PACKED
{
413 uint64_t current_addr
; /* offset into the ramblock of the chunk */
414 uint64_t chunk
; /* chunk to lookup if unregistering */
416 uint32_t current_index
; /* which ramblock the chunk belongs to */
418 uint64_t chunks
; /* how many sequential chunks to register */
421 static void register_to_network(RDMARegister
*reg
)
423 reg
->key
.current_addr
= htonll(reg
->key
.current_addr
);
424 reg
->current_index
= htonl(reg
->current_index
);
425 reg
->chunks
= htonll(reg
->chunks
);
428 static void network_to_register(RDMARegister
*reg
)
430 reg
->key
.current_addr
= ntohll(reg
->key
.current_addr
);
431 reg
->current_index
= ntohl(reg
->current_index
);
432 reg
->chunks
= ntohll(reg
->chunks
);
435 typedef struct QEMU_PACKED
{
436 uint32_t value
; /* if zero, we will madvise() */
437 uint32_t block_idx
; /* which ram block index */
438 uint64_t offset
; /* where in the remote ramblock this chunk */
439 uint64_t length
; /* length of the chunk */
442 static void compress_to_network(RDMACompress
*comp
)
444 comp
->value
= htonl(comp
->value
);
445 comp
->block_idx
= htonl(comp
->block_idx
);
446 comp
->offset
= htonll(comp
->offset
);
447 comp
->length
= htonll(comp
->length
);
450 static void network_to_compress(RDMACompress
*comp
)
452 comp
->value
= ntohl(comp
->value
);
453 comp
->block_idx
= ntohl(comp
->block_idx
);
454 comp
->offset
= ntohll(comp
->offset
);
455 comp
->length
= ntohll(comp
->length
);
459 * The result of the dest's memory registration produces an "rkey"
460 * which the source VM must reference in order to perform
461 * the RDMA operation.
463 typedef struct QEMU_PACKED
{
467 } RDMARegisterResult
;
469 static void result_to_network(RDMARegisterResult
*result
)
471 result
->rkey
= htonl(result
->rkey
);
472 result
->host_addr
= htonll(result
->host_addr
);
475 static void network_to_result(RDMARegisterResult
*result
)
477 result
->rkey
= ntohl(result
->rkey
);
478 result
->host_addr
= ntohll(result
->host_addr
);
481 const char *print_wrid(int wrid
);
482 static int qemu_rdma_exchange_send(RDMAContext
*rdma
, RDMAControlHeader
*head
,
483 uint8_t *data
, RDMAControlHeader
*resp
,
485 int (*callback
)(RDMAContext
*rdma
));
487 static inline uint64_t ram_chunk_index(const uint8_t *start
,
490 return ((uintptr_t) host
- (uintptr_t) start
) >> RDMA_REG_CHUNK_SHIFT
;
493 static inline uint8_t *ram_chunk_start(const RDMALocalBlock
*rdma_ram_block
,
496 return (uint8_t *) (((uintptr_t) rdma_ram_block
->local_host_addr
)
497 + (i
<< RDMA_REG_CHUNK_SHIFT
));
500 static inline uint8_t *ram_chunk_end(const RDMALocalBlock
*rdma_ram_block
,
503 uint8_t *result
= ram_chunk_start(rdma_ram_block
, i
) +
504 (1UL << RDMA_REG_CHUNK_SHIFT
);
506 if (result
> (rdma_ram_block
->local_host_addr
+ rdma_ram_block
->length
)) {
507 result
= rdma_ram_block
->local_host_addr
+ rdma_ram_block
->length
;
513 static int rdma_add_block(RDMAContext
*rdma
, void *host_addr
,
514 ram_addr_t block_offset
, uint64_t length
)
516 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
517 RDMALocalBlock
*block
= g_hash_table_lookup(rdma
->blockmap
,
518 (void *) block_offset
);
519 RDMALocalBlock
*old
= local
->block
;
521 assert(block
== NULL
);
523 local
->block
= g_malloc0(sizeof(RDMALocalBlock
) * (local
->nb_blocks
+ 1));
525 if (local
->nb_blocks
) {
528 for (x
= 0; x
< local
->nb_blocks
; x
++) {
529 g_hash_table_remove(rdma
->blockmap
, (void *)old
[x
].offset
);
530 g_hash_table_insert(rdma
->blockmap
, (void *)old
[x
].offset
,
533 memcpy(local
->block
, old
, sizeof(RDMALocalBlock
) * local
->nb_blocks
);
537 block
= &local
->block
[local
->nb_blocks
];
539 block
->local_host_addr
= host_addr
;
540 block
->offset
= block_offset
;
541 block
->length
= length
;
542 block
->index
= local
->nb_blocks
;
543 block
->nb_chunks
= ram_chunk_index(host_addr
, host_addr
+ length
) + 1UL;
544 block
->transit_bitmap
= bitmap_new(block
->nb_chunks
);
545 bitmap_clear(block
->transit_bitmap
, 0, block
->nb_chunks
);
546 block
->unregister_bitmap
= bitmap_new(block
->nb_chunks
);
547 bitmap_clear(block
->unregister_bitmap
, 0, block
->nb_chunks
);
548 block
->remote_keys
= g_malloc0(block
->nb_chunks
* sizeof(uint32_t));
550 block
->is_ram_block
= local
->init
? false : true;
552 g_hash_table_insert(rdma
->blockmap
, (void *) block_offset
, block
);
554 trace_rdma_add_block(local
->nb_blocks
, (uint64_t) block
->local_host_addr
,
555 block
->offset
, block
->length
,
556 (uint64_t) (block
->local_host_addr
+ block
->length
),
557 BITS_TO_LONGS(block
->nb_chunks
) *
558 sizeof(unsigned long) * 8,
567 * Memory regions need to be registered with the device and queue pairs setup
568 * in advanced before the migration starts. This tells us where the RAM blocks
569 * are so that we can register them individually.
571 static void qemu_rdma_init_one_block(void *host_addr
,
572 ram_addr_t block_offset
, ram_addr_t length
, void *opaque
)
574 rdma_add_block(opaque
, host_addr
, block_offset
, length
);
578 * Identify the RAMBlocks and their quantity. They will be references to
579 * identify chunk boundaries inside each RAMBlock and also be referenced
580 * during dynamic page registration.
582 static int qemu_rdma_init_ram_blocks(RDMAContext
*rdma
)
584 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
586 assert(rdma
->blockmap
== NULL
);
587 rdma
->blockmap
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
588 memset(local
, 0, sizeof *local
);
589 qemu_ram_foreach_block(qemu_rdma_init_one_block
, rdma
);
590 trace_qemu_rdma_init_ram_blocks(local
->nb_blocks
);
591 rdma
->block
= (RDMARemoteBlock
*) g_malloc0(sizeof(RDMARemoteBlock
) *
592 rdma
->local_ram_blocks
.nb_blocks
);
597 static int rdma_delete_block(RDMAContext
*rdma
, ram_addr_t block_offset
)
599 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
600 RDMALocalBlock
*block
= g_hash_table_lookup(rdma
->blockmap
,
601 (void *) block_offset
);
602 RDMALocalBlock
*old
= local
->block
;
610 for (j
= 0; j
< block
->nb_chunks
; j
++) {
611 if (!block
->pmr
[j
]) {
614 ibv_dereg_mr(block
->pmr
[j
]);
615 rdma
->total_registrations
--;
622 ibv_dereg_mr(block
->mr
);
623 rdma
->total_registrations
--;
627 g_free(block
->transit_bitmap
);
628 block
->transit_bitmap
= NULL
;
630 g_free(block
->unregister_bitmap
);
631 block
->unregister_bitmap
= NULL
;
633 g_free(block
->remote_keys
);
634 block
->remote_keys
= NULL
;
636 for (x
= 0; x
< local
->nb_blocks
; x
++) {
637 g_hash_table_remove(rdma
->blockmap
, (void *)old
[x
].offset
);
640 if (local
->nb_blocks
> 1) {
642 local
->block
= g_malloc0(sizeof(RDMALocalBlock
) *
643 (local
->nb_blocks
- 1));
646 memcpy(local
->block
, old
, sizeof(RDMALocalBlock
) * block
->index
);
649 if (block
->index
< (local
->nb_blocks
- 1)) {
650 memcpy(local
->block
+ block
->index
, old
+ (block
->index
+ 1),
651 sizeof(RDMALocalBlock
) *
652 (local
->nb_blocks
- (block
->index
+ 1)));
655 assert(block
== local
->block
);
659 trace_rdma_delete_block(local
->nb_blocks
,
660 (uint64_t)block
->local_host_addr
,
661 block
->offset
, block
->length
,
662 (uint64_t)(block
->local_host_addr
+ block
->length
),
663 BITS_TO_LONGS(block
->nb_chunks
) *
664 sizeof(unsigned long) * 8, block
->nb_chunks
);
670 if (local
->nb_blocks
) {
671 for (x
= 0; x
< local
->nb_blocks
; x
++) {
672 g_hash_table_insert(rdma
->blockmap
, (void *)local
->block
[x
].offset
,
681 * Put in the log file which RDMA device was opened and the details
682 * associated with that device.
684 static void qemu_rdma_dump_id(const char *who
, struct ibv_context
*verbs
)
686 struct ibv_port_attr port
;
688 if (ibv_query_port(verbs
, 1, &port
)) {
689 error_report("Failed to query port information");
693 printf("%s RDMA Device opened: kernel name %s "
694 "uverbs device name %s, "
695 "infiniband_verbs class device path %s, "
696 "infiniband class device path %s, "
697 "transport: (%d) %s\n",
700 verbs
->device
->dev_name
,
701 verbs
->device
->dev_path
,
702 verbs
->device
->ibdev_path
,
704 (port
.link_layer
== IBV_LINK_LAYER_INFINIBAND
) ? "Infiniband" :
705 ((port
.link_layer
== IBV_LINK_LAYER_ETHERNET
)
706 ? "Ethernet" : "Unknown"));
710 * Put in the log file the RDMA gid addressing information,
711 * useful for folks who have trouble understanding the
712 * RDMA device hierarchy in the kernel.
714 static void qemu_rdma_dump_gid(const char *who
, struct rdma_cm_id
*id
)
718 inet_ntop(AF_INET6
, &id
->route
.addr
.addr
.ibaddr
.sgid
, sgid
, sizeof sgid
);
719 inet_ntop(AF_INET6
, &id
->route
.addr
.addr
.ibaddr
.dgid
, dgid
, sizeof dgid
);
720 trace_qemu_rdma_dump_gid(who
, sgid
, dgid
);
724 * As of now, IPv6 over RoCE / iWARP is not supported by linux.
725 * We will try the next addrinfo struct, and fail if there are
726 * no other valid addresses to bind against.
728 * If user is listening on '[::]', then we will not have a opened a device
729 * yet and have no way of verifying if the device is RoCE or not.
731 * In this case, the source VM will throw an error for ALL types of
732 * connections (both IPv4 and IPv6) if the destination machine does not have
733 * a regular infiniband network available for use.
735 * The only way to guarantee that an error is thrown for broken kernels is
736 * for the management software to choose a *specific* interface at bind time
737 * and validate what time of hardware it is.
739 * Unfortunately, this puts the user in a fix:
741 * If the source VM connects with an IPv4 address without knowing that the
742 * destination has bound to '[::]' the migration will unconditionally fail
743 * unless the management software is explicitly listening on the the IPv4
744 * address while using a RoCE-based device.
746 * If the source VM connects with an IPv6 address, then we're OK because we can
747 * throw an error on the source (and similarly on the destination).
749 * But in mixed environments, this will be broken for a while until it is fixed
752 * We do provide a *tiny* bit of help in this function: We can list all of the
753 * devices in the system and check to see if all the devices are RoCE or
756 * If we detect that we have a *pure* RoCE environment, then we can safely
757 * thrown an error even if the management software has specified '[::]' as the
760 * However, if there is are multiple hetergeneous devices, then we cannot make
761 * this assumption and the user just has to be sure they know what they are
764 * Patches are being reviewed on linux-rdma.
766 static int qemu_rdma_broken_ipv6_kernel(Error
**errp
, struct ibv_context
*verbs
)
768 struct ibv_port_attr port_attr
;
770 /* This bug only exists in linux, to our knowledge. */
774 * Verbs are only NULL if management has bound to '[::]'.
776 * Let's iterate through all the devices and see if there any pure IB
777 * devices (non-ethernet).
779 * If not, then we can safely proceed with the migration.
780 * Otherwise, there are no guarantees until the bug is fixed in linux.
784 struct ibv_device
** dev_list
= ibv_get_device_list(&num_devices
);
785 bool roce_found
= false;
786 bool ib_found
= false;
788 for (x
= 0; x
< num_devices
; x
++) {
789 verbs
= ibv_open_device(dev_list
[x
]);
791 if (ibv_query_port(verbs
, 1, &port_attr
)) {
792 ibv_close_device(verbs
);
793 ERROR(errp
, "Could not query initial IB port");
797 if (port_attr
.link_layer
== IBV_LINK_LAYER_INFINIBAND
) {
799 } else if (port_attr
.link_layer
== IBV_LINK_LAYER_ETHERNET
) {
803 ibv_close_device(verbs
);
809 fprintf(stderr
, "WARN: migrations may fail:"
810 " IPv6 over RoCE / iWARP in linux"
811 " is broken. But since you appear to have a"
812 " mixed RoCE / IB environment, be sure to only"
813 " migrate over the IB fabric until the kernel "
814 " fixes the bug.\n");
816 ERROR(errp
, "You only have RoCE / iWARP devices in your systems"
817 " and your management software has specified '[::]'"
818 ", but IPv6 over RoCE / iWARP is not supported in Linux.");
827 * If we have a verbs context, that means that some other than '[::]' was
828 * used by the management software for binding. In which case we can
829 * actually warn the user about a potentially broken kernel.
832 /* IB ports start with 1, not 0 */
833 if (ibv_query_port(verbs
, 1, &port_attr
)) {
834 ERROR(errp
, "Could not query initial IB port");
838 if (port_attr
.link_layer
== IBV_LINK_LAYER_ETHERNET
) {
839 ERROR(errp
, "Linux kernel's RoCE / iWARP does not support IPv6 "
840 "(but patches on linux-rdma in progress)");
850 * Figure out which RDMA device corresponds to the requested IP hostname
851 * Also create the initial connection manager identifiers for opening
854 static int qemu_rdma_resolve_host(RDMAContext
*rdma
, Error
**errp
)
857 struct rdma_addrinfo
*res
;
859 struct rdma_cm_event
*cm_event
;
860 char ip
[40] = "unknown";
861 struct rdma_addrinfo
*e
;
863 if (rdma
->host
== NULL
|| !strcmp(rdma
->host
, "")) {
864 ERROR(errp
, "RDMA hostname has not been set");
868 /* create CM channel */
869 rdma
->channel
= rdma_create_event_channel();
870 if (!rdma
->channel
) {
871 ERROR(errp
, "could not create CM channel");
876 ret
= rdma_create_id(rdma
->channel
, &rdma
->cm_id
, NULL
, RDMA_PS_TCP
);
878 ERROR(errp
, "could not create channel id");
879 goto err_resolve_create_id
;
882 snprintf(port_str
, 16, "%d", rdma
->port
);
885 ret
= rdma_getaddrinfo(rdma
->host
, port_str
, NULL
, &res
);
887 ERROR(errp
, "could not rdma_getaddrinfo address %s", rdma
->host
);
888 goto err_resolve_get_addr
;
891 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
892 inet_ntop(e
->ai_family
,
893 &((struct sockaddr_in
*) e
->ai_dst_addr
)->sin_addr
, ip
, sizeof ip
);
894 trace_qemu_rdma_resolve_host_trying(rdma
->host
, ip
);
896 ret
= rdma_resolve_addr(rdma
->cm_id
, NULL
, e
->ai_dst_addr
,
897 RDMA_RESOLVE_TIMEOUT_MS
);
899 if (e
->ai_family
== AF_INET6
) {
900 ret
= qemu_rdma_broken_ipv6_kernel(errp
, rdma
->cm_id
->verbs
);
909 ERROR(errp
, "could not resolve address %s", rdma
->host
);
910 goto err_resolve_get_addr
;
913 qemu_rdma_dump_gid("source_resolve_addr", rdma
->cm_id
);
915 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
917 ERROR(errp
, "could not perform event_addr_resolved");
918 goto err_resolve_get_addr
;
921 if (cm_event
->event
!= RDMA_CM_EVENT_ADDR_RESOLVED
) {
922 ERROR(errp
, "result not equal to event_addr_resolved %s",
923 rdma_event_str(cm_event
->event
));
924 perror("rdma_resolve_addr");
925 rdma_ack_cm_event(cm_event
);
927 goto err_resolve_get_addr
;
929 rdma_ack_cm_event(cm_event
);
932 ret
= rdma_resolve_route(rdma
->cm_id
, RDMA_RESOLVE_TIMEOUT_MS
);
934 ERROR(errp
, "could not resolve rdma route");
935 goto err_resolve_get_addr
;
938 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
940 ERROR(errp
, "could not perform event_route_resolved");
941 goto err_resolve_get_addr
;
943 if (cm_event
->event
!= RDMA_CM_EVENT_ROUTE_RESOLVED
) {
944 ERROR(errp
, "result not equal to event_route_resolved: %s",
945 rdma_event_str(cm_event
->event
));
946 rdma_ack_cm_event(cm_event
);
948 goto err_resolve_get_addr
;
950 rdma_ack_cm_event(cm_event
);
951 rdma
->verbs
= rdma
->cm_id
->verbs
;
952 qemu_rdma_dump_id("source_resolve_host", rdma
->cm_id
->verbs
);
953 qemu_rdma_dump_gid("source_resolve_host", rdma
->cm_id
);
956 err_resolve_get_addr
:
957 rdma_destroy_id(rdma
->cm_id
);
959 err_resolve_create_id
:
960 rdma_destroy_event_channel(rdma
->channel
);
961 rdma
->channel
= NULL
;
966 * Create protection domain and completion queues
968 static int qemu_rdma_alloc_pd_cq(RDMAContext
*rdma
)
971 rdma
->pd
= ibv_alloc_pd(rdma
->verbs
);
973 error_report("failed to allocate protection domain");
977 /* create completion channel */
978 rdma
->comp_channel
= ibv_create_comp_channel(rdma
->verbs
);
979 if (!rdma
->comp_channel
) {
980 error_report("failed to allocate completion channel");
981 goto err_alloc_pd_cq
;
985 * Completion queue can be filled by both read and write work requests,
986 * so must reflect the sum of both possible queue sizes.
988 rdma
->cq
= ibv_create_cq(rdma
->verbs
, (RDMA_SIGNALED_SEND_MAX
* 3),
989 NULL
, rdma
->comp_channel
, 0);
991 error_report("failed to allocate completion queue");
992 goto err_alloc_pd_cq
;
999 ibv_dealloc_pd(rdma
->pd
);
1001 if (rdma
->comp_channel
) {
1002 ibv_destroy_comp_channel(rdma
->comp_channel
);
1005 rdma
->comp_channel
= NULL
;
1011 * Create queue pairs.
1013 static int qemu_rdma_alloc_qp(RDMAContext
*rdma
)
1015 struct ibv_qp_init_attr attr
= { 0 };
1018 attr
.cap
.max_send_wr
= RDMA_SIGNALED_SEND_MAX
;
1019 attr
.cap
.max_recv_wr
= 3;
1020 attr
.cap
.max_send_sge
= 1;
1021 attr
.cap
.max_recv_sge
= 1;
1022 attr
.send_cq
= rdma
->cq
;
1023 attr
.recv_cq
= rdma
->cq
;
1024 attr
.qp_type
= IBV_QPT_RC
;
1026 ret
= rdma_create_qp(rdma
->cm_id
, rdma
->pd
, &attr
);
1031 rdma
->qp
= rdma
->cm_id
->qp
;
1035 static int qemu_rdma_reg_whole_ram_blocks(RDMAContext
*rdma
)
1038 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
1040 for (i
= 0; i
< local
->nb_blocks
; i
++) {
1041 local
->block
[i
].mr
=
1042 ibv_reg_mr(rdma
->pd
,
1043 local
->block
[i
].local_host_addr
,
1044 local
->block
[i
].length
,
1045 IBV_ACCESS_LOCAL_WRITE
|
1046 IBV_ACCESS_REMOTE_WRITE
1048 if (!local
->block
[i
].mr
) {
1049 perror("Failed to register local dest ram block!\n");
1052 rdma
->total_registrations
++;
1055 if (i
>= local
->nb_blocks
) {
1059 for (i
--; i
>= 0; i
--) {
1060 ibv_dereg_mr(local
->block
[i
].mr
);
1061 rdma
->total_registrations
--;
1069 * Find the ram block that corresponds to the page requested to be
1070 * transmitted by QEMU.
1072 * Once the block is found, also identify which 'chunk' within that
1073 * block that the page belongs to.
1075 * This search cannot fail or the migration will fail.
1077 static int qemu_rdma_search_ram_block(RDMAContext
*rdma
,
1078 uint64_t block_offset
,
1081 uint64_t *block_index
,
1082 uint64_t *chunk_index
)
1084 uint64_t current_addr
= block_offset
+ offset
;
1085 RDMALocalBlock
*block
= g_hash_table_lookup(rdma
->blockmap
,
1086 (void *) block_offset
);
1088 assert(current_addr
>= block
->offset
);
1089 assert((current_addr
+ length
) <= (block
->offset
+ block
->length
));
1091 *block_index
= block
->index
;
1092 *chunk_index
= ram_chunk_index(block
->local_host_addr
,
1093 block
->local_host_addr
+ (current_addr
- block
->offset
));
1099 * Register a chunk with IB. If the chunk was already registered
1100 * previously, then skip.
1102 * Also return the keys associated with the registration needed
1103 * to perform the actual RDMA operation.
1105 static int qemu_rdma_register_and_get_keys(RDMAContext
*rdma
,
1106 RDMALocalBlock
*block
, uint8_t *host_addr
,
1107 uint32_t *lkey
, uint32_t *rkey
, int chunk
,
1108 uint8_t *chunk_start
, uint8_t *chunk_end
)
1112 *lkey
= block
->mr
->lkey
;
1115 *rkey
= block
->mr
->rkey
;
1120 /* allocate memory to store chunk MRs */
1122 block
->pmr
= g_malloc0(block
->nb_chunks
* sizeof(struct ibv_mr
*));
1126 * If 'rkey', then we're the destination, so grant access to the source.
1128 * If 'lkey', then we're the source VM, so grant access only to ourselves.
1130 if (!block
->pmr
[chunk
]) {
1131 uint64_t len
= chunk_end
- chunk_start
;
1133 trace_qemu_rdma_register_and_get_keys(len
, chunk_start
);
1135 block
->pmr
[chunk
] = ibv_reg_mr(rdma
->pd
,
1137 (rkey
? (IBV_ACCESS_LOCAL_WRITE
|
1138 IBV_ACCESS_REMOTE_WRITE
) : 0));
1140 if (!block
->pmr
[chunk
]) {
1141 perror("Failed to register chunk!");
1142 fprintf(stderr
, "Chunk details: block: %d chunk index %d"
1143 " start %" PRIu64
" end %" PRIu64
" host %" PRIu64
1144 " local %" PRIu64
" registrations: %d\n",
1145 block
->index
, chunk
, (uint64_t) chunk_start
,
1146 (uint64_t) chunk_end
, (uint64_t) host_addr
,
1147 (uint64_t) block
->local_host_addr
,
1148 rdma
->total_registrations
);
1151 rdma
->total_registrations
++;
1155 *lkey
= block
->pmr
[chunk
]->lkey
;
1158 *rkey
= block
->pmr
[chunk
]->rkey
;
1164 * Register (at connection time) the memory used for control
1167 static int qemu_rdma_reg_control(RDMAContext
*rdma
, int idx
)
1169 rdma
->wr_data
[idx
].control_mr
= ibv_reg_mr(rdma
->pd
,
1170 rdma
->wr_data
[idx
].control
, RDMA_CONTROL_MAX_BUFFER
,
1171 IBV_ACCESS_LOCAL_WRITE
| IBV_ACCESS_REMOTE_WRITE
);
1172 if (rdma
->wr_data
[idx
].control_mr
) {
1173 rdma
->total_registrations
++;
1176 error_report("qemu_rdma_reg_control failed");
1180 const char *print_wrid(int wrid
)
1182 if (wrid
>= RDMA_WRID_RECV_CONTROL
) {
1183 return wrid_desc
[RDMA_WRID_RECV_CONTROL
];
1185 return wrid_desc
[wrid
];
1189 * RDMA requires memory registration (mlock/pinning), but this is not good for
1192 * In preparation for the future where LRU information or workload-specific
1193 * writable writable working set memory access behavior is available to QEMU
1194 * it would be nice to have in place the ability to UN-register/UN-pin
1195 * particular memory regions from the RDMA hardware when it is determine that
1196 * those regions of memory will likely not be accessed again in the near future.
1198 * While we do not yet have such information right now, the following
1199 * compile-time option allows us to perform a non-optimized version of this
1202 * By uncommenting this option, you will cause *all* RDMA transfers to be
1203 * unregistered immediately after the transfer completes on both sides of the
1204 * connection. This has no effect in 'rdma-pin-all' mode, only regular mode.
1206 * This will have a terrible impact on migration performance, so until future
1207 * workload information or LRU information is available, do not attempt to use
1208 * this feature except for basic testing.
1210 //#define RDMA_UNREGISTRATION_EXAMPLE
1213 * Perform a non-optimized memory unregistration after every transfer
1214 * for demonsration purposes, only if pin-all is not requested.
1216 * Potential optimizations:
1217 * 1. Start a new thread to run this function continuously
1219 - and for receipt of unregister messages
1221 * 3. Use workload hints.
1223 static int qemu_rdma_unregister_waiting(RDMAContext
*rdma
)
1225 while (rdma
->unregistrations
[rdma
->unregister_current
]) {
1227 uint64_t wr_id
= rdma
->unregistrations
[rdma
->unregister_current
];
1229 (wr_id
& RDMA_WRID_CHUNK_MASK
) >> RDMA_WRID_CHUNK_SHIFT
;
1231 (wr_id
& RDMA_WRID_BLOCK_MASK
) >> RDMA_WRID_BLOCK_SHIFT
;
1232 RDMALocalBlock
*block
=
1233 &(rdma
->local_ram_blocks
.block
[index
]);
1234 RDMARegister reg
= { .current_index
= index
};
1235 RDMAControlHeader resp
= { .type
= RDMA_CONTROL_UNREGISTER_FINISHED
,
1237 RDMAControlHeader head
= { .len
= sizeof(RDMARegister
),
1238 .type
= RDMA_CONTROL_UNREGISTER_REQUEST
,
1242 trace_qemu_rdma_unregister_waiting_proc(chunk
,
1243 rdma
->unregister_current
);
1245 rdma
->unregistrations
[rdma
->unregister_current
] = 0;
1246 rdma
->unregister_current
++;
1248 if (rdma
->unregister_current
== RDMA_SIGNALED_SEND_MAX
) {
1249 rdma
->unregister_current
= 0;
1254 * Unregistration is speculative (because migration is single-threaded
1255 * and we cannot break the protocol's inifinband message ordering).
1256 * Thus, if the memory is currently being used for transmission,
1257 * then abort the attempt to unregister and try again
1258 * later the next time a completion is received for this memory.
1260 clear_bit(chunk
, block
->unregister_bitmap
);
1262 if (test_bit(chunk
, block
->transit_bitmap
)) {
1263 trace_qemu_rdma_unregister_waiting_inflight(chunk
);
1267 trace_qemu_rdma_unregister_waiting_send(chunk
);
1269 ret
= ibv_dereg_mr(block
->pmr
[chunk
]);
1270 block
->pmr
[chunk
] = NULL
;
1271 block
->remote_keys
[chunk
] = 0;
1274 perror("unregistration chunk failed");
1277 rdma
->total_registrations
--;
1279 reg
.key
.chunk
= chunk
;
1280 register_to_network(®
);
1281 ret
= qemu_rdma_exchange_send(rdma
, &head
, (uint8_t *) ®
,
1287 trace_qemu_rdma_unregister_waiting_complete(chunk
);
1293 static uint64_t qemu_rdma_make_wrid(uint64_t wr_id
, uint64_t index
,
1296 uint64_t result
= wr_id
& RDMA_WRID_TYPE_MASK
;
1298 result
|= (index
<< RDMA_WRID_BLOCK_SHIFT
);
1299 result
|= (chunk
<< RDMA_WRID_CHUNK_SHIFT
);
1305 * Set bit for unregistration in the next iteration.
1306 * We cannot transmit right here, but will unpin later.
1308 static void qemu_rdma_signal_unregister(RDMAContext
*rdma
, uint64_t index
,
1309 uint64_t chunk
, uint64_t wr_id
)
1311 if (rdma
->unregistrations
[rdma
->unregister_next
] != 0) {
1312 error_report("rdma migration: queue is full");
1314 RDMALocalBlock
*block
= &(rdma
->local_ram_blocks
.block
[index
]);
1316 if (!test_and_set_bit(chunk
, block
->unregister_bitmap
)) {
1317 trace_qemu_rdma_signal_unregister_append(chunk
,
1318 rdma
->unregister_next
);
1320 rdma
->unregistrations
[rdma
->unregister_next
++] =
1321 qemu_rdma_make_wrid(wr_id
, index
, chunk
);
1323 if (rdma
->unregister_next
== RDMA_SIGNALED_SEND_MAX
) {
1324 rdma
->unregister_next
= 0;
1327 trace_qemu_rdma_signal_unregister_already(chunk
);
1333 * Consult the connection manager to see a work request
1334 * (of any kind) has completed.
1335 * Return the work request ID that completed.
1337 static uint64_t qemu_rdma_poll(RDMAContext
*rdma
, uint64_t *wr_id_out
,
1344 ret
= ibv_poll_cq(rdma
->cq
, 1, &wc
);
1347 *wr_id_out
= RDMA_WRID_NONE
;
1352 error_report("ibv_poll_cq return %d", ret
);
1356 wr_id
= wc
.wr_id
& RDMA_WRID_TYPE_MASK
;
1358 if (wc
.status
!= IBV_WC_SUCCESS
) {
1359 fprintf(stderr
, "ibv_poll_cq wc.status=%d %s!\n",
1360 wc
.status
, ibv_wc_status_str(wc
.status
));
1361 fprintf(stderr
, "ibv_poll_cq wrid=%s!\n", wrid_desc
[wr_id
]);
1366 if (rdma
->control_ready_expected
&&
1367 (wr_id
>= RDMA_WRID_RECV_CONTROL
)) {
1368 trace_qemu_rdma_poll_recv(wrid_desc
[RDMA_WRID_RECV_CONTROL
],
1369 wr_id
- RDMA_WRID_RECV_CONTROL
, wr_id
, rdma
->nb_sent
);
1370 rdma
->control_ready_expected
= 0;
1373 if (wr_id
== RDMA_WRID_RDMA_WRITE
) {
1375 (wc
.wr_id
& RDMA_WRID_CHUNK_MASK
) >> RDMA_WRID_CHUNK_SHIFT
;
1377 (wc
.wr_id
& RDMA_WRID_BLOCK_MASK
) >> RDMA_WRID_BLOCK_SHIFT
;
1378 RDMALocalBlock
*block
= &(rdma
->local_ram_blocks
.block
[index
]);
1380 trace_qemu_rdma_poll_write(print_wrid(wr_id
), wr_id
, rdma
->nb_sent
,
1382 block
->local_host_addr
, (void *)block
->remote_host_addr
);
1384 clear_bit(chunk
, block
->transit_bitmap
);
1386 if (rdma
->nb_sent
> 0) {
1390 if (!rdma
->pin_all
) {
1392 * FYI: If one wanted to signal a specific chunk to be unregistered
1393 * using LRU or workload-specific information, this is the function
1394 * you would call to do so. That chunk would then get asynchronously
1395 * unregistered later.
1397 #ifdef RDMA_UNREGISTRATION_EXAMPLE
1398 qemu_rdma_signal_unregister(rdma
, index
, chunk
, wc
.wr_id
);
1402 trace_qemu_rdma_poll_other(print_wrid(wr_id
), wr_id
, rdma
->nb_sent
);
1405 *wr_id_out
= wc
.wr_id
;
1407 *byte_len
= wc
.byte_len
;
1414 * Block until the next work request has completed.
1416 * First poll to see if a work request has already completed,
1419 * If we encounter completed work requests for IDs other than
1420 * the one we're interested in, then that's generally an error.
1422 * The only exception is actual RDMA Write completions. These
1423 * completions only need to be recorded, but do not actually
1424 * need further processing.
1426 static int qemu_rdma_block_for_wrid(RDMAContext
*rdma
, int wrid_requested
,
1429 int num_cq_events
= 0, ret
= 0;
1432 uint64_t wr_id
= RDMA_WRID_NONE
, wr_id_in
;
1434 if (ibv_req_notify_cq(rdma
->cq
, 0)) {
1438 while (wr_id
!= wrid_requested
) {
1439 ret
= qemu_rdma_poll(rdma
, &wr_id_in
, byte_len
);
1444 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
1446 if (wr_id
== RDMA_WRID_NONE
) {
1449 if (wr_id
!= wrid_requested
) {
1450 trace_qemu_rdma_block_for_wrid_miss(print_wrid(wrid_requested
),
1451 wrid_requested
, print_wrid(wr_id
), wr_id
);
1455 if (wr_id
== wrid_requested
) {
1461 * Coroutine doesn't start until process_incoming_migration()
1462 * so don't yield unless we know we're running inside of a coroutine.
1464 if (rdma
->migration_started_on_destination
) {
1465 yield_until_fd_readable(rdma
->comp_channel
->fd
);
1468 if (ibv_get_cq_event(rdma
->comp_channel
, &cq
, &cq_ctx
)) {
1469 perror("ibv_get_cq_event");
1470 goto err_block_for_wrid
;
1475 if (ibv_req_notify_cq(cq
, 0)) {
1476 goto err_block_for_wrid
;
1479 while (wr_id
!= wrid_requested
) {
1480 ret
= qemu_rdma_poll(rdma
, &wr_id_in
, byte_len
);
1482 goto err_block_for_wrid
;
1485 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
1487 if (wr_id
== RDMA_WRID_NONE
) {
1490 if (wr_id
!= wrid_requested
) {
1491 trace_qemu_rdma_block_for_wrid_miss(print_wrid(wrid_requested
),
1492 wrid_requested
, print_wrid(wr_id
), wr_id
);
1496 if (wr_id
== wrid_requested
) {
1497 goto success_block_for_wrid
;
1501 success_block_for_wrid
:
1502 if (num_cq_events
) {
1503 ibv_ack_cq_events(cq
, num_cq_events
);
1508 if (num_cq_events
) {
1509 ibv_ack_cq_events(cq
, num_cq_events
);
1515 * Post a SEND message work request for the control channel
1516 * containing some data and block until the post completes.
1518 static int qemu_rdma_post_send_control(RDMAContext
*rdma
, uint8_t *buf
,
1519 RDMAControlHeader
*head
)
1522 RDMAWorkRequestData
*wr
= &rdma
->wr_data
[RDMA_WRID_CONTROL
];
1523 struct ibv_send_wr
*bad_wr
;
1524 struct ibv_sge sge
= {
1525 .addr
= (uint64_t)(wr
->control
),
1526 .length
= head
->len
+ sizeof(RDMAControlHeader
),
1527 .lkey
= wr
->control_mr
->lkey
,
1529 struct ibv_send_wr send_wr
= {
1530 .wr_id
= RDMA_WRID_SEND_CONTROL
,
1531 .opcode
= IBV_WR_SEND
,
1532 .send_flags
= IBV_SEND_SIGNALED
,
1537 trace_qemu_rdma_post_send_control(control_desc
[head
->type
]);
1540 * We don't actually need to do a memcpy() in here if we used
1541 * the "sge" properly, but since we're only sending control messages
1542 * (not RAM in a performance-critical path), then its OK for now.
1544 * The copy makes the RDMAControlHeader simpler to manipulate
1545 * for the time being.
1547 assert(head
->len
<= RDMA_CONTROL_MAX_BUFFER
- sizeof(*head
));
1548 memcpy(wr
->control
, head
, sizeof(RDMAControlHeader
));
1549 control_to_network((void *) wr
->control
);
1552 memcpy(wr
->control
+ sizeof(RDMAControlHeader
), buf
, head
->len
);
1556 ret
= ibv_post_send(rdma
->qp
, &send_wr
, &bad_wr
);
1559 error_report("Failed to use post IB SEND for control");
1563 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_SEND_CONTROL
, NULL
);
1565 error_report("rdma migration: send polling control error");
1572 * Post a RECV work request in anticipation of some future receipt
1573 * of data on the control channel.
1575 static int qemu_rdma_post_recv_control(RDMAContext
*rdma
, int idx
)
1577 struct ibv_recv_wr
*bad_wr
;
1578 struct ibv_sge sge
= {
1579 .addr
= (uint64_t)(rdma
->wr_data
[idx
].control
),
1580 .length
= RDMA_CONTROL_MAX_BUFFER
,
1581 .lkey
= rdma
->wr_data
[idx
].control_mr
->lkey
,
1584 struct ibv_recv_wr recv_wr
= {
1585 .wr_id
= RDMA_WRID_RECV_CONTROL
+ idx
,
1591 if (ibv_post_recv(rdma
->qp
, &recv_wr
, &bad_wr
)) {
1599 * Block and wait for a RECV control channel message to arrive.
1601 static int qemu_rdma_exchange_get_response(RDMAContext
*rdma
,
1602 RDMAControlHeader
*head
, int expecting
, int idx
)
1605 int ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RECV_CONTROL
+ idx
,
1609 error_report("rdma migration: recv polling control error!");
1613 network_to_control((void *) rdma
->wr_data
[idx
].control
);
1614 memcpy(head
, rdma
->wr_data
[idx
].control
, sizeof(RDMAControlHeader
));
1616 trace_qemu_rdma_exchange_get_response_start(control_desc
[expecting
]);
1618 if (expecting
== RDMA_CONTROL_NONE
) {
1619 trace_qemu_rdma_exchange_get_response_none(control_desc
[head
->type
],
1621 } else if (head
->type
!= expecting
|| head
->type
== RDMA_CONTROL_ERROR
) {
1622 error_report("Was expecting a %s (%d) control message"
1623 ", but got: %s (%d), length: %d",
1624 control_desc
[expecting
], expecting
,
1625 control_desc
[head
->type
], head
->type
, head
->len
);
1628 if (head
->len
> RDMA_CONTROL_MAX_BUFFER
- sizeof(*head
)) {
1629 error_report("too long length: %d", head
->len
);
1632 if (sizeof(*head
) + head
->len
!= byte_len
) {
1633 error_report("Malformed length: %d byte_len %d", head
->len
, byte_len
);
1641 * When a RECV work request has completed, the work request's
1642 * buffer is pointed at the header.
1644 * This will advance the pointer to the data portion
1645 * of the control message of the work request's buffer that
1646 * was populated after the work request finished.
1648 static void qemu_rdma_move_header(RDMAContext
*rdma
, int idx
,
1649 RDMAControlHeader
*head
)
1651 rdma
->wr_data
[idx
].control_len
= head
->len
;
1652 rdma
->wr_data
[idx
].control_curr
=
1653 rdma
->wr_data
[idx
].control
+ sizeof(RDMAControlHeader
);
1657 * This is an 'atomic' high-level operation to deliver a single, unified
1658 * control-channel message.
1660 * Additionally, if the user is expecting some kind of reply to this message,
1661 * they can request a 'resp' response message be filled in by posting an
1662 * additional work request on behalf of the user and waiting for an additional
1665 * The extra (optional) response is used during registration to us from having
1666 * to perform an *additional* exchange of message just to provide a response by
1667 * instead piggy-backing on the acknowledgement.
1669 static int qemu_rdma_exchange_send(RDMAContext
*rdma
, RDMAControlHeader
*head
,
1670 uint8_t *data
, RDMAControlHeader
*resp
,
1672 int (*callback
)(RDMAContext
*rdma
))
1677 * Wait until the dest is ready before attempting to deliver the message
1678 * by waiting for a READY message.
1680 if (rdma
->control_ready_expected
) {
1681 RDMAControlHeader resp
;
1682 ret
= qemu_rdma_exchange_get_response(rdma
,
1683 &resp
, RDMA_CONTROL_READY
, RDMA_WRID_READY
);
1690 * If the user is expecting a response, post a WR in anticipation of it.
1693 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_DATA
);
1695 error_report("rdma migration: error posting"
1696 " extra control recv for anticipated result!");
1702 * Post a WR to replace the one we just consumed for the READY message.
1704 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
1706 error_report("rdma migration: error posting first control recv!");
1711 * Deliver the control message that was requested.
1713 ret
= qemu_rdma_post_send_control(rdma
, data
, head
);
1716 error_report("Failed to send control buffer!");
1721 * If we're expecting a response, block and wait for it.
1725 trace_qemu_rdma_exchange_send_issue_callback();
1726 ret
= callback(rdma
);
1732 trace_qemu_rdma_exchange_send_waiting(control_desc
[resp
->type
]);
1733 ret
= qemu_rdma_exchange_get_response(rdma
, resp
,
1734 resp
->type
, RDMA_WRID_DATA
);
1740 qemu_rdma_move_header(rdma
, RDMA_WRID_DATA
, resp
);
1742 *resp_idx
= RDMA_WRID_DATA
;
1744 trace_qemu_rdma_exchange_send_received(control_desc
[resp
->type
]);
1747 rdma
->control_ready_expected
= 1;
1753 * This is an 'atomic' high-level operation to receive a single, unified
1754 * control-channel message.
1756 static int qemu_rdma_exchange_recv(RDMAContext
*rdma
, RDMAControlHeader
*head
,
1759 RDMAControlHeader ready
= {
1761 .type
= RDMA_CONTROL_READY
,
1767 * Inform the source that we're ready to receive a message.
1769 ret
= qemu_rdma_post_send_control(rdma
, NULL
, &ready
);
1772 error_report("Failed to send control buffer!");
1777 * Block and wait for the message.
1779 ret
= qemu_rdma_exchange_get_response(rdma
, head
,
1780 expecting
, RDMA_WRID_READY
);
1786 qemu_rdma_move_header(rdma
, RDMA_WRID_READY
, head
);
1789 * Post a new RECV work request to replace the one we just consumed.
1791 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
1793 error_report("rdma migration: error posting second control recv!");
1801 * Write an actual chunk of memory using RDMA.
1803 * If we're using dynamic registration on the dest-side, we have to
1804 * send a registration command first.
1806 static int qemu_rdma_write_one(QEMUFile
*f
, RDMAContext
*rdma
,
1807 int current_index
, uint64_t current_addr
,
1811 struct ibv_send_wr send_wr
= { 0 };
1812 struct ibv_send_wr
*bad_wr
;
1813 int reg_result_idx
, ret
, count
= 0;
1814 uint64_t chunk
, chunks
;
1815 uint8_t *chunk_start
, *chunk_end
;
1816 RDMALocalBlock
*block
= &(rdma
->local_ram_blocks
.block
[current_index
]);
1818 RDMARegisterResult
*reg_result
;
1819 RDMAControlHeader resp
= { .type
= RDMA_CONTROL_REGISTER_RESULT
};
1820 RDMAControlHeader head
= { .len
= sizeof(RDMARegister
),
1821 .type
= RDMA_CONTROL_REGISTER_REQUEST
,
1826 sge
.addr
= (uint64_t)(block
->local_host_addr
+
1827 (current_addr
- block
->offset
));
1828 sge
.length
= length
;
1830 chunk
= ram_chunk_index(block
->local_host_addr
, (uint8_t *) sge
.addr
);
1831 chunk_start
= ram_chunk_start(block
, chunk
);
1833 if (block
->is_ram_block
) {
1834 chunks
= length
/ (1UL << RDMA_REG_CHUNK_SHIFT
);
1836 if (chunks
&& ((length
% (1UL << RDMA_REG_CHUNK_SHIFT
)) == 0)) {
1840 chunks
= block
->length
/ (1UL << RDMA_REG_CHUNK_SHIFT
);
1842 if (chunks
&& ((block
->length
% (1UL << RDMA_REG_CHUNK_SHIFT
)) == 0)) {
1847 trace_qemu_rdma_write_one_top(chunks
+ 1,
1849 (1UL << RDMA_REG_CHUNK_SHIFT
) / 1024 / 1024);
1851 chunk_end
= ram_chunk_end(block
, chunk
+ chunks
);
1853 if (!rdma
->pin_all
) {
1854 #ifdef RDMA_UNREGISTRATION_EXAMPLE
1855 qemu_rdma_unregister_waiting(rdma
);
1859 while (test_bit(chunk
, block
->transit_bitmap
)) {
1861 trace_qemu_rdma_write_one_block(count
++, current_index
, chunk
,
1862 sge
.addr
, length
, rdma
->nb_sent
, block
->nb_chunks
);
1864 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RDMA_WRITE
, NULL
);
1867 error_report("Failed to Wait for previous write to complete "
1868 "block %d chunk %" PRIu64
1869 " current %" PRIu64
" len %" PRIu64
" %d",
1870 current_index
, chunk
, sge
.addr
, length
, rdma
->nb_sent
);
1875 if (!rdma
->pin_all
|| !block
->is_ram_block
) {
1876 if (!block
->remote_keys
[chunk
]) {
1878 * This chunk has not yet been registered, so first check to see
1879 * if the entire chunk is zero. If so, tell the other size to
1880 * memset() + madvise() the entire chunk without RDMA.
1883 if (can_use_buffer_find_nonzero_offset((void *)sge
.addr
, length
)
1884 && buffer_find_nonzero_offset((void *)sge
.addr
,
1885 length
) == length
) {
1886 RDMACompress comp
= {
1887 .offset
= current_addr
,
1889 .block_idx
= current_index
,
1893 head
.len
= sizeof(comp
);
1894 head
.type
= RDMA_CONTROL_COMPRESS
;
1896 trace_qemu_rdma_write_one_zero(chunk
, sge
.length
,
1897 current_index
, current_addr
);
1899 compress_to_network(&comp
);
1900 ret
= qemu_rdma_exchange_send(rdma
, &head
,
1901 (uint8_t *) &comp
, NULL
, NULL
, NULL
);
1907 acct_update_position(f
, sge
.length
, true);
1913 * Otherwise, tell other side to register.
1915 reg
.current_index
= current_index
;
1916 if (block
->is_ram_block
) {
1917 reg
.key
.current_addr
= current_addr
;
1919 reg
.key
.chunk
= chunk
;
1921 reg
.chunks
= chunks
;
1923 trace_qemu_rdma_write_one_sendreg(chunk
, sge
.length
, current_index
,
1926 register_to_network(®
);
1927 ret
= qemu_rdma_exchange_send(rdma
, &head
, (uint8_t *) ®
,
1928 &resp
, ®_result_idx
, NULL
);
1933 /* try to overlap this single registration with the one we sent. */
1934 if (qemu_rdma_register_and_get_keys(rdma
, block
,
1935 (uint8_t *) sge
.addr
,
1936 &sge
.lkey
, NULL
, chunk
,
1937 chunk_start
, chunk_end
)) {
1938 error_report("cannot get lkey");
1942 reg_result
= (RDMARegisterResult
*)
1943 rdma
->wr_data
[reg_result_idx
].control_curr
;
1945 network_to_result(reg_result
);
1947 trace_qemu_rdma_write_one_recvregres(block
->remote_keys
[chunk
],
1948 reg_result
->rkey
, chunk
);
1950 block
->remote_keys
[chunk
] = reg_result
->rkey
;
1951 block
->remote_host_addr
= reg_result
->host_addr
;
1953 /* already registered before */
1954 if (qemu_rdma_register_and_get_keys(rdma
, block
,
1955 (uint8_t *)sge
.addr
,
1956 &sge
.lkey
, NULL
, chunk
,
1957 chunk_start
, chunk_end
)) {
1958 error_report("cannot get lkey!");
1963 send_wr
.wr
.rdma
.rkey
= block
->remote_keys
[chunk
];
1965 send_wr
.wr
.rdma
.rkey
= block
->remote_rkey
;
1967 if (qemu_rdma_register_and_get_keys(rdma
, block
, (uint8_t *)sge
.addr
,
1968 &sge
.lkey
, NULL
, chunk
,
1969 chunk_start
, chunk_end
)) {
1970 error_report("cannot get lkey!");
1976 * Encode the ram block index and chunk within this wrid.
1977 * We will use this information at the time of completion
1978 * to figure out which bitmap to check against and then which
1979 * chunk in the bitmap to look for.
1981 send_wr
.wr_id
= qemu_rdma_make_wrid(RDMA_WRID_RDMA_WRITE
,
1982 current_index
, chunk
);
1984 send_wr
.opcode
= IBV_WR_RDMA_WRITE
;
1985 send_wr
.send_flags
= IBV_SEND_SIGNALED
;
1986 send_wr
.sg_list
= &sge
;
1987 send_wr
.num_sge
= 1;
1988 send_wr
.wr
.rdma
.remote_addr
= block
->remote_host_addr
+
1989 (current_addr
- block
->offset
);
1991 trace_qemu_rdma_write_one_post(chunk
, sge
.addr
, send_wr
.wr
.rdma
.remote_addr
,
1995 * ibv_post_send() does not return negative error numbers,
1996 * per the specification they are positive - no idea why.
1998 ret
= ibv_post_send(rdma
->qp
, &send_wr
, &bad_wr
);
2000 if (ret
== ENOMEM
) {
2001 trace_qemu_rdma_write_one_queue_full();
2002 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RDMA_WRITE
, NULL
);
2004 error_report("rdma migration: failed to make "
2005 "room in full send queue! %d", ret
);
2011 } else if (ret
> 0) {
2012 perror("rdma migration: post rdma write failed");
2016 set_bit(chunk
, block
->transit_bitmap
);
2017 acct_update_position(f
, sge
.length
, false);
2018 rdma
->total_writes
++;
2024 * Push out any unwritten RDMA operations.
2026 * We support sending out multiple chunks at the same time.
2027 * Not all of them need to get signaled in the completion queue.
2029 static int qemu_rdma_write_flush(QEMUFile
*f
, RDMAContext
*rdma
)
2033 if (!rdma
->current_length
) {
2037 ret
= qemu_rdma_write_one(f
, rdma
,
2038 rdma
->current_index
, rdma
->current_addr
, rdma
->current_length
);
2046 trace_qemu_rdma_write_flush(rdma
->nb_sent
);
2049 rdma
->current_length
= 0;
2050 rdma
->current_addr
= 0;
2055 static inline int qemu_rdma_buffer_mergable(RDMAContext
*rdma
,
2056 uint64_t offset
, uint64_t len
)
2058 RDMALocalBlock
*block
;
2062 if (rdma
->current_index
< 0) {
2066 if (rdma
->current_chunk
< 0) {
2070 block
= &(rdma
->local_ram_blocks
.block
[rdma
->current_index
]);
2071 host_addr
= block
->local_host_addr
+ (offset
- block
->offset
);
2072 chunk_end
= ram_chunk_end(block
, rdma
->current_chunk
);
2074 if (rdma
->current_length
== 0) {
2079 * Only merge into chunk sequentially.
2081 if (offset
!= (rdma
->current_addr
+ rdma
->current_length
)) {
2085 if (offset
< block
->offset
) {
2089 if ((offset
+ len
) > (block
->offset
+ block
->length
)) {
2093 if ((host_addr
+ len
) > chunk_end
) {
2101 * We're not actually writing here, but doing three things:
2103 * 1. Identify the chunk the buffer belongs to.
2104 * 2. If the chunk is full or the buffer doesn't belong to the current
2105 * chunk, then start a new chunk and flush() the old chunk.
2106 * 3. To keep the hardware busy, we also group chunks into batches
2107 * and only require that a batch gets acknowledged in the completion
2108 * qeueue instead of each individual chunk.
2110 static int qemu_rdma_write(QEMUFile
*f
, RDMAContext
*rdma
,
2111 uint64_t block_offset
, uint64_t offset
,
2114 uint64_t current_addr
= block_offset
+ offset
;
2115 uint64_t index
= rdma
->current_index
;
2116 uint64_t chunk
= rdma
->current_chunk
;
2119 /* If we cannot merge it, we flush the current buffer first. */
2120 if (!qemu_rdma_buffer_mergable(rdma
, current_addr
, len
)) {
2121 ret
= qemu_rdma_write_flush(f
, rdma
);
2125 rdma
->current_length
= 0;
2126 rdma
->current_addr
= current_addr
;
2128 ret
= qemu_rdma_search_ram_block(rdma
, block_offset
,
2129 offset
, len
, &index
, &chunk
);
2131 error_report("ram block search failed");
2134 rdma
->current_index
= index
;
2135 rdma
->current_chunk
= chunk
;
2139 rdma
->current_length
+= len
;
2141 /* flush it if buffer is too large */
2142 if (rdma
->current_length
>= RDMA_MERGE_MAX
) {
2143 return qemu_rdma_write_flush(f
, rdma
);
2149 static void qemu_rdma_cleanup(RDMAContext
*rdma
)
2151 struct rdma_cm_event
*cm_event
;
2154 if (rdma
->cm_id
&& rdma
->connected
) {
2155 if (rdma
->error_state
) {
2156 RDMAControlHeader head
= { .len
= 0,
2157 .type
= RDMA_CONTROL_ERROR
,
2160 error_report("Early error. Sending error.");
2161 qemu_rdma_post_send_control(rdma
, NULL
, &head
);
2164 ret
= rdma_disconnect(rdma
->cm_id
);
2166 trace_qemu_rdma_cleanup_waiting_for_disconnect();
2167 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
2169 rdma_ack_cm_event(cm_event
);
2172 trace_qemu_rdma_cleanup_disconnect();
2173 rdma
->connected
= false;
2176 g_free(rdma
->block
);
2179 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2180 if (rdma
->wr_data
[idx
].control_mr
) {
2181 rdma
->total_registrations
--;
2182 ibv_dereg_mr(rdma
->wr_data
[idx
].control_mr
);
2184 rdma
->wr_data
[idx
].control_mr
= NULL
;
2187 if (rdma
->local_ram_blocks
.block
) {
2188 while (rdma
->local_ram_blocks
.nb_blocks
) {
2189 rdma_delete_block(rdma
, rdma
->local_ram_blocks
.block
->offset
);
2194 ibv_destroy_cq(rdma
->cq
);
2197 if (rdma
->comp_channel
) {
2198 ibv_destroy_comp_channel(rdma
->comp_channel
);
2199 rdma
->comp_channel
= NULL
;
2202 ibv_dealloc_pd(rdma
->pd
);
2205 if (rdma
->listen_id
) {
2206 rdma_destroy_id(rdma
->listen_id
);
2207 rdma
->listen_id
= NULL
;
2211 rdma_destroy_qp(rdma
->cm_id
);
2214 rdma_destroy_id(rdma
->cm_id
);
2217 if (rdma
->channel
) {
2218 rdma_destroy_event_channel(rdma
->channel
);
2219 rdma
->channel
= NULL
;
2226 static int qemu_rdma_source_init(RDMAContext
*rdma
, Error
**errp
, bool pin_all
)
2229 Error
*local_err
= NULL
, **temp
= &local_err
;
2232 * Will be validated against destination's actual capabilities
2233 * after the connect() completes.
2235 rdma
->pin_all
= pin_all
;
2237 ret
= qemu_rdma_resolve_host(rdma
, temp
);
2239 goto err_rdma_source_init
;
2242 ret
= qemu_rdma_alloc_pd_cq(rdma
);
2244 ERROR(temp
, "rdma migration: error allocating pd and cq! Your mlock()"
2245 " limits may be too low. Please check $ ulimit -a # and "
2246 "search for 'ulimit -l' in the output");
2247 goto err_rdma_source_init
;
2250 ret
= qemu_rdma_alloc_qp(rdma
);
2252 ERROR(temp
, "rdma migration: error allocating qp!");
2253 goto err_rdma_source_init
;
2256 ret
= qemu_rdma_init_ram_blocks(rdma
);
2258 ERROR(temp
, "rdma migration: error initializing ram blocks!");
2259 goto err_rdma_source_init
;
2262 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2263 ret
= qemu_rdma_reg_control(rdma
, idx
);
2265 ERROR(temp
, "rdma migration: error registering %d control!",
2267 goto err_rdma_source_init
;
2273 err_rdma_source_init
:
2274 error_propagate(errp
, local_err
);
2275 qemu_rdma_cleanup(rdma
);
2279 static int qemu_rdma_connect(RDMAContext
*rdma
, Error
**errp
)
2281 RDMACapabilities cap
= {
2282 .version
= RDMA_CONTROL_VERSION_CURRENT
,
2285 struct rdma_conn_param conn_param
= { .initiator_depth
= 2,
2287 .private_data
= &cap
,
2288 .private_data_len
= sizeof(cap
),
2290 struct rdma_cm_event
*cm_event
;
2294 * Only negotiate the capability with destination if the user
2295 * on the source first requested the capability.
2297 if (rdma
->pin_all
) {
2298 trace_qemu_rdma_connect_pin_all_requested();
2299 cap
.flags
|= RDMA_CAPABILITY_PIN_ALL
;
2302 caps_to_network(&cap
);
2304 ret
= rdma_connect(rdma
->cm_id
, &conn_param
);
2306 perror("rdma_connect");
2307 ERROR(errp
, "connecting to destination!");
2308 rdma_destroy_id(rdma
->cm_id
);
2310 goto err_rdma_source_connect
;
2313 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
2315 perror("rdma_get_cm_event after rdma_connect");
2316 ERROR(errp
, "connecting to destination!");
2317 rdma_ack_cm_event(cm_event
);
2318 rdma_destroy_id(rdma
->cm_id
);
2320 goto err_rdma_source_connect
;
2323 if (cm_event
->event
!= RDMA_CM_EVENT_ESTABLISHED
) {
2324 perror("rdma_get_cm_event != EVENT_ESTABLISHED after rdma_connect");
2325 ERROR(errp
, "connecting to destination!");
2326 rdma_ack_cm_event(cm_event
);
2327 rdma_destroy_id(rdma
->cm_id
);
2329 goto err_rdma_source_connect
;
2331 rdma
->connected
= true;
2333 memcpy(&cap
, cm_event
->param
.conn
.private_data
, sizeof(cap
));
2334 network_to_caps(&cap
);
2337 * Verify that the *requested* capabilities are supported by the destination
2338 * and disable them otherwise.
2340 if (rdma
->pin_all
&& !(cap
.flags
& RDMA_CAPABILITY_PIN_ALL
)) {
2341 ERROR(errp
, "Server cannot support pinning all memory. "
2342 "Will register memory dynamically.");
2343 rdma
->pin_all
= false;
2346 trace_qemu_rdma_connect_pin_all_outcome(rdma
->pin_all
);
2348 rdma_ack_cm_event(cm_event
);
2350 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
2352 ERROR(errp
, "posting second control recv!");
2353 goto err_rdma_source_connect
;
2356 rdma
->control_ready_expected
= 1;
2360 err_rdma_source_connect
:
2361 qemu_rdma_cleanup(rdma
);
2365 static int qemu_rdma_dest_init(RDMAContext
*rdma
, Error
**errp
)
2368 struct rdma_cm_id
*listen_id
;
2369 char ip
[40] = "unknown";
2370 struct rdma_addrinfo
*res
, *e
;
2373 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2374 rdma
->wr_data
[idx
].control_len
= 0;
2375 rdma
->wr_data
[idx
].control_curr
= NULL
;
2378 if (!rdma
->host
|| !rdma
->host
[0]) {
2379 ERROR(errp
, "RDMA host is not set!");
2380 rdma
->error_state
= -EINVAL
;
2383 /* create CM channel */
2384 rdma
->channel
= rdma_create_event_channel();
2385 if (!rdma
->channel
) {
2386 ERROR(errp
, "could not create rdma event channel");
2387 rdma
->error_state
= -EINVAL
;
2392 ret
= rdma_create_id(rdma
->channel
, &listen_id
, NULL
, RDMA_PS_TCP
);
2394 ERROR(errp
, "could not create cm_id!");
2395 goto err_dest_init_create_listen_id
;
2398 snprintf(port_str
, 16, "%d", rdma
->port
);
2399 port_str
[15] = '\0';
2401 ret
= rdma_getaddrinfo(rdma
->host
, port_str
, NULL
, &res
);
2403 ERROR(errp
, "could not rdma_getaddrinfo address %s", rdma
->host
);
2404 goto err_dest_init_bind_addr
;
2407 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
2408 inet_ntop(e
->ai_family
,
2409 &((struct sockaddr_in
*) e
->ai_dst_addr
)->sin_addr
, ip
, sizeof ip
);
2410 trace_qemu_rdma_dest_init_trying(rdma
->host
, ip
);
2411 ret
= rdma_bind_addr(listen_id
, e
->ai_dst_addr
);
2415 if (e
->ai_family
== AF_INET6
) {
2416 ret
= qemu_rdma_broken_ipv6_kernel(errp
, listen_id
->verbs
);
2425 ERROR(errp
, "Error: could not rdma_bind_addr!");
2426 goto err_dest_init_bind_addr
;
2429 rdma
->listen_id
= listen_id
;
2430 qemu_rdma_dump_gid("dest_init", listen_id
);
2433 err_dest_init_bind_addr
:
2434 rdma_destroy_id(listen_id
);
2435 err_dest_init_create_listen_id
:
2436 rdma_destroy_event_channel(rdma
->channel
);
2437 rdma
->channel
= NULL
;
2438 rdma
->error_state
= ret
;
2443 static void *qemu_rdma_data_init(const char *host_port
, Error
**errp
)
2445 RDMAContext
*rdma
= NULL
;
2446 InetSocketAddress
*addr
;
2449 rdma
= g_malloc0(sizeof(RDMAContext
));
2450 memset(rdma
, 0, sizeof(RDMAContext
));
2451 rdma
->current_index
= -1;
2452 rdma
->current_chunk
= -1;
2454 addr
= inet_parse(host_port
, NULL
);
2456 rdma
->port
= atoi(addr
->port
);
2457 rdma
->host
= g_strdup(addr
->host
);
2459 ERROR(errp
, "bad RDMA migration address '%s'", host_port
);
2464 qapi_free_InetSocketAddress(addr
);
2471 * QEMUFile interface to the control channel.
2472 * SEND messages for control only.
2473 * VM's ram is handled with regular RDMA messages.
2475 static int qemu_rdma_put_buffer(void *opaque
, const uint8_t *buf
,
2476 int64_t pos
, int size
)
2478 QEMUFileRDMA
*r
= opaque
;
2479 QEMUFile
*f
= r
->file
;
2480 RDMAContext
*rdma
= r
->rdma
;
2481 size_t remaining
= size
;
2482 uint8_t * data
= (void *) buf
;
2485 CHECK_ERROR_STATE();
2488 * Push out any writes that
2489 * we're queued up for VM's ram.
2491 ret
= qemu_rdma_write_flush(f
, rdma
);
2493 rdma
->error_state
= ret
;
2498 RDMAControlHeader head
;
2500 r
->len
= MIN(remaining
, RDMA_SEND_INCREMENT
);
2501 remaining
-= r
->len
;
2504 head
.type
= RDMA_CONTROL_QEMU_FILE
;
2506 ret
= qemu_rdma_exchange_send(rdma
, &head
, data
, NULL
, NULL
, NULL
);
2509 rdma
->error_state
= ret
;
2519 static size_t qemu_rdma_fill(RDMAContext
*rdma
, uint8_t *buf
,
2524 if (rdma
->wr_data
[idx
].control_len
) {
2525 trace_qemu_rdma_fill(rdma
->wr_data
[idx
].control_len
, size
);
2527 len
= MIN(size
, rdma
->wr_data
[idx
].control_len
);
2528 memcpy(buf
, rdma
->wr_data
[idx
].control_curr
, len
);
2529 rdma
->wr_data
[idx
].control_curr
+= len
;
2530 rdma
->wr_data
[idx
].control_len
-= len
;
2537 * QEMUFile interface to the control channel.
2538 * RDMA links don't use bytestreams, so we have to
2539 * return bytes to QEMUFile opportunistically.
2541 static int qemu_rdma_get_buffer(void *opaque
, uint8_t *buf
,
2542 int64_t pos
, int size
)
2544 QEMUFileRDMA
*r
= opaque
;
2545 RDMAContext
*rdma
= r
->rdma
;
2546 RDMAControlHeader head
;
2549 CHECK_ERROR_STATE();
2552 * First, we hold on to the last SEND message we
2553 * were given and dish out the bytes until we run
2556 r
->len
= qemu_rdma_fill(r
->rdma
, buf
, size
, 0);
2562 * Once we run out, we block and wait for another
2563 * SEND message to arrive.
2565 ret
= qemu_rdma_exchange_recv(rdma
, &head
, RDMA_CONTROL_QEMU_FILE
);
2568 rdma
->error_state
= ret
;
2573 * SEND was received with new bytes, now try again.
2575 return qemu_rdma_fill(r
->rdma
, buf
, size
, 0);
2579 * Block until all the outstanding chunks have been delivered by the hardware.
2581 static int qemu_rdma_drain_cq(QEMUFile
*f
, RDMAContext
*rdma
)
2585 if (qemu_rdma_write_flush(f
, rdma
) < 0) {
2589 while (rdma
->nb_sent
) {
2590 ret
= qemu_rdma_block_for_wrid(rdma
, RDMA_WRID_RDMA_WRITE
, NULL
);
2592 error_report("rdma migration: complete polling error!");
2597 qemu_rdma_unregister_waiting(rdma
);
2602 static int qemu_rdma_close(void *opaque
)
2604 trace_qemu_rdma_close();
2605 QEMUFileRDMA
*r
= opaque
;
2607 qemu_rdma_cleanup(r
->rdma
);
2617 * This means that 'block_offset' is a full virtual address that does not
2618 * belong to a RAMBlock of the virtual machine and instead
2619 * represents a private malloc'd memory area that the caller wishes to
2623 * Offset is an offset to be added to block_offset and used
2624 * to also lookup the corresponding RAMBlock.
2627 * Initiate an transfer this size.
2630 * A 'hint' or 'advice' that means that we wish to speculatively
2631 * and asynchronously unregister this memory. In this case, there is no
2632 * guarantee that the unregister will actually happen, for example,
2633 * if the memory is being actively transmitted. Additionally, the memory
2634 * may be re-registered at any future time if a write within the same
2635 * chunk was requested again, even if you attempted to unregister it
2638 * @size < 0 : TODO, not yet supported
2639 * Unregister the memory NOW. This means that the caller does not
2640 * expect there to be any future RDMA transfers and we just want to clean
2641 * things up. This is used in case the upper layer owns the memory and
2642 * cannot wait for qemu_fclose() to occur.
2644 * @bytes_sent : User-specificed pointer to indicate how many bytes were
2645 * sent. Usually, this will not be more than a few bytes of
2646 * the protocol because most transfers are sent asynchronously.
2648 static size_t qemu_rdma_save_page(QEMUFile
*f
, void *opaque
,
2649 ram_addr_t block_offset
, ram_addr_t offset
,
2650 size_t size
, uint64_t *bytes_sent
)
2652 QEMUFileRDMA
*rfile
= opaque
;
2653 RDMAContext
*rdma
= rfile
->rdma
;
2656 CHECK_ERROR_STATE();
2662 * Add this page to the current 'chunk'. If the chunk
2663 * is full, or the page doen't belong to the current chunk,
2664 * an actual RDMA write will occur and a new chunk will be formed.
2666 ret
= qemu_rdma_write(f
, rdma
, block_offset
, offset
, size
);
2668 error_report("rdma migration: write error! %d", ret
);
2673 * We always return 1 bytes because the RDMA
2674 * protocol is completely asynchronous. We do not yet know
2675 * whether an identified chunk is zero or not because we're
2676 * waiting for other pages to potentially be merged with
2677 * the current chunk. So, we have to call qemu_update_position()
2678 * later on when the actual write occurs.
2684 uint64_t index
, chunk
;
2686 /* TODO: Change QEMUFileOps prototype to be signed: size_t => long
2688 ret = qemu_rdma_drain_cq(f, rdma);
2690 fprintf(stderr, "rdma: failed to synchronously drain"
2691 " completion queue before unregistration.\n");
2697 ret
= qemu_rdma_search_ram_block(rdma
, block_offset
,
2698 offset
, size
, &index
, &chunk
);
2701 error_report("ram block search failed");
2705 qemu_rdma_signal_unregister(rdma
, index
, chunk
, 0);
2708 * TODO: Synchronous, guaranteed unregistration (should not occur during
2709 * fast-path). Otherwise, unregisters will process on the next call to
2710 * qemu_rdma_drain_cq()
2712 qemu_rdma_unregister_waiting(rdma);
2718 * Drain the Completion Queue if possible, but do not block,
2721 * If nothing to poll, the end of the iteration will do this
2722 * again to make sure we don't overflow the request queue.
2725 uint64_t wr_id
, wr_id_in
;
2726 int ret
= qemu_rdma_poll(rdma
, &wr_id_in
, NULL
);
2728 error_report("rdma migration: polling error! %d", ret
);
2732 wr_id
= wr_id_in
& RDMA_WRID_TYPE_MASK
;
2734 if (wr_id
== RDMA_WRID_NONE
) {
2739 return RAM_SAVE_CONTROL_DELAYED
;
2741 rdma
->error_state
= ret
;
2745 static int qemu_rdma_accept(RDMAContext
*rdma
)
2747 RDMACapabilities cap
;
2748 struct rdma_conn_param conn_param
= {
2749 .responder_resources
= 2,
2750 .private_data
= &cap
,
2751 .private_data_len
= sizeof(cap
),
2753 struct rdma_cm_event
*cm_event
;
2754 struct ibv_context
*verbs
;
2758 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
2760 goto err_rdma_dest_wait
;
2763 if (cm_event
->event
!= RDMA_CM_EVENT_CONNECT_REQUEST
) {
2764 rdma_ack_cm_event(cm_event
);
2765 goto err_rdma_dest_wait
;
2768 memcpy(&cap
, cm_event
->param
.conn
.private_data
, sizeof(cap
));
2770 network_to_caps(&cap
);
2772 if (cap
.version
< 1 || cap
.version
> RDMA_CONTROL_VERSION_CURRENT
) {
2773 error_report("Unknown source RDMA version: %d, bailing...",
2775 rdma_ack_cm_event(cm_event
);
2776 goto err_rdma_dest_wait
;
2780 * Respond with only the capabilities this version of QEMU knows about.
2782 cap
.flags
&= known_capabilities
;
2785 * Enable the ones that we do know about.
2786 * Add other checks here as new ones are introduced.
2788 if (cap
.flags
& RDMA_CAPABILITY_PIN_ALL
) {
2789 rdma
->pin_all
= true;
2792 rdma
->cm_id
= cm_event
->id
;
2793 verbs
= cm_event
->id
->verbs
;
2795 rdma_ack_cm_event(cm_event
);
2797 trace_qemu_rdma_accept_pin_state(rdma
->pin_all
);
2799 caps_to_network(&cap
);
2801 trace_qemu_rdma_accept_pin_verbsc(verbs
);
2804 rdma
->verbs
= verbs
;
2805 } else if (rdma
->verbs
!= verbs
) {
2806 error_report("ibv context not matching %p, %p!", rdma
->verbs
,
2808 goto err_rdma_dest_wait
;
2811 qemu_rdma_dump_id("dest_init", verbs
);
2813 ret
= qemu_rdma_alloc_pd_cq(rdma
);
2815 error_report("rdma migration: error allocating pd and cq!");
2816 goto err_rdma_dest_wait
;
2819 ret
= qemu_rdma_alloc_qp(rdma
);
2821 error_report("rdma migration: error allocating qp!");
2822 goto err_rdma_dest_wait
;
2825 ret
= qemu_rdma_init_ram_blocks(rdma
);
2827 error_report("rdma migration: error initializing ram blocks!");
2828 goto err_rdma_dest_wait
;
2831 for (idx
= 0; idx
< RDMA_WRID_MAX
; idx
++) {
2832 ret
= qemu_rdma_reg_control(rdma
, idx
);
2834 error_report("rdma: error registering %d control", idx
);
2835 goto err_rdma_dest_wait
;
2839 qemu_set_fd_handler2(rdma
->channel
->fd
, NULL
, NULL
, NULL
, NULL
);
2841 ret
= rdma_accept(rdma
->cm_id
, &conn_param
);
2843 error_report("rdma_accept returns %d", ret
);
2844 goto err_rdma_dest_wait
;
2847 ret
= rdma_get_cm_event(rdma
->channel
, &cm_event
);
2849 error_report("rdma_accept get_cm_event failed %d", ret
);
2850 goto err_rdma_dest_wait
;
2853 if (cm_event
->event
!= RDMA_CM_EVENT_ESTABLISHED
) {
2854 error_report("rdma_accept not event established");
2855 rdma_ack_cm_event(cm_event
);
2856 goto err_rdma_dest_wait
;
2859 rdma_ack_cm_event(cm_event
);
2860 rdma
->connected
= true;
2862 ret
= qemu_rdma_post_recv_control(rdma
, RDMA_WRID_READY
);
2864 error_report("rdma migration: error posting second control recv");
2865 goto err_rdma_dest_wait
;
2868 qemu_rdma_dump_gid("dest_connect", rdma
->cm_id
);
2873 rdma
->error_state
= ret
;
2874 qemu_rdma_cleanup(rdma
);
2879 * During each iteration of the migration, we listen for instructions
2880 * by the source VM to perform dynamic page registrations before they
2881 * can perform RDMA operations.
2883 * We respond with the 'rkey'.
2885 * Keep doing this until the source tells us to stop.
2887 static int qemu_rdma_registration_handle(QEMUFile
*f
, void *opaque
,
2890 RDMAControlHeader reg_resp
= { .len
= sizeof(RDMARegisterResult
),
2891 .type
= RDMA_CONTROL_REGISTER_RESULT
,
2894 RDMAControlHeader unreg_resp
= { .len
= 0,
2895 .type
= RDMA_CONTROL_UNREGISTER_FINISHED
,
2898 RDMAControlHeader blocks
= { .type
= RDMA_CONTROL_RAM_BLOCKS_RESULT
,
2900 QEMUFileRDMA
*rfile
= opaque
;
2901 RDMAContext
*rdma
= rfile
->rdma
;
2902 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
2903 RDMAControlHeader head
;
2904 RDMARegister
*reg
, *registers
;
2906 RDMARegisterResult
*reg_result
;
2907 static RDMARegisterResult results
[RDMA_CONTROL_MAX_COMMANDS_PER_MESSAGE
];
2908 RDMALocalBlock
*block
;
2915 CHECK_ERROR_STATE();
2918 trace_qemu_rdma_registration_handle_wait(flags
);
2920 ret
= qemu_rdma_exchange_recv(rdma
, &head
, RDMA_CONTROL_NONE
);
2926 if (head
.repeat
> RDMA_CONTROL_MAX_COMMANDS_PER_MESSAGE
) {
2927 error_report("rdma: Too many requests in this message (%d)."
2928 "Bailing.", head
.repeat
);
2933 switch (head
.type
) {
2934 case RDMA_CONTROL_COMPRESS
:
2935 comp
= (RDMACompress
*) rdma
->wr_data
[idx
].control_curr
;
2936 network_to_compress(comp
);
2938 trace_qemu_rdma_registration_handle_compress(comp
->length
,
2941 block
= &(rdma
->local_ram_blocks
.block
[comp
->block_idx
]);
2943 host_addr
= block
->local_host_addr
+
2944 (comp
->offset
- block
->offset
);
2946 ram_handle_compressed(host_addr
, comp
->value
, comp
->length
);
2949 case RDMA_CONTROL_REGISTER_FINISHED
:
2950 trace_qemu_rdma_registration_handle_finished();
2953 case RDMA_CONTROL_RAM_BLOCKS_REQUEST
:
2954 trace_qemu_rdma_registration_handle_ram_blocks();
2956 if (rdma
->pin_all
) {
2957 ret
= qemu_rdma_reg_whole_ram_blocks(rdma
);
2959 error_report("rdma migration: error dest "
2960 "registering ram blocks");
2966 * Dest uses this to prepare to transmit the RAMBlock descriptions
2967 * to the source VM after connection setup.
2968 * Both sides use the "remote" structure to communicate and update
2969 * their "local" descriptions with what was sent.
2971 for (i
= 0; i
< local
->nb_blocks
; i
++) {
2972 rdma
->block
[i
].remote_host_addr
=
2973 (uint64_t)(local
->block
[i
].local_host_addr
);
2975 if (rdma
->pin_all
) {
2976 rdma
->block
[i
].remote_rkey
= local
->block
[i
].mr
->rkey
;
2979 rdma
->block
[i
].offset
= local
->block
[i
].offset
;
2980 rdma
->block
[i
].length
= local
->block
[i
].length
;
2982 remote_block_to_network(&rdma
->block
[i
]);
2985 blocks
.len
= rdma
->local_ram_blocks
.nb_blocks
2986 * sizeof(RDMARemoteBlock
);
2989 ret
= qemu_rdma_post_send_control(rdma
,
2990 (uint8_t *) rdma
->block
, &blocks
);
2993 error_report("rdma migration: error sending remote info");
2998 case RDMA_CONTROL_REGISTER_REQUEST
:
2999 trace_qemu_rdma_registration_handle_register(head
.repeat
);
3001 reg_resp
.repeat
= head
.repeat
;
3002 registers
= (RDMARegister
*) rdma
->wr_data
[idx
].control_curr
;
3004 for (count
= 0; count
< head
.repeat
; count
++) {
3006 uint8_t *chunk_start
, *chunk_end
;
3008 reg
= ®isters
[count
];
3009 network_to_register(reg
);
3011 reg_result
= &results
[count
];
3013 trace_qemu_rdma_registration_handle_register_loop(count
,
3014 reg
->current_index
, reg
->key
.current_addr
, reg
->chunks
);
3016 block
= &(rdma
->local_ram_blocks
.block
[reg
->current_index
]);
3017 if (block
->is_ram_block
) {
3018 host_addr
= (block
->local_host_addr
+
3019 (reg
->key
.current_addr
- block
->offset
));
3020 chunk
= ram_chunk_index(block
->local_host_addr
,
3021 (uint8_t *) host_addr
);
3023 chunk
= reg
->key
.chunk
;
3024 host_addr
= block
->local_host_addr
+
3025 (reg
->key
.chunk
* (1UL << RDMA_REG_CHUNK_SHIFT
));
3027 chunk_start
= ram_chunk_start(block
, chunk
);
3028 chunk_end
= ram_chunk_end(block
, chunk
+ reg
->chunks
);
3029 if (qemu_rdma_register_and_get_keys(rdma
, block
,
3030 (uint8_t *)host_addr
, NULL
, ®_result
->rkey
,
3031 chunk
, chunk_start
, chunk_end
)) {
3032 error_report("cannot get rkey");
3037 reg_result
->host_addr
= (uint64_t) block
->local_host_addr
;
3039 trace_qemu_rdma_registration_handle_register_rkey(
3042 result_to_network(reg_result
);
3045 ret
= qemu_rdma_post_send_control(rdma
,
3046 (uint8_t *) results
, ®_resp
);
3049 error_report("Failed to send control buffer");
3053 case RDMA_CONTROL_UNREGISTER_REQUEST
:
3054 trace_qemu_rdma_registration_handle_unregister(head
.repeat
);
3055 unreg_resp
.repeat
= head
.repeat
;
3056 registers
= (RDMARegister
*) rdma
->wr_data
[idx
].control_curr
;
3058 for (count
= 0; count
< head
.repeat
; count
++) {
3059 reg
= ®isters
[count
];
3060 network_to_register(reg
);
3062 trace_qemu_rdma_registration_handle_unregister_loop(count
,
3063 reg
->current_index
, reg
->key
.chunk
);
3065 block
= &(rdma
->local_ram_blocks
.block
[reg
->current_index
]);
3067 ret
= ibv_dereg_mr(block
->pmr
[reg
->key
.chunk
]);
3068 block
->pmr
[reg
->key
.chunk
] = NULL
;
3071 perror("rdma unregistration chunk failed");
3076 rdma
->total_registrations
--;
3078 trace_qemu_rdma_registration_handle_unregister_success(
3082 ret
= qemu_rdma_post_send_control(rdma
, NULL
, &unreg_resp
);
3085 error_report("Failed to send control buffer");
3089 case RDMA_CONTROL_REGISTER_RESULT
:
3090 error_report("Invalid RESULT message at dest.");
3094 error_report("Unknown control message %s", control_desc
[head
.type
]);
3101 rdma
->error_state
= ret
;
3106 static int qemu_rdma_registration_start(QEMUFile
*f
, void *opaque
,
3109 QEMUFileRDMA
*rfile
= opaque
;
3110 RDMAContext
*rdma
= rfile
->rdma
;
3112 CHECK_ERROR_STATE();
3114 trace_qemu_rdma_registration_start(flags
);
3115 qemu_put_be64(f
, RAM_SAVE_FLAG_HOOK
);
3122 * Inform dest that dynamic registrations are done for now.
3123 * First, flush writes, if any.
3125 static int qemu_rdma_registration_stop(QEMUFile
*f
, void *opaque
,
3128 Error
*local_err
= NULL
, **errp
= &local_err
;
3129 QEMUFileRDMA
*rfile
= opaque
;
3130 RDMAContext
*rdma
= rfile
->rdma
;
3131 RDMAControlHeader head
= { .len
= 0, .repeat
= 1 };
3134 CHECK_ERROR_STATE();
3137 ret
= qemu_rdma_drain_cq(f
, rdma
);
3143 if (flags
== RAM_CONTROL_SETUP
) {
3144 RDMAControlHeader resp
= {.type
= RDMA_CONTROL_RAM_BLOCKS_RESULT
};
3145 RDMALocalBlocks
*local
= &rdma
->local_ram_blocks
;
3146 int reg_result_idx
, i
, j
, nb_remote_blocks
;
3148 head
.type
= RDMA_CONTROL_RAM_BLOCKS_REQUEST
;
3149 trace_qemu_rdma_registration_stop_ram();
3152 * Make sure that we parallelize the pinning on both sides.
3153 * For very large guests, doing this serially takes a really
3154 * long time, so we have to 'interleave' the pinning locally
3155 * with the control messages by performing the pinning on this
3156 * side before we receive the control response from the other
3157 * side that the pinning has completed.
3159 ret
= qemu_rdma_exchange_send(rdma
, &head
, NULL
, &resp
,
3160 ®_result_idx
, rdma
->pin_all
?
3161 qemu_rdma_reg_whole_ram_blocks
: NULL
);
3163 ERROR(errp
, "receiving remote info!");
3167 nb_remote_blocks
= resp
.len
/ sizeof(RDMARemoteBlock
);
3170 * The protocol uses two different sets of rkeys (mutually exclusive):
3171 * 1. One key to represent the virtual address of the entire ram block.
3172 * (dynamic chunk registration disabled - pin everything with one rkey.)
3173 * 2. One to represent individual chunks within a ram block.
3174 * (dynamic chunk registration enabled - pin individual chunks.)
3176 * Once the capability is successfully negotiated, the destination transmits
3177 * the keys to use (or sends them later) including the virtual addresses
3178 * and then propagates the remote ram block descriptions to his local copy.
3181 if (local
->nb_blocks
!= nb_remote_blocks
) {
3182 ERROR(errp
, "ram blocks mismatch #1! "
3183 "Your QEMU command line parameters are probably "
3184 "not identical on both the source and destination.");
3188 qemu_rdma_move_header(rdma
, reg_result_idx
, &resp
);
3190 rdma
->wr_data
[reg_result_idx
].control_curr
, resp
.len
);
3191 for (i
= 0; i
< nb_remote_blocks
; i
++) {
3192 network_to_remote_block(&rdma
->block
[i
]);
3194 /* search local ram blocks */
3195 for (j
= 0; j
< local
->nb_blocks
; j
++) {
3196 if (rdma
->block
[i
].offset
!= local
->block
[j
].offset
) {
3200 if (rdma
->block
[i
].length
!= local
->block
[j
].length
) {
3201 ERROR(errp
, "ram blocks mismatch #2! "
3202 "Your QEMU command line parameters are probably "
3203 "not identical on both the source and destination.");
3206 local
->block
[j
].remote_host_addr
=
3207 rdma
->block
[i
].remote_host_addr
;
3208 local
->block
[j
].remote_rkey
= rdma
->block
[i
].remote_rkey
;
3212 if (j
>= local
->nb_blocks
) {
3213 ERROR(errp
, "ram blocks mismatch #3! "
3214 "Your QEMU command line parameters are probably "
3215 "not identical on both the source and destination.");
3221 trace_qemu_rdma_registration_stop(flags
);
3223 head
.type
= RDMA_CONTROL_REGISTER_FINISHED
;
3224 ret
= qemu_rdma_exchange_send(rdma
, &head
, NULL
, NULL
, NULL
, NULL
);
3232 rdma
->error_state
= ret
;
3236 static int qemu_rdma_get_fd(void *opaque
)
3238 QEMUFileRDMA
*rfile
= opaque
;
3239 RDMAContext
*rdma
= rfile
->rdma
;
3241 return rdma
->comp_channel
->fd
;
3244 static const QEMUFileOps rdma_read_ops
= {
3245 .get_buffer
= qemu_rdma_get_buffer
,
3246 .get_fd
= qemu_rdma_get_fd
,
3247 .close
= qemu_rdma_close
,
3248 .hook_ram_load
= qemu_rdma_registration_handle
,
3251 static const QEMUFileOps rdma_write_ops
= {
3252 .put_buffer
= qemu_rdma_put_buffer
,
3253 .close
= qemu_rdma_close
,
3254 .before_ram_iterate
= qemu_rdma_registration_start
,
3255 .after_ram_iterate
= qemu_rdma_registration_stop
,
3256 .save_page
= qemu_rdma_save_page
,
3259 static void *qemu_fopen_rdma(RDMAContext
*rdma
, const char *mode
)
3261 QEMUFileRDMA
*r
= g_malloc0(sizeof(QEMUFileRDMA
));
3263 if (qemu_file_mode_is_not_valid(mode
)) {
3269 if (mode
[0] == 'w') {
3270 r
->file
= qemu_fopen_ops(r
, &rdma_write_ops
);
3272 r
->file
= qemu_fopen_ops(r
, &rdma_read_ops
);
3278 static void rdma_accept_incoming_migration(void *opaque
)
3280 RDMAContext
*rdma
= opaque
;
3283 Error
*local_err
= NULL
, **errp
= &local_err
;
3285 trace_qemu_dma_accept_incoming_migration();
3286 ret
= qemu_rdma_accept(rdma
);
3289 ERROR(errp
, "RDMA Migration initialization failed!");
3293 trace_qemu_dma_accept_incoming_migration_accepted();
3295 f
= qemu_fopen_rdma(rdma
, "rb");
3297 ERROR(errp
, "could not qemu_fopen_rdma!");
3298 qemu_rdma_cleanup(rdma
);
3302 rdma
->migration_started_on_destination
= 1;
3303 process_incoming_migration(f
);
3306 void rdma_start_incoming_migration(const char *host_port
, Error
**errp
)
3310 Error
*local_err
= NULL
;
3312 trace_rdma_start_incoming_migration();
3313 rdma
= qemu_rdma_data_init(host_port
, &local_err
);
3319 ret
= qemu_rdma_dest_init(rdma
, &local_err
);
3325 trace_rdma_start_incoming_migration_after_dest_init();
3327 ret
= rdma_listen(rdma
->listen_id
, 5);
3330 ERROR(errp
, "listening on socket!");
3334 trace_rdma_start_incoming_migration_after_rdma_listen();
3336 qemu_set_fd_handler2(rdma
->channel
->fd
, NULL
,
3337 rdma_accept_incoming_migration
, NULL
,
3338 (void *)(intptr_t) rdma
);
3341 error_propagate(errp
, local_err
);
3345 void rdma_start_outgoing_migration(void *opaque
,
3346 const char *host_port
, Error
**errp
)
3348 MigrationState
*s
= opaque
;
3349 Error
*local_err
= NULL
, **temp
= &local_err
;
3350 RDMAContext
*rdma
= qemu_rdma_data_init(host_port
, &local_err
);
3354 ERROR(temp
, "Failed to initialize RDMA data structures! %d", ret
);
3358 ret
= qemu_rdma_source_init(rdma
, &local_err
,
3359 s
->enabled_capabilities
[MIGRATION_CAPABILITY_RDMA_PIN_ALL
]);
3365 trace_rdma_start_outgoing_migration_after_rdma_source_init();
3366 ret
= qemu_rdma_connect(rdma
, &local_err
);
3372 trace_rdma_start_outgoing_migration_after_rdma_connect();
3374 s
->file
= qemu_fopen_rdma(rdma
, "wb");
3375 migrate_fd_connect(s
);
3378 error_propagate(errp
, local_err
);
3380 migrate_fd_error(s
);