xref: /freebsd/contrib/ofed/librdmacm/examples/rdma_xserver.c (revision 87181516ef48be852d5e5fee53c6e0dbfc62f21e)
1*d6b92ffaSHans Petter Selasky /*
2*d6b92ffaSHans Petter Selasky  * Copyright (c) 2005-2014 Intel Corporation.  All rights reserved.
3*d6b92ffaSHans Petter Selasky  *
4*d6b92ffaSHans Petter Selasky  * This software is available to you under the OpenIB.org BSD license
5*d6b92ffaSHans Petter Selasky  * below:
6*d6b92ffaSHans Petter Selasky  *
7*d6b92ffaSHans Petter Selasky  *     Redistribution and use in source and binary forms, with or
8*d6b92ffaSHans Petter Selasky  *     without modification, are permitted provided that the following
9*d6b92ffaSHans Petter Selasky  *     conditions are met:
10*d6b92ffaSHans Petter Selasky  *
11*d6b92ffaSHans Petter Selasky  *      - Redistributions of source code must retain the above
12*d6b92ffaSHans Petter Selasky  *        copyright notice, this list of conditions and the following
13*d6b92ffaSHans Petter Selasky  *        disclaimer.
14*d6b92ffaSHans Petter Selasky  *
15*d6b92ffaSHans Petter Selasky  *      - Redistributions in binary form must reproduce the above
16*d6b92ffaSHans Petter Selasky  *        copyright notice, this list of conditions and the following
17*d6b92ffaSHans Petter Selasky  *        disclaimer in the documentation and/or other materials
18*d6b92ffaSHans Petter Selasky  *        provided with the distribution.
19*d6b92ffaSHans Petter Selasky  *
20*d6b92ffaSHans Petter Selasky  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21*d6b92ffaSHans Petter Selasky  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22*d6b92ffaSHans Petter Selasky  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AWV
23*d6b92ffaSHans Petter Selasky  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24*d6b92ffaSHans Petter Selasky  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25*d6b92ffaSHans Petter Selasky  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26*d6b92ffaSHans Petter Selasky  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27*d6b92ffaSHans Petter Selasky  * SOFTWARE.
28*d6b92ffaSHans Petter Selasky  */
29*d6b92ffaSHans Petter Selasky 
30*d6b92ffaSHans Petter Selasky #include <endian.h>
31*d6b92ffaSHans Petter Selasky #include <stdio.h>
32*d6b92ffaSHans Petter Selasky #include <stdlib.h>
33*d6b92ffaSHans Petter Selasky #include <string.h>
34*d6b92ffaSHans Petter Selasky #include <errno.h>
35*d6b92ffaSHans Petter Selasky #include <getopt.h>
36*d6b92ffaSHans Petter Selasky #include <netdb.h>
37*d6b92ffaSHans Petter Selasky #include <ctype.h>
38*d6b92ffaSHans Petter Selasky #include <rdma/rdma_cma.h>
39*d6b92ffaSHans Petter Selasky #include <rdma/rdma_verbs.h>
40*d6b92ffaSHans Petter Selasky 
41*d6b92ffaSHans Petter Selasky static const char *port = "7471";
42*d6b92ffaSHans Petter Selasky 
43*d6b92ffaSHans Petter Selasky static struct rdma_cm_id *listen_id, *id;
44*d6b92ffaSHans Petter Selasky static struct ibv_mr *mr;
45*d6b92ffaSHans Petter Selasky static struct rdma_addrinfo hints;
46*d6b92ffaSHans Petter Selasky 
47*d6b92ffaSHans Petter Selasky static uint8_t recv_msg[16];
48*d6b92ffaSHans Petter Selasky static __be32 srqn;
49*d6b92ffaSHans Petter Selasky 
create_srq(void)50*d6b92ffaSHans Petter Selasky static int create_srq(void)
51*d6b92ffaSHans Petter Selasky {
52*d6b92ffaSHans Petter Selasky 	struct ibv_srq_init_attr attr;
53*d6b92ffaSHans Petter Selasky 	int ret;
54*d6b92ffaSHans Petter Selasky 	uint32_t tmp_srqn;
55*d6b92ffaSHans Petter Selasky 
56*d6b92ffaSHans Petter Selasky 	attr.attr.max_wr = 1;
57*d6b92ffaSHans Petter Selasky 	attr.attr.max_sge = 1;
58*d6b92ffaSHans Petter Selasky 	attr.attr.srq_limit = 0;
59*d6b92ffaSHans Petter Selasky 	attr.srq_context = id;
60*d6b92ffaSHans Petter Selasky 
61*d6b92ffaSHans Petter Selasky 	ret = rdma_create_srq(id, NULL, &attr);
62*d6b92ffaSHans Petter Selasky 	if (ret)
63*d6b92ffaSHans Petter Selasky 		perror("rdma_create_srq:");
64*d6b92ffaSHans Petter Selasky 
65*d6b92ffaSHans Petter Selasky 	if (id->srq) {
66*d6b92ffaSHans Petter Selasky 		ibv_get_srq_num(id->srq, &tmp_srqn);
67*d6b92ffaSHans Petter Selasky 		srqn = htobe32(tmp_srqn);
68*d6b92ffaSHans Petter Selasky 	}
69*d6b92ffaSHans Petter Selasky 	return ret;
70*d6b92ffaSHans Petter Selasky }
71*d6b92ffaSHans Petter Selasky 
test(void)72*d6b92ffaSHans Petter Selasky static int test(void)
73*d6b92ffaSHans Petter Selasky {
74*d6b92ffaSHans Petter Selasky 	struct rdma_addrinfo *res;
75*d6b92ffaSHans Petter Selasky 	struct ibv_qp_init_attr attr;
76*d6b92ffaSHans Petter Selasky 	struct rdma_conn_param param;
77*d6b92ffaSHans Petter Selasky 	struct ibv_wc wc;
78*d6b92ffaSHans Petter Selasky 	int ret;
79*d6b92ffaSHans Petter Selasky 
80*d6b92ffaSHans Petter Selasky 	ret = rdma_getaddrinfo(NULL, port, &hints, &res);
81*d6b92ffaSHans Petter Selasky 	if (ret) {
82*d6b92ffaSHans Petter Selasky 		printf("rdma_getaddrinfo: %s\n", gai_strerror(ret));
83*d6b92ffaSHans Petter Selasky 		return ret;
84*d6b92ffaSHans Petter Selasky 	}
85*d6b92ffaSHans Petter Selasky 
86*d6b92ffaSHans Petter Selasky 	memset(&attr, 0, sizeof attr);
87*d6b92ffaSHans Petter Selasky 	attr.cap.max_send_wr = attr.cap.max_recv_wr = 1;
88*d6b92ffaSHans Petter Selasky 	attr.cap.max_send_sge = attr.cap.max_recv_sge = 1;
89*d6b92ffaSHans Petter Selasky 	ret = rdma_create_ep(&listen_id, res, NULL, &attr);
90*d6b92ffaSHans Petter Selasky 	rdma_freeaddrinfo(res);
91*d6b92ffaSHans Petter Selasky 	if (ret) {
92*d6b92ffaSHans Petter Selasky 		perror("rdma_create_ep");
93*d6b92ffaSHans Petter Selasky 		return ret;
94*d6b92ffaSHans Petter Selasky 	}
95*d6b92ffaSHans Petter Selasky 
96*d6b92ffaSHans Petter Selasky 	ret = rdma_listen(listen_id, 0);
97*d6b92ffaSHans Petter Selasky 	if (ret) {
98*d6b92ffaSHans Petter Selasky 		perror("rdma_listen");
99*d6b92ffaSHans Petter Selasky 		return ret;
100*d6b92ffaSHans Petter Selasky 	}
101*d6b92ffaSHans Petter Selasky 
102*d6b92ffaSHans Petter Selasky 	ret = rdma_get_request(listen_id, &id);
103*d6b92ffaSHans Petter Selasky 	if (ret) {
104*d6b92ffaSHans Petter Selasky 		perror("rdma_get_request");
105*d6b92ffaSHans Petter Selasky 		return ret;
106*d6b92ffaSHans Petter Selasky 	}
107*d6b92ffaSHans Petter Selasky 
108*d6b92ffaSHans Petter Selasky 	if (hints.ai_qp_type == IBV_QPT_XRC_RECV) {
109*d6b92ffaSHans Petter Selasky 		ret = create_srq();
110*d6b92ffaSHans Petter Selasky 		if (ret)
111*d6b92ffaSHans Petter Selasky 			return ret;
112*d6b92ffaSHans Petter Selasky 	}
113*d6b92ffaSHans Petter Selasky 
114*d6b92ffaSHans Petter Selasky 	mr = rdma_reg_msgs(id, recv_msg, sizeof recv_msg);
115*d6b92ffaSHans Petter Selasky 	if (!mr) {
116*d6b92ffaSHans Petter Selasky 		perror("rdma_reg_msgs");
117*d6b92ffaSHans Petter Selasky 		return ret;
118*d6b92ffaSHans Petter Selasky 	}
119*d6b92ffaSHans Petter Selasky 
120*d6b92ffaSHans Petter Selasky 	ret = rdma_post_recv(id, NULL, recv_msg, sizeof recv_msg, mr);
121*d6b92ffaSHans Petter Selasky 	if (ret) {
122*d6b92ffaSHans Petter Selasky 		perror("rdma_post_recv");
123*d6b92ffaSHans Petter Selasky 		return ret;
124*d6b92ffaSHans Petter Selasky 	}
125*d6b92ffaSHans Petter Selasky 
126*d6b92ffaSHans Petter Selasky 	memset(&param, 0, sizeof param);
127*d6b92ffaSHans Petter Selasky 	param.private_data = &srqn;
128*d6b92ffaSHans Petter Selasky 	param.private_data_len = sizeof srqn;
129*d6b92ffaSHans Petter Selasky 	ret = rdma_accept(id, &param);
130*d6b92ffaSHans Petter Selasky 	if (ret) {
131*d6b92ffaSHans Petter Selasky 		perror("rdma_accept");
132*d6b92ffaSHans Petter Selasky 		return ret;
133*d6b92ffaSHans Petter Selasky 	}
134*d6b92ffaSHans Petter Selasky 
135*d6b92ffaSHans Petter Selasky 	ret = rdma_get_recv_comp(id, &wc);
136*d6b92ffaSHans Petter Selasky 	if (ret <= 0) {
137*d6b92ffaSHans Petter Selasky 		perror("rdma_get_recv_comp");
138*d6b92ffaSHans Petter Selasky 		return ret;
139*d6b92ffaSHans Petter Selasky 	}
140*d6b92ffaSHans Petter Selasky 
141*d6b92ffaSHans Petter Selasky 	rdma_disconnect(id);
142*d6b92ffaSHans Petter Selasky 	rdma_dereg_mr(mr);
143*d6b92ffaSHans Petter Selasky 	rdma_destroy_ep(id);
144*d6b92ffaSHans Petter Selasky 	rdma_destroy_ep(listen_id);
145*d6b92ffaSHans Petter Selasky 	return 0;
146*d6b92ffaSHans Petter Selasky }
147*d6b92ffaSHans Petter Selasky 
main(int argc,char ** argv)148*d6b92ffaSHans Petter Selasky int main(int argc, char **argv)
149*d6b92ffaSHans Petter Selasky {
150*d6b92ffaSHans Petter Selasky 	int op, ret;
151*d6b92ffaSHans Petter Selasky 
152*d6b92ffaSHans Petter Selasky 	hints.ai_flags = RAI_PASSIVE;
153*d6b92ffaSHans Petter Selasky 	hints.ai_port_space = RDMA_PS_TCP;
154*d6b92ffaSHans Petter Selasky 	hints.ai_qp_type = IBV_QPT_RC;
155*d6b92ffaSHans Petter Selasky 
156*d6b92ffaSHans Petter Selasky 	while ((op = getopt(argc, argv, "p:c:")) != -1) {
157*d6b92ffaSHans Petter Selasky 		switch (op) {
158*d6b92ffaSHans Petter Selasky 		case 'p':
159*d6b92ffaSHans Petter Selasky 			port = optarg;
160*d6b92ffaSHans Petter Selasky 			break;
161*d6b92ffaSHans Petter Selasky 		case 'c':
162*d6b92ffaSHans Petter Selasky 			switch (tolower(optarg[0])) {
163*d6b92ffaSHans Petter Selasky 			case 'r':
164*d6b92ffaSHans Petter Selasky 				break;
165*d6b92ffaSHans Petter Selasky 			case 'x':
166*d6b92ffaSHans Petter Selasky 				hints.ai_port_space = RDMA_PS_IB;
167*d6b92ffaSHans Petter Selasky 				hints.ai_qp_type = IBV_QPT_XRC_RECV;
168*d6b92ffaSHans Petter Selasky 				break;
169*d6b92ffaSHans Petter Selasky 			default:
170*d6b92ffaSHans Petter Selasky 				goto err;
171*d6b92ffaSHans Petter Selasky 			}
172*d6b92ffaSHans Petter Selasky 			break;
173*d6b92ffaSHans Petter Selasky 		default:
174*d6b92ffaSHans Petter Selasky 			goto err;
175*d6b92ffaSHans Petter Selasky 		}
176*d6b92ffaSHans Petter Selasky 	}
177*d6b92ffaSHans Petter Selasky 
178*d6b92ffaSHans Petter Selasky 	printf("%s: start\n", argv[0]);
179*d6b92ffaSHans Petter Selasky 	ret = test();
180*d6b92ffaSHans Petter Selasky 	printf("%s: end %d\n", argv[0], ret);
181*d6b92ffaSHans Petter Selasky 	return ret;
182*d6b92ffaSHans Petter Selasky 
183*d6b92ffaSHans Petter Selasky err:
184*d6b92ffaSHans Petter Selasky 	printf("usage: %s\n", argv[0]);
185*d6b92ffaSHans Petter Selasky 	printf("\t[-p port_number]\n");
186*d6b92ffaSHans Petter Selasky 	printf("\t[-c communication type]\n");
187*d6b92ffaSHans Petter Selasky 	printf("\t    r - RC: reliable-connected (default)\n");
188*d6b92ffaSHans Petter Selasky 	printf("\t    x - XRC: extended-reliable-connected\n");
189*d6b92ffaSHans Petter Selasky 	exit(1);
190*d6b92ffaSHans Petter Selasky }
191