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

news2024/11/16 9:25:29

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
运行结果:
在这里插入图片描述
在这里插入图片描述
可以看到 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;
}

运行结果:
在这里插入图片描述

在这里插入图片描述

可以看到客户端发送一次消息之后便结束了,服务端却一直等待连接,直到按下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-receive双边原语的三种需求版本,从单次发送到两者都能多次发送。理解其中的代码逻辑,想要发送消息之前对端得创建一个recv队列用来接收消息。发送完了有一个发送完成队列,接收完了也有一个接收完成队列。最后双方断开连接需要一起断开,不能某一方执行disconnect另一方不执行。本次实验有一个关键点:

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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1387501.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C++|29.纯虚函数/接口(待完成)

纯虚函数是一种特殊的虚函数。 普通的虚函数允许子类的同名函数对其进行重写&#xff0c;同时普通的虚函数本身是可以单独进行使用的。 而纯虚函数是一个空壳&#xff0c;强制要求所派生的类在继承的过程中必要将该虚函数进行实现。 如上图&#xff0c;纯虚函数只需要在vir…

程序员的健康手册

大家好&#xff0c;我是 javapub。 马上迎来 2024 农历新年&#xff0c;这个是 COVID-19 后的第一个春节。用女朋友的话来说&#xff0c;这几年像在梦里一样&#xff0c;可能生活了几十年的人都想象不到会发生这样的事。不过不论世界怎么变&#xff0c;我们都要过生活、过好当…

SpringBoot教程(二) | 创建SpringBoot项目

SpringBoot教程(二) | 创建SpringBoot项目 接下来我们要学习一下如何创建一个spring boot项目。 我们采用的环境信息 JDK1.8 Maven 3.6 Idea 2021.1 所谓spring Boot项目&#xff0c;其实本质上就是一个maven/gradle项目&#xff0c;这是里面通过引入springBoot专门设置好…

【AI】人工智能和水下机器视觉

目录 一、初识水下机器视觉 ——不同点 ——难点 二、AI如何助力水下机器视觉 三、应用场景 四、关键技术 水下机器视觉&#xff0c;非常复杂&#xff0c;今天来简单讨论一下。因为目标识别更难。 水下机器视觉是机器视觉技术在水下环境中的应用&#xff0c;它与普通机器…

k8s的存储卷(数据卷)

1、存储卷&#xff1a;容器内的目录和宿主机的目录进行挂载 2、容器在系统上的生命周期是短暂的&#xff0c;delete&#xff0c;k8s用控制器创建的pod&#xff0c;delete相当于重启&#xff0c;容器的状态也会恢复到初始状态&#xff0c;一旦回到初始状态&#xff0c;所有的后…

transbigdata笔记:数据栅格化

1 area_to_grid 在边界或形状中生成矩形栅格 1.1 主要使用方法 transbigdata.area_to_grid(location, accuracy500, methodrect, paramsauto) 1.2 主要参数 location (bounds(List) or shape(GeoDataFrame) 生成栅格的位置。 如果边界为 [lon1&#xff0c; lat1&#xff0…

宁夏银行关键系统基于OceanBase的创新实践

宁夏银行成立于 1998 年&#xff0c;是宁夏第一家“宁”字号地方商业银行&#xff0c;西部地区第一家以省级行政区命名的地方商业银行。2016 年&#xff0c;被中国人民银行评为宁夏地区系统性重要银行。目前&#xff0c;全行设分支机构 97 家&#xff0c;其中总行营业部 1 家&a…

50天精通Golang(第18天)

web开发介绍、iris框架安装、HTTP请求和返回、Iris路由处理 一 Web项目开发介绍及实战项目介绍 1.1 引言 本系列课程我们将学些Golang语言中的Web开发框架Iris的相关知识和用法。通过本系列视频课程&#xff0c;大家能够从零到一经历一个完整项目的开发&#xff0c;并在课程…

《新课程教学》(电子版)是正规期刊吗?能评职称吗?

《新课程教学》&#xff08;电子版&#xff09;主要出版内容为学科教学理论、学科教学实践经验和成果&#xff0c;主要读者对象为中小学教师&#xff0c;期刊设卷首语、名家讲堂、课程与教学、教学实践、考试评价、教育信息化、教学琐谈、教育管理、教师心语、一线课堂、重温经…

高通sm7250与765G芯片是什么关系?(一百八十一)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 优质专栏&#xff1a;多媒…

idea写sql语句快捷键提醒,mapper注解开发,mybatis

第一步&#xff1a;注入SQL语言 1.显示上下文操作&#xff08;没有这个选项的话就选中sql然后直接alt回车快捷键&#xff09;2.注入语言或引用 3.mysql 第二步&#xff1a;配置MySQL数据库连接 1.首先点击侧边的数据库&#xff0c;再点击上面的加号 2.点击数据源&#xff…

不可不知的网络信息安全隐患

随着互联网的普及和信息技术的飞速发展&#xff0c;网络安全问题日益凸显。网络攻击、网络诈骗、网络病毒等问题时刻威胁着人们的隐私和财产安全。 1.黑客攻击黑客攻击是网络信息安全面临的最大威胁之一。 一是及时更新服务器系统安全漏洞补丁&#xff1b;二是要加强服务器安全…

显示CPU架构的有关信息 lscpu

文章目录 显示CPU架构的有关信息 lscpu默认实例更多信息 显示CPU架构的有关信息 lscpu Linux的CPU设备查看器。lscpu命令用来显示cpu的相关信息。 lscpu从sysfs和/proc/cpuinfo收集cpu体系结构信息&#xff0c;命令的输出比较易读 。 命令输出的信息包含cpu数量&#xff0c;线…

高级分布式系统-第15讲 分布式机器学习--联邦学习

高级分布式系统汇总&#xff1a;高级分布式系统目录汇总-CSDN博客 联邦学习 两种常见的架构&#xff1a;客户-服务器架构和对等网络架构 联邦学习在传统的分布式机器学习基础上的变化。 传统的分布式机器学习&#xff1a;在数据中心或计算集群中使用并行训练&#xff0c;因为…

排序——计数排序

文章目录 概念思路绝对映射&#xff1a;相对映射 代码实现特性结果演示 概念 计数排序是一个非基于比较的排序算法&#xff0c;该算法于1954年由 Harold H. Seward 提出。它的优势在于在对一定范围内的整数排序时&#xff0c;它的复杂度为Ο(nk)&#xff08;其中k是整数的范围…

QT-day6

作业1&#xff1a;数据库增删查改 #include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this);if (!db.contains("stu.db")){db QSqlDatabase::addDatabase(&q…

能耗监测系统在某大型公共建筑的研究与应用——安科瑞赵嘉敏

摘要 大型公共建筑总面积不足城镇建筑总面积的4%&#xff0c;但总能耗却占全国城镇总耗电量的22%&#xff0c;大型公共建筑单位面积年耗电量达到70&#xff5e;300KWh&#xff0c;为普通居民住宅的10&#xff5e;20倍。公共建筑是节能大户&#xff0c;做好公共建筑节能工作&am…

力扣 | 49. 字母异位词分组

这里使用HashMap Java package _49;import java.util.*;public class Problem_49_GroupAnagrams {public List<List<String>> groupAngrams(String [] strs){Map<String,List<String>> map new HashMap<>(); // int [] arr new int[]{}…

国科大软件安全原理期末复习笔记

1 软件安全总论 1.软件的三大特性&#xff1a;复杂性、互连性、可扩展性&#xff1b; 2.基本概念&#xff1a;缺陷、漏洞、风险 缺陷&#xff08;bug&#xff09;&#xff1a;软件在设计和实现上的错误&#xff1b;漏洞&#xff08;vulnerability&#xff09;&#xff1a;漏洞…

车厢重组#洛谷

题目描述 在一个旧式的火车站旁边有一座桥&#xff0c;其桥面可以绕河中心的桥墩水平旋转。一个车站的职工发现桥的长度最多能容纳两节车厢&#xff0c;如果将桥旋转 180 180 180 度&#xff0c;则可以把相邻两节车厢的位置交换&#xff0c;用这种方法可以重新排列车厢的顺序…