rdma基于ibverbs和rdmacm写一个示例包含client和serverclient多次发送消息并受到server的相应用c++写
以下是一个基于ibverbs和rdmacm的RDMA示例,包含了一个客户端和一个服务器端。客户端会多次发送消息给服务器端,并接收服务器端的响应。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <infiniband/verbs.h>
#include <rdma/rdma_cma.h>
#define BUFFER_SIZE 1024
#define SERVER_PORT 12345
struct rdma_context {
struct ibv_context* context;
struct ibv_pd* pd;
struct ibv_mr* mr;
struct ibv_cq* cq;
struct ibv_qp* qp;
struct ibv_port_attr port_attr;
char* buffer;
};
void build_context(struct rdma_context* ctx) {
struct ibv_device** dev_list;
struct ibv_device* ib_dev;
int num_devices, i;
dev_list = ibv_get_device_list(&num_devices);
if (!dev_list) {
perror("Failed to get IB devices list");
exit(1);
}
if (num_devices == 0) {
perror("No IB devices found");
exit(1);
}
for (i = 0; i < num_devices; i++) {
if (strstr(ibv_get_device_name(dev_list[i]), "mlx")) {
ib_dev = dev_list[i];
break;
}
}
if (!ib_dev) {
perror("No Mellanox IB devices found");
exit(1);
}
ctx->context = ibv_open_device(ib_dev);
if (!ctx->context) {
perror("Failed to open IB device");
exit(1);
}
ctx->pd = ibv_alloc_pd(ctx->context);
if (!ctx->pd) {
perror("Failed to allocate PD");
exit(1);
}
ctx->mr = ibv_reg_mr(ctx->pd, ctx->buffer, BUFFER_SIZE, IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE);
if (!ctx->mr) {
perror("Failed to register MR");
exit(1);
}
ctx->cq = ibv_create_cq(ctx->context, 1, NULL, NULL, 0);
if (!ctx->cq) {
perror("Failed to create CQ");
exit(1);
}
struct ibv_qp_init_attr qp_init_attr;
memset(&qp_init_attr, 0, sizeof(qp_init_attr));
qp_init_attr.qp_type = IBV_QPT_RC;
qp_init_attr.send_cq = ctx->cq;
qp_init_attr.recv_cq = ctx->cq;
qp_init_attr.cap.max_send_wr = 1;
qp_init_attr.cap.max_recv_wr = 1;
qp_init_attr.cap.max_send_sge = 1;
qp_init_attr.cap.max_recv_sge = 1;
ctx->qp = ibv_create_qp(ctx->pd, &qp_init_attr);
if (!ctx->qp) {
perror("Failed to create QP");
exit(1);
}
ibv_query_port(ctx->context, 1, &ctx->port_attr);
}
void build_connection(struct rdma_context* ctx, struct rdma_cm_id* cm_id) {
struct rdma_conn_param conn_param;
memset(&conn_param, 0, sizeof(conn_param));
conn_param.responder_resources = 1;
conn_param.initiator_depth = 1;
conn_param.retry_count = 7;
conn_param.rnr_retry_count = 7;
if (rdma_connect(cm_id, &conn_param)) {
perror("Failed to connect");
exit(1);
}
struct ibv_qp_attr qp_attr;
memset(&qp_attr, 0, sizeof(qp_attr));
qp_attr.qp_state = IBV_QPS_RTR;
qp_attr.path_mtu = IBV_MTU_1024;
qp_attr.dest_qp_num = cm_id->qp->qp_num;
qp_attr.rq_psn = 0;
qp_attr.max_dest_rd_atomic = 1;
qp_attr.min_rnr_timer = 12;
qp_attr.ah_attr.is_global = 0;
qp_attr.ah_attr.dlid = cm_id->route.addr.dst_lid;
qp_attr.ah_attr.sl = 0;
qp_attr.ah_attr.src_path_bits = 0;
qp_attr.ah_attr.port_num = 1;
if (ibv_modify_qp(ctx->qp, &qp_attr, IBV_QP_STATE | IBV_QP_AV | IBV_QP_PATH_MTU | IBV_QP_DEST_QPN | IBV_QP_RQ_PSN | IBV_QP_MAX_DEST_RD_ATOMIC | IBV_QP_MIN_RNR_TIMER)) {
perror("Failed to modify QP to RTR");
exit(1);
}
memset(&qp_attr, 0, sizeof(qp_attr));
qp_attr.qp_state = IBV_QPS_RTS;
qp_attr.sq_psn = 0;
qp_attr.timeout = 14;
qp_attr.retry_cnt = 7;
qp_attr.rnr_retry = 7;
qp_attr.max_rd_atomic = 1;
if (ibv_modify_qp(ctx->qp, &qp_attr, IBV_QP_STATE | IBV_QP_SQ_PSN | IBV_QP_TIMEOUT | IBV_QP_RETRY_CNT | IBV_QP_RNR_RETRY | IBV_QP_MAX_QP_RD_ATOMIC)) {
perror("Failed to modify QP to RTS");
exit(1);
}
}
void wait_for_completion(struct rdma_context* ctx) {
struct ibv_wc wc;
while (ibv_poll_cq(ctx->cq, 1, &wc) == 0);
if (wc.status != IBV_WC_SUCCESS) {
perror("Failed to poll CQ");
exit(1);
}
}
void on_completion(struct ibv_wc* wc) {
if (wc->opcode == IBV_WC_RECV) {
std::cout << "Server: " << wc->wr_id << " received: " << ctx->buffer << std::endl;
} else if (wc->opcode == IBV_WC_SEND) {
std::cout << "Client: " << wc->wr_id << " sent: " << ctx->buffer << std::endl;
}
}
int main() {
struct rdma_context ctx;
struct rdma_cm_id* cm_id;
struct rdma_event_channel* ec;
struct sockaddr_in server_addr;
ctx.buffer = new char[BUFFER_SIZE];
memset(ctx.buffer, 0, BUFFER_SIZE);
build_context(&ctx);
ec = rdma_create_event_channel();
if (!ec) {
perror("Failed to create event channel");
exit(1);
}
if (rdma_create_id(ec, &cm_id, NULL, RDMA_PS_TCP)) {
perror("Failed to create ID");
exit(1);
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
if (inet_pton(AF_INET, "127.0.0.1", &(server_addr.sin_addr)) <= 0) {
perror("Failed to convert IP address");
exit(1);
}
if (rdma_resolve_addr(cm_id, NULL, (struct sockaddr*)&server_addr, 2000)) {
perror("Failed to resolve address");
exit(1);
}
struct rdma_cm_event* event;
if (rdma_get_cm_event(ec, &event)) {
perror("Failed to get CM event");
exit(1);
}
if (event->event != RDMA_CM_EVENT_ADDR_RESOLVED) {
perror("Unexpected CM event");
exit(1);
}
rdma_ack_cm_event(event);
if (rdma_resolve_route(cm_id, 2000)) {
perror("Failed to resolve route");
exit(1);
}
if (rdma_get_cm_event(ec, &event)) {
perror("Failed to get CM event");
exit(1);
}
if (event->event != RDMA_CM_EVENT_ROUTE_RESOLVED) {
perror("Unexpected CM event");
exit(1);
}
rdma_ack_cm_event(event);
build_connection(&ctx, cm_id);
for (int i = 0; i < 5; i++) {
sprintf(ctx.buffer, "Message %d", i);
struct ibv_send_wr send_wr;
memset(&send_wr, 0, sizeof(send_wr));
send_wr.wr_id = i;
send_wr.sg_list = &ctx->mr->sg_list[0];
send_wr.num_sge = 1;
send_wr.opcode = IBV_WR_SEND;
send_wr.send_flags = IBV_SEND_SIGNALED;
if (ibv_post_send(ctx->qp, &send_wr, NULL)) {
perror("Failed to post send");
exit(1);
}
wait_for_completion(&ctx);
}
delete[] ctx.buffer;
ibv_destroy_qp(ctx.qp);
ibv_destroy_cq(ctx.cq);
ibv_dereg_mr(ctx.mr);
ibv_dealloc_pd(ctx.pd);
ibv_close_device(ctx.context);
rdma_destroy_id(cm_id);
rdma_destroy_event_channel(ec);
return 0;
}
请确保你的系统已经安装了InfiniBand和RDMA相关的开发包,并且将代码中的IP地址设为服务器的IP地址。服务器端的代码类似,只需将客户端和服务器端的代码分别保存为不同的文件,并分别编译运行。
编译命令:
g++ -o client client.cpp -libverbs -lrdmacm
g++ -o server server.cpp -libverbs -lrdmacm
运行命令:
./server
./client
这样,客户端就会向服务器端发送5条消息,并接收服务器端的响应
原文地址: http://www.cveoy.top/t/topic/iDiD 著作权归作者所有。请勿转载和采集!