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 <netinet/in.h>
41 #include <arpa/inet.h>
42 #include <sys/socket.h>
48 #include <rdma/rdma_cma.h>
52 struct rdma_cm_id *cma_id;
64 struct rdma_event_channel *channel;
66 struct cmatest_node *nodes;
70 struct sockaddr_in6 dst_in;
71 struct sockaddr *dst_addr;
72 struct sockaddr_in6 src_in;
73 struct sockaddr *src_addr;
76 static struct cmatest test;
77 static int connections = 1;
78 static int message_size = 100;
79 static int message_count = 10;
81 static int unmapped_addr;
82 static char *dst_addr;
83 static char *src_addr;
84 static enum rdma_port_space port_space = RDMA_PS_UDP;
86 static int create_message(struct cmatest_node *node)
94 node->mem = malloc(message_size + sizeof(struct ibv_grh));
96 printf("failed message allocation\n");
99 node->mr = ibv_reg_mr(node->pd, node->mem,
100 message_size + sizeof(struct ibv_grh),
101 IBV_ACCESS_LOCAL_WRITE);
103 printf("failed to reg MR\n");
112 static int verify_test_params(struct cmatest_node *node)
114 struct ibv_port_attr port_attr;
117 ret = ibv_query_port(node->cma_id->verbs, node->cma_id->port_num,
122 if (message_count && message_size > (1 << (port_attr.active_mtu + 7))) {
123 printf("mckey: message_size %d is larger than active mtu %d\n",
124 message_size, 1 << (port_attr.active_mtu + 7));
131 static int init_node(struct cmatest_node *node)
133 struct ibv_qp_init_attr init_qp_attr;
136 node->pd = ibv_alloc_pd(node->cma_id->verbs);
139 printf("mckey: unable to allocate PD\n");
143 cqe = message_count ? message_count * 2 : 2;
144 node->cq = ibv_create_cq(node->cma_id->verbs, cqe, node, 0, 0);
147 printf("mckey: unable to create CQ\n");
151 memset(&init_qp_attr, 0, sizeof init_qp_attr);
152 init_qp_attr.cap.max_send_wr = message_count ? message_count : 1;
153 init_qp_attr.cap.max_recv_wr = message_count ? message_count : 1;
154 init_qp_attr.cap.max_send_sge = 1;
155 init_qp_attr.cap.max_recv_sge = 1;
156 init_qp_attr.qp_context = node;
157 init_qp_attr.sq_sig_all = 0;
158 init_qp_attr.qp_type = IBV_QPT_UD;
159 init_qp_attr.send_cq = node->cq;
160 init_qp_attr.recv_cq = node->cq;
161 ret = rdma_create_qp(node->cma_id, node->pd, &init_qp_attr);
163 perror("mckey: unable to create QP");
167 ret = create_message(node);
169 printf("mckey: failed to create messages: %d\n", ret);
176 static int post_recvs(struct cmatest_node *node)
178 struct ibv_recv_wr recv_wr, *recv_failure;
186 recv_wr.sg_list = &sge;
188 recv_wr.wr_id = (uintptr_t) node;
190 sge.length = message_size + sizeof(struct ibv_grh);
191 sge.lkey = node->mr->lkey;
192 sge.addr = (uintptr_t) node->mem;
194 for (i = 0; i < message_count && !ret; i++ ) {
195 ret = ibv_post_recv(node->cma_id->qp, &recv_wr, &recv_failure);
197 printf("failed to post receives: %d\n", ret);
204 static int post_sends(struct cmatest_node *node, int signal_flag)
206 struct ibv_send_wr send_wr, *bad_send_wr;
210 if (!node->connected || !message_count)
214 send_wr.sg_list = &sge;
216 send_wr.opcode = IBV_WR_SEND_WITH_IMM;
217 send_wr.send_flags = signal_flag;
218 send_wr.wr_id = (unsigned long)node;
219 send_wr.imm_data = htonl(node->cma_id->qp->qp_num);
221 send_wr.wr.ud.ah = node->ah;
222 send_wr.wr.ud.remote_qpn = node->remote_qpn;
223 send_wr.wr.ud.remote_qkey = node->remote_qkey;
225 sge.length = message_size;
226 sge.lkey = node->mr->lkey;
227 sge.addr = (uintptr_t) node->mem;
229 for (i = 0; i < message_count && !ret; i++) {
230 ret = ibv_post_send(node->cma_id->qp, &send_wr, &bad_send_wr);
232 printf("failed to post sends: %d\n", ret);
237 static void connect_error(void)
239 test.connects_left--;
242 static int addr_handler(struct cmatest_node *node)
246 ret = verify_test_params(node);
250 ret = init_node(node);
255 ret = post_recvs(node);
260 ret = rdma_join_multicast(node->cma_id, test.dst_addr, node);
262 perror("mckey: failure joining");
271 static int join_handler(struct cmatest_node *node,
272 struct rdma_ud_param *param)
276 inet_ntop(AF_INET6, param->ah_attr.grh.dgid.raw, buf, 40);
277 printf("mckey: joined dgid: %s mlid 0x%x sl %d\n", buf,
278 param->ah_attr.dlid, param->ah_attr.sl);
280 node->remote_qpn = param->qp_num;
281 node->remote_qkey = param->qkey;
282 node->ah = ibv_create_ah(node->pd, ¶m->ah_attr);
284 printf("mckey: failure creating address handle\n");
289 test.connects_left--;
296 static int cma_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event)
300 switch (event->event) {
301 case RDMA_CM_EVENT_ADDR_RESOLVED:
302 ret = addr_handler(cma_id->context);
304 case RDMA_CM_EVENT_MULTICAST_JOIN:
305 ret = join_handler(cma_id->context, &event->param.ud);
307 case RDMA_CM_EVENT_ADDR_ERROR:
308 case RDMA_CM_EVENT_ROUTE_ERROR:
309 case RDMA_CM_EVENT_MULTICAST_ERROR:
310 printf("mckey: event: %s, error: %d\n",
311 rdma_event_str(event->event), event->status);
315 case RDMA_CM_EVENT_DEVICE_REMOVAL:
316 /* Cleanup will occur after test completes. */
324 static void *cma_thread(void *arg)
326 struct rdma_cm_event *event;
330 ret = rdma_get_cm_event(test.channel, &event);
332 perror("rdma_get_cm_event");
336 switch (event->event) {
337 case RDMA_CM_EVENT_MULTICAST_ERROR:
338 case RDMA_CM_EVENT_ADDR_CHANGE:
339 printf("mckey: event: %s, status: %d\n",
340 rdma_event_str(event->event), event->status);
346 rdma_ack_cm_event(event);
351 static void destroy_node(struct cmatest_node *node)
357 ibv_destroy_ah(node->ah);
359 if (node->cma_id->qp)
360 rdma_destroy_qp(node->cma_id);
363 ibv_destroy_cq(node->cq);
366 ibv_dereg_mr(node->mr);
371 ibv_dealloc_pd(node->pd);
373 /* Destroy the RDMA ID after all device resources */
374 rdma_destroy_id(node->cma_id);
377 static int alloc_nodes(void)
381 test.nodes = malloc(sizeof *test.nodes * connections);
383 printf("mckey: unable to allocate memory for test nodes\n");
386 memset(test.nodes, 0, sizeof *test.nodes * connections);
388 for (i = 0; i < connections; i++) {
389 test.nodes[i].id = i;
390 ret = rdma_create_id(test.channel, &test.nodes[i].cma_id,
391 &test.nodes[i], port_space);
398 rdma_destroy_id(test.nodes[i].cma_id);
403 static void destroy_nodes(void)
407 for (i = 0; i < connections; i++)
408 destroy_node(&test.nodes[i]);
412 static int poll_cqs(void)
417 for (i = 0; i < connections; i++) {
418 if (!test.nodes[i].connected)
421 for (done = 0; done < message_count; done += ret) {
422 ret = ibv_poll_cq(test.nodes[i].cq, 8, wc);
424 printf("mckey: failed polling CQ: %d\n", ret);
432 static int connect_events(void)
434 struct rdma_cm_event *event;
437 while (test.connects_left && !ret) {
438 ret = rdma_get_cm_event(test.channel, &event);
440 ret = cma_handler(event->id, event);
441 rdma_ack_cm_event(event);
447 static int get_addr(char *dst, struct sockaddr *addr)
449 struct addrinfo *res;
452 ret = getaddrinfo(dst, NULL, NULL, &res);
454 printf("getaddrinfo failed - invalid hostname or IP address\n");
458 memcpy(addr, res->ai_addr, res->ai_addrlen);
467 printf("mckey: starting %s\n", is_sender ? "client" : "server");
469 ret = get_addr(src_addr, (struct sockaddr *) &test.src_in);
474 ret = get_addr(dst_addr, (struct sockaddr *) &test.dst_in);
478 printf("mckey: joining\n");
479 for (i = 0; i < connections; i++) {
481 ret = rdma_bind_addr(test.nodes[i].cma_id,
484 perror("mckey: addr bind failure");
491 ret = addr_handler(&test.nodes[i]);
493 ret = rdma_resolve_addr(test.nodes[i].cma_id,
494 test.src_addr, test.dst_addr,
497 perror("mckey: resolve addr failure");
503 ret = connect_events();
507 pthread_create(&test.cmathread, NULL, cma_thread, NULL);
510 * Pause to give SM chance to configure switches. We don't want to
511 * handle reliability issue in this simple test program.
517 printf("initiating data transfers\n");
518 for (i = 0; i < connections; i++) {
519 ret = post_sends(&test.nodes[i], 0);
524 printf("receiving data transfers\n");
529 printf("data transfers complete\n");
532 for (i = 0; i < connections; i++) {
533 ret = rdma_leave_multicast(test.nodes[i].cma_id,
536 perror("mckey: failure leaving");
541 int main(int argc, char **argv)
546 while ((op = getopt(argc, argv, "m:M:sb:c:C:S:p:")) != -1) {
560 test.src_addr = (struct sockaddr *) &test.src_in;
563 connections = atoi(optarg);
566 message_count = atoi(optarg);
569 message_size = atoi(optarg);
572 port_space = strtol(optarg, NULL, 0);
575 printf("usage: %s\n", argv[0]);
576 printf("\t-m multicast_address\n");
577 printf("\t[-M unmapped_multicast_address]\n"
578 "\t replaces -m and requires -b\n");
579 printf("\t[-s(ender)]\n");
580 printf("\t[-b bind_address]\n");
581 printf("\t[-c connections]\n");
582 printf("\t[-C message_count]\n");
583 printf("\t[-S message_size]\n");
584 printf("\t[-p port_space - %#x for UDP (default), "
585 "%#x for IPOIB]\n", RDMA_PS_UDP, RDMA_PS_IPOIB);
590 if (unmapped_addr && !src_addr) {
591 printf("unmapped multicast address requires binding "
592 "to source address\n");
596 test.dst_addr = (struct sockaddr *) &test.dst_in;
597 test.connects_left = connections;
599 test.channel = rdma_create_event_channel();
601 perror("failed to create event channel");
610 printf("test complete\n");
612 rdma_destroy_event_channel(test.channel);
614 printf("return status %d\n", ret);