Untitled

mail@pastecode.io avatar
unknown
plain_text
23 days ago
3.4 kB
3
Indexable
Never
#include "rdma_common.h"

static int setup_socket(struct connection *conn, const char *server_name)
{
    struct sockaddr_in addr;
    struct addrinfo *res;
    int ret;

    conn->sock = socket(AF_INET, SOCK_STREAM, 0);
    if (conn->sock < 0) {
        fprintf(stderr, "Failed to create socket: %s\n", strerror(errno));
        return -1;
    }

    ret = getaddrinfo(server_name, NULL, NULL, &res);
    if (ret) {
        fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(ret));
        return -1;
    }

    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(TCP_PORT);
    memcpy(&addr.sin_addr, &((struct sockaddr_in *)res->ai_addr)->sin_addr, sizeof(addr.sin_addr));

    freeaddrinfo(res);

    if (connect(conn->sock, (struct sockaddr *)&addr, sizeof(addr))) {
        fprintf(stderr, "Failed to connect to server: %s\n", strerror(errno));
        return -1;
    }

    printf("Connected to server\n");
    return 0;
}

static int exchange_qp_info(struct connection *conn)
{
    if (write(conn->sock, &conn->local_qp_info, sizeof(conn->local_qp_info)) != sizeof(conn->local_qp_info)) {
        fprintf(stderr, "Failed to send local QP info\n");
        return -1;
    }

    if (read(conn->sock, &conn->remote_qp_info, sizeof(conn->remote_qp_info)) != sizeof(conn->remote_qp_info)) {
        fprintf(stderr, "Failed to receive remote QP info\n");
        return -1;
    }

    printf("QP info exchanged\n");
    return 0;
}

int main(int argc, char *argv[])
{
    struct connection conn;
    struct ibv_wc wc;
    int ne;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s <server-address>\n", argv[0]);
        return 1;
    }

    memset(&conn, 0, sizeof(conn));
    conn.ib_port = 1; // Use port 1 by default

    if (setup_socket(&conn, argv[1])) {
        fprintf(stderr, "Failed to setup socket\n");
        return 1;
    }

    if (setup_connection(&conn, NULL, conn.ib_port)) {
        fprintf(stderr, "Failed to setup RDMA connection\n");
        return 1;
    }

    if (exchange_qp_info(&conn)) {
        fprintf(stderr, "Failed to exchange QP info\n");
        return 1;
    }

    if (modify_qp_to_rtr(conn.qp, conn.ib_port, conn.remote_qp_info.qp_num, conn.remote_qp_info.lid, NULL)) {
        fprintf(stderr, "Failed to modify QP to RTR\n");
        return 1;
    }

    if (modify_qp_to_rts(conn.qp)) {
        fprintf(stderr, "Failed to modify QP to RTS\n");
        return 1;
    }

    strcpy(conn.buf, "Hello from client!");
    printf("Sending message: %s\n", conn.buf);

    if (post_send(&conn)) {
        fprintf(stderr, "Failed to post send\n");
        return 1;
    }

    do {
        ne = ibv_poll_cq(conn.cq, 1, &wc);
    } while (ne == 0);

    if (ne < 0) {
        fprintf(stderr, "Failed to poll CQ: %s\n", strerror(errno));
        return 1;
    }

    if (wc.status != IBV_WC_SUCCESS) {
        fprintf(stderr, "Work completion failed with status %s (%d)\n", 
                ibv_wc_status_str(wc.status), wc.status);
        return 1;
    }

    printf("Message sent successfully\n");

    // Clean up
    ibv_destroy_qp(conn.qp);
    ibv_destroy_cq(conn.cq);
    ibv_dereg_mr(conn.mr);
    ibv_dealloc_pd(conn.pd);
    ibv_close_device(conn.context);
    free(conn.buf);
    close(conn.sock);

    return 0;
}
Leave a Comment