+ rdma->total_bytes = 0;
+ rdma->enabled = 1;
+ qemu_rdma_dump_gid("server_connect", rdma->rdma_ctx.cm_id);
+ return 0;
+
+err_rdma_server_wait:
+ qemu_rdma_cleanup(rdma);
+ return -1;
+
+}
+
+int rdma_start_incoming_migration(const char * host_port, Error **errp)
+{
+ RDMAData *rdma = g_malloc0(sizeof(RDMAData));
+ QEMUFile *f;
+ int ret;
+
+ if ((ret = qemu_rdma_data_init(rdma, host_port, errp)) < 0)
+ return ret;
+
+ ret = qemu_rdma_server_init(rdma, NULL);
+
+ DPRINTF("Starting RDMA-based incoming migration\n");
+
+ if (!ret) {
+ DPRINTF("qemu_rdma_server_init success\n");
+ ret = qemu_rdma_server_prepare(rdma, NULL);
+
+ if (!ret) {
+ DPRINTF("qemu_rdma_server_prepare success\n");
+
+ ret = rdma_accept_incoming_migration(rdma, NULL);
+ if(!ret)
+ DPRINTF("qemu_rdma_accept_incoming_migration success\n");
+ f = qemu_fopen_rdma(rdma, "rb");
+ if (f == NULL) {
+ fprintf(stderr, "could not qemu_fopen RDMA\n");
+ ret = -EIO;
+ }
+
+ process_incoming_migration(f);
+ }
+ }
+
+ return ret;
+}
+
+void rdma_start_outgoing_migration(void *opaque, const char *host_port, Error
**errp)
+{
+ RDMAData *rdma = g_malloc0(sizeof(RDMAData));
+ MigrationState *s = opaque;
+ int ret;
+
+ if (qemu_rdma_data_init(rdma, host_port, errp) < 0)
+ return;
+
+ ret = qemu_rdma_client_init(rdma, NULL);
+ if(!ret) {
+ DPRINTF("qemu_rdma_client_init success\n");
+ ret = qemu_rdma_client_connect(rdma, NULL);
+
+ if(!ret) {
+ s->file = qemu_fopen_rdma(rdma, "wb");
+ DPRINTF("qemu_rdma_client_connect success\n");
+ migrate_fd_connect(s);
+ return;
+ }
+ }
+
+ migrate_fd_error(s);
+}
+
+size_t save_rdma_page(QEMUFile *f, ram_addr_t block_offset, ram_addr_t offset,
int cont, size_t size)
+{
+ int ret;
+ size_t bytes_sent = 0;
+ ram_addr_t current_addr;
+ RDMAData * rdma = migrate_use_rdma(f);
+
+ current_addr = block_offset + offset;
+
+ /*
+ * Add this page to the current 'chunk'. If the chunk
+ * is full, an actual RDMA write will occur.
+ */
+ if ((ret = qemu_rdma_write(rdma, current_addr, size)) < 0) {
+ fprintf(stderr, "rdma migration: write error! %d\n", ret);
+ return ret;
+ }
+
+ /*
+ * Drain the Completion Queue if possible.
+ * If not, the end of the iteration will do this
+ * again to make sure we don't overflow the
+ * request queue.
+ */
+ while (1) {
+ int ret = qemu_rdma_poll(rdma);
+ if (ret == RDMA_WRID_NONE) {
+ break;
+ }
+ if (ret < 0) {
+ fprintf(stderr, "rdma migration: polling error! %d\n", ret);
+ return ret;
+ }
+ }
+
+ bytes_sent += size;
+ return bytes_sent;
+}
+
+size_t qemu_rdma_fill(void * opaque, uint8_t *buf, int size)
+{
+ RDMAData * rdma = opaque;
+ size_t len = 0;
+
+ if(rdma->qemu_file_len) {
+ DPRINTF("RDMA %" PRId64 " of %d bytes already in buffer\n",
+ rdma->qemu_file_len, size);
+
+ len = MIN(size, rdma->qemu_file_len);
+ memcpy(buf, rdma->qemu_file_curr, len);
+ rdma->qemu_file_curr += len;
+ rdma->qemu_file_len -= len;
+ }
+
+ return len;
+}