2 * Copyright (c) 2005-2006 Intel Corporation. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39 #include <sys/types.h>
40 #include <netinet/in.h>
41 #include <sys/socket.h>
46 #include <rdma/rdma_cma.h>
50 struct rdma_cm_id *cma_id;
62 struct rdma_event_channel *channel;
63 struct cmatest_node *nodes;
67 struct sockaddr_in dst_in;
68 struct sockaddr *dst_addr;
69 struct sockaddr_in src_in;
70 struct sockaddr *src_addr;
73 static struct cmatest test;
74 static int connections = 1;
75 static int message_size = 100;
76 static int message_count = 10;
77 static uint16_t port = 7174;
78 static uint8_t set_tos = 0;
80 static char *dst_addr;
81 static char *src_addr;
82 static enum rdma_port_space port_space = RDMA_PS_UDP;
84 static int create_message(struct cmatest_node *node)
92 node->mem = malloc(message_size + sizeof(struct ibv_grh));
94 printf("failed message allocation\n");
97 node->mr = ibv_reg_mr(node->pd, node->mem,
98 message_size + sizeof(struct ibv_grh),
99 IBV_ACCESS_LOCAL_WRITE);
101 printf("failed to reg MR\n");
110 static int verify_test_params(struct cmatest_node *node)
112 struct ibv_port_attr port_attr;
115 ret = ibv_query_port(node->cma_id->verbs, node->cma_id->port_num,
120 if (message_count && message_size > (1 << (port_attr.active_mtu + 7))) {
121 printf("udaddy: message_size %d is larger than active mtu %d\n",
122 message_size, 1 << (port_attr.active_mtu + 7));
129 static int init_node(struct cmatest_node *node)
131 struct ibv_qp_init_attr init_qp_attr;
134 node->pd = ibv_alloc_pd(node->cma_id->verbs);
137 printf("udaddy: unable to allocate PD\n");
141 cqe = message_count ? message_count * 2 : 2;
142 node->cq = ibv_create_cq(node->cma_id->verbs, cqe, node, 0, 0);
145 printf("udaddy: unable to create CQ\n");
149 memset(&init_qp_attr, 0, sizeof init_qp_attr);
150 init_qp_attr.cap.max_send_wr = message_count ? message_count : 1;
151 init_qp_attr.cap.max_recv_wr = message_count ? message_count : 1;
152 init_qp_attr.cap.max_send_sge = 1;
153 init_qp_attr.cap.max_recv_sge = 1;
154 init_qp_attr.qp_context = node;
155 init_qp_attr.sq_sig_all = 0;
156 init_qp_attr.qp_type = IBV_QPT_UD;
157 init_qp_attr.send_cq = node->cq;
158 init_qp_attr.recv_cq = node->cq;
159 ret = rdma_create_qp(node->cma_id, node->pd, &init_qp_attr);
161 perror("udaddy: unable to create QP");
165 ret = create_message(node);
167 printf("udaddy: failed to create messages: %d\n", ret);
174 static int post_recvs(struct cmatest_node *node)
176 struct ibv_recv_wr recv_wr, *recv_failure;
184 recv_wr.sg_list = &sge;
186 recv_wr.wr_id = (uintptr_t) node;
188 sge.length = message_size + sizeof(struct ibv_grh);
189 sge.lkey = node->mr->lkey;
190 sge.addr = (uintptr_t) node->mem;
192 for (i = 0; i < message_count && !ret; i++ ) {
193 ret = ibv_post_recv(node->cma_id->qp, &recv_wr, &recv_failure);
195 printf("failed to post receives: %d\n", ret);
202 static int post_sends(struct cmatest_node *node, int signal_flag)
204 struct ibv_send_wr send_wr, *bad_send_wr;
208 if (!node->connected || !message_count)
212 send_wr.sg_list = &sge;
214 send_wr.opcode = IBV_WR_SEND_WITH_IMM;
215 send_wr.send_flags = signal_flag;
216 send_wr.wr_id = (unsigned long)node;
217 send_wr.imm_data = htonl(node->cma_id->qp->qp_num);
219 send_wr.wr.ud.ah = node->ah;
220 send_wr.wr.ud.remote_qpn = node->remote_qpn;
221 send_wr.wr.ud.remote_qkey = node->remote_qkey;
223 sge.length = message_size;
224 sge.lkey = node->mr->lkey;
225 sge.addr = (uintptr_t) node->mem;
227 for (i = 0; i < message_count && !ret; i++) {
228 ret = ibv_post_send(node->cma_id->qp, &send_wr, &bad_send_wr);
230 printf("failed to post sends: %d\n", ret);
235 static void connect_error(void)
237 test.connects_left--;
240 static int addr_handler(struct cmatest_node *node)
245 ret = rdma_set_option(node->cma_id, RDMA_OPTION_ID,
246 RDMA_OPTION_ID_TOS, &tos, sizeof tos);
248 perror("udaddy: set TOS option failed");
251 ret = rdma_resolve_route(node->cma_id, 2000);
253 perror("udaddy: resolve route failed");
259 static int route_handler(struct cmatest_node *node)
261 struct rdma_conn_param conn_param;
264 ret = verify_test_params(node);
268 ret = init_node(node);
272 ret = post_recvs(node);
276 memset(&conn_param, 0, sizeof conn_param);
277 ret = rdma_connect(node->cma_id, &conn_param);
279 perror("udaddy: failure connecting");
288 static int connect_handler(struct rdma_cm_id *cma_id)
290 struct cmatest_node *node;
291 struct rdma_conn_param conn_param;
294 if (test.conn_index == connections) {
298 node = &test.nodes[test.conn_index++];
300 node->cma_id = cma_id;
301 cma_id->context = node;
303 ret = verify_test_params(node);
307 ret = init_node(node);
311 ret = post_recvs(node);
315 memset(&conn_param, 0, sizeof conn_param);
316 conn_param.qp_num = node->cma_id->qp->qp_num;
317 ret = rdma_accept(node->cma_id, &conn_param);
319 perror("udaddy: failure accepting");
323 test.connects_left--;
330 printf("udaddy: failing connection request\n");
331 rdma_reject(cma_id, NULL, 0);
335 static int resolved_handler(struct cmatest_node *node,
336 struct rdma_cm_event *event)
338 node->remote_qpn = event->param.ud.qp_num;
339 node->remote_qkey = event->param.ud.qkey;
340 node->ah = ibv_create_ah(node->pd, &event->param.ud.ah_attr);
342 printf("udaddy: failure creating address handle\n");
347 test.connects_left--;
354 static int cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
358 switch (event->event) {
359 case RDMA_CM_EVENT_ADDR_RESOLVED:
360 ret = addr_handler(cma_id->context);
362 case RDMA_CM_EVENT_ROUTE_RESOLVED:
363 ret = route_handler(cma_id->context);
365 case RDMA_CM_EVENT_CONNECT_REQUEST:
366 ret = connect_handler(cma_id);
368 case RDMA_CM_EVENT_ESTABLISHED:
369 ret = resolved_handler(cma_id->context, event);
371 case RDMA_CM_EVENT_ADDR_ERROR:
372 case RDMA_CM_EVENT_ROUTE_ERROR:
373 case RDMA_CM_EVENT_CONNECT_ERROR:
374 case RDMA_CM_EVENT_UNREACHABLE:
375 case RDMA_CM_EVENT_REJECTED:
376 printf("udaddy: event: %s, error: %d\n",
377 rdma_event_str(event->event), event->status);
381 case RDMA_CM_EVENT_DEVICE_REMOVAL:
382 /* Cleanup will occur after test completes. */
390 static void destroy_node(struct cmatest_node *node)
396 ibv_destroy_ah(node->ah);
398 if (node->cma_id->qp)
399 rdma_destroy_qp(node->cma_id);
402 ibv_destroy_cq(node->cq);
405 ibv_dereg_mr(node->mr);
410 ibv_dealloc_pd(node->pd);
412 /* Destroy the RDMA ID after all device resources */
413 rdma_destroy_id(node->cma_id);
416 static int alloc_nodes(void)
420 test.nodes = malloc(sizeof *test.nodes * connections);
422 printf("udaddy: unable to allocate memory for test nodes\n");
425 memset(test.nodes, 0, sizeof *test.nodes * connections);
427 for (i = 0; i < connections; i++) {
428 test.nodes[i].id = i;
430 ret = rdma_create_id(test.channel,
431 &test.nodes[i].cma_id,
432 &test.nodes[i], port_space);
440 rdma_destroy_id(test.nodes[i].cma_id);
445 static void destroy_nodes(void)
449 for (i = 0; i < connections; i++)
450 destroy_node(&test.nodes[i]);
454 static void create_reply_ah(struct cmatest_node *node, struct ibv_wc *wc)
456 struct ibv_qp_attr attr;
457 struct ibv_qp_init_attr init_attr;
459 node->ah = ibv_create_ah_from_wc(node->pd, wc, node->mem,
460 node->cma_id->port_num);
461 node->remote_qpn = ntohl(wc->imm_data);
463 ibv_query_qp(node->cma_id->qp, &attr, IBV_QP_QKEY, &init_attr);
464 node->remote_qkey = attr.qkey;
467 static int poll_cqs(void)
472 for (i = 0; i < connections; i++) {
473 if (!test.nodes[i].connected)
476 for (done = 0; done < message_count; done += ret) {
477 ret = ibv_poll_cq(test.nodes[i].cq, 8, wc);
479 printf("udaddy: failed polling CQ: %d\n", ret);
483 if (ret && !test.nodes[i].ah)
484 create_reply_ah(&test.nodes[i], wc);
490 static int connect_events(void)
492 struct rdma_cm_event *event;
495 while (test.connects_left && !ret) {
496 ret = rdma_get_cm_event(test.channel, &event);
498 ret = cma_handler(event->id, event);
499 rdma_ack_cm_event(event);
505 static int get_addr(char *dst, struct sockaddr_in *addr)
507 struct addrinfo *res;
510 ret = getaddrinfo(dst, NULL, NULL, &res);
512 printf("getaddrinfo failed - invalid hostname or IP address\n");
516 if (res->ai_family != PF_INET) {
521 *addr = *(struct sockaddr_in *) res->ai_addr;
527 static int run_server(void)
529 struct rdma_cm_id *listen_id;
532 printf("udaddy: starting server\n");
533 ret = rdma_create_id(test.channel, &listen_id, &test, port_space);
535 perror("udaddy: listen request failed");
540 ret = get_addr(src_addr, &test.src_in);
544 test.src_in.sin_family = PF_INET;
546 test.src_in.sin_port = port;
547 ret = rdma_bind_addr(listen_id, test.src_addr);
549 perror("udaddy: bind address failed");
553 ret = rdma_listen(listen_id, 0);
555 perror("udaddy: failure trying to listen");
562 printf("receiving data transfers\n");
567 printf("sending replies\n");
568 for (i = 0; i < connections; i++) {
569 ret = post_sends(&test.nodes[i], IBV_SEND_SIGNALED);
577 printf("data transfers complete\n");
580 rdma_destroy_id(listen_id);
584 static int run_client(void)
588 printf("udaddy: starting client\n");
590 ret = get_addr(src_addr, &test.src_in);
595 ret = get_addr(dst_addr, &test.dst_in);
599 test.dst_in.sin_port = port;
601 printf("udaddy: connecting\n");
602 for (i = 0; i < connections; i++) {
603 ret = rdma_resolve_addr(test.nodes[i].cma_id,
604 src_addr ? test.src_addr : NULL,
605 test.dst_addr, 2000);
607 perror("udaddy: failure getting addr");
613 ret = connect_events();
618 printf("initiating data transfers\n");
619 for (i = 0; i < connections; i++) {
620 ret = post_sends(&test.nodes[i], 0);
624 printf("receiving data transfers\n");
629 printf("data transfers complete\n");
635 int main(int argc, char **argv)
639 while ((op = getopt(argc, argv, "s:b:c:C:S:t:p:")) != -1) {
648 connections = atoi(optarg);
651 message_count = atoi(optarg);
654 message_size = atoi(optarg);
658 tos = (uint8_t) atoi(optarg);
661 port_space = strtol(optarg, NULL, 0);
664 printf("usage: %s\n", argv[0]);
665 printf("\t[-s server_address]\n");
666 printf("\t[-b bind_address]\n");
667 printf("\t[-c connections]\n");
668 printf("\t[-C message_count]\n");
669 printf("\t[-S message_size]\n");
670 printf("\t[-t type_of_service]\n");
671 printf("\t[-p port_space - %#x for UDP (default), "
672 "%#x for IPOIB]\n", RDMA_PS_UDP, RDMA_PS_IPOIB);
677 test.dst_addr = (struct sockaddr *) &test.dst_in;
678 test.src_addr = (struct sockaddr *) &test.src_in;
679 test.connects_left = connections;
681 test.channel = rdma_create_event_channel();
683 perror("failed to create event channel");
695 printf("test complete\n");
697 rdma_destroy_event_channel(test.channel);
699 printf("return status %d\n", ret);