2 * Copyright (c) 2005-2007 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 <arpa/inet.h>
41 #include <sys/socket.h>
46 #include <rdma/rdma_cma.h>
47 #include <infiniband/ib.h>
51 struct rdma_cm_id *cma_id;
63 struct rdma_event_channel *channel;
65 struct cmatest_node *nodes;
69 struct sockaddr_storage dst_in;
70 struct sockaddr *dst_addr;
71 struct sockaddr_storage src_in;
72 struct sockaddr *src_addr;
75 static struct cmatest test;
76 static int connections = 1;
77 static int message_size = 100;
78 static int message_count = 10;
80 static int unmapped_addr;
81 static char *dst_addr;
82 static char *src_addr;
83 static enum rdma_port_space port_space = RDMA_PS_UDP;
85 static int create_message(struct cmatest_node *node)
93 node->mem = malloc(message_size + sizeof(struct ibv_grh));
95 printf("failed message allocation\n");
98 node->mr = ibv_reg_mr(node->pd, node->mem,
99 message_size + sizeof(struct ibv_grh),
100 IBV_ACCESS_LOCAL_WRITE);
102 printf("failed to reg MR\n");
111 static int verify_test_params(struct cmatest_node *node)
113 struct ibv_port_attr port_attr;
116 ret = ibv_query_port(node->cma_id->verbs, node->cma_id->port_num,
121 if (message_count && message_size > (1 << (port_attr.active_mtu + 7))) {
122 printf("mckey: message_size %d is larger than active mtu %d\n",
123 message_size, 1 << (port_attr.active_mtu + 7));
130 static int init_node(struct cmatest_node *node)
132 struct ibv_qp_init_attr init_qp_attr;
135 node->pd = ibv_alloc_pd(node->cma_id->verbs);
138 printf("mckey: unable to allocate PD\n");
142 cqe = message_count ? message_count * 2 : 2;
143 node->cq = ibv_create_cq(node->cma_id->verbs, cqe, node, NULL, 0);
146 printf("mckey: unable to create CQ\n");
150 memset(&init_qp_attr, 0, sizeof init_qp_attr);
151 init_qp_attr.cap.max_send_wr = message_count ? message_count : 1;
152 init_qp_attr.cap.max_recv_wr = message_count ? message_count : 1;
153 init_qp_attr.cap.max_send_sge = 1;
154 init_qp_attr.cap.max_recv_sge = 1;
155 init_qp_attr.qp_context = node;
156 init_qp_attr.sq_sig_all = 0;
157 init_qp_attr.qp_type = IBV_QPT_UD;
158 init_qp_attr.send_cq = node->cq;
159 init_qp_attr.recv_cq = node->cq;
160 ret = rdma_create_qp(node->cma_id, node->pd, &init_qp_attr);
162 perror("mckey: unable to create QP");
166 ret = create_message(node);
168 printf("mckey: failed to create messages: %d\n", ret);
175 static int post_recvs(struct cmatest_node *node)
177 struct ibv_recv_wr recv_wr, *recv_failure;
185 recv_wr.sg_list = &sge;
187 recv_wr.wr_id = (uintptr_t) node;
189 sge.length = message_size + sizeof(struct ibv_grh);
190 sge.lkey = node->mr->lkey;
191 sge.addr = (uintptr_t) node->mem;
193 for (i = 0; i < message_count && !ret; i++ ) {
194 ret = ibv_post_recv(node->cma_id->qp, &recv_wr, &recv_failure);
196 printf("failed to post receives: %d\n", ret);
203 static int post_sends(struct cmatest_node *node, int signal_flag)
205 struct ibv_send_wr send_wr, *bad_send_wr;
209 if (!node->connected || !message_count)
213 send_wr.sg_list = &sge;
215 send_wr.opcode = IBV_WR_SEND_WITH_IMM;
216 send_wr.send_flags = signal_flag;
217 send_wr.wr_id = (unsigned long)node;
218 send_wr.imm_data = htobe32(node->cma_id->qp->qp_num);
220 send_wr.wr.ud.ah = node->ah;
221 send_wr.wr.ud.remote_qpn = node->remote_qpn;
222 send_wr.wr.ud.remote_qkey = node->remote_qkey;
224 sge.length = message_size;
225 sge.lkey = node->mr->lkey;
226 sge.addr = (uintptr_t) node->mem;
228 for (i = 0; i < message_count && !ret; i++) {
229 ret = ibv_post_send(node->cma_id->qp, &send_wr, &bad_send_wr);
231 printf("failed to post sends: %d\n", ret);
236 static void connect_error(void)
238 test.connects_left--;
241 static int addr_handler(struct cmatest_node *node)
245 ret = verify_test_params(node);
249 ret = init_node(node);
254 ret = post_recvs(node);
259 ret = rdma_join_multicast(node->cma_id, test.dst_addr, node);
261 perror("mckey: failure joining");
270 static int join_handler(struct cmatest_node *node,
271 struct rdma_ud_param *param)
275 inet_ntop(AF_INET6, param->ah_attr.grh.dgid.raw, buf, 40);
276 printf("mckey: joined dgid: %s mlid 0x%x sl %d\n", buf,
277 param->ah_attr.dlid, param->ah_attr.sl);
279 node->remote_qpn = param->qp_num;
280 node->remote_qkey = param->qkey;
281 node->ah = ibv_create_ah(node->pd, ¶m->ah_attr);
283 printf("mckey: failure creating address handle\n");
288 test.connects_left--;
295 static int cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
299 switch (event->event) {
300 case RDMA_CM_EVENT_ADDR_RESOLVED:
301 ret = addr_handler(cma_id->context);
303 case RDMA_CM_EVENT_MULTICAST_JOIN:
304 ret = join_handler(cma_id->context, &event->param.ud);
306 case RDMA_CM_EVENT_ADDR_ERROR:
307 case RDMA_CM_EVENT_ROUTE_ERROR:
308 case RDMA_CM_EVENT_MULTICAST_ERROR:
309 printf("mckey: event: %s, error: %d\n",
310 rdma_event_str(event->event), event->status);
314 case RDMA_CM_EVENT_DEVICE_REMOVAL:
315 /* Cleanup will occur after test completes. */
323 static void *cma_thread(void *arg)
325 struct rdma_cm_event *event;
329 ret = rdma_get_cm_event(test.channel, &event);
331 perror("rdma_get_cm_event");
335 switch (event->event) {
336 case RDMA_CM_EVENT_MULTICAST_ERROR:
337 case RDMA_CM_EVENT_ADDR_CHANGE:
338 printf("mckey: event: %s, status: %d\n",
339 rdma_event_str(event->event), event->status);
345 rdma_ack_cm_event(event);
350 static void destroy_node(struct cmatest_node *node)
356 ibv_destroy_ah(node->ah);
358 if (node->cma_id->qp)
359 rdma_destroy_qp(node->cma_id);
362 ibv_destroy_cq(node->cq);
365 ibv_dereg_mr(node->mr);
370 ibv_dealloc_pd(node->pd);
372 /* Destroy the RDMA ID after all device resources */
373 rdma_destroy_id(node->cma_id);
376 static int alloc_nodes(void)
380 test.nodes = malloc(sizeof *test.nodes * connections);
382 printf("mckey: unable to allocate memory for test nodes\n");
385 memset(test.nodes, 0, sizeof *test.nodes * connections);
387 for (i = 0; i < connections; i++) {
388 test.nodes[i].id = i;
389 ret = rdma_create_id(test.channel, &test.nodes[i].cma_id,
390 &test.nodes[i], port_space);
397 rdma_destroy_id(test.nodes[i].cma_id);
402 static void destroy_nodes(void)
406 for (i = 0; i < connections; i++)
407 destroy_node(&test.nodes[i]);
411 static int poll_cqs(void)
416 for (i = 0; i < connections; i++) {
417 if (!test.nodes[i].connected)
420 for (done = 0; done < message_count; done += ret) {
421 ret = ibv_poll_cq(test.nodes[i].cq, 8, wc);
423 printf("mckey: failed polling CQ: %d\n", ret);
431 static int connect_events(void)
433 struct rdma_cm_event *event;
436 while (test.connects_left && !ret) {
437 ret = rdma_get_cm_event(test.channel, &event);
439 ret = cma_handler(event->id, event);
440 rdma_ack_cm_event(event);
446 static int get_addr(char *dst, struct sockaddr *addr)
448 struct addrinfo *res;
451 ret = getaddrinfo(dst, NULL, NULL, &res);
453 printf("getaddrinfo failed (%s) - invalid hostname or IP address\n", gai_strerror(ret));
457 memcpy(addr, res->ai_addr, res->ai_addrlen);
462 static int get_dst_addr(char *dst, struct sockaddr *addr)
464 struct sockaddr_ib *sib;
467 return get_addr(dst, addr);
469 sib = (struct sockaddr_ib *) addr;
470 memset(sib, 0, sizeof *sib);
471 sib->sib_family = AF_IB;
472 inet_pton(AF_INET6, dst, &sib->sib_addr);
480 printf("mckey: starting %s\n", is_sender ? "client" : "server");
482 ret = get_addr(src_addr, (struct sockaddr *) &test.src_in);
487 ret = get_dst_addr(dst_addr, (struct sockaddr *) &test.dst_in);
491 printf("mckey: joining\n");
492 for (i = 0; i < connections; i++) {
494 ret = rdma_bind_addr(test.nodes[i].cma_id,
497 perror("mckey: addr bind failure");
504 ret = addr_handler(&test.nodes[i]);
506 ret = rdma_resolve_addr(test.nodes[i].cma_id,
507 test.src_addr, test.dst_addr,
510 perror("mckey: resolve addr failure");
516 ret = connect_events();
520 pthread_create(&test.cmathread, NULL, cma_thread, NULL);
523 * Pause to give SM chance to configure switches. We don't want to
524 * handle reliability issue in this simple test program.
530 printf("initiating data transfers\n");
531 for (i = 0; i < connections; i++) {
532 ret = post_sends(&test.nodes[i], 0);
537 printf("receiving data transfers\n");
542 printf("data transfers complete\n");
545 for (i = 0; i < connections; i++) {
546 err = rdma_leave_multicast(test.nodes[i].cma_id,
549 perror("mckey: failure leaving");
556 int main(int argc, char **argv)
561 while ((op = getopt(argc, argv, "m:M:sb:c:C:S:p:")) != -1) {
575 test.src_addr = (struct sockaddr *) &test.src_in;
578 connections = atoi(optarg);
581 message_count = atoi(optarg);
584 message_size = atoi(optarg);
587 port_space = strtol(optarg, NULL, 0);
590 printf("usage: %s\n", argv[0]);
591 printf("\t-m multicast_address\n");
592 printf("\t[-M unmapped_multicast_address]\n"
593 "\t replaces -m and requires -b\n");
594 printf("\t[-s(ender)]\n");
595 printf("\t[-b bind_address]\n");
596 printf("\t[-c connections]\n");
597 printf("\t[-C message_count]\n");
598 printf("\t[-S message_size]\n");
599 printf("\t[-p port_space - %#x for UDP (default), "
600 "%#x for IPOIB]\n", RDMA_PS_UDP, RDMA_PS_IPOIB);
605 if (unmapped_addr && !src_addr) {
606 printf("unmapped multicast address requires binding "
607 "to source address\n");
611 test.dst_addr = (struct sockaddr *) &test.dst_in;
612 test.connects_left = connections;
614 test.channel = rdma_create_event_channel();
616 perror("failed to create event channel");
625 printf("test complete\n");
627 rdma_destroy_event_channel(test.channel);
629 printf("return status %d\n", ret);