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 <sys/socket.h>
44 #include <rdma/rdma_cma.h>
49 struct rdma_cm_id *cma_id;
61 struct rdma_event_channel *channel;
62 struct cmatest_node *nodes;
66 struct rdma_addrinfo *rai;
69 static struct cmatest test;
70 static int connections = 1;
71 static int message_size = 100;
72 static int message_count = 10;
73 static const char *port = "7174";
74 static uint8_t set_tos = 0;
76 static char *dst_addr;
77 static char *src_addr;
78 static struct rdma_addrinfo hints;
80 static int create_message(struct cmatest_node *node)
88 node->mem = malloc(message_size + sizeof(struct ibv_grh));
90 printf("failed message allocation\n");
93 node->mr = ibv_reg_mr(node->pd, node->mem,
94 message_size + sizeof(struct ibv_grh),
95 IBV_ACCESS_LOCAL_WRITE);
97 printf("failed to reg MR\n");
106 static int verify_test_params(struct cmatest_node *node)
108 struct ibv_port_attr port_attr;
111 ret = ibv_query_port(node->cma_id->verbs, node->cma_id->port_num,
116 if (message_count && message_size > (1 << (port_attr.active_mtu + 7))) {
117 printf("udaddy: message_size %d is larger than active mtu %d\n",
118 message_size, 1 << (port_attr.active_mtu + 7));
125 static int init_node(struct cmatest_node *node)
127 struct ibv_qp_init_attr init_qp_attr;
130 node->pd = ibv_alloc_pd(node->cma_id->verbs);
133 printf("udaddy: unable to allocate PD\n");
137 cqe = message_count ? message_count * 2 : 2;
138 node->cq = ibv_create_cq(node->cma_id->verbs, cqe, node, NULL, 0);
141 printf("udaddy: unable to create CQ\n");
145 memset(&init_qp_attr, 0, sizeof init_qp_attr);
146 init_qp_attr.cap.max_send_wr = message_count ? message_count : 1;
147 init_qp_attr.cap.max_recv_wr = message_count ? message_count : 1;
148 init_qp_attr.cap.max_send_sge = 1;
149 init_qp_attr.cap.max_recv_sge = 1;
150 init_qp_attr.qp_context = node;
151 init_qp_attr.sq_sig_all = 0;
152 init_qp_attr.qp_type = IBV_QPT_UD;
153 init_qp_attr.send_cq = node->cq;
154 init_qp_attr.recv_cq = node->cq;
155 ret = rdma_create_qp(node->cma_id, node->pd, &init_qp_attr);
157 perror("udaddy: unable to create QP");
161 ret = create_message(node);
163 printf("udaddy: failed to create messages: %d\n", ret);
170 static int post_recvs(struct cmatest_node *node)
172 struct ibv_recv_wr recv_wr, *recv_failure;
180 recv_wr.sg_list = &sge;
182 recv_wr.wr_id = (uintptr_t) node;
184 sge.length = message_size + sizeof(struct ibv_grh);
185 sge.lkey = node->mr->lkey;
186 sge.addr = (uintptr_t) node->mem;
188 for (i = 0; i < message_count && !ret; i++ ) {
189 ret = ibv_post_recv(node->cma_id->qp, &recv_wr, &recv_failure);
191 printf("failed to post receives: %d\n", ret);
198 static int post_sends(struct cmatest_node *node, int signal_flag)
200 struct ibv_send_wr send_wr, *bad_send_wr;
204 if (!node->connected || !message_count)
208 send_wr.sg_list = &sge;
210 send_wr.opcode = IBV_WR_SEND_WITH_IMM;
211 send_wr.send_flags = signal_flag;
212 send_wr.wr_id = (unsigned long)node;
213 send_wr.imm_data = htobe32(node->cma_id->qp->qp_num);
215 send_wr.wr.ud.ah = node->ah;
216 send_wr.wr.ud.remote_qpn = node->remote_qpn;
217 send_wr.wr.ud.remote_qkey = node->remote_qkey;
219 sge.length = message_size;
220 sge.lkey = node->mr->lkey;
221 sge.addr = (uintptr_t) node->mem;
223 for (i = 0; i < message_count && !ret; i++) {
224 ret = ibv_post_send(node->cma_id->qp, &send_wr, &bad_send_wr);
226 printf("failed to post sends: %d\n", ret);
231 static void connect_error(void)
233 test.connects_left--;
236 static int addr_handler(struct cmatest_node *node)
241 ret = rdma_set_option(node->cma_id, RDMA_OPTION_ID,
242 RDMA_OPTION_ID_TOS, &tos, sizeof tos);
244 perror("udaddy: set TOS option failed");
247 ret = rdma_resolve_route(node->cma_id, 2000);
249 perror("udaddy: resolve route failed");
255 static int route_handler(struct cmatest_node *node)
257 struct rdma_conn_param conn_param;
260 ret = verify_test_params(node);
264 ret = init_node(node);
268 ret = post_recvs(node);
272 memset(&conn_param, 0, sizeof conn_param);
273 conn_param.private_data = test.rai->ai_connect;
274 conn_param.private_data_len = test.rai->ai_connect_len;
275 ret = rdma_connect(node->cma_id, &conn_param);
277 perror("udaddy: failure connecting");
286 static int connect_handler(struct rdma_cm_id *cma_id)
288 struct cmatest_node *node;
289 struct rdma_conn_param conn_param;
292 if (test.conn_index == connections) {
296 node = &test.nodes[test.conn_index++];
298 node->cma_id = cma_id;
299 cma_id->context = node;
301 ret = verify_test_params(node);
305 ret = init_node(node);
309 ret = post_recvs(node);
313 memset(&conn_param, 0, sizeof conn_param);
314 conn_param.qp_num = node->cma_id->qp->qp_num;
315 ret = rdma_accept(node->cma_id, &conn_param);
317 perror("udaddy: failure accepting");
321 test.connects_left--;
328 printf("udaddy: failing connection request\n");
329 rdma_reject(cma_id, NULL, 0);
333 static int resolved_handler(struct cmatest_node *node,
334 struct rdma_cm_event *event)
336 node->remote_qpn = event->param.ud.qp_num;
337 node->remote_qkey = event->param.ud.qkey;
338 node->ah = ibv_create_ah(node->pd, &event->param.ud.ah_attr);
340 printf("udaddy: failure creating address handle\n");
345 test.connects_left--;
352 static int cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
356 switch (event->event) {
357 case RDMA_CM_EVENT_ADDR_RESOLVED:
358 ret = addr_handler(cma_id->context);
360 case RDMA_CM_EVENT_ROUTE_RESOLVED:
361 ret = route_handler(cma_id->context);
363 case RDMA_CM_EVENT_CONNECT_REQUEST:
364 ret = connect_handler(cma_id);
366 case RDMA_CM_EVENT_ESTABLISHED:
367 ret = resolved_handler(cma_id->context, event);
369 case RDMA_CM_EVENT_ADDR_ERROR:
370 case RDMA_CM_EVENT_ROUTE_ERROR:
371 case RDMA_CM_EVENT_CONNECT_ERROR:
372 case RDMA_CM_EVENT_UNREACHABLE:
373 case RDMA_CM_EVENT_REJECTED:
374 printf("udaddy: event: %s, error: %d\n",
375 rdma_event_str(event->event), event->status);
379 case RDMA_CM_EVENT_DEVICE_REMOVAL:
380 /* Cleanup will occur after test completes. */
388 static void destroy_node(struct cmatest_node *node)
394 ibv_destroy_ah(node->ah);
396 if (node->cma_id->qp)
397 rdma_destroy_qp(node->cma_id);
400 ibv_destroy_cq(node->cq);
403 ibv_dereg_mr(node->mr);
408 ibv_dealloc_pd(node->pd);
410 /* Destroy the RDMA ID after all device resources */
411 rdma_destroy_id(node->cma_id);
414 static int alloc_nodes(void)
418 test.nodes = malloc(sizeof *test.nodes * connections);
420 printf("udaddy: unable to allocate memory for test nodes\n");
423 memset(test.nodes, 0, sizeof *test.nodes * connections);
425 for (i = 0; i < connections; i++) {
426 test.nodes[i].id = i;
428 ret = rdma_create_id(test.channel,
429 &test.nodes[i].cma_id,
430 &test.nodes[i], hints.ai_port_space);
438 rdma_destroy_id(test.nodes[i].cma_id);
443 static void destroy_nodes(void)
447 for (i = 0; i < connections; i++)
448 destroy_node(&test.nodes[i]);
452 static void create_reply_ah(struct cmatest_node *node, struct ibv_wc *wc)
454 struct ibv_qp_attr attr;
455 struct ibv_qp_init_attr init_attr;
457 node->ah = ibv_create_ah_from_wc(node->pd, wc, node->mem,
458 node->cma_id->port_num);
459 node->remote_qpn = be32toh(wc->imm_data);
461 ibv_query_qp(node->cma_id->qp, &attr, IBV_QP_QKEY, &init_attr);
462 node->remote_qkey = attr.qkey;
465 static int poll_cqs(void)
470 for (i = 0; i < connections; i++) {
471 if (!test.nodes[i].connected)
474 for (done = 0; done < message_count; done += ret) {
475 ret = ibv_poll_cq(test.nodes[i].cq, 8, wc);
477 printf("udaddy: failed polling CQ: %d\n", ret);
481 if (ret && !test.nodes[i].ah)
482 create_reply_ah(&test.nodes[i], wc);
488 static int connect_events(void)
490 struct rdma_cm_event *event;
493 while (test.connects_left && !ret) {
494 ret = rdma_get_cm_event(test.channel, &event);
496 ret = cma_handler(event->id, event);
497 rdma_ack_cm_event(event);
503 static int run_server(void)
505 struct rdma_cm_id *listen_id;
508 printf("udaddy: starting server\n");
509 ret = rdma_create_id(test.channel, &listen_id, &test, hints.ai_port_space);
511 perror("udaddy: listen request failed");
515 ret = get_rdma_addr(src_addr, dst_addr, port, &hints, &test.rai);
517 printf("udaddy: getrdmaaddr error: %s\n", gai_strerror(ret));
521 ret = rdma_bind_addr(listen_id, test.rai->ai_src_addr);
523 perror("udaddy: bind address failed");
527 ret = rdma_listen(listen_id, 0);
529 perror("udaddy: failure trying to listen");
536 printf("receiving data transfers\n");
541 printf("sending replies\n");
542 for (i = 0; i < connections; i++) {
543 ret = post_sends(&test.nodes[i], IBV_SEND_SIGNALED);
551 printf("data transfers complete\n");
554 rdma_destroy_id(listen_id);
558 static int run_client(void)
562 printf("udaddy: starting client\n");
564 ret = get_rdma_addr(src_addr, dst_addr, port, &hints, &test.rai);
566 printf("udaddy: getaddrinfo error: %s\n", gai_strerror(ret));
570 printf("udaddy: connecting\n");
571 for (i = 0; i < connections; i++) {
572 ret = rdma_resolve_addr(test.nodes[i].cma_id, test.rai->ai_src_addr,
573 test.rai->ai_dst_addr, 2000);
575 perror("udaddy: failure getting addr");
581 ret = connect_events();
586 printf("initiating data transfers\n");
587 for (i = 0; i < connections; i++) {
588 ret = post_sends(&test.nodes[i], 0);
592 printf("receiving data transfers\n");
597 printf("data transfers complete\n");
603 int main(int argc, char **argv)
607 hints.ai_port_space = RDMA_PS_UDP;
608 while ((op = getopt(argc, argv, "s:b:c:C:S:t:p:P:f:")) != -1) {
617 connections = atoi(optarg);
620 message_count = atoi(optarg);
623 message_size = atoi(optarg);
627 tos = (uint8_t) strtoul(optarg, NULL, 0);
629 case 'p': /* for backwards compatibility - use -P */
630 hints.ai_port_space = strtol(optarg, NULL, 0);
633 if (!strncasecmp("ip", optarg, 2)) {
634 hints.ai_flags = RAI_NUMERICHOST;
635 } else if (!strncasecmp("gid", optarg, 3)) {
636 hints.ai_flags = RAI_NUMERICHOST | RAI_FAMILY;
637 hints.ai_family = AF_IB;
638 } else if (strncasecmp("name", optarg, 4)) {
639 fprintf(stderr, "Warning: unknown address format\n");
643 if (!strncasecmp("ipoib", optarg, 5)) {
644 hints.ai_port_space = RDMA_PS_IPOIB;
645 } else if (strncasecmp("udp", optarg, 3)) {
646 fprintf(stderr, "Warning: unknown port space format\n");
650 printf("usage: %s\n", argv[0]);
651 printf("\t[-s server_address]\n");
652 printf("\t[-b bind_address]\n");
653 printf("\t[-f address_format]\n");
654 printf("\t name, ip, ipv6, or gid\n");
655 printf("\t[-P port_space]\n");
656 printf("\t udp or ipoib\n");
657 printf("\t[-c connections]\n");
658 printf("\t[-C message_count]\n");
659 printf("\t[-S message_size]\n");
660 printf("\t[-t type_of_service]\n");
661 printf("\t[-p port_space - %#x for UDP (default), "
662 "%#x for IPOIB]\n", RDMA_PS_UDP, RDMA_PS_IPOIB);
667 test.connects_left = connections;
669 test.channel = rdma_create_event_channel();
671 perror("failed to create event channel");
681 hints.ai_flags |= RAI_PASSIVE;
685 printf("test complete\n");
687 rdma_destroy_event_channel(test.channel);
689 rdma_freeaddrinfo(test.rai);
691 printf("return status %d\n", ret);