资源简介
c语言利用RDMA 实现客户端和服务器端发送接收消息。包含server.c和client.c ,gcc编译方法和使用方法
代码片段和文件信息
#include
#include
#include
#include
#include
#include
#define TEST_NZ(x) do { if ( (x)) die(“error: “ #x “ failed (returned non-zero).“ ); } while (0)
#define TEST_Z(x) do { if (!(x)) die(“error: “ #x “ failed (returned zero/null).“); } while (0)
const int BUFFER_SIZE = 1024;//单位为字节,此时大小为1KB
const int TIMEOUT_IN_MS = 500; /* ms */
struct context {
struct ibv_context *ctx;
struct ibv_pd *pd;
struct ibv_cq *cq;
struct ibv_comp_channel *comp_channel;
pthread_t cq_poller_thread;
};
struct connection {
struct rdma_cm_id *id;
struct ibv_qp *qp;
struct ibv_mr *recv_mr;
struct ibv_mr *send_mr;
char *recv_region;
char *send_region;
int num_completions;
};
static void die(const char *reason);
static void build_context(struct ibv_context *verbs);
static void build_qp_attr(struct ibv_qp_init_attr *qp_attr);
static void * poll_cq(void *);
static void post_receives(struct connection *conn);
static void register_memory(struct connection *conn);//注册内存
static int on_addr_resolved(struct rdma_cm_id *id);
static void on_completion(struct ibv_wc *wc);
static int on_connection(void *context);
static int on_disconnect(struct rdma_cm_id *id);
static int on_event(struct rdma_cm_event *event);
static int on_route_resolved(struct rdma_cm_id *id);
static struct context *s_ctx = NULL;
int main(int argc char **argv)
{
struct addrinfo *addr;
struct rdma_cm_event *event = NULL;
struct rdma_cm_id *conn= NULL;
struct rdma_event_channel *ec = NULL;
if (argc != 3)
die(“usage: client “);
TEST_NZ(getaddrinfo(argv[1] argv[2] NULL &addr));//addr指针参数返回一个指向addrinfo结构链表的指针
TEST_Z(ec = rdma_create_event_channel());//创建通道,有连接请求时,用来通知应用程序的通道
TEST_NZ(rdma_create_id(ec &conn NULL RDMA_PS_TCP));//创建了rdma_cm_id对象,并将其注册到驱动中。类似与套接字
TEST_NZ(rdma_resolve_addr(conn NULL addr->ai_addr TIMEOUT_IN_MS));//绑定一个特定的地址
freeaddrinfo(addr);
while (rdma_get_cm_event(ec &event) == 0) {//判断有通道,正常情况下,会得到一个事件,也就是客户端发起连接
struct rdma_cm_event event_copy;
memcpy(&event_copy event sizeof(*event));
rdma_ack_cm_event(event);
if (on_event(&event_copy))
break;
}
rdma_destroy_event_channel(ec);
return 0;
}
void die(const char *reason)
{
fprintf(stderr “%s\n“ reason);
exit(EXIT_FAILURE);
}
void build_context(struct ibv_context *verbs)
{
if (s_ctx) {
if (s_ctx->ctx != verbs)
die(“cannot handle events in more than one context.“);
return;
}
s_ctx = (struct context *)malloc(sizeof(struct context));
s_ctx->ctx = verbs;
TEST_Z(s_ctx->pd = ibv_alloc_pd(s_ctx->ctx));
TEST_Z(s_ctx->comp_channel = ibv_create_comp_channel(s_ctx->ctx));
TEST_Z(s_ctx->cq = ibv_create_cq(s_ctx->ctx 10 NULL s_ctx->comp_channel 0)); /* cqe=10 is arbitrary */
TEST_NZ(ibv_req_notify_cq(s_ctx->cq 0));
TEST_NZ(pthread_create(&s_ctx->cq_poller_thread NULL poll_cq NULL));
}
void build_qp_attr(struc
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 7395 2019-03-18 16:14 RDMA-basic-client-server\client.c
文件 51 2019-04-10 22:08 RDMA-basic-client-server\gcc编译.txt
文件 150 2018-07-05 10:40 RDMA-basic-client-server\Makefile
文件 8505 2018-11-09 16:41 RDMA-basic-client-server\server.c
文件 58 2019-04-10 22:11 RDMA-basic-client-server\使用方法.txt
目录 0 2019-04-10 22:10 RDMA-basic-client-server
----------- --------- ---------- ----- ----
16159 6
评论
共有 条评论