1.
Low Latency Technology 2012 Fall 행사중 RDMA는 많이 아쉬운 행사입니다. 해외에서 개발자를 초청했지만 개발자가 준비했던 만큼 충실히 전달하지 못했기때문입니다. 물론 동시통역을 하면 좀 다른 모습을 보였을 듯 합니다만 RDMA와 관련한 전문통역자가 있을리 만무하고 예산문제도 있었서 포기했습니다.
최초 4시간을 기준으로 만들었던 프로그램입니다. 말이 통하지 않는 사람들이 모여서 강의를 하려니까 시간관리를 잘 하지 못하고 일찍 마무리하였습니다. 샘플코드를 설명하는 부분도 예상과 달랐습니다. pair programming을 하는 방식으로 강의를 할 줄 알았습니다.(^^) 그런데 아니더군요. 행사 당일 부족했던 부분을 문서로 채우려고 합니다. 개발자들의 언어는 영어도 아니고 한글도 아니고 C/C++이기때문에 프로그래밍 언어가 발표자의 생각을 더 잘 전달하리라 생각합니다.
총 18개의 샘플입니다. 참석자들에게 배포하기 위하여 하나의 문서로 만드니까 약 500쪽이 나옵니다. 작지 않은 분량입니다. 더불어 Infiniband를 이용한 RDMA 프로그래밍을 아래를 참고하시길 바랍니다. 좀 오랜 자료입니다.
또한 iWARP환경을 구축하기 어려울 경우 Soft-iWARP환경을 구축하여 개발환경을 구축하는 것도 방법입니다.
2.
행사 당일 발표하였던 자료를 원하시면 아래 메일을 이용하여 요청을 해주시길 바랍니다. 반드시 소속 및 하시는 일을 적어주셔야 보내드립니다. 이점 잊지 마시길 바랍니다.
아래는 행사중 소개하였던 예제중 일부입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 |
//1 “server connect with connection management abstraction call” //2 “server connect with connection management abstraction function” //3 “create event channel” //4 “create connection manager identifier” //5 “initialize connection manager abstraction context call” //6 “initialize connection management abstraction context function” //7 “allocate protection domain” //8 “allocate memory region” //9 “create completion channel” //10 “create receive completion queue” //11 “create send completion queue” //12 “create queue pair” //13 “post receive” //14 “rdma accept” //15 “iwarp verbs rdma send work request” //16 “iwarp verbs poll completion queue” //17 “iwarp verbs post send” //18 “iwarp verbs post receive” //19 “iwarp verbs poll completion queue” //20 “finish test server” /* * Copyright (c) 2005 Topspin Communications. All rights reserved. * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved. * Copyright (c) 2005 Hewlett Packard, Inc (Grant Grundler) * Copyright (c) 2007 Chelsio Communications, Inc. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU * General Public License (GPL) Version 2, available from the file * COPYING in the main directory of this source tree, or the * OpenIB.org BSD license below: * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: * * - Redistributions of source code must retain the above * copyright notice, this list of conditions and the following * disclaimer. * * - Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials * provided with the distribution. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * $Id$ */ #if HAVE_CONFIG_H # include <config.h> #endif /* HAVE_CONFIG_H */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/time.h> #include <netdb.h> #include <malloc.h> #include <getopt.h> #include <arpa/inet.h> #include <byteswap.h> #include <time.h> #include <signal.h> #include <infiniband/verbs.h> #include <rdma/rdma_cma.h> #include "get_clock.h" #define PINGPONG_RDMA_WRID 3 static int page_size; static int done; static int verbose; static int myserver; struct report_options { int unsorted; int histogram; int cycles; /* report delta's in cycles, not microsec's */ }; struct pingpong_context { struct ibv_context *context; struct ibv_pd *pd; struct ibv_mr *mr; struct ibv_cq *scq; struct ibv_cq *rcq; struct ibv_qp *qp; struct ibv_comp_channel *ch; struct rdma_event_channel *rch; void *buf; volatile char *post_buf; volatile char *poll_buf; int size; int tx_depth; struct ibv_sge list; struct ibv_send_wr wr; struct rdma_cm_id *cm_id; }; struct pingpong_dest { int lid; int qpn; int psn; unsigned rkey; unsigned long long vaddr; }; static void pp_post_recv(struct pingpong_context *ctx, int size, int count) { struct ibv_sge list; struct ibv_recv_wr wr, *bad_wr; int rc; list.addr = (uintptr_t) ctx->buf; list.length = size; list.lkey = ctx->mr->lkey; wr.next = NULL; wr.wr_id = 0xdead0001; wr.sg_list = &list; wr.num_sge = 1; while (!done && count--) { wr.wr_id++; rc = ibv_post_recv(ctx->qp, &wr, &bad_wr); if (rc) { fprintf(stderr, "%s ibv_post_recv failed %d\n", __FUNCTION__, rc); } } } static struct pingpong_context *pp_create_cma_ctx() { struct pingpong_context *ctx; ctx = malloc(sizeof *ctx); if (!ctx) return NULL; return ctx; } static struct pingpong_context *pp_init_cma_ctx(struct pingpong_context *ctx, struct rdma_cm_id *cm_id, unsigned size, int tx_depth, int port) //comment6 “initialize connection management abstraction context function” { ctx->size = size; ctx->tx_depth = tx_depth; ctx->buf = memalign(page_size, size * 2); if (!ctx->buf) { fprintf(stderr, "Couldn't allocate work buf.\n"); return NULL; } memset(ctx->buf, 0, size * 2); ctx->post_buf = (char*)ctx->buf + (size - 1); ctx->poll_buf = (char*)ctx->buf + (2 * size - 1); ctx->cm_id = cm_id; ctx->context = cm_id->verbs; if (!ctx->context) { fprintf(stderr, "%s Unbound cm_id!!\n", __FUNCTION__); return NULL; } ctx->pd = ibv_alloc_pd(ctx->context); //comment7 “allocate protection domain” if (!ctx->pd) { fprintf(stderr, "Couldn't allocate PD\n"); return NULL; } /* We dont really want IBV_ACCESS_LOCAL_WRITE, but IB spec says: * The Consumer is not allowed to assign Remote Write or Remote Atomic to * a Memory Region that has not been assigned Local Write. */ ctx->mr = ibv_reg_mr(ctx->pd, ctx->buf, size * 2, IBV_ACCESS_REMOTE_WRITE | IBV_ACCESS_LOCAL_WRITE); //comment8 “allocate memory region” if (!ctx->mr) { fprintf(stderr, "Couldn't allocate MR\n"); return NULL; } ctx->ch = ibv_create_comp_channel(ctx->context); //comment9 “create completion channel” if (!ctx->ch) { fprintf(stderr, "Couldn't create comp channel\n"); return NULL; } ctx->rcq = ibv_create_cq(ctx->context, tx_depth, ctx, ctx->ch, 0); //comment10 “create receive completion queue” if (!ctx->rcq) { fprintf(stderr, "Couldn't create RCQ\n"); return NULL; } ctx->scq = ibv_create_cq(ctx->context, tx_depth, ctx, ctx->ch, 0); //comment11 “create send completion queue” if (!ctx->scq) { fprintf(stderr, "Couldn't create SCQ\n"); return NULL; } { struct ibv_qp_init_attr attr = { .send_cq = ctx->scq, .recv_cq = ctx->rcq, .cap = { .max_send_wr = tx_depth, .max_recv_wr = tx_depth*2, .max_send_sge = 1, .max_recv_sge = 1, .max_inline_data = 64, }, .qp_type = IBV_QPT_RC }; if (rdma_create_qp(ctx->cm_id, ctx->pd, &attr)) {//comment12 “create queue pair” fprintf(stderr, "Couldn't create QP\n"); return NULL; } ctx->qp = ctx->cm_id->qp; } return ctx; } static struct pingpong_context *pp_server_connect_cma(unsigned short port, int size, int tx_depth, struct pingpong_dest *my_dest, struct pingpong_dest *rem_dest) //comment2 “server connect with connection management abstraction function” { struct rdma_cm_id *listen_id; struct rdma_cm_event *event; struct rdma_conn_param conn_param; int ret; struct sockaddr_in sin; struct rdma_cm_id *child_cm_id; struct pingpong_context *ctx; ctx = pp_create_cma_ctx(); if (!ctx) { fprintf(stderr, "Couldn't create cma context\n"); return NULL; } ctx->rch = rdma_create_event_channel();//comment3 “create event channel” if (!ctx->rch) { fprintf(stderr, "Couldn't create RDMA event channel\n"); return NULL; } ret = rdma_create_id(ctx->rch, &listen_id, NULL, RDMA_PS_TCP); //comment4 “create connection manager identifier” if (ret) { fprintf(stderr, "%s rdma_create_id failed %d\n", __FUNCTION__, ret); return NULL; } sin.sin_addr.s_addr = 0; sin.sin_family = PF_INET; sin.sin_port = htons(port); ret = rdma_bind_addr(listen_id, (struct sockaddr *)&sin); if (ret) { fprintf(stderr,"%s rdma_bind_addr failed %d\n", __FUNCTION__, ret); goto err2; } ret = rdma_listen(listen_id, 0); if (ret) { fprintf(stderr,"%s rdma_listen failed %d\n", __FUNCTION__, ret); goto err2; } if (myserver) { printf("SERVER READY"); fflush(stdout); } ret = rdma_get_cm_event(ctx->rch, &event); if (ret) goto err2; if (event->event != RDMA_CM_EVENT_CONNECT_REQUEST) { fprintf(stderr,"%s bad event waiting for connect request %d\n", __FUNCTION__, event->event); goto err1; } if (!event->param.conn.private_data || (event->param.conn.private_data_len < sizeof(*rem_dest))) { ret = 1; fprintf(stderr,"%s bad private data len %d\n", __FUNCTION__, event->param.conn.private_data_len); goto err1; } #if 0 if (event->param.conn.private_data_len != sizeof(*rem_dest)) { fprintf(stderr, "%s - warning: private data is %d bytes, expected %d bytes\n", __FUNCTION__, event->param.conn.private_data_len, (int)sizeof(*rem_dest)); } #endif memcpy(rem_dest, event->param.conn.private_data, sizeof(*rem_dest)); child_cm_id = (struct rdma_cm_id *)event->id; ctx = pp_init_cma_ctx(ctx, child_cm_id, size, tx_depth, port); //comment5 “initialize connection manager abstraction context call” if (!ctx) { fprintf(stderr,"%s pp_init_cma_ctx failed\n", __FUNCTION__); goto err0; } my_dest->qpn = 0; my_dest->psn = 0xbb; my_dest->rkey = ctx->mr->rkey; my_dest->vaddr = (uintptr_t)ctx->buf + ctx->size; memset(&conn_param, 0, sizeof conn_param); conn_param.responder_resources = 1; conn_param.initiator_depth = 1; conn_param.private_data = my_dest; conn_param.private_data_len = sizeof(*my_dest); pp_post_recv(ctx, size, tx_depth); //comment13 “post receive” ret = rdma_accept(ctx->cm_id, &conn_param); //comment14 “rdma accept” if (ret) { fprintf(stderr,"%s rdma_accept failed %d\n", __FUNCTION__, ret); goto err0; } rdma_ack_cm_event(event); ret = rdma_get_cm_event(ctx->rch, &event); if (ret) { fprintf(stderr,"rdma_get_cm_event error %d\n", ret); rdma_destroy_id(child_cm_id); goto err2; } if (event->event != RDMA_CM_EVENT_ESTABLISHED) { fprintf(stderr,"%s bad event waiting for established %d\n", __FUNCTION__, event->event); goto err0; } rdma_ack_cm_event(event); return ctx; err0: rdma_destroy_id(child_cm_id); err1: rdma_ack_cm_event(event); err2: rdma_destroy_id(listen_id); fprintf(stderr,"%s NOT connected!\n", __FUNCTION__); return NULL; } static unsigned get_dst_addr(const char *dst) { struct addrinfo *res; int ret; unsigned addr; ret = getaddrinfo(dst, NULL, NULL, &res); if (ret) { fprintf(stderr, "%s getaddrinfo failed - invalid hostname or IP address\n", __FUNCTION__); return 0; } if (res->ai_family != PF_INET) { return 0; } addr = ((struct sockaddr_in*)res->ai_addr)->sin_addr.s_addr; freeaddrinfo(res); return addr; } static struct pingpong_context *pp_client_connect_cma(const char *servername, unsigned short port, int size, int tx_depth, struct pingpong_dest *my_dest, struct pingpong_dest *rem_dest) { struct rdma_cm_event *event; struct rdma_conn_param conn_param; int ret; struct sockaddr_in sin; struct rdma_cm_id *cm_id; struct pingpong_context *ctx; sin.sin_addr.s_addr = get_dst_addr(servername); if (!sin.sin_addr.s_addr) { return NULL; } ctx = pp_create_cma_ctx(); if (!ctx) { fprintf(stderr, "Couldn't create cma context\n"); return NULL; } ctx->rch = rdma_create_event_channel(); if (!ctx->rch) { fprintf(stderr, "Couldn't create RDMA event channel\n"); return NULL; } ret = rdma_create_id(ctx->rch, &cm_id, NULL, RDMA_PS_TCP); if (ret) { fprintf(stderr,"%s rdma_create_id failed %d\n", __FUNCTION__, ret); return NULL; } sin.sin_family = PF_INET; sin.sin_port = htons(port); ret = rdma_resolve_addr(cm_id, NULL, (struct sockaddr *)&sin, 2000); if (ret) { fprintf(stderr,"%s rdma_resolve_addr failed %d\n", __FUNCTION__, ret); goto err2; } ret = rdma_get_cm_event(ctx->rch, &event); if (ret) goto err2; if (event->event != RDMA_CM_EVENT_ADDR_RESOLVED) { fprintf(stderr,"%s/%d unexpected CM event %d\n", __FUNCTION__, __LINE__, event->event); goto err1; } rdma_ack_cm_event(event); ret = rdma_resolve_route(cm_id, 2000); if (ret) { fprintf(stderr,"%s rdma_resolve_route failed %d\n", __FUNCTION__, ret); goto err2; } ret = rdma_get_cm_event(ctx->rch, &event); if (ret) goto err2; if (event->event != RDMA_CM_EVENT_ROUTE_RESOLVED) { fprintf(stderr,"%s/%d unexpected CM event %d\n", __FUNCTION__, __LINE__, event->event); goto err1; } rdma_ack_cm_event(event); ctx = pp_init_cma_ctx(ctx, cm_id, size, tx_depth, port); if (!ctx) { fprintf(stderr,"%s pp_init_cma_ctx failed\n", __FUNCTION__); goto err2; } my_dest->qpn = 0; my_dest->psn = 0xaa; my_dest->rkey = ctx->mr->rkey; my_dest->vaddr = (uintptr_t)ctx->buf + ctx->size; memset(&conn_param, 0, sizeof conn_param); conn_param.responder_resources = 1; conn_param.initiator_depth = 1; conn_param.retry_count = 5; conn_param.private_data = my_dest; conn_param.private_data_len = sizeof(*my_dest); pp_post_recv(ctx, size, tx_depth*2); ret = rdma_connect(ctx->cm_id, &conn_param); if (ret) { fprintf(stderr,"%s rdma_connect failure %d\n", __FUNCTION__, ret); goto err2; } ret = rdma_get_cm_event(ctx->rch, &event); if (ret) goto err2; if (event->event != RDMA_CM_EVENT_ESTABLISHED) { fprintf(stderr,"%s/%d unexpected CM event %d\n", __FUNCTION__, __LINE__, event->event); goto err1; } if (!event->param.conn.private_data || (event->param.conn.private_data_len < sizeof(*rem_dest))) { fprintf(stderr,"%s bad private data ptr %p len %d\n", __FUNCTION__, event->param.conn.private_data, event->param.conn.private_data_len); goto err1; } #if 0 if (event->param.conn.private_data_len != sizeof(*rem_dest)) { fprintf(stderr, "%s - warning: private data is %d bytes, expected %d bytes\n", __FUNCTION__, event->param.conn.private_data_len, (int)sizeof(*rem_dest)); } #endif memcpy(rem_dest, event->param.conn.private_data, sizeof(*rem_dest)); rdma_ack_cm_event(event); return ctx; err1: rdma_ack_cm_event(event); err2: fprintf(stderr,"NOT connected!\n"); rdma_destroy_id(cm_id); return NULL; } static void usage(const char *argv0) { printf("Usage:\n"); printf(" %s start a server and wait for connection\n", argv0); printf(" %s <host> connect to server at <host>\n", argv0); printf("\n"); printf("Options:\n"); printf(" -p, --port=<port> listen on/connect to port <port> (default 18515)\n"); printf(" -s, --size=<size> size of message to exchange (default 1)\n"); printf(" -t, --tx-depth=<dep> size of tx queue (default 1)\n"); printf(" -P, --poll Spin polling for completions (default is blocking)\n"); printf(" -I, --inline Use IBV_SEND_INLINE (default no inline)\n"); printf(" -T, --time=<secs> Run test for <secs> seconds (default is 10)\n"); printf(" -v, --verbose be verbose.\n"); printf(" -A, --abortmode abort instead of graceful close\n"); printf(" -n, --nortr server starts test (no rtr)\n"); } static void print_report(int size, int iters, struct timeval *start_time, struct timeval *stop_time) { time_t sec; suseconds_t usec; float elapsed; double bytes_sent = 2 * size * iters; double throughput; if (stop_time->tv_usec < start_time->tv_usec) { stop_time->tv_usec += 1000000; stop_time->tv_sec -= 1; } sec = stop_time->tv_sec - start_time->tv_sec; usec = stop_time->tv_usec - start_time->tv_usec; elapsed = (float)sec + ((float)usec/(float)1000000.0); throughput = iters / elapsed; printf("Bytes %g elapsed sec %d usec %d\n", bytes_sent, (unsigned)sec, (unsigned)usec); printf("Average transfer rate: %g messages/sec\n", throughput); printf("RTT/2 = %g us\n", (double)1 / throughput / (double)2 * (double)(1000 * 1000)); } int abortmode; void sig_handler(int foo) { done = 1; if (verbose) printf("Alarm\n"); if (abortmode) exit(1); } int main(int argc, char *argv[]) { int port = 18515; int size = 1; int tx_depth = 1; struct pingpong_context *ctx; char *servername = NULL; struct pingpong_dest rem_dest; struct ibv_qp *qp; struct ibv_send_wr *wr; int scnt, rcnt, ccnt; struct timeval start_time, stop_time; struct timezone tz; int poll = 0; struct ibv_cq *ev_cq; void *ev_ctx; int send_inline = 0; int run_time = 10; struct ibv_wc wc; int ne; int zerobyte = 0; struct ibv_send_wr *bad_wr; int count; int peer2peer = 0; /* Parameter parsing. */ while (1) { int c; static struct option long_options[] = { { .name = "port", .has_arg = 1, .val = 'p' }, { .name = "size", .has_arg = 1, .val = 's' }, { .name = "tx-depth", .has_arg = 1, .val = 't' }, { .name = "poll", .has_arg = 0, .val = 'P' }, { .name = "inline", .has_arg = 0, .val = 'I' }, { .name = "time", .has_arg = 1, .val = 'T' }, { .name = "verbose", .has_arg = 0, .val = 'v' }, { .name = "myserver", .has_arg = 0, .val = 'm' }, { .name = "abortmode", .has_arg = 0, .val = 'A' }, { .name = "nortr", .has_arg = 0, .val = 'n' }, { 0 } }; c = getopt_long(argc, argv, "nAp:s:t:T:PIv", long_options, NULL); if (c == -1) break; switch (c) { case 'n': peer2peer = 1; break; case 'A': abortmode = 1; break; case 'm': myserver = 1; break; case 'v': verbose = 1; break; case 'I': send_inline = IBV_SEND_INLINE; break; case 'P': poll = 1; break; case 'p': port = strtol(optarg, NULL, 0); if (port < 0 || port > 65535) { usage(argv[0]); return 1; } break; case 's': size = strtol(optarg, NULL, 0); if (size < 0) { usage(argv[0]); return 3; } if (size == 0) { zerobyte=1; size = 1; /* so we alloc memory ok */ } break; case 't': tx_depth = strtol(optarg, NULL, 0); if (tx_depth < 1) { usage(argv[0]); return 4; } break; case 'T': run_time = strtol(optarg, NULL, 0); if (run_time < 0) { usage(argv[0]); return 4; } break; default: usage(argv[0]); return 5; } } if (optind == argc - 1) servername = strdupa(argv[optind]); else if (optind < argc) { usage(argv[0]); return 6; } /* * Done with parameter parsing. Perform setup. */ srand48(getpid() * time(NULL)); page_size = sysconf(_SC_PAGESIZE); struct pingpong_dest my_dest; memset(&rem_dest, 0, sizeof(rem_dest)); if (servername) ctx = pp_client_connect_cma(servername, port, size, tx_depth, &my_dest, &rem_dest); else ctx = pp_server_connect_cma(port, size, tx_depth, &my_dest, &rem_dest); //comment1 “server connect with connection management abstraction call” if (!ctx) { fprintf(stderr, "pp_connect_cma(%s,%d) failed!\n", servername, port); return 7; } if (verbose) { printf(" local address: PSN %#06x RKey %#08x VAddr %#016Lx\n", my_dest.psn, my_dest.rkey, my_dest.vaddr); printf(" remote address: PSN %#06x RKey %#08x VAddr %#016Lx\n", rem_dest.psn, rem_dest.rkey, rem_dest.vaddr); } wr = &ctx->wr; ctx->list.addr = (uintptr_t)(ctx->buf + size); if (zerobyte) ctx->list.length = 0; else ctx->list.length = ctx->size; ctx->list.lkey = ctx->mr->lkey; wr->wr_id = PINGPONG_RDMA_WRID; wr->sg_list = &ctx->list; wr->num_sge = 1; wr->opcode = IBV_WR_SEND; //comment15 “iwarp verbs rdma send work request” wr->send_flags = IBV_SEND_SIGNALED; if (ctx->size <= 64) wr->send_flags |= send_inline; wr->next = NULL; scnt = 0; rcnt = 0; ccnt = 0; qp = ctx->qp; if (!poll && ibv_req_notify_cq(ctx->scq, 0)) { fprintf(stderr, "ibv_req_notify failed!\n"); return 1; } if (!poll && ibv_req_notify_cq(ctx->rcq, 0)) { fprintf(stderr, "ibv_req_notify failed!\n"); return 1; } /* Done with setup. Start the test. */ signal(SIGALRM, sig_handler); alarm(run_time); gettimeofday(&start_time, &tz); while (!done) { if (scnt >= 1 || (peer2peer && !servername) || (!peer2peer && servername)) { int count = tx_depth; /* Wait for recv completions */ while (!done && count--) { do { ne = ibv_poll_cq(ctx->rcq, 1, &wc); //comment16 “iwarp verbs poll completion queue” if (!poll && ne == 0) { if (ibv_get_cq_event(ctx->ch, &ev_cq, &ev_ctx)) { fprintf(stderr, "Failed to get cq event!\n"); return 1; } if (ev_cq != ctx->rcq) { fprintf(stderr, "Unkown CQ!\n"); return 1; } ibv_ack_cq_events(ctx->rcq, 1); ibv_req_notify_cq(ctx->rcq, 0); } if (done) break; } while (ne == 0); if (ne < 0) { fprintf(stderr, "poll CQ failed %d\n", ne); break; } if (wc.status != IBV_WC_SUCCESS) { fprintf(stderr, "Completion wth error at %s:\n", servername ? "client" : "server"); fprintf(stderr, "Failed status %d: wr_id %d\n", wc.status, (int) wc.wr_id); fprintf(stderr, "scnt=%d, rcnt=%d, ccnt=%d\n", scnt, rcnt, ccnt); break; } if (done) break; } } count = tx_depth; while (!done && count--) { if (ibv_post_send(qp, wr, &bad_wr)) {//comment17 “iwarp verbs post send” fprintf(stderr, "Couldn't post send: scnt=%d\n", scnt); break; } scnt++; pp_post_recv(ctx, size, 1); //comment18 “iwarp verbs post receive” ++rcnt; } while (!done && ccnt < scnt) { do { ne = ibv_poll_cq(ctx->scq, 1, &wc); //comment19 “iwarp verbs poll completion queue” if (ne < 0) { fprintf(stderr, "poll CQ failed %d\n", ne); break; } } while (ne == 0); if (ne == 1) { if (wc.status != IBV_WC_SUCCESS) { fprintf(stderr, "Completion wth error at %s:\n", servername ? "client" : "server"); fprintf(stderr, "Failed status %d: wr_id %d\n", wc.status, (int) wc.wr_id); fprintf(stderr, "scnt=%d, rcnt=%d, ccnt=%d\n", scnt, rcnt, ccnt); break; } ++ccnt; } } } gettimeofday(&stop_time, &tz); //comment20 “finish test server” if (servername) { if (verbose) printf("scnt %u ccnt %u rcnt %u\n", scnt, ccnt, rcnt); print_report(size, ccnt, &start_time, &stop_time); if (myserver) { char *buf; size_t buflen; while (getline(&buf, &buflen, stdin) != -1) printf("server: %s", buf); } } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 |
//1 “client connect with connection management abstraction call” //2 “client connect with connection management abstraction function” //3 “create event channel” //4 “create connection manager identifier” //5 “initialize connection manager abstraction context call” //6 “initialize connection management abstraction context function” //7 “allocate protection domain” //8 “allocate memory region” //9 “create completion channel” //10 “create receive completion queue” //11 “create send completion queue” //12 “create queue pair” //13 “post receive” //14 “rdma connect” //15 “iwarp verbs rdma send work request” //16 “iwarp verbs poll completion queue” //17 “iwarp verbs post send” //18 “iwarp verbs post receive” //19 “iwarp verbs poll completion queue” //20 “finish test client” /* * Copyright (c) 2005 Topspin Communications. All rights reserved. * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved. * Copyright (c) 2005 Hewlett Packard, Inc (Grant Grundler) * Copyright (c) 2007 Chelsio Communications, Inc. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU * General Public License (GPL) Version 2, available from the file * COPYING in the main directory of this source tree, or the * OpenIB.org BSD license below: * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: * * - Redistributions of source code must retain the above * copyright notice, this list of conditions and the following * disclaimer. * * - Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials * provided with the distribution. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * * $Id$ */ #if HAVE_CONFIG_H # include <config.h> #endif /* HAVE_CONFIG_H */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/time.h> #include <netdb.h> #include <malloc.h> #include <getopt.h> #include <arpa/inet.h> #include <byteswap.h> #include <time.h> #include <signal.h> #include <infiniband/verbs.h> #include <rdma/rdma_cma.h> #include "get_clock.h" #define PINGPONG_RDMA_WRID 3 static int page_size; static int done; static int verbose; static int myserver; struct report_options { int unsorted; int histogram; int cycles; /* report delta's in cycles, not microsec's */ }; struct pingpong_context { struct ibv_context *context; struct ibv_pd *pd; struct ibv_mr *mr; struct ibv_cq *scq; struct ibv_cq *rcq; struct ibv_qp *qp; struct ibv_comp_channel *ch; struct rdma_event_channel *rch; void *buf; volatile char *post_buf; volatile char *poll_buf; int size; int tx_depth; struct ibv_sge list; struct ibv_send_wr wr; struct rdma_cm_id *cm_id; }; struct pingpong_dest { int lid; int qpn; int psn; unsigned rkey; unsigned long long vaddr; }; static void pp_post_recv(struct pingpong_context *ctx, int size, int count) { struct ibv_sge list; struct ibv_recv_wr wr, *bad_wr; int rc; list.addr = (uintptr_t) ctx->buf; list.length = size; list.lkey = ctx->mr->lkey; wr.next = NULL; wr.wr_id = 0xdead0001; wr.sg_list = &list; wr.num_sge = 1; while (!done && count--) { wr.wr_id++; rc = ibv_post_recv(ctx->qp, &wr, &bad_wr); if (rc) { fprintf(stderr, "%s ibv_post_recv failed %d\n", __FUNCTION__, rc); } } } static struct pingpong_context *pp_create_cma_ctx() { struct pingpong_context *ctx; ctx = malloc(sizeof *ctx); if (!ctx) return NULL; return ctx; } static struct pingpong_context *pp_init_cma_ctx(struct pingpong_context *ctx, struct rdma_cm_id *cm_id, unsigned size, int tx_depth, int port) //comment6 “initialize connection management abstraction context function” { ctx->size = size; ctx->tx_depth = tx_depth; ctx->buf = memalign(page_size, size * 2); if (!ctx->buf) { fprintf(stderr, "Couldn't allocate work buf.\n"); return NULL; } memset(ctx->buf, 0, size * 2); ctx->post_buf = (char*)ctx->buf + (size - 1); ctx->poll_buf = (char*)ctx->buf + (2 * size - 1); ctx->cm_id = cm_id; ctx->context = cm_id->verbs; if (!ctx->context) { fprintf(stderr, "%s Unbound cm_id!!\n", __FUNCTION__); return NULL; } ctx->pd = ibv_alloc_pd(ctx->context); //comment7 “allocate protection domain” if (!ctx->pd) { fprintf(stderr, "Couldn't allocate PD\n"); return NULL; } /* We dont really want IBV_ACCESS_LOCAL_WRITE, but IB spec says: * The Consumer is not allowed to assign Remote Write or Remote Atomic to * a Memory Region that has not been assigned Local Write. */ ctx->mr = ibv_reg_mr(ctx->pd, ctx->buf, size * 2, IBV_ACCESS_REMOTE_WRITE | IBV_ACCESS_LOCAL_WRITE); //comment8 “allocate memory region” if (!ctx->mr) { fprintf(stderr, "Couldn't allocate MR\n"); return NULL; } ctx->ch = ibv_create_comp_channel(ctx->context); //comment9 “create completion channel” if (!ctx->ch) { fprintf(stderr, "Couldn't create comp channel\n"); return NULL; } ctx->rcq = ibv_create_cq(ctx->context, tx_depth, ctx, ctx->ch, 0); //comment10 “create receive completion queue” if (!ctx->rcq) { fprintf(stderr, "Couldn't create RCQ\n"); return NULL; } ctx->scq = ibv_create_cq(ctx->context, tx_depth, ctx, ctx->ch, 0); //comment11 “create send completion queue” if (!ctx->scq) { fprintf(stderr, "Couldn't create SCQ\n"); return NULL; } { struct ibv_qp_init_attr attr = { .send_cq = ctx->scq, .recv_cq = ctx->rcq, .cap = { .max_send_wr = tx_depth, .max_recv_wr = tx_depth*2, .max_send_sge = 1, .max_recv_sge = 1, .max_inline_data = 64, }, .qp_type = IBV_QPT_RC }; if (rdma_create_qp(ctx->cm_id, ctx->pd, &attr)) {//comment12 “create queue pair” fprintf(stderr, "Couldn't create QP\n"); return NULL; } ctx->qp = ctx->cm_id->qp; } return ctx; } static struct pingpong_context *pp_server_connect_cma(unsigned short port, int size, int tx_depth, struct pingpong_dest *my_dest, struct pingpong_dest *rem_dest) { struct rdma_cm_id *listen_id; struct rdma_cm_event *event; struct rdma_conn_param conn_param; int ret; struct sockaddr_in sin; struct rdma_cm_id *child_cm_id; struct pingpong_context *ctx; ctx = pp_create_cma_ctx(); if (!ctx) { fprintf(stderr, "Couldn't create cma context\n"); return NULL; } ctx->rch = rdma_create_event_channel(); if (!ctx->rch) { fprintf(stderr, "Couldn't create RDMA event channel\n"); return NULL; } ret = rdma_create_id(ctx->rch, &listen_id, NULL, RDMA_PS_TCP); if (ret) { fprintf(stderr, "%s rdma_create_id failed %d\n", __FUNCTION__, ret); return NULL; } sin.sin_addr.s_addr = 0; sin.sin_family = PF_INET; sin.sin_port = htons(port); ret = rdma_bind_addr(listen_id, (struct sockaddr *)&sin); if (ret) { fprintf(stderr,"%s rdma_bind_addr failed %d\n", __FUNCTION__, ret); goto err2; } ret = rdma_listen(listen_id, 0); if (ret) { fprintf(stderr,"%s rdma_listen failed %d\n", __FUNCTION__, ret); goto err2; } if (myserver) { printf("SERVER READY"); fflush(stdout); } ret = rdma_get_cm_event(ctx->rch, &event); if (ret) goto err2; if (event->event != RDMA_CM_EVENT_CONNECT_REQUEST) { fprintf(stderr,"%s bad event waiting for connect request %d\n", __FUNCTION__, event->event); goto err1; } if (!event->param.conn.private_data || (event->param.conn.private_data_len < sizeof(*rem_dest))) { ret = 1; fprintf(stderr,"%s bad private data len %d\n", __FUNCTION__, event->param.conn.private_data_len); goto err1; } #if 0 if (event->param.conn.private_data_len != sizeof(*rem_dest)) { fprintf(stderr, "%s - warning: private data is %d bytes, expected %d bytes\n", __FUNCTION__, event->param.conn.private_data_len, (int)sizeof(*rem_dest)); } #endif memcpy(rem_dest, event->param.conn.private_data, sizeof(*rem_dest)); child_cm_id = (struct rdma_cm_id *)event->id; ctx = pp_init_cma_ctx(ctx, child_cm_id, size, tx_depth, port); if (!ctx) { fprintf(stderr,"%s pp_init_cma_ctx failed\n", __FUNCTION__); goto err0; } my_dest->qpn = 0; my_dest->psn = 0xbb; my_dest->rkey = ctx->mr->rkey; my_dest->vaddr = (uintptr_t)ctx->buf + ctx->size; memset(&conn_param, 0, sizeof conn_param); conn_param.responder_resources = 1; conn_param.initiator_depth = 1; conn_param.private_data = my_dest; conn_param.private_data_len = sizeof(*my_dest); pp_post_recv(ctx, size, tx_depth); ret = rdma_accept(ctx->cm_id, &conn_param); if (ret) { fprintf(stderr,"%s rdma_accept failed %d\n", __FUNCTION__, ret); goto err0; } rdma_ack_cm_event(event); ret = rdma_get_cm_event(ctx->rch, &event); if (ret) { fprintf(stderr,"rdma_get_cm_event error %d\n", ret); rdma_destroy_id(child_cm_id); goto err2; } if (event->event != RDMA_CM_EVENT_ESTABLISHED) { fprintf(stderr,"%s bad event waiting for established %d\n", __FUNCTION__, event->event); goto err0; } rdma_ack_cm_event(event); return ctx; err0: rdma_destroy_id(child_cm_id); err1: rdma_ack_cm_event(event); err2: rdma_destroy_id(listen_id); fprintf(stderr,"%s NOT connected!\n", __FUNCTION__); return NULL; } static unsigned get_dst_addr(const char *dst) { struct addrinfo *res; int ret; unsigned addr; ret = getaddrinfo(dst, NULL, NULL, &res); if (ret) { fprintf(stderr, "%s getaddrinfo failed - invalid hostname or IP address\n", __FUNCTION__); return 0; } if (res->ai_family != PF_INET) { return 0; } addr = ((struct sockaddr_in*)res->ai_addr)->sin_addr.s_addr; freeaddrinfo(res); return addr; } static struct pingpong_context *pp_client_connect_cma(const char *servername, unsigned short port, int size, int tx_depth, struct pingpong_dest *my_dest, struct pingpong_dest *rem_dest) //comment2 “client connect with connection management abstraction function” { struct rdma_cm_event *event; struct rdma_conn_param conn_param; int ret; struct sockaddr_in sin; struct rdma_cm_id *cm_id; struct pingpong_context *ctx; sin.sin_addr.s_addr = get_dst_addr(servername); if (!sin.sin_addr.s_addr) { return NULL; } ctx = pp_create_cma_ctx(); if (!ctx) { fprintf(stderr, "Couldn't create cma context\n"); return NULL; } ctx->rch = rdma_create_event_channel();//comment3 “create event channel” if (!ctx->rch) { fprintf(stderr, "Couldn't create RDMA event channel\n"); return NULL; } ret = rdma_create_id(ctx->rch, &cm_id, NULL, RDMA_PS_TCP); //comment4 “create connection manager identifier” if (ret) { fprintf(stderr,"%s rdma_create_id failed %d\n", __FUNCTION__, ret); return NULL; } sin.sin_family = PF_INET; sin.sin_port = htons(port); ret = rdma_resolve_addr(cm_id, NULL, (struct sockaddr *)&sin, 2000); if (ret) { fprintf(stderr,"%s rdma_resolve_addr failed %d\n", __FUNCTION__, ret); goto err2; } ret = rdma_get_cm_event(ctx->rch, &event); if (ret) goto err2; if (event->event != RDMA_CM_EVENT_ADDR_RESOLVED) { fprintf(stderr,"%s/%d unexpected CM event %d\n", __FUNCTION__, __LINE__, event->event); goto err1; } rdma_ack_cm_event(event); ret = rdma_resolve_route(cm_id, 2000); if (ret) { fprintf(stderr,"%s rdma_resolve_route failed %d\n", __FUNCTION__, ret); goto err2; } ret = rdma_get_cm_event(ctx->rch, &event); if (ret) goto err2; if (event->event != RDMA_CM_EVENT_ROUTE_RESOLVED) { fprintf(stderr,"%s/%d unexpected CM event %d\n", __FUNCTION__, __LINE__, event->event); goto err1; } rdma_ack_cm_event(event); ctx = pp_init_cma_ctx(ctx, cm_id, size, tx_depth, port); //comment5 “initialize connection manager abstraction context call” if (!ctx) { fprintf(stderr,"%s pp_init_cma_ctx failed\n", __FUNCTION__); goto err2; } my_dest->qpn = 0; my_dest->psn = 0xaa; my_dest->rkey = ctx->mr->rkey; my_dest->vaddr = (uintptr_t)ctx->buf + ctx->size; memset(&conn_param, 0, sizeof conn_param); conn_param.responder_resources = 1; conn_param.initiator_depth = 1; conn_param.retry_count = 5; conn_param.private_data = my_dest; conn_param.private_data_len = sizeof(*my_dest); pp_post_recv(ctx, size, tx_depth*2); //comment13 “post receive” ret = rdma_connect(ctx->cm_id, &conn_param); //comment14 “rdma connect” if (ret) { fprintf(stderr,"%s rdma_connect failure %d\n", __FUNCTION__, ret); goto err2; } ret = rdma_get_cm_event(ctx->rch, &event); if (ret) goto err2; if (event->event != RDMA_CM_EVENT_ESTABLISHED) { fprintf(stderr,"%s/%d unexpected CM event %d\n", __FUNCTION__, __LINE__, event->event); goto err1; } if (!event->param.conn.private_data || (event->param.conn.private_data_len < sizeof(*rem_dest))) { fprintf(stderr,"%s bad private data ptr %p len %d\n", __FUNCTION__, event->param.conn.private_data, event->param.conn.private_data_len); goto err1; } #if 0 if (event->param.conn.private_data_len != sizeof(*rem_dest)) { fprintf(stderr, "%s - warning: private data is %d bytes, expected %d bytes\n", __FUNCTION__, event->param.conn.private_data_len, (int)sizeof(*rem_dest)); } #endif memcpy(rem_dest, event->param.conn.private_data, sizeof(*rem_dest)); rdma_ack_cm_event(event); return ctx; err1: rdma_ack_cm_event(event); err2: fprintf(stderr,"NOT connected!\n"); rdma_destroy_id(cm_id); return NULL; } static void usage(const char *argv0) { printf("Usage:\n"); printf(" %s start a server and wait for connection\n", argv0); printf(" %s <host> connect to server at <host>\n", argv0); printf("\n"); printf("Options:\n"); printf(" -p, --port=<port> listen on/connect to port <port> (default 18515)\n"); printf(" -s, --size=<size> size of message to exchange (default 1)\n"); printf(" -t, --tx-depth=<dep> size of tx queue (default 1)\n"); printf(" -P, --poll Spin polling for completions (default is blocking)\n"); printf(" -I, --inline Use IBV_SEND_INLINE (default no inline)\n"); printf(" -T, --time=<secs> Run test for <secs> seconds (default is 10)\n"); printf(" -v, --verbose be verbose.\n"); printf(" -A, --abortmode abort instead of graceful close\n"); printf(" -n, --nortr server starts test (no rtr)\n"); } static void print_report(int size, int iters, struct timeval *start_time, struct timeval *stop_time) { time_t sec; suseconds_t usec; float elapsed; double bytes_sent = 2 * size * iters; double throughput; if (stop_time->tv_usec < start_time->tv_usec) { stop_time->tv_usec += 1000000; stop_time->tv_sec -= 1; } sec = stop_time->tv_sec - start_time->tv_sec; usec = stop_time->tv_usec - start_time->tv_usec; elapsed = (float)sec + ((float)usec/(float)1000000.0); throughput = iters / elapsed; printf("Bytes %g elapsed sec %d usec %d\n", bytes_sent, (unsigned)sec, (unsigned)usec); printf("Average transfer rate: %g messages/sec\n", throughput); printf("RTT/2 = %g us\n", (double)1 / throughput / (double)2 * (double)(1000 * 1000)); } int abortmode; void sig_handler(int foo) { done = 1; if (verbose) printf("Alarm\n"); if (abortmode) exit(1); } int main(int argc, char *argv[]) { int port = 18515; int size = 1; int tx_depth = 1; struct pingpong_context *ctx; char *servername = NULL; struct pingpong_dest rem_dest; struct ibv_qp *qp; struct ibv_send_wr *wr; int scnt, rcnt, ccnt; struct timeval start_time, stop_time; struct timezone tz; int poll = 0; struct ibv_cq *ev_cq; void *ev_ctx; int send_inline = 0; int run_time = 10; struct ibv_wc wc; int ne; int zerobyte = 0; struct ibv_send_wr *bad_wr; int count; int peer2peer = 0; /* Parameter parsing. */ while (1) { int c; static struct option long_options[] = { { .name = "port", .has_arg = 1, .val = 'p' }, { .name = "size", .has_arg = 1, .val = 's' }, { .name = "tx-depth", .has_arg = 1, .val = 't' }, { .name = "poll", .has_arg = 0, .val = 'P' }, { .name = "inline", .has_arg = 0, .val = 'I' }, { .name = "time", .has_arg = 1, .val = 'T' }, { .name = "verbose", .has_arg = 0, .val = 'v' }, { .name = "myserver", .has_arg = 0, .val = 'm' }, { .name = "abortmode", .has_arg = 0, .val = 'A' }, { .name = "nortr", .has_arg = 0, .val = 'n' }, { 0 } }; c = getopt_long(argc, argv, "nAp:s:t:T:PIv", long_options, NULL); if (c == -1) break; switch (c) { case 'n': peer2peer = 1; break; case 'A': abortmode = 1; break; case 'm': myserver = 1; break; case 'v': verbose = 1; break; case 'I': send_inline = IBV_SEND_INLINE; break; case 'P': poll = 1; break; case 'p': port = strtol(optarg, NULL, 0); if (port < 0 || port > 65535) { usage(argv[0]); return 1; } break; case 's': size = strtol(optarg, NULL, 0); if (size < 0) { usage(argv[0]); return 3; } if (size == 0) { zerobyte=1; size = 1; /* so we alloc memory ok */ } break; case 't': tx_depth = strtol(optarg, NULL, 0); if (tx_depth < 1) { usage(argv[0]); return 4; } break; case 'T': run_time = strtol(optarg, NULL, 0); if (run_time < 0) { usage(argv[0]); return 4; } break; default: usage(argv[0]); return 5; } } if (optind == argc - 1) servername = strdupa(argv[optind]); else if (optind < argc) { usage(argv[0]); return 6; } /* * Done with parameter parsing. Perform setup. */ srand48(getpid() * time(NULL)); page_size = sysconf(_SC_PAGESIZE); struct pingpong_dest my_dest; memset(&rem_dest, 0, sizeof(rem_dest)); if (servername) ctx = pp_client_connect_cma(servername, port, size, tx_depth, &my_dest, &rem_dest); //comment1 “client connect with connection management abstraction call” else ctx = pp_server_connect_cma(port, size, tx_depth, &my_dest, &rem_dest); if (!ctx) { fprintf(stderr, "pp_connect_cma(%s,%d) failed!\n", servername, port); return 7; } if (verbose) { printf(" local address: PSN %#06x RKey %#08x VAddr %#016Lx\n", my_dest.psn, my_dest.rkey, my_dest.vaddr); printf(" remote address: PSN %#06x RKey %#08x VAddr %#016Lx\n", rem_dest.psn, rem_dest.rkey, rem_dest.vaddr); } wr = &ctx->wr; ctx->list.addr = (uintptr_t)(ctx->buf + size); if (zerobyte) ctx->list.length = 0; else ctx->list.length = ctx->size; ctx->list.lkey = ctx->mr->lkey; wr->wr_id = PINGPONG_RDMA_WRID; wr->sg_list = &ctx->list; wr->num_sge = 1; wr->opcode = IBV_WR_SEND; //comment15 “iwarp verbs rdma send work request” wr->send_flags = IBV_SEND_SIGNALED; if (ctx->size <= 64) wr->send_flags |= send_inline; wr->next = NULL; scnt = 0; rcnt = 0; ccnt = 0; qp = ctx->qp; if (!poll && ibv_req_notify_cq(ctx->scq, 0)) { fprintf(stderr, "ibv_req_notify failed!\n"); return 1; } if (!poll && ibv_req_notify_cq(ctx->rcq, 0)) { fprintf(stderr, "ibv_req_notify failed!\n"); return 1; } /* Done with setup. Start the test. */ signal(SIGALRM, sig_handler); alarm(run_time); gettimeofday(&start_time, &tz); while (!done) { if (scnt >= 1 || (peer2peer && !servername) || (!peer2peer && servername)) { int count = tx_depth; /* Wait for recv completions */ while (!done && count--) { do { ne = ibv_poll_cq(ctx->rcq, 1, &wc); //comment16 “iwarp verbs poll completion queue” if (!poll && ne == 0) { if (ibv_get_cq_event(ctx->ch, &ev_cq, &ev_ctx)) { fprintf(stderr, "Failed to get cq event!\n"); return 1; } if (ev_cq != ctx->rcq) { fprintf(stderr, "Unkown CQ!\n"); return 1; } ibv_ack_cq_events(ctx->rcq, 1); ibv_req_notify_cq(ctx->rcq, 0); } if (done) break; } while (ne == 0); if (ne < 0) { fprintf(stderr, "poll CQ failed %d\n", ne); break; } if (wc.status != IBV_WC_SUCCESS) { fprintf(stderr, "Completion wth error at %s:\n", servername ? "client" : "server"); fprintf(stderr, "Failed status %d: wr_id %d\n", wc.status, (int) wc.wr_id); fprintf(stderr, "scnt=%d, rcnt=%d, ccnt=%d\n", scnt, rcnt, ccnt); break; } if (done) break; } } count = tx_depth; while (!done && count--) { if (ibv_post_send(qp, wr, &bad_wr)) { //comment17 “iwarp verbs post send” fprintf(stderr, "Couldn't post send: scnt=%d\n", scnt); break; } scnt++; pp_post_recv(ctx, size, 1); //comment18 “iwarp verbs post receive” ++rcnt; } while (!done && ccnt < scnt) { do { ne = ibv_poll_cq(ctx->scq, 1, &wc); //comment19 “iwarp verbs poll completion queue” if (ne < 0) { fprintf(stderr, "poll CQ failed %d\n", ne); break; } } while (ne == 0); if (ne == 1) { if (wc.status != IBV_WC_SUCCESS) { fprintf(stderr, "Completion wth error at %s:\n", servername ? "client" : "server"); fprintf(stderr, "Failed status %d: wr_id %d\n", wc.status, (int) wc.wr_id); fprintf(stderr, "scnt=%d, rcnt=%d, ccnt=%d\n", scnt, rcnt, ccnt); break; } ++ccnt; } } } gettimeofday(&stop_time, &tz); //comment20 “finish test client” if (servername) { if (verbose) printf("scnt %u ccnt %u rcnt %u\n", scnt, ccnt, rcnt); print_report(size, ccnt, &start_time, &stop_time); if (myserver) { char *buf; size_t buflen; while (getline(&buf, &buflen, stdin) != -1) printf("server: %s", buf); } } return 0; } |