RDMA编程实践-SEND-RECEICVE原语应用

这篇具有很好参考价值的文章主要介绍了RDMA编程实践-SEND-RECEICVE原语应用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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;
	int ret;

	memset(&hints, 0, sizeof hints);
	hints.ai_flags = RAI_PASSIVE;
	hints.ai_port_space = RDMA_PS_TCP;
	ret = rdma_getaddrinfo(server, port, &hints, &res);
	if (ret) {
		printf("rdma_getaddrinfo: %s\n", gai_strerror(ret));
		return ret;
	}

	memset(&init_attr, 0, sizeof init_attr);
	init_attr.cap.max_send_wr = init_attr.cap.max_recv_wr = 1;
	init_attr.cap.max_send_sge = init_attr.cap.max_recv_sge = 1;
	init_attr.cap.max_inline_data = 16;
	init_attr.sq_sig_all = 1;
	ret = rdma_create_ep(&listen_id, res, NULL, &init_attr);
	if (ret) {
		perror("rdma_create_ep");
		goto out_free_addrinfo;
	}

	ret = rdma_listen(listen_id, 0);
	if (ret) {
		perror("rdma_listen");
		goto out_destroy_listen_ep;
	}

	ret = rdma_get_request(listen_id, &id);
	if (ret) {
		perror("rdma_get_request");
		goto out_destroy_listen_ep;
	}

	memset(&qp_attr, 0, sizeof qp_attr);
	memset(&init_attr, 0, sizeof init_attr);
	ret = ibv_query_qp(id->qp, &qp_attr, IBV_QP_CAP,
			   &init_attr);
	if (ret) {
		perror("ibv_query_qp");
		goto out_destroy_accept_ep;
	}
	if (init_attr.cap.max_inline_data >= 16)
		send_flags = IBV_SEND_INLINE;
	else
		printf("rdma_server: device doesn't support IBV_SEND_INLINE, "
		       "using sge sends\n");

	mr = rdma_reg_msgs(id, recv_msg, 16);
	if (!mr) {
		ret = -1;
		perror("rdma_reg_msgs for recv_msg");
		goto out_destroy_accept_ep;
	}
	if ((send_flags & IBV_SEND_INLINE) == 0) {
		send_mr = rdma_reg_msgs(id, send_msg, 16);
		if (!send_mr) {
			ret = -1;
			perror("rdma_reg_msgs for send_msg");
			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_accept(id, NULL);
	if (ret) {
		perror("rdma_accept");
		goto out_dereg_send;
	}

	while ((ret = rdma_get_recv_comp(id, &wc)) == 0);
	if (ret < 0) {
		perror("rdma_get_recv_comp");
		goto out_disconnect;
	}

    printf("server received: %s\n" , (char *)recv_msg);
    
    char *s = "ACK";
    memcpy(send_msg, s, strlen(s));
    printf("server 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");
	else
		ret = 0;

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_accept_ep:
	rdma_destroy_ep(id);
out_destroy_listen_ep:
	rdma_destroy_ep(listen_id);
out_free_addrinfo:
	rdma_freeaddrinfo(res);
	return ret;
}

int main(int argc, char **argv)
{
	int ret;

	printf("rdma_server: start\n");
	ret = run();
	printf("rdma_server: end %d\n", ret);
	return ret;
}

首先make编译完之后,在server端执行 ./server1,然后在客户端执行./client1
运行结果:
RDMA编程实践-SEND-RECEICVE原语应用,RDMA,RDMA,send-receive,c
RDMA编程实践-SEND-RECEICVE原语应用,RDMA,RDMA,send-receive,c
可以看到 client向server发送了hello world,server收到之后打印出来并回复给client端ACK消息,client收到之后并打印。最后双方断开连接,完成!

version2-客户端发送一次,服务端循环等待

client2的代码跟上面一样,server2代码不一样。
server2的逻辑:在run函数进来之后记录一个connect点,当远程客户端发送完信息后,释放连接的资源,跳转到connect阶段准备让下一个client连接。

/*
 * 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>


#define N 100
#define MAX_CAP 32

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[MAX_CAP];
static uint8_t recv_msg[MAX_CAP];

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;
	int ret;

    while(1)
    {

        memset(&hints, 0, sizeof hints);
        hints.ai_flags = RAI_PASSIVE;
        hints.ai_port_space = RDMA_PS_TCP;
        ret = rdma_getaddrinfo(server, port, &hints, &res);
        if (ret) {
            printf("rdma_getaddrinfo: %s\n", gai_strerror(ret));
            return ret;
        }

        memset(&init_attr, 0, sizeof init_attr);
        init_attr.cap.max_send_wr = init_attr.cap.max_recv_wr = N;
        init_attr.cap.max_send_sge = init_attr.cap.max_recv_sge = 1;
        init_attr.cap.max_inline_data = MAX_CAP;
        init_attr.sq_sig_all = 1;

        ret = rdma_create_ep(&listen_id, res, NULL, &init_attr);
        if (ret) {
            perror("rdma_create_ep");
            goto out_free_addrinfo;
        }

        ret = rdma_listen(listen_id, 0);
        if (ret) {
            perror("rdma_listen");
            goto out_destroy_listen_ep;
        }

        ret = rdma_get_request(listen_id, &id);
        if (ret) {
            perror("rdma_get_request");
            goto out_destroy_listen_ep;
        }

        memset(&qp_attr, 0, sizeof qp_attr);
        memset(&init_attr, 0, sizeof init_attr);
        ret = ibv_query_qp(id->qp, &qp_attr, IBV_QP_CAP,
                &init_attr);
        if (ret) {
            perror("ibv_query_qp");
            goto out_destroy_accept_ep;
        }
        if (init_attr.cap.max_inline_data >= MAX_CAP)
            send_flags = IBV_SEND_INLINE;
        else
            printf("rdma_server: device doesn't support IBV_SEND_INLINE, "
                "using sge sends\n");
        mr = rdma_reg_msgs(id, recv_msg, N);
        if (!mr) {
            ret = -1;
            perror("rdma_reg_msgs for recv_msg");
            goto out_destroy_accept_ep;
        }
        if ((send_flags & IBV_SEND_INLINE) == 0) {
            send_mr = rdma_reg_msgs(id, send_msg, MAX_CAP);
            if (!send_mr) {
                ret = -1;
                perror("rdma_reg_msgs for send_msg");
                goto out_dereg_recv;
            }
        }   
        

        ret = rdma_accept(id, NULL);
        if (ret) {
            perror("rdma_accept");
            goto out_dereg_send;
        }

        
        memset(recv_msg, 0 , sizeof recv_msg);
        memset(send_msg, 0 , sizeof send_msg);
        ret = rdma_post_recv(id, NULL, recv_msg, MAX_CAP, mr);
        if (ret) {
            perror("rdma_post_recv");
            goto out_dereg_send;
        }
        while ((ret = rdma_get_recv_comp(id, &wc)) == 0);
        if (ret < 0) {
            perror("rdma_get_recv_comp");
            goto out_disconnect;
        }
        
        printf("server received: %s\n", (char *)recv_msg);
        memcpy(send_msg, recv_msg, sizeof(recv_msg));
        
        ret = rdma_post_send(id, NULL, send_msg, MAX_CAP, send_mr, send_flags);
        if (ret) {
            perror("rdma_post_send");
            goto out_disconnect;
        }
        while ((ret = rdma_get_send_comp(id, &wc)) == 0); // 确认对方已经收到 对方会发送ack
        if (ret < 0)
            perror("rdma_get_send_comp");
        else
            ret = 0;
        
        rdma_disconnect(id);
        if ((send_flags & IBV_SEND_INLINE) == 0)
            rdma_dereg_mr(send_mr);
        rdma_dereg_mr(mr);
        rdma_destroy_ep(id);
        rdma_destroy_ep(listen_id);
        rdma_freeaddrinfo(res);  
    }
         
    

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_accept_ep:
    rdma_destroy_ep(id);
out_destroy_listen_ep:
    rdma_destroy_ep(listen_id);
out_free_addrinfo:
    rdma_freeaddrinfo(res);  

	return ret;
}

int main(int argc, char **argv)
{
	int ret;
	printf("rdma_server: start\n");
	ret = run();
	printf("rdma_server: end %d\n", ret);
	return ret;
}

运行结果:
RDMA编程实践-SEND-RECEICVE原语应用,RDMA,RDMA,send-receive,c

RDMA编程实践-SEND-RECEICVE原语应用,RDMA,RDMA,send-receive,c

可以看到客户端发送一次消息之后便结束了,服务端却一直等待连接,直到按下ctrl+c。

version3-客户端循环发送,服务端循环等待,一次连接

和上述版本2不同的时候,这里client和server只连接一次,然后可以多次发送消息。直到client发送的消息为disconnect

// client3.c
/*
 * Copyright (c) 2010 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 <netdb.h>
#include <errno.h>
#include <getopt.h>
#include <rdma/rdma_cma.h>
#include <rdma/rdma_verbs.h>

#define N 100
#define MAX_CAP 32

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[MAX_CAP];
static uint8_t recv_msg[MAX_CAP];

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 = 5;
    attr.cap.max_send_sge = attr.cap.max_recv_sge = 1;
    attr.cap.max_inline_data = MAX_CAP;
    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 >= MAX_CAP)
        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, MAX_CAP);
    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, MAX_CAP);
        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;
    // }

    // printf("123\n");

    ret = rdma_connect(id, NULL);
    if (ret) {
        perror("rdma_connect");
        goto out_dereg_send;
    }

    while(1)
    {
        // sleep(5);
        memset(recv_msg, 0 , sizeof recv_msg);
        memset(send_msg, 0 , sizeof send_msg);
        printf("input send message: ");
        scanf("%s", send_msg);
        getchar();
        
        ret = rdma_post_recv(id, NULL, recv_msg, MAX_CAP, mr);
        if (ret) {
            perror("rdma_post_recv");
            goto out_dereg_send;
        }

        ret = rdma_post_send(id, NULL, send_msg, MAX_CAP, 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;
        
        if(strcmp((char*)send_msg,"disconnect") == 0)
        {
            printf("disconnect\n");
            goto out_disconnect;
        }
        else
        {
            printf("%s\n", 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;
    //memcpy(send_msg, argv[1], 50);
	// while ((op = getopt(argc, argv, "s:p:")) != -1) {
	// 	switch (op) {
	// 	case 's':
	// 		server = optarg;
	// 		break;
	// 	case 'p':
	// 		port = optarg;
	// 		break;
	// 	default:
	// 		printf("usage: %s\n", argv[0]);
	// 		printf("\t[-s server_address]\n");
	// 		printf("\t[-p port_number]\n");
	// 		exit(1);
	// 	}
	// }

	printf("rdma_client: start\n");
	ret = run();
	printf("rdma_client: end %d\n", ret);
    
	return ret;
}
// server3.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>


#define N 100
#define MAX_CAP 32

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[MAX_CAP];
static uint8_t recv_msg[MAX_CAP];

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;
	int ret;

connect:
	memset(&hints, 0, sizeof hints);
	hints.ai_flags = RAI_PASSIVE;
	hints.ai_port_space = RDMA_PS_TCP;
	ret = rdma_getaddrinfo(server, port, &hints, &res);
	if (ret) {
		printf("rdma_getaddrinfo: %s\n", gai_strerror(ret));
		return ret;
	}

	memset(&init_attr, 0, sizeof init_attr);
	init_attr.cap.max_send_wr = init_attr.cap.max_recv_wr = N;
	init_attr.cap.max_send_sge = init_attr.cap.max_recv_sge = 1;
	init_attr.cap.max_inline_data = MAX_CAP;
	init_attr.sq_sig_all = 1;

	ret = rdma_create_ep(&listen_id, res, NULL, &init_attr);
	if (ret) {
		perror("rdma_create_ep");
		goto out_free_addrinfo;
	}

	ret = rdma_listen(listen_id, 0);
	if (ret) {
		perror("rdma_listen");
		goto out_destroy_listen_ep;
	}

    ret = rdma_get_request(listen_id, &id);
    if (ret) {
        perror("rdma_get_request");
        goto out_destroy_listen_ep;
    }

    memset(&qp_attr, 0, sizeof qp_attr);
    memset(&init_attr, 0, sizeof init_attr);
    ret = ibv_query_qp(id->qp, &qp_attr, IBV_QP_CAP,
            &init_attr);
    if (ret) {
        perror("ibv_query_qp");
        goto out_destroy_accept_ep;
    }
    if (init_attr.cap.max_inline_data >= MAX_CAP)
        send_flags = IBV_SEND_INLINE;
    else
        printf("rdma_server: device doesn't support IBV_SEND_INLINE, "
            "using sge sends\n");
    mr = rdma_reg_msgs(id, recv_msg, N);
    if (!mr) {
        ret = -1;
        perror("rdma_reg_msgs for recv_msg");
        goto out_destroy_accept_ep;
    }
    if ((send_flags & IBV_SEND_INLINE) == 0) {
        send_mr = rdma_reg_msgs(id, send_msg, MAX_CAP);
        if (!send_mr) {
            ret = -1;
            perror("rdma_reg_msgs for send_msg");
            goto out_dereg_recv;
        }
    }   
    

    ret = rdma_accept(id, NULL);
    if (ret) {
        perror("rdma_accept");
        goto out_dereg_send;
    }

    while (1) {
        memset(recv_msg, 0 , sizeof recv_msg);
        memset(send_msg, 0 , sizeof send_msg);
        ret = rdma_post_recv(id, NULL, recv_msg, MAX_CAP, mr);
        if (ret) {
            perror("rdma_post_recv");
            goto out_dereg_send;
        }
        while ((ret = rdma_get_recv_comp(id, &wc)) == 0);
        if (ret < 0) {
            perror("rdma_get_recv_comp");
            goto out_disconnect;
        }
        
        char *s = (char *)recv_msg;
        int total_length = strlen("server get ") + strlen(s); // 加1是为了存储字符串结束符'\0'
        char *recv_str = (char *)malloc(total_length);  // 分配足够的空间
        strcpy(recv_str, "server get ");
        strcat(recv_str, s);
        //printf("%s\n", recv_str);
        memcpy(send_msg, recv_str, strlen(recv_str));
        
        ret = rdma_post_send(id, NULL, send_msg, MAX_CAP, send_mr, send_flags);
        if (ret) {
            perror("rdma_post_send");
            goto out_disconnect;
        }

        if(strcmp((char*)recv_msg,"disconnect") == 0)
        {
            //printf("%s\n",recv_msg);
            printf("client disconnect\n");
            rdma_disconnect(id);
            if ((send_flags & IBV_SEND_INLINE) == 0)
                rdma_dereg_mr(send_mr);
            rdma_dereg_mr(mr);
            rdma_destroy_ep(id);
            rdma_destroy_ep(listen_id);
            rdma_freeaddrinfo(res);  
            //goto out_disconnect;
            goto connect;
            
        }
        else
        {
            printf("%s\n", recv_msg);
        }
        
        // while ((ret = rdma_get_send_comp(id, &wc)) == 0); // 确认对方已经收到 对方发送ack
        // printf("after send\n");
        // if (ret < 0)
        //     perror("rdma_get_send_comp");
        // else
		//     ret = 0;
         
    }

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_accept_ep:
    rdma_destroy_ep(id);
out_destroy_listen_ep:
    rdma_destroy_ep(listen_id);
out_free_addrinfo:
    rdma_freeaddrinfo(res);  

	return ret;
}

int main(int argc, char **argv)
{
	int op, ret;

	while ((op = getopt(argc, argv, "s:p:")) != -1) {
		switch (op) {
		case 's':
			server = optarg;
			break;
		case 'p':
			port = optarg;
			break;
		default:
			printf("usage: %s\n", argv[0]);
			printf("\t[-s server_address]\n");
			printf("\t[-p port_number]\n");
			exit(1);
		}
	}

	printf("rdma_server: start\n");
	ret = run();
	printf("rdma_server: end %d\n", ret);
	return ret;
}

运行结果:

RDMA编程实践-SEND-RECEICVE原语应用,RDMA,RDMA,send-receive,c
RDMA编程实践-SEND-RECEICVE原语应用,RDMA,RDMA,send-receive,c

总结:

本文实现了rdma中send-receive双边原语的三种需求版本,从单次发送到两者都能多次发送。理解其中的代码逻辑,想要发送消息之前对端得创建一个recv队列用来接收消息。发送完了有一个发送完成队列,接收完了也有一个接收完成队列。最后双方断开连接需要一起断开,不能某一方执行disconnect另一方不执行。本次实验有一个关键点:

while ((ret = rdma_get_send_comp(id, &wc)) == 0)

这一行代码是等待发送成功,发送成功之后,对方会给一个隐式信息表示我已经收到。这里耗费得时间比较长一点,在版本3中,如果不注释掉,在server端的receive队列还没有建立好,这就导致client发送了消息,server还没有收到,双方就陷入了死循环中。文章来源地址https://www.toymoban.com/news/detail-804907.html

到了这里,关于RDMA编程实践-SEND-RECEICVE原语应用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • TCP Socket性能优化秘籍:掌握read、recv、readv、write、send、sendv的最佳实践

    💡一个热爱分享高性能服务器后台开发知识的博主,目标是通过理论与代码实践的结合,让世界上看似难以掌握的技术变得易于理解与掌握。技能涵盖了多个领域,包括C/C++、Linux、Nginx、MySQL、Redis、fastdfs、kafka、Docker、TCP/IP、协程、DPDK等。 👉 🎖️ CSDN实力新星,社区专家

    2024年02月13日
    浏览(39)
  • 大数据技术原理与应用实验指南——HBase编程实践

    一、 实验目的 (1) 熟练使用HBase操作常用的Shell命令。 (2) 熟悉HBase操作常用的Java API。 二、 实验内容 (1) 安装HBase软件。 (2) 编程实现指定功能,并利用Hadoop提供的Shell命令完成相同的任务(实现增、删、改、查基本操作,统计表的行数,打印表的记录等操作)。

    2024年02月21日
    浏览(40)
  • 大数据技术原理与应用实验4——MapReduce初级编程实践

    链接: 大数据技术原理与应用实验1——熟悉常用的HDFS操作 链接: 大数据技术原理与应用实验2——熟悉常用的Hbase操作 链接: 大数据技术原理与应用实验3——NoSQL和关系数据库的操作比较 (1)通过实验掌握基本的MapReduce编程方法; (2)掌握用MapReduce解决一些常见的数据处理

    2024年02月06日
    浏览(55)
  • 大数据技术原理及应用课实验7 :Spark初级编程实践

    实验7  Spark初级编程实践 一、实验目的 1. 掌握使用Spark访问本地文件和HDFS文件的方法 2. 掌握Spark应用程序的编写、编译和运行方法 二、实验平台 1. 操作系统:Ubuntu18.04(或Ubuntu16.04); 2. Spark版本:2.4.0; 3. Hadoop版本:3.1.3。 三、实验步骤(每个步骤下均需有运行截图) 实

    2024年01月22日
    浏览(52)
  • 大数据技术原理及应用课实验5 :MapReduce初级编程实践

    目录 一、实验目的 二、实验平台 三、实验步骤(每个步骤下均需有运行截图) (一)编程实现文件合并和去重操作 (二)编写程序实现对输入文件的排序 (三)对给定的表格进行信息挖掘 四、实验总结 五、优化及改进(选做) 实验5  MapReduce初级编程实践 1. 通过实验掌

    2024年01月21日
    浏览(48)
  • FPGA - 7系列 FPGA内部结构之CLB -03- CLB相关原语以及应用

    本文节选UG474的第二章,进行整理翻译。CLB资源被FPGA综合工具自动有效地使用,不需要任何特殊的FPGA专用编码。一些HDL编码建议和技术可以帮助优化设计以获得最大效率。 这些指南是为有效使用7系列CLB的设计建议提供的快速核对表。7系列CLB的设计建议: 资源利用 使用通用

    2024年02月03日
    浏览(49)
  • 大数据技术原理与应用实验指南——HDFS JAVA API编程实践

    1. 实验目的 (1) 熟练使用HDFS操作常用的Shell命令。 (2) 熟悉HDFS操作常用的Java API。 2. 实验内容 (1) 编程实现指定功能,并利用Hadoop提供的Shell命令完成相同任务(文件的上传、下载、输出、删除、移动等操作)。 (2) 有余力编写一个Java程序,读取HDFS中的指定文件。

    2024年02月19日
    浏览(42)
  • 【Java基础教程】(四十七)网络编程篇:网络通讯概念,TCP、UDP协议,Socket与ServerSocket类使用实践与应用场景~

    了解多线程与网络编程的操作关系; 了解网络程序开发的主要模式; 了解 TCP 程序的基本实现; 在Java中,网络编程的核心意义是实现不同电脑主机之间的数据交互。Java采用了一种简化的概念,将这个过程进一步抽象为JVM(Java虚拟机)进程之间的通信。可以在同一台电脑上

    2024年02月15日
    浏览(67)
  • 优化 RDMA 代码的建议和技巧-rdma性能优化技巧-避坑指南

    DMA 代表直接内存访问。这意味着应用程序可以在 CPU 干预的情况下直接访问(读/写)主机内存。如果您在主机之间执行此操作,它将成为远程直接内存访问 (RDMA) 在阅读有关 RDMA 的内容时,您会注意到一些用于描述其优点的术语。 “零复制 Zero Copy”、“内核绕过 Kernel Bypas

    2024年02月03日
    浏览(55)
  • 【XINLIX 原语】XILINX 原语的使用之 IBUFDS 差分转单端、OBUFDS 单端转差分

    目录 IBUFGDS IBUFDS 介绍 IBUFDS 示意图 例化方式 OBUFDS OBUFDS 介绍 OBUFDS 示意图 例化方式   在 XILINX 中有许多原语,常见的差分转单端 IBUFDS、单端转差分 OBUFDS。 IBUFDS 即专用差分输入时钟缓冲器(Dedicated Differential Signaling Input Buffer with Selectable I/O Interface) IBUFDS :在实验工程中如

    2024年02月16日
    浏览(52)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包