RDMA编程实践
本文描述了RDMA编程过程中的SEND-RECEIVE双边原语的代码实现。包含多个版本,1、client向server发送消息,server回复client收到消息(ACK),然后两边断开连接。2、server端循环等待客户端建立连接,client发送一次消息后,双方断开连接。3、server端循环等待客户端建立连接,一旦建立,client端可以一直向server端发送消息,直到发送消息为disconnect,server和client断开链接,但是server此时仍然可以等待别的client发送消息。
代码基于代码基于send-receive样例实现。关于代码注释,可以参考代码解释:
Makefile文件、会编译当前目录下的所有.c文件:
.PHONY: all clean
CC := gcc
CFLAGS := -Wall -g
LDLIBS := -lrdmacm -libverbs -lpthread -g
SRCS := $(wildcard *.c)
APPS := $(SRCS:.c=)
all: $(APPS)
%: %.c
$(CC) $(CFLAGS) $< -o $@ $(LDLIBS)
clean:
rm -f $(APPS)
version1 客户端-服务端消息一次传递
在这个阶段,我们希望能实现下面这样一个场景。client与server端相连接,client端能够发送一条消息给server,server收到该条消息之后恢复一条消息给client端表示我已经确认收到。之后两者断开连接。
代码:
// client1.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <errno.h>
#include <getopt.h>
#include <rdma/rdma_cma.h>
#include <rdma/rdma_verbs.h>
static const char *server = "10.10.10.1";
static const char *port = "7471";
static struct rdma_cm_id *id;
static struct ibv_mr *mr, *send_mr;
static int send_flags;
static uint8_t send_msg[16];
static uint8_t recv_msg[16];
static int run(void)
{
struct rdma_addrinfo hints, *res;
struct ibv_qp_init_attr attr;
struct ibv_wc wc;
int ret;
memset(&hints, 0, sizeof hints);
hints.ai_port_space = RDMA_PS_TCP;
ret = rdma_getaddrinfo(server, port, &hints, &res);
if (ret) {
printf("rdma_getaddrinfo: %s\n", gai_strerror(ret));
goto out;
}
memset(&attr, 0, sizeof attr);
attr.cap.max_send_wr = attr.cap.max_recv_wr = 1;
attr.cap.max_send_sge = attr.cap.max_recv_sge = 1;
attr.cap.max_inline_data = 16;
attr.qp_context = id;
attr.sq_sig_all = 1;
ret = rdma_create_ep(&id, res, NULL, &attr);
// Check to see if we got inline data allowed or not
if (attr.cap.max_inline_data >= 16)
send_flags = IBV_SEND_INLINE;
else
printf("rdma_client: device doesn't support IBV_SEND_INLINE, "
"using sge sends\n");
if (ret) {
perror("rdma_create_ep");
goto out_free_addrinfo;
}
mr = rdma_reg_msgs(id, recv_msg, 16);
if (!mr) {
perror("rdma_reg_msgs for recv_msg");
ret = -1;
goto out_destroy_ep;
}
if ((send_flags & IBV_SEND_INLINE) == 0) {
send_mr = rdma_reg_msgs(id, send_msg, 16);
if (!send_mr) {
perror("rdma_reg_msgs for send_msg");
ret = -1;
goto out_dereg_recv;
}
}
ret = rdma_post_recv(id, NULL, recv_msg, 16, mr);
if (ret) {
perror("rdma_post_recv");
goto out_dereg_send;
}
ret = rdma_connect(id, NULL);
if (ret) {
perror("rdma_connect");
goto out_dereg_send;
}
printf("client send: %s\n", (char *)send_msg);
ret = rdma_post_send(id, NULL, send_msg, 16, send_mr, send_flags);
if (ret) {
perror("rdma_post_send");
goto out_disconnect;
}
while ((ret = rdma_get_send_comp(id, &wc)) == 0);
if (ret < 0) {
perror("rdma_get_send_comp");
goto out_disconnect;
}
while ((ret = rdma_get_recv_comp(id, &wc)) == 0);
if (ret < 0)
perror("rdma_get_recv_comp");
else
ret = 0;
printf("client received: %s\n", (char *) recv_msg);
out_disconnect:
rdma_disconnect(id);
out_dereg_send:
if ((send_flags & IBV_SEND_INLINE) == 0)
rdma_dereg_mr(send_mr);
out_dereg_recv:
rdma_dereg_mr(mr);
out_destroy_ep:
rdma_destroy_ep(id);
out_free_addrinfo:
rdma_freeaddrinfo(res);
out:
return ret;
}
int main(int argc, char **argv)
{
int ret;
char *s = "hello world";
// printf("client send: %s\n", s);
memcpy(send_msg, s , strlen(s));
printf("rdma_client: start\n");
ret = run();
printf("rdma_client: end %d\n", ret);
return ret;
}
server端代码
// server1.c
/*
* Copyright (c) 2005-2009 Intel Corporation. All rights reserved.
*
* This software is available to you under the OpenIB.org BSD license
* below:
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AWV
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <netdb.h>
#include <rdma/rdma_cma.h>
#include <rdma/rdma_verbs.h>
static const char *server = "0.0.0.0";
static const char *port = "7471";
static struct rdma_cm_id *listen_id, *id;
static struct ibv_mr *mr, *send_mr;
static int send_flags;
static uint8_t send_msg[16];
static uint8_t recv_msg[16];
static int run(void)
{
struct rdma_addrinfo hints, *res;
struct ibv_qp_init_attr init_attr;
struct ibv_qp_attr qp_attr;
struct ibv_wc wc;