static int qemu_rdma_accept(RDMAContext *rdma)

{

    RDMACapabilities cap;

    struct rdma_conn_param conn_param = {

                                            .responder_resources = 2,

                                            .private_data = &cap,

                                            .private_data_len = sizeof(cap),

                                         };

    struct rdma_cm_event *cm_event;

    struct ibv_context *verbs;

    int ret = -EINVAL;

    int idx;



    ret = rdma_get_cm_event(rdma->channel, &cm_event);

    if (ret) {

        goto err_rdma_dest_wait;

    }



    if (cm_event->event != RDMA_CM_EVENT_CONNECT_REQUEST) {

        rdma_ack_cm_event(cm_event);

        goto err_rdma_dest_wait;

    }



    memcpy(&cap, cm_event->param.conn.private_data, sizeof(cap));



    network_to_caps(&cap);



    if (cap.version < 1 || cap.version > RDMA_CONTROL_VERSION_CURRENT) {

            fprintf(stderr, "Unknown source RDMA version: %d, bailing...\n",

                            cap.version);

            rdma_ack_cm_event(cm_event);

            goto err_rdma_dest_wait;

    }



    /*

     * Respond with only the capabilities this version of QEMU knows about.

     */

    cap.flags &= known_capabilities;



    /*

     * Enable the ones that we do know about.

     * Add other checks here as new ones are introduced.

     */

    if (cap.flags & RDMA_CAPABILITY_PIN_ALL) {

        rdma->pin_all = true;

    }



    rdma->cm_id = cm_event->id;

    verbs = cm_event->id->verbs;



    rdma_ack_cm_event(cm_event);



    DPRINTF("Memory pin all: %s\n", rdma->pin_all ? "enabled" : "disabled");



    caps_to_network(&cap);



    DPRINTF("verbs context after listen: %p\n", verbs);



    if (!rdma->verbs) {

        rdma->verbs = verbs;

    } else if (rdma->verbs != verbs) {

            fprintf(stderr, "ibv context not matching %p, %p!\n",

                    rdma->verbs, verbs);

            goto err_rdma_dest_wait;

    }



    qemu_rdma_dump_id("dest_init", verbs);



    ret = qemu_rdma_alloc_pd_cq(rdma);

    if (ret) {

        fprintf(stderr, "rdma migration: error allocating pd and cq!\n");

        goto err_rdma_dest_wait;

    }



    ret = qemu_rdma_alloc_qp(rdma);

    if (ret) {

        fprintf(stderr, "rdma migration: error allocating qp!\n");

        goto err_rdma_dest_wait;

    }



    ret = qemu_rdma_init_ram_blocks(rdma);

    if (ret) {

        fprintf(stderr, "rdma migration: error initializing ram blocks!\n");

        goto err_rdma_dest_wait;

    }



    for (idx = 0; idx < RDMA_WRID_MAX; idx++) {

        ret = qemu_rdma_reg_control(rdma, idx);

        if (ret) {

            fprintf(stderr, "rdma: error registering %d control!\n", idx);

            goto err_rdma_dest_wait;

        }

    }



    qemu_set_fd_handler2(rdma->channel->fd, NULL, NULL, NULL, NULL);



    ret = rdma_accept(rdma->cm_id, &conn_param);

    if (ret) {

        fprintf(stderr, "rdma_accept returns %d!\n", ret);

        goto err_rdma_dest_wait;

    }



    ret = rdma_get_cm_event(rdma->channel, &cm_event);

    if (ret) {

        fprintf(stderr, "rdma_accept get_cm_event failed %d!\n", ret);

        goto err_rdma_dest_wait;

    }



    if (cm_event->event != RDMA_CM_EVENT_ESTABLISHED) {

        fprintf(stderr, "rdma_accept not event established!\n");

        rdma_ack_cm_event(cm_event);

        goto err_rdma_dest_wait;

    }



    rdma_ack_cm_event(cm_event);

    rdma->connected = true;



    ret = qemu_rdma_post_recv_control(rdma, RDMA_WRID_READY);

    if (ret) {

        fprintf(stderr, "rdma migration: error posting second control recv!\n");

        goto err_rdma_dest_wait;

    }



    qemu_rdma_dump_gid("dest_connect", rdma->cm_id);



    return 0;



err_rdma_dest_wait:

    rdma->error_state = ret;

    qemu_rdma_cleanup(rdma);

    return ret;

}

static int qemu_rdma_accept(RDMAContext *rdma)

{

    RDMACapabilities cap;

    struct rdma_conn_param conn_param = {

                                            .responder_resources = 2,

                                            .private_data = &cap,

                                            .private_data_len = sizeof(cap),

                                         };

    struct rdma_cm_event *cm_event;

    struct ibv_context *verbs;

    int ret = -EINVAL;

    int idx;



    ret = rdma_get_cm_event(rdma->channel, &cm_event);

    if (ret) {

        goto err_rdma_dest_wait;

    }



    if (cm_event->event != RDMA_CM_EVENT_CONNECT_REQUEST) {

        rdma_ack_cm_event(cm_event);

        goto err_rdma_dest_wait;

    }



    memcpy(&cap, cm_event->param.conn.private_data, sizeof(cap));



    network_to_caps(&cap);



    if (cap.version < 1 || cap.version > RDMA_CONTROL_VERSION_CURRENT) {

            fprintf(stderr, "Unknown source RDMA version: %d, bailing...\n",

                            cap.version);

            rdma_ack_cm_event(cm_event);

            goto err_rdma_dest_wait;

    }



    





    cap.flags &= known_capabilities;



    







    if (cap.flags & RDMA_CAPABILITY_PIN_ALL) {

        rdma->pin_all = true;

    }



    rdma->cm_id = cm_event->id;

    verbs = cm_event->id->verbs;



    rdma_ack_cm_event(cm_event);



    DPRINTF("Memory pin all: %s\n", rdma->pin_all ? "enabled" : "disabled");



    caps_to_network(&cap);



    DPRINTF("verbs context after listen: %p\n", verbs);



    if (!rdma->verbs) {

        rdma->verbs = verbs;

    } else if (rdma->verbs != verbs) {

            fprintf(stderr, "ibv context not matching %p, %p!\n",

                    rdma->verbs, verbs);

            goto err_rdma_dest_wait;

    }



    qemu_rdma_dump_id("dest_init", verbs);



    ret = qemu_rdma_alloc_pd_cq(rdma);

    if (ret) {

        fprintf(stderr, "rdma migration: error allocating pd and cq!\n");

        goto err_rdma_dest_wait;

    }



    ret = qemu_rdma_alloc_qp(rdma);

    if (ret) {

        fprintf(stderr, "rdma migration: error allocating qp!\n");

        goto err_rdma_dest_wait;

    }



    ret = qemu_rdma_init_ram_blocks(rdma);

    if (ret) {

        fprintf(stderr, "rdma migration: error initializing ram blocks!\n");

        goto err_rdma_dest_wait;

    }



    for (idx = 0; idx < RDMA_WRID_MAX; idx++) {

        ret = qemu_rdma_reg_control(rdma, idx);

        if (ret) {

            fprintf(stderr, "rdma: error registering %d control!\n", idx);

            goto err_rdma_dest_wait;

        }

    }



    qemu_set_fd_handler2(rdma->channel->fd, NULL, NULL, NULL, NULL);



    ret = rdma_accept(rdma->cm_id, &conn_param);

    if (ret) {

        fprintf(stderr, "rdma_accept returns %d!\n", ret);

        goto err_rdma_dest_wait;

    }



    ret = rdma_get_cm_event(rdma->channel, &cm_event);

    if (ret) {

        fprintf(stderr, "rdma_accept get_cm_event failed %d!\n", ret);

        goto err_rdma_dest_wait;

    }



    if (cm_event->event != RDMA_CM_EVENT_ESTABLISHED) {

        fprintf(stderr, "rdma_accept not event established!\n");

        rdma_ack_cm_event(cm_event);

        goto err_rdma_dest_wait;

    }



    rdma_ack_cm_event(cm_event);

    rdma->connected = true;



    ret = qemu_rdma_post_recv_control(rdma, RDMA_WRID_READY);

    if (ret) {

        fprintf(stderr, "rdma migration: error posting second control recv!\n");

        goto err_rdma_dest_wait;

    }



    qemu_rdma_dump_gid("dest_connect", rdma->cm_id);



    return 0;



err_rdma_dest_wait:

    rdma->error_state = ret;

    qemu_rdma_cleanup(rdma);

    return ret;

}

static int FUNC_0(RDMAContext *VAR_0)

{

    RDMACapabilities cap;

    struct rdma_conn_param VAR_1 = {

                                            .responder_resources = 2,

                                            .private_data = &cap,

                                            .private_data_len = sizeof(cap),

                                         };

    struct rdma_cm_event *VAR_2;

    struct ibv_context *VAR_3;

    int VAR_4 = -EINVAL;

    int VAR_5;



    VAR_4 = rdma_get_cm_event(VAR_0->channel, &VAR_2);

    if (VAR_4) {

        goto err_rdma_dest_wait;

    }



    if (VAR_2->event != RDMA_CM_EVENT_CONNECT_REQUEST) {

        rdma_ack_cm_event(VAR_2);

        goto err_rdma_dest_wait;

    }



    memcpy(&cap, VAR_2->param.conn.private_data, sizeof(cap));



    network_to_caps(&cap);



    if (cap.version < 1 || cap.version > RDMA_CONTROL_VERSION_CURRENT) {

            fprintf(stderr, "Unknown source RDMA version: %d, bailing...\n",

                            cap.version);

            rdma_ack_cm_event(VAR_2);

            goto err_rdma_dest_wait;

    }



    





    cap.flags &= known_capabilities;



    







    if (cap.flags & RDMA_CAPABILITY_PIN_ALL) {

        VAR_0->pin_all = true;

    }



    VAR_0->cm_id = VAR_2->id;

    VAR_3 = VAR_2->id->VAR_3;



    rdma_ack_cm_event(VAR_2);



    DPRINTF("Memory pin all: %s\n", VAR_0->pin_all ? "enabled" : "disabled");



    caps_to_network(&cap);



    DPRINTF("VAR_3 context after listen: %p\n", VAR_3);



    if (!VAR_0->VAR_3) {

        VAR_0->VAR_3 = VAR_3;

    } else if (VAR_0->VAR_3 != VAR_3) {

            fprintf(stderr, "ibv context not matching %p, %p!\n",

                    VAR_0->VAR_3, VAR_3);

            goto err_rdma_dest_wait;

    }



    qemu_rdma_dump_id("dest_init", VAR_3);



    VAR_4 = qemu_rdma_alloc_pd_cq(VAR_0);

    if (VAR_4) {

        fprintf(stderr, "VAR_0 migration: error allocating pd and cq!\n");

        goto err_rdma_dest_wait;

    }



    VAR_4 = qemu_rdma_alloc_qp(VAR_0);

    if (VAR_4) {

        fprintf(stderr, "VAR_0 migration: error allocating qp!\n");

        goto err_rdma_dest_wait;

    }



    VAR_4 = qemu_rdma_init_ram_blocks(VAR_0);

    if (VAR_4) {

        fprintf(stderr, "VAR_0 migration: error initializing ram blocks!\n");

        goto err_rdma_dest_wait;

    }



    for (VAR_5 = 0; VAR_5 < RDMA_WRID_MAX; VAR_5++) {

        VAR_4 = qemu_rdma_reg_control(VAR_0, VAR_5);

        if (VAR_4) {

            fprintf(stderr, "VAR_0: error registering %d control!\n", VAR_5);

            goto err_rdma_dest_wait;

        }

    }



    qemu_set_fd_handler2(VAR_0->channel->fd, NULL, NULL, NULL, NULL);



    VAR_4 = rdma_accept(VAR_0->cm_id, &VAR_1);

    if (VAR_4) {

        fprintf(stderr, "rdma_accept returns %d!\n", VAR_4);

        goto err_rdma_dest_wait;

    }



    VAR_4 = rdma_get_cm_event(VAR_0->channel, &VAR_2);

    if (VAR_4) {

        fprintf(stderr, "rdma_accept get_cm_event failed %d!\n", VAR_4);

        goto err_rdma_dest_wait;

    }



    if (VAR_2->event != RDMA_CM_EVENT_ESTABLISHED) {

        fprintf(stderr, "rdma_accept not event established!\n");

        rdma_ack_cm_event(VAR_2);

        goto err_rdma_dest_wait;

    }



    rdma_ack_cm_event(VAR_2);

    VAR_0->connected = true;



    VAR_4 = qemu_rdma_post_recv_control(VAR_0, RDMA_WRID_READY);

    if (VAR_4) {

        fprintf(stderr, "VAR_0 migration: error posting second control recv!\n");

        goto err_rdma_dest_wait;

    }



    qemu_rdma_dump_gid("dest_connect", VAR_0->cm_id);



    return 0;



err_rdma_dest_wait:

    VAR_0->error_state = VAR_4;

    qemu_rdma_cleanup(VAR_0);

    return VAR_4;

}

static int FUNC_0(RDMAContext *VAR_0)
{ RDMACapabilities cap; struct rdma_conn_param VAR_1 = { .responder_resources = 2,
.private_data = &cap,
.private_data_len = sizeof(cap),
}; struct rdma_cm_event *VAR_2; struct ibv_context *VAR_3; int VAR_4 = -EINVAL; int VAR_5; VAR_4 = rdma_get_cm_event(VAR_0->channel, &VAR_2); if (VAR_4) { goto err_rdma_dest_wait; } if (VAR_2->event != RDMA_CM_EVENT_CONNECT_REQUEST) { rdma_ack_cm_event(VAR_2); goto err_rdma_dest_wait; } memcpy(&cap, VAR_2->param.conn.private_data, sizeof(cap)); network_to_caps(&cap); if (cap.version < 1 || cap.version > RDMA_CONTROL_VERSION_CURRENT) { fprintf(stderr, "Unknown source RDMA version: %d, bailing...\n",
cap.version); rdma_ack_cm_event(VAR_2); goto err_rdma_dest_wait; } cap.flags &= known_capabilities; if (cap.flags & RDMA_CAPABILITY_PIN_ALL) { VAR_0->pin_all = true; } VAR_0->cm_id = VAR_2->id; VAR_3 = VAR_2->id->VAR_3; rdma_ack_cm_event(VAR_2); DPRINTF("Memory pin all: %s\n", VAR_0->pin_all ? "enabled" : "disabled"); caps_to_network(&cap); DPRINTF("VAR_3 context after listen: %p\n", VAR_3); if (!VAR_0->VAR_3) { VAR_0->VAR_3 = VAR_3; } else if (VAR_0->VAR_3 != VAR_3) { fprintf(stderr, "ibv context not matching %p, %p!\n",
VAR_0->VAR_3, VAR_3); goto err_rdma_dest_wait; } qemu_rdma_dump_id("dest_init", VAR_3); VAR_4 = qemu_rdma_alloc_pd_cq(VAR_0); if (VAR_4) { fprintf(stderr, "VAR_0 migration: error allocating pd and cq!\n"); goto err_rdma_dest_wait; } VAR_4 = qemu_rdma_alloc_qp(VAR_0); if (VAR_4) { fprintf(stderr, "VAR_0 migration: error allocating qp!\n"); goto err_rdma_dest_wait; } VAR_4 = qemu_rdma_init_ram_blocks(VAR_0); if (VAR_4) { fprintf(stderr, "VAR_0 migration: error initializing ram blocks!\n"); goto err_rdma_dest_wait; } for (VAR_5 = 0; VAR_5 < RDMA_WRID_MAX; VAR_5++) { VAR_4 = qemu_rdma_reg_control(VAR_0, VAR_5); if (VAR_4) { fprintf(stderr, "VAR_0: error registering %d control!\n", VAR_5); goto err_rdma_dest_wait; } } qemu_set_fd_handler2(VAR_0->channel->fd, NULL, NULL, NULL, NULL); VAR_4 = rdma_accept(VAR_0->cm_id, &VAR_1); if (VAR_4) { fprintf(stderr, "rdma_accept returns %d!\n", VAR_4); goto err_rdma_dest_wait; } VAR_4 = rdma_get_cm_event(VAR_0->channel, &VAR_2); if (VAR_4) { fprintf(stderr, "rdma_accept get_cm_event failed %d!\n", VAR_4); goto err_rdma_dest_wait; } if (VAR_2->event != RDMA_CM_EVENT_ESTABLISHED) { fprintf(stderr, "rdma_accept not event established!\n"); rdma_ack_cm_event(VAR_2); goto err_rdma_dest_wait; } rdma_ack_cm_event(VAR_2); VAR_0->connected = true; VAR_4 = qemu_rdma_post_recv_control(VAR_0, RDMA_WRID_READY); if (VAR_4) { fprintf(stderr, "VAR_0 migration: error posting second control recv!\n"); goto err_rdma_dest_wait; } qemu_rdma_dump_gid("dest_connect", VAR_0->cm_id); return 0; err_rdma_dest_wait:
VAR_0->error_state = VAR_4; qemu_rdma_cleanup(VAR_0); return VAR_4; }