2 * Copyright (c) 2005-2009 Intel Corporation. All rights reserved.
4 * This software is available to you under the OpenIB.org BSD license
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
11 * - Redistributions of source code must retain the above
12 * copyright notice, this list of conditions and the following
15 * - Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AWV
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 #include <rdma/rdma_cma.h>
37 #include <rdma/rdma_verbs.h>
39 static const char *port = "7471";
41 static struct rdma_cm_id *listen_id, *id;
42 static struct ibv_mr *mr, *send_mr;
43 static int send_flags;
44 static uint8_t send_msg[16];
45 static uint8_t recv_msg[16];
49 struct rdma_addrinfo hints, *res;
50 struct ibv_qp_init_attr init_attr;
51 struct ibv_qp_attr qp_attr;
55 memset(&hints, 0, sizeof hints);
56 hints.ai_flags = RAI_PASSIVE;
57 hints.ai_port_space = RDMA_PS_TCP;
58 ret = rdma_getaddrinfo(NULL, port, &hints, &res);
60 printf("rdma_getaddrinfo: %s\n", gai_strerror(ret));
64 memset(&init_attr, 0, sizeof init_attr);
65 init_attr.cap.max_send_wr = init_attr.cap.max_recv_wr = 1;
66 init_attr.cap.max_send_sge = init_attr.cap.max_recv_sge = 1;
67 init_attr.cap.max_inline_data = 16;
68 init_attr.sq_sig_all = 1;
69 ret = rdma_create_ep(&listen_id, res, NULL, &init_attr);
71 perror("rdma_create_ep");
72 goto out_free_addrinfo;
75 ret = rdma_listen(listen_id, 0);
77 perror("rdma_listen");
78 goto out_destroy_listen_ep;
81 ret = rdma_get_request(listen_id, &id);
83 perror("rdma_get_request");
84 goto out_destroy_listen_ep;
87 memset(&qp_attr, 0, sizeof qp_attr);
88 memset(&init_attr, 0, sizeof init_attr);
89 ret = ibv_query_qp(id->qp, &qp_attr, IBV_QP_CAP,
92 perror("ibv_query_qp");
93 goto out_destroy_accept_ep;
95 if (init_attr.cap.max_inline_data >= 16)
96 send_flags = IBV_SEND_INLINE;
98 printf("rdma_server: device doesn't support IBV_SEND_INLINE, "
101 mr = rdma_reg_msgs(id, recv_msg, 16);
104 perror("rdma_reg_msgs for recv_msg");
105 goto out_destroy_accept_ep;
107 if ((send_flags & IBV_SEND_INLINE) == 0) {
108 send_mr = rdma_reg_msgs(id, send_msg, 16);
111 perror("rdma_reg_msgs for send_msg");
116 ret = rdma_post_recv(id, NULL, recv_msg, 16, mr);
118 perror("rdma_post_recv");
122 ret = rdma_accept(id, NULL);
124 perror("rdma_accept");
128 while ((ret = rdma_get_recv_comp(id, &wc)) == 0);
130 perror("rdma_get_recv_comp");
134 ret = rdma_post_send(id, NULL, send_msg, 16, send_mr, send_flags);
136 perror("rdma_post_send");
140 while ((ret = rdma_get_send_comp(id, &wc)) == 0);
142 perror("rdma_get_send_comp");
149 if ((send_flags & IBV_SEND_INLINE) == 0)
150 rdma_dereg_mr(send_mr);
153 out_destroy_accept_ep:
155 out_destroy_listen_ep:
156 rdma_destroy_ep(listen_id);
158 rdma_freeaddrinfo(res);
162 int main(int argc, char **argv)
166 while ((op = getopt(argc, argv, "p:")) != -1) {
172 printf("usage: %s\n", argv[0]);
173 printf("\t[-p port_number]\n");
178 printf("rdma_server: start\n");
180 printf("rdma_server: end %d\n", ret);