xref: /freebsd/contrib/ofed/librdmacm/examples/rdma_xclient.c (revision 87181516ef48be852d5e5fee53c6e0dbfc62f21e)
1*d6b92ffaSHans Petter Selasky /*
2*d6b92ffaSHans Petter Selasky  * Copyright (c) 2010-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 <netdb.h>
35*d6b92ffaSHans Petter Selasky #include <errno.h>
36*d6b92ffaSHans Petter Selasky #include <getopt.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 *server = "127.0.0.1";
42*d6b92ffaSHans Petter Selasky static char port[6] = "7471";
43*d6b92ffaSHans Petter Selasky 
44*d6b92ffaSHans Petter Selasky static struct rdma_cm_id *id;
45*d6b92ffaSHans Petter Selasky static struct ibv_mr *mr;
46*d6b92ffaSHans Petter Selasky static struct rdma_addrinfo hints;
47*d6b92ffaSHans Petter Selasky 
48*d6b92ffaSHans Petter Selasky static uint8_t send_msg[16];
49*d6b92ffaSHans Petter Selasky static uint32_t srqn;
50*d6b92ffaSHans Petter Selasky 
post_send(void)51*d6b92ffaSHans Petter Selasky static int post_send(void)
52*d6b92ffaSHans Petter Selasky {
53*d6b92ffaSHans Petter Selasky 	struct ibv_send_wr wr, *bad;
54*d6b92ffaSHans Petter Selasky 	struct ibv_sge sge;
55*d6b92ffaSHans Petter Selasky 	int ret;
56*d6b92ffaSHans Petter Selasky 
57*d6b92ffaSHans Petter Selasky 	sge.addr = (uint64_t) (uintptr_t) send_msg;
58*d6b92ffaSHans Petter Selasky 	sge.length = (uint32_t) sizeof send_msg;
59*d6b92ffaSHans Petter Selasky 	sge.lkey = 0;
60*d6b92ffaSHans Petter Selasky 	wr.wr_id = (uintptr_t) NULL;
61*d6b92ffaSHans Petter Selasky 	wr.next = NULL;
62*d6b92ffaSHans Petter Selasky 	wr.sg_list = &sge;
63*d6b92ffaSHans Petter Selasky 	wr.num_sge = 1;
64*d6b92ffaSHans Petter Selasky 	wr.opcode = IBV_WR_SEND;
65*d6b92ffaSHans Petter Selasky 	wr.send_flags = IBV_SEND_INLINE;
66*d6b92ffaSHans Petter Selasky 	if (hints.ai_qp_type == IBV_QPT_XRC_SEND)
67*d6b92ffaSHans Petter Selasky 		wr.qp_type.xrc.remote_srqn = srqn;
68*d6b92ffaSHans Petter Selasky 
69*d6b92ffaSHans Petter Selasky 	ret = ibv_post_send(id->qp, &wr, &bad);
70*d6b92ffaSHans Petter Selasky 	if (ret)
71*d6b92ffaSHans Petter Selasky 		perror("rdma_post_send");
72*d6b92ffaSHans Petter Selasky 
73*d6b92ffaSHans Petter Selasky 	return ret;
74*d6b92ffaSHans Petter Selasky }
75*d6b92ffaSHans Petter Selasky 
test(void)76*d6b92ffaSHans Petter Selasky static int test(void)
77*d6b92ffaSHans Petter Selasky {
78*d6b92ffaSHans Petter Selasky 	struct rdma_addrinfo *res;
79*d6b92ffaSHans Petter Selasky 	struct ibv_qp_init_attr attr;
80*d6b92ffaSHans Petter Selasky 	struct ibv_wc wc;
81*d6b92ffaSHans Petter Selasky 	int ret;
82*d6b92ffaSHans Petter Selasky 
83*d6b92ffaSHans Petter Selasky 	ret = rdma_getaddrinfo(server, port, &hints, &res);
84*d6b92ffaSHans Petter Selasky 	if (ret) {
85*d6b92ffaSHans Petter Selasky 		printf("rdma_getaddrinfo: %s\n", gai_strerror(ret));
86*d6b92ffaSHans Petter Selasky 		return ret;
87*d6b92ffaSHans Petter Selasky 	}
88*d6b92ffaSHans Petter Selasky 
89*d6b92ffaSHans Petter Selasky 	memset(&attr, 0, sizeof attr);
90*d6b92ffaSHans Petter Selasky 	attr.cap.max_send_wr = attr.cap.max_recv_wr = 1;
91*d6b92ffaSHans Petter Selasky 	attr.cap.max_send_sge = attr.cap.max_recv_sge = 1;
92*d6b92ffaSHans Petter Selasky 	attr.sq_sig_all = 1;
93*d6b92ffaSHans Petter Selasky 	ret = rdma_create_ep(&id, res, NULL, &attr);
94*d6b92ffaSHans Petter Selasky 	rdma_freeaddrinfo(res);
95*d6b92ffaSHans Petter Selasky 	if (ret) {
96*d6b92ffaSHans Petter Selasky 		perror("rdma_create_ep");
97*d6b92ffaSHans Petter Selasky 		return ret;
98*d6b92ffaSHans Petter Selasky 	}
99*d6b92ffaSHans Petter Selasky 
100*d6b92ffaSHans Petter Selasky 	mr = rdma_reg_msgs(id, send_msg, sizeof send_msg);
101*d6b92ffaSHans Petter Selasky 	if (!mr) {
102*d6b92ffaSHans Petter Selasky 		perror("rdma_reg_msgs");
103*d6b92ffaSHans Petter Selasky 		return ret;
104*d6b92ffaSHans Petter Selasky 	}
105*d6b92ffaSHans Petter Selasky 
106*d6b92ffaSHans Petter Selasky 	ret = rdma_connect(id, NULL);
107*d6b92ffaSHans Petter Selasky 	if (ret) {
108*d6b92ffaSHans Petter Selasky 		perror("rdma_connect");
109*d6b92ffaSHans Petter Selasky 		return ret;
110*d6b92ffaSHans Petter Selasky 	}
111*d6b92ffaSHans Petter Selasky 
112*d6b92ffaSHans Petter Selasky 	if (hints.ai_qp_type == IBV_QPT_XRC_SEND)
113*d6b92ffaSHans Petter Selasky 		srqn = be32toh(*(__be32 *) id->event->param.conn.private_data);
114*d6b92ffaSHans Petter Selasky 
115*d6b92ffaSHans Petter Selasky 	ret = post_send();
116*d6b92ffaSHans Petter Selasky 	if (ret) {
117*d6b92ffaSHans Petter Selasky 		perror("post_send");
118*d6b92ffaSHans Petter Selasky 		return ret;
119*d6b92ffaSHans Petter Selasky 	}
120*d6b92ffaSHans Petter Selasky 
121*d6b92ffaSHans Petter Selasky 	ret = rdma_get_send_comp(id, &wc);
122*d6b92ffaSHans Petter Selasky 	if (ret <= 0) {
123*d6b92ffaSHans Petter Selasky 		perror("rdma_get_recv_comp");
124*d6b92ffaSHans Petter Selasky 		return ret;
125*d6b92ffaSHans Petter Selasky 	}
126*d6b92ffaSHans Petter Selasky 
127*d6b92ffaSHans Petter Selasky 	rdma_disconnect(id);
128*d6b92ffaSHans Petter Selasky 	rdma_dereg_mr(mr);
129*d6b92ffaSHans Petter Selasky 	rdma_destroy_ep(id);
130*d6b92ffaSHans Petter Selasky 	return 0;
131*d6b92ffaSHans Petter Selasky }
132*d6b92ffaSHans Petter Selasky 
main(int argc,char ** argv)133*d6b92ffaSHans Petter Selasky int main(int argc, char **argv)
134*d6b92ffaSHans Petter Selasky {
135*d6b92ffaSHans Petter Selasky 	int op, ret;
136*d6b92ffaSHans Petter Selasky 
137*d6b92ffaSHans Petter Selasky 	hints.ai_port_space = RDMA_PS_TCP;
138*d6b92ffaSHans Petter Selasky 	hints.ai_qp_type = IBV_QPT_RC;
139*d6b92ffaSHans Petter Selasky 
140*d6b92ffaSHans Petter Selasky 	while ((op = getopt(argc, argv, "s:p:c:")) != -1) {
141*d6b92ffaSHans Petter Selasky 		switch (op) {
142*d6b92ffaSHans Petter Selasky 		case 's':
143*d6b92ffaSHans Petter Selasky 			server = optarg;
144*d6b92ffaSHans Petter Selasky 			break;
145*d6b92ffaSHans Petter Selasky 		case 'p':
146*d6b92ffaSHans Petter Selasky 			strncpy(port, optarg, sizeof port - 1);
147*d6b92ffaSHans Petter Selasky 			break;
148*d6b92ffaSHans Petter Selasky 		case 'c':
149*d6b92ffaSHans Petter Selasky 			switch (tolower(optarg[0])) {
150*d6b92ffaSHans Petter Selasky 			case 'r':
151*d6b92ffaSHans Petter Selasky 				break;
152*d6b92ffaSHans Petter Selasky 			case 'x':
153*d6b92ffaSHans Petter Selasky 				hints.ai_port_space = RDMA_PS_IB;
154*d6b92ffaSHans Petter Selasky 				hints.ai_qp_type = IBV_QPT_XRC_SEND;
155*d6b92ffaSHans Petter Selasky 				break;
156*d6b92ffaSHans Petter Selasky 			default:
157*d6b92ffaSHans Petter Selasky 				goto err;
158*d6b92ffaSHans Petter Selasky 			}
159*d6b92ffaSHans Petter Selasky 			break;
160*d6b92ffaSHans Petter Selasky 		default:
161*d6b92ffaSHans Petter Selasky 			goto err;
162*d6b92ffaSHans Petter Selasky 		}
163*d6b92ffaSHans Petter Selasky 	}
164*d6b92ffaSHans Petter Selasky 
165*d6b92ffaSHans Petter Selasky 	printf("%s: start\n", argv[0]);
166*d6b92ffaSHans Petter Selasky 	ret = test();
167*d6b92ffaSHans Petter Selasky 	printf("%s: end %d\n", argv[0], ret);
168*d6b92ffaSHans Petter Selasky 	return ret;
169*d6b92ffaSHans Petter Selasky 
170*d6b92ffaSHans Petter Selasky err:
171*d6b92ffaSHans Petter Selasky 	printf("usage: %s\n", argv[0]);
172*d6b92ffaSHans Petter Selasky 	printf("\t[-s server]\n");
173*d6b92ffaSHans Petter Selasky 	printf("\t[-p port_number]\n");
174*d6b92ffaSHans Petter Selasky 	printf("\t[-c communication type]\n");
175*d6b92ffaSHans Petter Selasky 	printf("\t    r - RC: reliable-connected (default)\n");
176*d6b92ffaSHans Petter Selasky 	printf("\t    x - XRC: extended-reliable-connected\n");
177*d6b92ffaSHans Petter Selasky 	exit(1);
178*d6b92ffaSHans Petter Selasky }
179