2 * Copyright (c) 2005 Topspin Communications. 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
35 #endif /* HAVE_CONFIG_H */
41 #include <sys/types.h>
42 #include <sys/socket.h>
46 #include <arpa/inet.h>
52 PINGPONG_RECV_WRID = 1,
53 PINGPONG_SEND_WRID = 2,
58 struct pingpong_context {
59 struct ibv_context *context;
60 struct ibv_comp_channel *channel;
70 struct ibv_port_attr portinfo;
73 struct pingpong_dest {
80 static int pp_connect_ctx(struct pingpong_context *ctx, int port, int my_psn,
81 int sl, struct pingpong_dest *dest, int sgid_idx)
83 struct ibv_ah_attr ah_attr = {
90 struct ibv_qp_attr attr = {
91 .qp_state = IBV_QPS_RTR
94 if (ibv_modify_qp(ctx->qp, &attr, IBV_QP_STATE)) {
95 fprintf(stderr, "Failed to modify QP to RTR\n");
99 attr.qp_state = IBV_QPS_RTS;
100 attr.sq_psn = my_psn;
102 if (ibv_modify_qp(ctx->qp, &attr,
105 fprintf(stderr, "Failed to modify QP to RTS\n");
109 if (dest->gid.global.interface_id) {
110 ah_attr.is_global = 1;
111 ah_attr.grh.hop_limit = 1;
112 ah_attr.grh.dgid = dest->gid;
113 ah_attr.grh.sgid_index = sgid_idx;
116 ctx->ah = ibv_create_ah(ctx->pd, &ah_attr);
118 fprintf(stderr, "Failed to create AH\n");
125 static struct pingpong_dest *pp_client_exch_dest(const char *servername, int port,
126 const struct pingpong_dest *my_dest)
128 struct addrinfo *res, *t;
129 struct addrinfo hints = {
130 .ai_family = AF_INET,
131 .ai_socktype = SOCK_STREAM
134 char msg[sizeof "0000:000000:000000:00000000000000000000000000000000"];
137 struct pingpong_dest *rem_dest = NULL;
140 if (asprintf(&service, "%d", port) < 0)
143 n = getaddrinfo(servername, service, &hints, &res);
146 fprintf(stderr, "%s for %s:%d\n", gai_strerror(n), servername, port);
151 for (t = res; t; t = t->ai_next) {
152 sockfd = socket(t->ai_family, t->ai_socktype, t->ai_protocol);
154 if (!connect(sockfd, t->ai_addr, t->ai_addrlen))
165 fprintf(stderr, "Couldn't connect to %s:%d\n", servername, port);
169 gid_to_wire_gid(&my_dest->gid, gid);
170 sprintf(msg, "%04x:%06x:%06x:%s", my_dest->lid, my_dest->qpn, my_dest->psn, gid);
171 if (write(sockfd, msg, sizeof msg) != sizeof msg) {
172 fprintf(stderr, "Couldn't send local address\n");
176 if (read(sockfd, msg, sizeof msg) != sizeof msg) {
177 perror("client read");
178 fprintf(stderr, "Couldn't read remote address\n");
182 write(sockfd, "done", sizeof "done");
184 rem_dest = malloc(sizeof *rem_dest);
188 sscanf(msg, "%x:%x:%x:%s", &rem_dest->lid, &rem_dest->qpn, &rem_dest->psn, gid);
189 wire_gid_to_gid(gid, &rem_dest->gid);
196 static struct pingpong_dest *pp_server_exch_dest(struct pingpong_context *ctx,
197 int ib_port, int port, int sl,
198 const struct pingpong_dest *my_dest,
201 struct addrinfo *res, *t;
202 struct addrinfo hints = {
203 .ai_flags = AI_PASSIVE,
204 .ai_family = AF_INET,
205 .ai_socktype = SOCK_STREAM
208 char msg[sizeof "0000:000000:000000:00000000000000000000000000000000"];
210 int sockfd = -1, connfd;
211 struct pingpong_dest *rem_dest = NULL;
214 if (asprintf(&service, "%d", port) < 0)
217 n = getaddrinfo(NULL, service, &hints, &res);
220 fprintf(stderr, "%s for port %d\n", gai_strerror(n), port);
225 for (t = res; t; t = t->ai_next) {
226 sockfd = socket(t->ai_family, t->ai_socktype, t->ai_protocol);
230 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n);
232 if (!bind(sockfd, t->ai_addr, t->ai_addrlen))
243 fprintf(stderr, "Couldn't listen to port %d\n", port);
248 connfd = accept(sockfd, NULL, 0);
251 fprintf(stderr, "accept() failed\n");
255 n = read(connfd, msg, sizeof msg);
256 if (n != sizeof msg) {
257 perror("server read");
258 fprintf(stderr, "%d/%d: Couldn't read remote address\n", n, (int) sizeof msg);
262 rem_dest = malloc(sizeof *rem_dest);
266 sscanf(msg, "%x:%x:%x:%s", &rem_dest->lid, &rem_dest->qpn, &rem_dest->psn, gid);
267 wire_gid_to_gid(gid, &rem_dest->gid);
269 if (pp_connect_ctx(ctx, ib_port, my_dest->psn, sl, rem_dest, sgid_idx)) {
270 fprintf(stderr, "Couldn't connect to remote QP\n");
276 gid_to_wire_gid(&my_dest->gid, gid);
277 sprintf(msg, "%04x:%06x:%06x:%s", my_dest->lid, my_dest->qpn, my_dest->psn, gid);
278 if (write(connfd, msg, sizeof msg) != sizeof msg) {
279 fprintf(stderr, "Couldn't send local address\n");
285 read(connfd, msg, sizeof msg);
292 static struct pingpong_context *pp_init_ctx(struct ibv_device *ib_dev, int size,
293 int rx_depth, int port,
296 struct pingpong_context *ctx;
298 ctx = malloc(sizeof *ctx);
303 ctx->rx_depth = rx_depth;
305 ctx->buf = malloc(roundup(size + 40, page_size));
307 fprintf(stderr, "Couldn't allocate work buf.\n");
311 memset(ctx->buf, 0, size + 40);
313 ctx->context = ibv_open_device(ib_dev);
315 fprintf(stderr, "Couldn't get context for %s\n",
316 ibv_get_device_name(ib_dev));
321 ctx->channel = ibv_create_comp_channel(ctx->context);
323 fprintf(stderr, "Couldn't create completion channel\n");
329 ctx->pd = ibv_alloc_pd(ctx->context);
331 fprintf(stderr, "Couldn't allocate PD\n");
335 ctx->mr = ibv_reg_mr(ctx->pd, ctx->buf, size + 40, IBV_ACCESS_LOCAL_WRITE);
337 fprintf(stderr, "Couldn't register MR\n");
341 ctx->cq = ibv_create_cq(ctx->context, rx_depth + 1, NULL,
344 fprintf(stderr, "Couldn't create CQ\n");
349 struct ibv_qp_init_attr attr = {
354 .max_recv_wr = rx_depth,
358 .qp_type = IBV_QPT_UD,
361 ctx->qp = ibv_create_qp(ctx->pd, &attr);
363 fprintf(stderr, "Couldn't create QP\n");
369 struct ibv_qp_attr attr = {
370 .qp_state = IBV_QPS_INIT,
376 if (ibv_modify_qp(ctx->qp, &attr,
381 fprintf(stderr, "Failed to modify QP to INIT\n");
389 int pp_close_ctx(struct pingpong_context *ctx)
391 if (ibv_destroy_qp(ctx->qp)) {
392 fprintf(stderr, "Couldn't destroy QP\n");
396 if (ibv_destroy_cq(ctx->cq)) {
397 fprintf(stderr, "Couldn't destroy CQ\n");
401 if (ibv_dereg_mr(ctx->mr)) {
402 fprintf(stderr, "Couldn't deregister MR\n");
406 if (ibv_destroy_ah(ctx->ah)) {
407 fprintf(stderr, "Couldn't destroy AH\n");
411 if (ibv_dealloc_pd(ctx->pd)) {
412 fprintf(stderr, "Couldn't deallocate PD\n");
417 if (ibv_destroy_comp_channel(ctx->channel)) {
418 fprintf(stderr, "Couldn't destroy completion channel\n");
423 if (ibv_close_device(ctx->context)) {
424 fprintf(stderr, "Couldn't release context\n");
434 static int pp_post_recv(struct pingpong_context *ctx, int n)
436 struct ibv_sge list = {
437 .addr = (uintptr_t) ctx->buf,
438 .length = ctx->size + 40,
439 .lkey = ctx->mr->lkey
441 struct ibv_recv_wr wr = {
442 .wr_id = PINGPONG_RECV_WRID,
446 struct ibv_recv_wr *bad_wr;
449 for (i = 0; i < n; ++i)
450 if (ibv_post_recv(ctx->qp, &wr, &bad_wr))
456 static int pp_post_send(struct pingpong_context *ctx, uint32_t qpn)
458 struct ibv_sge list = {
459 .addr = (uintptr_t) ctx->buf + 40,
461 .lkey = ctx->mr->lkey
463 struct ibv_send_wr wr = {
464 .wr_id = PINGPONG_SEND_WRID,
467 .opcode = IBV_WR_SEND,
468 .send_flags = IBV_SEND_SIGNALED,
473 .remote_qkey = 0x11111111
477 struct ibv_send_wr *bad_wr;
479 return ibv_post_send(ctx->qp, &wr, &bad_wr);
482 static void usage(const char *argv0)
485 printf(" %s start a server and wait for connection\n", argv0);
486 printf(" %s <host> connect to server at <host>\n", argv0);
488 printf("Options:\n");
489 printf(" -p, --port=<port> listen on/connect to port <port> (default 18515)\n");
490 printf(" -d, --ib-dev=<dev> use IB device <dev> (default first device found)\n");
491 printf(" -i, --ib-port=<port> use port <port> of IB device (default 1)\n");
492 printf(" -s, --size=<size> size of message to exchange (default 1024)\n");
493 printf(" -r, --rx-depth=<dep> number of receives to post at a time (default 500)\n");
494 printf(" -n, --iters=<iters> number of exchanges (default 1000)\n");
495 printf(" -e, --events sleep on CQ events (default poll)\n");
496 printf(" -g, --gid-idx=<gid index> local port gid index\n");
499 int main(int argc, char *argv[])
501 struct ibv_device **dev_list;
502 struct ibv_device *ib_dev;
503 struct pingpong_context *ctx;
504 struct pingpong_dest my_dest;
505 struct pingpong_dest *rem_dest;
506 struct timeval start, end;
507 char *ib_devname = NULL;
508 char *servername = NULL;
517 int num_cq_events = 0;
522 srand48(getpid() * time(NULL));
527 static struct option long_options[] = {
528 { .name = "port", .has_arg = 1, .val = 'p' },
529 { .name = "ib-dev", .has_arg = 1, .val = 'd' },
530 { .name = "ib-port", .has_arg = 1, .val = 'i' },
531 { .name = "size", .has_arg = 1, .val = 's' },
532 { .name = "rx-depth", .has_arg = 1, .val = 'r' },
533 { .name = "iters", .has_arg = 1, .val = 'n' },
534 { .name = "sl", .has_arg = 1, .val = 'l' },
535 { .name = "events", .has_arg = 0, .val = 'e' },
536 { .name = "gid-idx", .has_arg = 1, .val = 'g' },
540 c = getopt_long(argc, argv, "p:d:i:s:r:n:l:eg:", long_options, NULL);
546 port = strtol(optarg, NULL, 0);
547 if (port < 0 || port > 65535) {
554 ib_devname = strdup(optarg);
558 ib_port = strtol(optarg, NULL, 0);
566 size = strtol(optarg, NULL, 0);
570 rx_depth = strtol(optarg, NULL, 0);
574 iters = strtol(optarg, NULL, 0);
578 sl = strtol(optarg, NULL, 0);
586 gidx = strtol(optarg, NULL, 0);
595 if (optind == argc - 1)
596 servername = strdup(argv[optind]);
597 else if (optind < argc) {
602 page_size = sysconf(_SC_PAGESIZE);
604 dev_list = ibv_get_device_list(NULL);
606 perror("Failed to get IB devices list");
613 fprintf(stderr, "No IB devices found\n");
618 for (i = 0; dev_list[i]; ++i)
619 if (!strcmp(ibv_get_device_name(dev_list[i]), ib_devname))
621 ib_dev = dev_list[i];
623 fprintf(stderr, "IB device %s not found\n", ib_devname);
628 ctx = pp_init_ctx(ib_dev, size, rx_depth, ib_port, use_event);
632 routs = pp_post_recv(ctx, ctx->rx_depth);
633 if (routs < ctx->rx_depth) {
634 fprintf(stderr, "Couldn't post receive (%d)\n", routs);
639 if (ibv_req_notify_cq(ctx->cq, 0)) {
640 fprintf(stderr, "Couldn't request CQ notification\n");
644 if (pp_get_port_info(ctx->context, ib_port, &ctx->portinfo)) {
645 fprintf(stderr, "Couldn't get port info\n");
648 my_dest.lid = ctx->portinfo.lid;
650 my_dest.qpn = ctx->qp->qp_num;
651 my_dest.psn = lrand48() & 0xffffff;
654 if (ibv_query_gid(ctx->context, ib_port, gidx, &my_dest.gid)) {
655 fprintf(stderr, "Could not get local gid for gid index %d\n", gidx);
659 memset(&my_dest.gid, 0, sizeof my_dest.gid);
661 inet_ntop(AF_INET6, &my_dest.gid, gid, sizeof gid);
662 printf(" local address: LID 0x%04x, QPN 0x%06x, PSN 0x%06x: GID %s\n",
663 my_dest.lid, my_dest.qpn, my_dest.psn, gid);
666 rem_dest = pp_client_exch_dest(servername, port, &my_dest);
668 rem_dest = pp_server_exch_dest(ctx, ib_port, port, sl, &my_dest, gidx);
673 inet_ntop(AF_INET6, &rem_dest->gid, gid, sizeof gid);
674 printf(" remote address: LID 0x%04x, QPN 0x%06x, PSN 0x%06x, GID %s\n",
675 rem_dest->lid, rem_dest->qpn, rem_dest->psn, gid);
678 if (pp_connect_ctx(ctx, ib_port, my_dest.psn, sl, rem_dest, gidx))
681 ctx->pending = PINGPONG_RECV_WRID;
684 if (pp_post_send(ctx, rem_dest->qpn)) {
685 fprintf(stderr, "Couldn't post send\n");
688 ctx->pending |= PINGPONG_SEND_WRID;
691 if (gettimeofday(&start, NULL)) {
692 perror("gettimeofday");
697 while (rcnt < iters || scnt < iters) {
699 struct ibv_cq *ev_cq;
702 if (ibv_get_cq_event(ctx->channel, &ev_cq, &ev_ctx)) {
703 fprintf(stderr, "Failed to get cq_event\n");
709 if (ev_cq != ctx->cq) {
710 fprintf(stderr, "CQ event for unknown CQ %p\n", ev_cq);
714 if (ibv_req_notify_cq(ctx->cq, 0)) {
715 fprintf(stderr, "Couldn't request CQ notification\n");
725 ne = ibv_poll_cq(ctx->cq, 2, wc);
727 fprintf(stderr, "poll CQ failed %d\n", ne);
730 } while (!use_event && ne < 1);
732 for (i = 0; i < ne; ++i) {
733 if (wc[i].status != IBV_WC_SUCCESS) {
734 fprintf(stderr, "Failed status %s (%d) for wr_id %d\n",
735 ibv_wc_status_str(wc[i].status),
736 wc[i].status, (int) wc[i].wr_id);
740 switch ((int) wc[i].wr_id) {
741 case PINGPONG_SEND_WRID:
745 case PINGPONG_RECV_WRID:
747 routs += pp_post_recv(ctx, ctx->rx_depth - routs);
748 if (routs < ctx->rx_depth) {
750 "Couldn't post receive (%d)\n",
760 fprintf(stderr, "Completion for unknown wr_id %d\n",
765 ctx->pending &= ~(int) wc[i].wr_id;
766 if (scnt < iters && !ctx->pending) {
767 if (pp_post_send(ctx, rem_dest->qpn)) {
768 fprintf(stderr, "Couldn't post send\n");
771 ctx->pending = PINGPONG_RECV_WRID |
778 if (gettimeofday(&end, NULL)) {
779 perror("gettimeofday");
784 float usec = (end.tv_sec - start.tv_sec) * 1000000 +
785 (end.tv_usec - start.tv_usec);
786 long long bytes = (long long) size * iters * 2;
788 printf("%lld bytes in %.2f seconds = %.2f Mbit/sec\n",
789 bytes, usec / 1000000., bytes * 8. / usec);
790 printf("%d iters in %.2f seconds = %.2f usec/iter\n",
791 iters, usec / 1000000., usec / iters);
794 ibv_ack_cq_events(ctx->cq, num_cq_events);
796 if (pp_close_ctx(ctx))
799 ibv_free_device_list(dev_list);