Untitled
unknown
plain_text
a month ago
3.4 kB
3
Indexable
Never
#include "rdma_common.h" static int setup_socket(struct connection *conn) { struct sockaddr_in addr; int client_sock; conn->sock = socket(AF_INET, SOCK_STREAM, 0); if (conn->sock < 0) { fprintf(stderr, "Failed to create socket: %s\n", strerror(errno)); return -1; } memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = htons(TCP_PORT); if (bind(conn->sock, (struct sockaddr *)&addr, sizeof(addr))) { fprintf(stderr, "Failed to bind socket: %s\n", strerror(errno)); return -1; } if (listen(conn->sock, 1)) { fprintf(stderr, "Failed to listen on socket: %s\n", strerror(errno)); return -1; } printf("Waiting for client connection...\n"); client_sock = accept(conn->sock, NULL, NULL); if (client_sock < 0) { fprintf(stderr, "Failed to accept client connection: %s\n", strerror(errno)); return -1; } close(conn->sock); conn->sock = client_sock; printf("Client connected\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; memset(&conn, 0, sizeof(conn)); conn.ib_port = 1; // Use port 1 by default if (setup_socket(&conn)) { 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; } printf("Waiting for client message...\n"); if (post_recv(&conn)) { fprintf(stderr, "Failed to post receive\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("Received message: %s\n", conn.buf); // 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