1 /* 2 * Copyright (c) 2005 Ammasso, Inc. All rights reserved. 3 * Copyright (c) 2006-2009 Open Grid Computing, Inc. All rights reserved. 4 * 5 * This software is available to you under a choice of one of two 6 * licenses. You may choose to be licensed under the terms of the GNU 7 * General Public License (GPL) Version 2, available from the file 8 * COPYING in the main directory of this source tree, or the 9 * OpenIB.org BSD license below: 10 * 11 * Redistribution and use in source and binary forms, with or 12 * without modification, are permitted provided that the following 13 * conditions are met: 14 * 15 * - Redistributions of source code must retain the above 16 * copyright notice, this list of conditions and the following 17 * disclaimer. 18 * 19 * - Redistributions in binary form must reproduce the above 20 * copyright notice, this list of conditions and the following 21 * disclaimer in the documentation and/or other materials 22 * provided with the distribution. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 31 * SOFTWARE. 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include <linux/module.h> 38 #include <linux/moduleparam.h> 39 #include <linux/slab.h> 40 #include <linux/err.h> 41 #include <linux/string.h> 42 #include <linux/list.h> 43 #include <linux/in.h> 44 #include <linux/device.h> 45 #include <linux/pci.h> 46 #include <linux/sched.h> 47 48 #include <asm/atomic.h> 49 50 #include <rdma/ib_verbs.h> 51 #include <rdma/rdma_cm.h> 52 53 #include "krping.h" 54 #include "getopt.h" 55 56 extern int krping_debug; 57 #define DEBUG_LOG(cb, x...) if (krping_debug) krping_printf((cb)->cookie, x) 58 #define PRINTF(cb, x...) krping_printf((cb)->cookie, x) 59 60 MODULE_AUTHOR("Steve Wise"); 61 MODULE_DESCRIPTION("RDMA ping client/server"); 62 MODULE_LICENSE("Dual BSD/GPL"); 63 MODULE_VERSION(krping, 1); 64 MODULE_DEPEND(krping, linuxkpi, 1, 1, 1); 65 66 static __inline uint64_t 67 get_cycles(void) 68 { 69 uint32_t low, high; 70 __asm __volatile("rdtsc" : "=a" (low), "=d" (high)); 71 return (low | ((u_int64_t)high << 32)); 72 } 73 74 typedef uint64_t cycles_t; 75 76 enum mem_type { 77 DMA = 1, 78 FASTREG = 2, 79 MW = 3, 80 MR = 4 81 }; 82 83 static const struct krping_option krping_opts[] = { 84 {"count", OPT_INT, 'C'}, 85 {"size", OPT_INT, 'S'}, 86 {"addr", OPT_STRING, 'a'}, 87 {"port", OPT_INT, 'p'}, 88 {"verbose", OPT_NOPARAM, 'v'}, 89 {"validate", OPT_NOPARAM, 'V'}, 90 {"server", OPT_NOPARAM, 's'}, 91 {"client", OPT_NOPARAM, 'c'}, 92 {"mem_mode", OPT_STRING, 'm'}, 93 {"server_inv", OPT_NOPARAM, 'I'}, 94 {"wlat", OPT_NOPARAM, 'l'}, 95 {"rlat", OPT_NOPARAM, 'L'}, 96 {"bw", OPT_NOPARAM, 'B'}, 97 {"duplex", OPT_NOPARAM, 'd'}, 98 {"txdepth", OPT_INT, 'T'}, 99 {"poll", OPT_NOPARAM, 'P'}, 100 {"local_dma_lkey", OPT_NOPARAM, 'Z'}, 101 {"read_inv", OPT_NOPARAM, 'R'}, 102 {"fr", OPT_NOPARAM, 'f'}, 103 {NULL, 0, 0} 104 }; 105 106 #define htonll(x) cpu_to_be64((x)) 107 #define ntohll(x) cpu_to_be64((x)) 108 109 static struct mutex krping_mutex; 110 111 /* 112 * List of running krping threads. 113 */ 114 static LIST_HEAD(krping_cbs); 115 116 /* 117 * krping "ping/pong" loop: 118 * client sends source rkey/addr/len 119 * server receives source rkey/add/len 120 * server rdma reads "ping" data from source 121 * server sends "go ahead" on rdma read completion 122 * client sends sink rkey/addr/len 123 * server receives sink rkey/addr/len 124 * server rdma writes "pong" data to sink 125 * server sends "go ahead" on rdma write completion 126 * <repeat loop> 127 */ 128 129 /* 130 * These states are used to signal events between the completion handler 131 * and the main client or server thread. 132 * 133 * Once CONNECTED, they cycle through RDMA_READ_ADV, RDMA_WRITE_ADV, 134 * and RDMA_WRITE_COMPLETE for each ping. 135 */ 136 enum test_state { 137 IDLE = 1, 138 CONNECT_REQUEST, 139 ADDR_RESOLVED, 140 ROUTE_RESOLVED, 141 CONNECTED, 142 RDMA_READ_ADV, 143 RDMA_READ_COMPLETE, 144 RDMA_WRITE_ADV, 145 RDMA_WRITE_COMPLETE, 146 ERROR 147 }; 148 149 struct krping_rdma_info { 150 uint64_t buf; 151 uint32_t rkey; 152 uint32_t size; 153 }; 154 155 /* 156 * Default max buffer size for IO... 157 */ 158 #define RPING_BUFSIZE 128*1024 159 #define RPING_SQ_DEPTH 64 160 161 /* 162 * Control block struct. 163 */ 164 struct krping_cb { 165 void *cookie; 166 int server; /* 0 iff client */ 167 struct ib_cq *cq; 168 struct ib_pd *pd; 169 struct ib_qp *qp; 170 171 enum mem_type mem; 172 struct ib_mr *dma_mr; 173 174 struct ib_fast_reg_page_list *page_list; 175 int page_list_len; 176 struct ib_send_wr fastreg_wr; 177 struct ib_send_wr invalidate_wr; 178 struct ib_mr *fastreg_mr; 179 int server_invalidate; 180 int read_inv; 181 u8 key; 182 183 struct ib_mw *mw; 184 struct ib_mw_bind bind_attr; 185 186 struct ib_recv_wr rq_wr; /* recv work request record */ 187 struct ib_sge recv_sgl; /* recv single SGE */ 188 struct krping_rdma_info recv_buf;/* malloc'd buffer */ 189 u64 recv_dma_addr; 190 DECLARE_PCI_UNMAP_ADDR(recv_mapping) 191 struct ib_mr *recv_mr; 192 193 struct ib_send_wr sq_wr; /* send work requrest record */ 194 struct ib_sge send_sgl; 195 struct krping_rdma_info send_buf;/* single send buf */ 196 u64 send_dma_addr; 197 DECLARE_PCI_UNMAP_ADDR(send_mapping) 198 struct ib_mr *send_mr; 199 200 struct ib_send_wr rdma_sq_wr; /* rdma work request record */ 201 struct ib_sge rdma_sgl; /* rdma single SGE */ 202 char *rdma_buf; /* used as rdma sink */ 203 u64 rdma_dma_addr; 204 DECLARE_PCI_UNMAP_ADDR(rdma_mapping) 205 struct ib_mr *rdma_mr; 206 207 uint32_t remote_rkey; /* remote guys RKEY */ 208 uint64_t remote_addr; /* remote guys TO */ 209 uint32_t remote_len; /* remote guys LEN */ 210 211 char *start_buf; /* rdma read src */ 212 u64 start_dma_addr; 213 DECLARE_PCI_UNMAP_ADDR(start_mapping) 214 struct ib_mr *start_mr; 215 216 enum test_state state; /* used for cond/signalling */ 217 wait_queue_head_t sem; 218 struct krping_stats stats; 219 220 uint16_t port; /* dst port in NBO */ 221 struct in_addr addr; /* dst addr in NBO */ 222 char *addr_str; /* dst addr string */ 223 int verbose; /* verbose logging */ 224 int count; /* ping count */ 225 int size; /* ping data size */ 226 int validate; /* validate ping data */ 227 int wlat; /* run wlat test */ 228 int rlat; /* run rlat test */ 229 int bw; /* run bw test */ 230 int duplex; /* run bw full duplex test */ 231 int poll; /* poll or block for rlat test */ 232 int txdepth; /* SQ depth */ 233 int local_dma_lkey; /* use 0 for lkey */ 234 int frtest; /* fastreg test */ 235 236 /* CM stuff */ 237 struct rdma_cm_id *cm_id; /* connection on client side,*/ 238 /* listener on server side. */ 239 struct rdma_cm_id *child_cm_id; /* connection on server side */ 240 struct list_head list; 241 }; 242 243 static int krping_cma_event_handler(struct rdma_cm_id *cma_id, 244 struct rdma_cm_event *event) 245 { 246 int ret; 247 struct krping_cb *cb = cma_id->context; 248 249 DEBUG_LOG(cb, "cma_event type %d cma_id %p (%s)\n", event->event, 250 cma_id, (cma_id == cb->cm_id) ? "parent" : "child"); 251 252 switch (event->event) { 253 case RDMA_CM_EVENT_ADDR_RESOLVED: 254 cb->state = ADDR_RESOLVED; 255 ret = rdma_resolve_route(cma_id, 2000); 256 if (ret) { 257 PRINTF(cb, "rdma_resolve_route error %d\n", ret); 258 wake_up_interruptible(&cb->sem); 259 } 260 break; 261 262 case RDMA_CM_EVENT_ROUTE_RESOLVED: 263 cb->state = ROUTE_RESOLVED; 264 wake_up_interruptible(&cb->sem); 265 break; 266 267 case RDMA_CM_EVENT_CONNECT_REQUEST: 268 cb->state = CONNECT_REQUEST; 269 cb->child_cm_id = cma_id; 270 DEBUG_LOG(cb, "child cma %p\n", cb->child_cm_id); 271 wake_up_interruptible(&cb->sem); 272 break; 273 274 case RDMA_CM_EVENT_ESTABLISHED: 275 DEBUG_LOG(cb, "ESTABLISHED\n"); 276 if (!cb->server) { 277 cb->state = CONNECTED; 278 } 279 wake_up_interruptible(&cb->sem); 280 break; 281 282 case RDMA_CM_EVENT_ADDR_ERROR: 283 case RDMA_CM_EVENT_ROUTE_ERROR: 284 case RDMA_CM_EVENT_CONNECT_ERROR: 285 case RDMA_CM_EVENT_UNREACHABLE: 286 case RDMA_CM_EVENT_REJECTED: 287 PRINTF(cb, "cma event %d, error %d\n", event->event, 288 event->status); 289 cb->state = ERROR; 290 wake_up_interruptible(&cb->sem); 291 break; 292 293 case RDMA_CM_EVENT_DISCONNECTED: 294 PRINTF(cb, "DISCONNECT EVENT...\n"); 295 cb->state = ERROR; 296 wake_up_interruptible(&cb->sem); 297 break; 298 299 case RDMA_CM_EVENT_DEVICE_REMOVAL: 300 PRINTF(cb, "cma detected device removal!!!!\n"); 301 break; 302 303 default: 304 PRINTF(cb, "oof bad type!\n"); 305 wake_up_interruptible(&cb->sem); 306 break; 307 } 308 return 0; 309 } 310 311 static int server_recv(struct krping_cb *cb, struct ib_wc *wc) 312 { 313 if (wc->byte_len != sizeof(cb->recv_buf)) { 314 PRINTF(cb, "Received bogus data, size %d\n", 315 wc->byte_len); 316 return -1; 317 } 318 319 cb->remote_rkey = ntohl(cb->recv_buf.rkey); 320 cb->remote_addr = ntohll(cb->recv_buf.buf); 321 cb->remote_len = ntohl(cb->recv_buf.size); 322 DEBUG_LOG(cb, "Received rkey %x addr %llx len %d from peer\n", 323 cb->remote_rkey, (unsigned long long)cb->remote_addr, 324 cb->remote_len); 325 326 if (cb->state <= CONNECTED || cb->state == RDMA_WRITE_COMPLETE) 327 cb->state = RDMA_READ_ADV; 328 else 329 cb->state = RDMA_WRITE_ADV; 330 331 return 0; 332 } 333 334 static int client_recv(struct krping_cb *cb, struct ib_wc *wc) 335 { 336 if (wc->byte_len != sizeof(cb->recv_buf)) { 337 PRINTF(cb, "Received bogus data, size %d\n", 338 wc->byte_len); 339 return -1; 340 } 341 342 if (cb->state == RDMA_READ_ADV) 343 cb->state = RDMA_WRITE_ADV; 344 else 345 cb->state = RDMA_WRITE_COMPLETE; 346 347 return 0; 348 } 349 350 static void krping_cq_event_handler(struct ib_cq *cq, void *ctx) 351 { 352 struct krping_cb *cb = ctx; 353 struct ib_wc wc; 354 struct ib_recv_wr *bad_wr; 355 int ret; 356 357 BUG_ON(cb->cq != cq); 358 if (cb->state == ERROR) { 359 PRINTF(cb, "cq completion in ERROR state\n"); 360 return; 361 } 362 if (cb->frtest) { 363 PRINTF(cb, "cq completion event in frtest!\n"); 364 return; 365 } 366 if (!cb->wlat && !cb->rlat && !cb->bw) 367 ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP); 368 while ((ret = ib_poll_cq(cb->cq, 1, &wc)) == 1) { 369 if (wc.status) { 370 if (wc.status == IB_WC_WR_FLUSH_ERR) { 371 DEBUG_LOG(cb, "cq flushed\n"); 372 continue; 373 } else { 374 PRINTF(cb, "cq completion failed with " 375 "wr_id %Lx status %d opcode %d vender_err %x\n", 376 wc.wr_id, wc.status, wc.opcode, wc.vendor_err); 377 goto error; 378 } 379 } 380 381 switch (wc.opcode) { 382 case IB_WC_SEND: 383 DEBUG_LOG(cb, "send completion\n"); 384 cb->stats.send_bytes += cb->send_sgl.length; 385 cb->stats.send_msgs++; 386 break; 387 388 case IB_WC_RDMA_WRITE: 389 DEBUG_LOG(cb, "rdma write completion\n"); 390 cb->stats.write_bytes += cb->rdma_sq_wr.sg_list->length; 391 cb->stats.write_msgs++; 392 cb->state = RDMA_WRITE_COMPLETE; 393 wake_up_interruptible(&cb->sem); 394 break; 395 396 case IB_WC_RDMA_READ: 397 DEBUG_LOG(cb, "rdma read completion\n"); 398 cb->stats.read_bytes += cb->rdma_sq_wr.sg_list->length; 399 cb->stats.read_msgs++; 400 cb->state = RDMA_READ_COMPLETE; 401 wake_up_interruptible(&cb->sem); 402 break; 403 404 case IB_WC_RECV: 405 DEBUG_LOG(cb, "recv completion\n"); 406 cb->stats.recv_bytes += sizeof(cb->recv_buf); 407 cb->stats.recv_msgs++; 408 if (cb->wlat || cb->rlat || cb->bw) 409 ret = server_recv(cb, &wc); 410 else 411 ret = cb->server ? server_recv(cb, &wc) : 412 client_recv(cb, &wc); 413 if (ret) { 414 PRINTF(cb, "recv wc error: %d\n", ret); 415 goto error; 416 } 417 418 ret = ib_post_recv(cb->qp, &cb->rq_wr, &bad_wr); 419 if (ret) { 420 PRINTF(cb, "post recv error: %d\n", 421 ret); 422 goto error; 423 } 424 wake_up_interruptible(&cb->sem); 425 break; 426 427 default: 428 PRINTF(cb, 429 "%s:%d Unexpected opcode %d, Shutting down\n", 430 __func__, __LINE__, wc.opcode); 431 goto error; 432 } 433 } 434 if (ret) { 435 PRINTF(cb, "poll error %d\n", ret); 436 goto error; 437 } 438 return; 439 error: 440 cb->state = ERROR; 441 wake_up_interruptible(&cb->sem); 442 } 443 444 static int krping_accept(struct krping_cb *cb) 445 { 446 struct rdma_conn_param conn_param; 447 int ret; 448 449 DEBUG_LOG(cb, "accepting client connection request\n"); 450 451 memset(&conn_param, 0, sizeof conn_param); 452 conn_param.responder_resources = 1; 453 conn_param.initiator_depth = 1; 454 455 ret = rdma_accept(cb->child_cm_id, &conn_param); 456 if (ret) { 457 PRINTF(cb, "rdma_accept error: %d\n", ret); 458 return ret; 459 } 460 461 if (!cb->wlat && !cb->rlat && !cb->bw) { 462 wait_event_interruptible(cb->sem, cb->state >= CONNECTED); 463 if (cb->state == ERROR) { 464 PRINTF(cb, "wait for CONNECTED state %d\n", 465 cb->state); 466 return -1; 467 } 468 } 469 return 0; 470 } 471 472 static void krping_setup_wr(struct krping_cb *cb) 473 { 474 cb->recv_sgl.addr = cb->recv_dma_addr; 475 cb->recv_sgl.length = sizeof cb->recv_buf; 476 if (cb->local_dma_lkey) 477 cb->recv_sgl.lkey = cb->qp->device->local_dma_lkey; 478 else if (cb->mem == DMA) 479 cb->recv_sgl.lkey = cb->dma_mr->lkey; 480 else 481 cb->recv_sgl.lkey = cb->recv_mr->lkey; 482 cb->rq_wr.sg_list = &cb->recv_sgl; 483 cb->rq_wr.num_sge = 1; 484 485 cb->send_sgl.addr = cb->send_dma_addr; 486 cb->send_sgl.length = sizeof cb->send_buf; 487 if (cb->local_dma_lkey) 488 cb->send_sgl.lkey = cb->qp->device->local_dma_lkey; 489 else if (cb->mem == DMA) 490 cb->send_sgl.lkey = cb->dma_mr->lkey; 491 else 492 cb->send_sgl.lkey = cb->send_mr->lkey; 493 494 cb->sq_wr.opcode = IB_WR_SEND; 495 cb->sq_wr.send_flags = IB_SEND_SIGNALED; 496 cb->sq_wr.sg_list = &cb->send_sgl; 497 cb->sq_wr.num_sge = 1; 498 499 if (cb->server || cb->wlat || cb->rlat || cb->bw) { 500 cb->rdma_sgl.addr = cb->rdma_dma_addr; 501 if (cb->mem == MR) 502 cb->rdma_sgl.lkey = cb->rdma_mr->lkey; 503 cb->rdma_sq_wr.send_flags = IB_SEND_SIGNALED; 504 cb->rdma_sq_wr.sg_list = &cb->rdma_sgl; 505 cb->rdma_sq_wr.num_sge = 1; 506 } 507 508 switch(cb->mem) { 509 case FASTREG: 510 511 /* 512 * A chain of 2 WRs, INVALDATE_MR + FAST_REG_MR. 513 * both unsignaled. The client uses them to reregister 514 * the rdma buffers with a new key each iteration. 515 */ 516 cb->fastreg_wr.opcode = IB_WR_FAST_REG_MR; 517 cb->fastreg_wr.wr.fast_reg.page_shift = PAGE_SHIFT; 518 cb->fastreg_wr.wr.fast_reg.length = cb->size; 519 cb->fastreg_wr.wr.fast_reg.page_list = cb->page_list; 520 cb->fastreg_wr.wr.fast_reg.page_list_len = cb->page_list_len; 521 522 cb->invalidate_wr.next = &cb->fastreg_wr; 523 cb->invalidate_wr.opcode = IB_WR_LOCAL_INV; 524 break; 525 case MW: 526 cb->bind_attr.wr_id = 0xabbaabba; 527 cb->bind_attr.send_flags = 0; /* unsignaled */ 528 cb->bind_attr.bind_info.length = cb->size; 529 break; 530 default: 531 break; 532 } 533 } 534 535 static int krping_setup_buffers(struct krping_cb *cb) 536 { 537 int ret; 538 struct ib_phys_buf buf; 539 u64 iovbase; 540 541 DEBUG_LOG(cb, "krping_setup_buffers called on cb %p\n", cb); 542 543 cb->recv_dma_addr = dma_map_single(cb->pd->device->dma_device, 544 &cb->recv_buf, 545 sizeof(cb->recv_buf), DMA_BIDIRECTIONAL); 546 pci_unmap_addr_set(cb, recv_mapping, cb->recv_dma_addr); 547 cb->send_dma_addr = dma_map_single(cb->pd->device->dma_device, 548 &cb->send_buf, sizeof(cb->send_buf), 549 DMA_BIDIRECTIONAL); 550 pci_unmap_addr_set(cb, send_mapping, cb->send_dma_addr); 551 552 if (cb->mem == DMA) { 553 cb->dma_mr = ib_get_dma_mr(cb->pd, IB_ACCESS_LOCAL_WRITE| 554 IB_ACCESS_REMOTE_READ| 555 IB_ACCESS_REMOTE_WRITE); 556 if (IS_ERR(cb->dma_mr)) { 557 DEBUG_LOG(cb, "reg_dmamr failed\n"); 558 ret = PTR_ERR(cb->dma_mr); 559 goto bail; 560 } 561 } else { 562 if (!cb->local_dma_lkey) { 563 buf.addr = cb->recv_dma_addr; 564 buf.size = sizeof cb->recv_buf; 565 DEBUG_LOG(cb, "recv buf dma_addr %llx size %d\n", buf.addr, 566 (int)buf.size); 567 iovbase = cb->recv_dma_addr; 568 cb->recv_mr = ib_reg_phys_mr(cb->pd, &buf, 1, 569 IB_ACCESS_LOCAL_WRITE, 570 &iovbase); 571 572 if (IS_ERR(cb->recv_mr)) { 573 DEBUG_LOG(cb, "recv_buf reg_mr failed\n"); 574 ret = PTR_ERR(cb->recv_mr); 575 goto bail; 576 } 577 578 buf.addr = cb->send_dma_addr; 579 buf.size = sizeof cb->send_buf; 580 DEBUG_LOG(cb, "send buf dma_addr %llx size %d\n", buf.addr, 581 (int)buf.size); 582 iovbase = cb->send_dma_addr; 583 cb->send_mr = ib_reg_phys_mr(cb->pd, &buf, 1, 584 0, &iovbase); 585 586 if (IS_ERR(cb->send_mr)) { 587 DEBUG_LOG(cb, "send_buf reg_mr failed\n"); 588 ret = PTR_ERR(cb->send_mr); 589 goto bail; 590 } 591 } 592 } 593 594 cb->rdma_buf = kmalloc(cb->size, GFP_KERNEL); 595 if (!cb->rdma_buf) { 596 DEBUG_LOG(cb, "rdma_buf malloc failed\n"); 597 ret = -ENOMEM; 598 goto bail; 599 } 600 601 cb->rdma_dma_addr = dma_map_single(cb->pd->device->dma_device, 602 cb->rdma_buf, cb->size, 603 DMA_BIDIRECTIONAL); 604 pci_unmap_addr_set(cb, rdma_mapping, cb->rdma_dma_addr); 605 if (cb->mem != DMA) { 606 switch (cb->mem) { 607 case FASTREG: 608 cb->page_list_len = (((cb->size - 1) & PAGE_MASK) + 609 PAGE_SIZE) >> PAGE_SHIFT; 610 cb->page_list = ib_alloc_fast_reg_page_list( 611 cb->pd->device, 612 cb->page_list_len); 613 if (IS_ERR(cb->page_list)) { 614 DEBUG_LOG(cb, "recv_buf reg_mr failed\n"); 615 ret = PTR_ERR(cb->page_list); 616 goto bail; 617 } 618 cb->fastreg_mr = ib_alloc_fast_reg_mr(cb->pd, 619 cb->page_list->max_page_list_len); 620 if (IS_ERR(cb->fastreg_mr)) { 621 DEBUG_LOG(cb, "recv_buf reg_mr failed\n"); 622 ret = PTR_ERR(cb->fastreg_mr); 623 goto bail; 624 } 625 DEBUG_LOG(cb, "fastreg rkey 0x%x page_list %p" 626 " page_list_len %u\n", cb->fastreg_mr->rkey, 627 cb->page_list, cb->page_list_len); 628 break; 629 case MW: 630 cb->mw = ib_alloc_mw(cb->pd,IB_MW_TYPE_1); 631 if (IS_ERR(cb->mw)) { 632 DEBUG_LOG(cb, "recv_buf alloc_mw failed\n"); 633 ret = PTR_ERR(cb->mw); 634 goto bail; 635 } 636 DEBUG_LOG(cb, "mw rkey 0x%x\n", cb->mw->rkey); 637 /*FALLTHROUGH*/ 638 case MR: 639 buf.addr = cb->rdma_dma_addr; 640 buf.size = cb->size; 641 iovbase = cb->rdma_dma_addr; 642 cb->rdma_mr = ib_reg_phys_mr(cb->pd, &buf, 1, 643 IB_ACCESS_REMOTE_READ| 644 IB_ACCESS_REMOTE_WRITE, 645 &iovbase); 646 if (IS_ERR(cb->rdma_mr)) { 647 DEBUG_LOG(cb, "rdma_buf reg_mr failed\n"); 648 ret = PTR_ERR(cb->rdma_mr); 649 goto bail; 650 } 651 DEBUG_LOG(cb, "rdma buf dma_addr %llx size %d mr rkey 0x%x\n", 652 buf.addr, (int)buf.size, cb->rdma_mr->rkey); 653 break; 654 default: 655 ret = -EINVAL; 656 goto bail; 657 break; 658 } 659 } 660 661 if (!cb->server || cb->wlat || cb->rlat || cb->bw) { 662 663 cb->start_buf = kmalloc(cb->size, GFP_KERNEL); 664 if (!cb->start_buf) { 665 DEBUG_LOG(cb, "start_buf malloc failed\n"); 666 ret = -ENOMEM; 667 goto bail; 668 } 669 670 cb->start_dma_addr = dma_map_single(cb->pd->device->dma_device, 671 cb->start_buf, cb->size, 672 DMA_BIDIRECTIONAL); 673 pci_unmap_addr_set(cb, start_mapping, cb->start_dma_addr); 674 675 if (cb->mem == MR || cb->mem == MW) { 676 unsigned flags = IB_ACCESS_REMOTE_READ; 677 678 if (cb->wlat || cb->rlat || cb->bw) 679 flags |= IB_ACCESS_REMOTE_WRITE; 680 681 buf.addr = cb->start_dma_addr; 682 buf.size = cb->size; 683 DEBUG_LOG(cb, "start buf dma_addr %llx size %d\n", 684 buf.addr, (int)buf.size); 685 iovbase = cb->start_dma_addr; 686 cb->start_mr = ib_reg_phys_mr(cb->pd, &buf, 1, 687 flags, 688 &iovbase); 689 690 if (IS_ERR(cb->start_mr)) { 691 DEBUG_LOG(cb, "start_buf reg_mr failed\n"); 692 ret = PTR_ERR(cb->start_mr); 693 goto bail; 694 } 695 } 696 } 697 698 krping_setup_wr(cb); 699 DEBUG_LOG(cb, "allocated & registered buffers...\n"); 700 return 0; 701 bail: 702 if (cb->fastreg_mr && !IS_ERR(cb->fastreg_mr)) 703 ib_dereg_mr(cb->fastreg_mr); 704 if (cb->mw && !IS_ERR(cb->mw)) 705 ib_dealloc_mw(cb->mw); 706 if (cb->rdma_mr && !IS_ERR(cb->rdma_mr)) 707 ib_dereg_mr(cb->rdma_mr); 708 if (cb->page_list && !IS_ERR(cb->page_list)) 709 ib_free_fast_reg_page_list(cb->page_list); 710 if (cb->dma_mr && !IS_ERR(cb->dma_mr)) 711 ib_dereg_mr(cb->dma_mr); 712 if (cb->recv_mr && !IS_ERR(cb->recv_mr)) 713 ib_dereg_mr(cb->recv_mr); 714 if (cb->send_mr && !IS_ERR(cb->send_mr)) 715 ib_dereg_mr(cb->send_mr); 716 if (cb->rdma_buf) 717 kfree(cb->rdma_buf); 718 if (cb->start_buf) 719 kfree(cb->start_buf); 720 return ret; 721 } 722 723 static void krping_free_buffers(struct krping_cb *cb) 724 { 725 DEBUG_LOG(cb, "krping_free_buffers called on cb %p\n", cb); 726 727 if (cb->dma_mr) 728 ib_dereg_mr(cb->dma_mr); 729 if (cb->send_mr) 730 ib_dereg_mr(cb->send_mr); 731 if (cb->recv_mr) 732 ib_dereg_mr(cb->recv_mr); 733 if (cb->rdma_mr) 734 ib_dereg_mr(cb->rdma_mr); 735 if (cb->start_mr) 736 ib_dereg_mr(cb->start_mr); 737 if (cb->fastreg_mr) 738 ib_dereg_mr(cb->fastreg_mr); 739 if (cb->mw) 740 ib_dealloc_mw(cb->mw); 741 742 dma_unmap_single(cb->pd->device->dma_device, 743 pci_unmap_addr(cb, recv_mapping), 744 sizeof(cb->recv_buf), DMA_BIDIRECTIONAL); 745 dma_unmap_single(cb->pd->device->dma_device, 746 pci_unmap_addr(cb, send_mapping), 747 sizeof(cb->send_buf), DMA_BIDIRECTIONAL); 748 dma_unmap_single(cb->pd->device->dma_device, 749 pci_unmap_addr(cb, rdma_mapping), 750 cb->size, DMA_BIDIRECTIONAL); 751 kfree(cb->rdma_buf); 752 if (cb->start_buf) { 753 dma_unmap_single(cb->pd->device->dma_device, 754 pci_unmap_addr(cb, start_mapping), 755 cb->size, DMA_BIDIRECTIONAL); 756 kfree(cb->start_buf); 757 } 758 } 759 760 static int krping_create_qp(struct krping_cb *cb) 761 { 762 struct ib_qp_init_attr init_attr; 763 int ret; 764 765 memset(&init_attr, 0, sizeof(init_attr)); 766 init_attr.cap.max_send_wr = cb->txdepth; 767 init_attr.cap.max_recv_wr = 2; 768 init_attr.cap.max_recv_sge = 1; 769 init_attr.cap.max_send_sge = 1; 770 init_attr.qp_type = IB_QPT_RC; 771 init_attr.send_cq = cb->cq; 772 init_attr.recv_cq = cb->cq; 773 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR; 774 775 if (cb->server) { 776 ret = rdma_create_qp(cb->child_cm_id, cb->pd, &init_attr); 777 if (!ret) 778 cb->qp = cb->child_cm_id->qp; 779 } else { 780 ret = rdma_create_qp(cb->cm_id, cb->pd, &init_attr); 781 if (!ret) 782 cb->qp = cb->cm_id->qp; 783 } 784 785 return ret; 786 } 787 788 static void krping_free_qp(struct krping_cb *cb) 789 { 790 ib_destroy_qp(cb->qp); 791 ib_destroy_cq(cb->cq); 792 ib_dealloc_pd(cb->pd); 793 } 794 795 static int krping_setup_qp(struct krping_cb *cb, struct rdma_cm_id *cm_id) 796 { 797 int ret; 798 cb->pd = ib_alloc_pd(cm_id->device); 799 if (IS_ERR(cb->pd)) { 800 PRINTF(cb, "ib_alloc_pd failed\n"); 801 return PTR_ERR(cb->pd); 802 } 803 DEBUG_LOG(cb, "created pd %p\n", cb->pd); 804 805 strlcpy(cb->stats.name, cb->pd->device->name, sizeof(cb->stats.name)); 806 807 cb->cq = ib_create_cq(cm_id->device, krping_cq_event_handler, NULL, 808 cb, cb->txdepth * 2, 0); 809 if (IS_ERR(cb->cq)) { 810 PRINTF(cb, "ib_create_cq failed\n"); 811 ret = PTR_ERR(cb->cq); 812 goto err1; 813 } 814 DEBUG_LOG(cb, "created cq %p\n", cb->cq); 815 816 if (!cb->wlat && !cb->rlat && !cb->bw && !cb->frtest) { 817 ret = ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP); 818 if (ret) { 819 PRINTF(cb, "ib_create_cq failed\n"); 820 goto err2; 821 } 822 } 823 824 ret = krping_create_qp(cb); 825 if (ret) { 826 PRINTF(cb, "krping_create_qp failed: %d\n", ret); 827 goto err2; 828 } 829 DEBUG_LOG(cb, "created qp %p\n", cb->qp); 830 return 0; 831 err2: 832 ib_destroy_cq(cb->cq); 833 err1: 834 ib_dealloc_pd(cb->pd); 835 return ret; 836 } 837 838 /* 839 * return the (possibly rebound) rkey for the rdma buffer. 840 * FASTREG mode: invalidate and rebind via fastreg wr. 841 * MW mode: rebind the MW. 842 * other modes: just return the mr rkey. 843 */ 844 static u32 krping_rdma_rkey(struct krping_cb *cb, u64 buf, int post_inv) 845 { 846 u32 rkey = 0xffffffff; 847 u64 p; 848 struct ib_send_wr *bad_wr; 849 int i; 850 int ret; 851 852 switch (cb->mem) { 853 case FASTREG: 854 cb->invalidate_wr.ex.invalidate_rkey = cb->fastreg_mr->rkey; 855 856 /* 857 * Update the fastreg key. 858 */ 859 ib_update_fast_reg_key(cb->fastreg_mr, ++cb->key); 860 cb->fastreg_wr.wr.fast_reg.rkey = cb->fastreg_mr->rkey; 861 862 /* 863 * Update the fastreg WR with new buf info. 864 */ 865 if (buf == (u64)cb->start_dma_addr) 866 cb->fastreg_wr.wr.fast_reg.access_flags = IB_ACCESS_REMOTE_READ; 867 else 868 cb->fastreg_wr.wr.fast_reg.access_flags = IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE; 869 cb->fastreg_wr.wr.fast_reg.iova_start = buf; 870 p = (u64)(buf & PAGE_MASK); 871 for (i=0; i < cb->fastreg_wr.wr.fast_reg.page_list_len; 872 i++, p += PAGE_SIZE) { 873 cb->page_list->page_list[i] = p; 874 DEBUG_LOG(cb, "page_list[%d] 0x%llx\n", i, p); 875 } 876 877 DEBUG_LOG(cb, "post_inv = %d, fastreg new rkey 0x%x shift %u len %u" 878 " iova_start %llx page_list_len %u\n", 879 post_inv, 880 cb->fastreg_wr.wr.fast_reg.rkey, 881 cb->fastreg_wr.wr.fast_reg.page_shift, 882 cb->fastreg_wr.wr.fast_reg.length, 883 cb->fastreg_wr.wr.fast_reg.iova_start, 884 cb->fastreg_wr.wr.fast_reg.page_list_len); 885 886 if (post_inv) 887 ret = ib_post_send(cb->qp, &cb->invalidate_wr, &bad_wr); 888 else 889 ret = ib_post_send(cb->qp, &cb->fastreg_wr, &bad_wr); 890 if (ret) { 891 PRINTF(cb, "post send error %d\n", ret); 892 cb->state = ERROR; 893 } 894 rkey = cb->fastreg_mr->rkey; 895 break; 896 case MW: 897 /* 898 * Update the MW with new buf info. 899 */ 900 if (buf == (u64)cb->start_dma_addr) { 901 cb->bind_attr.bind_info.mw_access_flags = IB_ACCESS_REMOTE_READ; 902 cb->bind_attr.bind_info.mr = cb->start_mr; 903 } else { 904 cb->bind_attr.bind_info.mw_access_flags = IB_ACCESS_REMOTE_WRITE; 905 cb->bind_attr.bind_info.mr = cb->rdma_mr; 906 } 907 cb->bind_attr.bind_info.addr = buf; 908 DEBUG_LOG(cb, "binding mw rkey 0x%x to buf %llx mr rkey 0x%x\n", 909 cb->mw->rkey, buf, cb->bind_attr.bind_info.mr->rkey); 910 ret = ib_bind_mw(cb->qp, cb->mw, &cb->bind_attr); 911 if (ret) { 912 PRINTF(cb, "bind mw error %d\n", ret); 913 cb->state = ERROR; 914 } else 915 rkey = cb->mw->rkey; 916 break; 917 case MR: 918 if (buf == (u64)cb->start_dma_addr) 919 rkey = cb->start_mr->rkey; 920 else 921 rkey = cb->rdma_mr->rkey; 922 break; 923 case DMA: 924 rkey = cb->dma_mr->rkey; 925 break; 926 default: 927 PRINTF(cb, "%s:%d case ERROR\n", __func__, __LINE__); 928 cb->state = ERROR; 929 break; 930 } 931 return rkey; 932 } 933 934 static void krping_format_send(struct krping_cb *cb, u64 buf) 935 { 936 struct krping_rdma_info *info = &cb->send_buf; 937 u32 rkey; 938 939 /* 940 * Client side will do fastreg or mw bind before 941 * advertising the rdma buffer. Server side 942 * sends have no data. 943 */ 944 if (!cb->server || cb->wlat || cb->rlat || cb->bw) { 945 rkey = krping_rdma_rkey(cb, buf, !cb->server_invalidate); 946 info->buf = htonll(buf); 947 info->rkey = htonl(rkey); 948 info->size = htonl(cb->size); 949 DEBUG_LOG(cb, "RDMA addr %llx rkey %x len %d\n", 950 (unsigned long long)buf, rkey, cb->size); 951 } 952 } 953 954 static void krping_test_server(struct krping_cb *cb) 955 { 956 struct ib_send_wr *bad_wr, inv; 957 int ret; 958 959 while (1) { 960 /* Wait for client's Start STAG/TO/Len */ 961 wait_event_interruptible(cb->sem, cb->state >= RDMA_READ_ADV); 962 if (cb->state != RDMA_READ_ADV) { 963 PRINTF(cb, "wait for RDMA_READ_ADV state %d\n", 964 cb->state); 965 break; 966 } 967 968 DEBUG_LOG(cb, "server received sink adv\n"); 969 970 cb->rdma_sq_wr.wr.rdma.rkey = cb->remote_rkey; 971 cb->rdma_sq_wr.wr.rdma.remote_addr = cb->remote_addr; 972 cb->rdma_sq_wr.sg_list->length = cb->remote_len; 973 cb->rdma_sgl.lkey = krping_rdma_rkey(cb, cb->rdma_dma_addr, 1); 974 cb->rdma_sq_wr.next = NULL; 975 976 /* Issue RDMA Read. */ 977 if (cb->read_inv) 978 cb->rdma_sq_wr.opcode = IB_WR_RDMA_READ_WITH_INV; 979 else { 980 981 cb->rdma_sq_wr.opcode = IB_WR_RDMA_READ; 982 if (cb->mem == FASTREG) { 983 /* 984 * Immediately follow the read with a 985 * fenced LOCAL_INV. 986 */ 987 cb->rdma_sq_wr.next = &inv; 988 memset(&inv, 0, sizeof inv); 989 inv.opcode = IB_WR_LOCAL_INV; 990 inv.ex.invalidate_rkey = cb->fastreg_mr->rkey; 991 inv.send_flags = IB_SEND_FENCE; 992 } 993 } 994 995 ret = ib_post_send(cb->qp, &cb->rdma_sq_wr, &bad_wr); 996 if (ret) { 997 PRINTF(cb, "post send error %d\n", ret); 998 break; 999 } 1000 cb->rdma_sq_wr.next = NULL; 1001 1002 DEBUG_LOG(cb, "server posted rdma read req \n"); 1003 1004 /* Wait for read completion */ 1005 wait_event_interruptible(cb->sem, 1006 cb->state >= RDMA_READ_COMPLETE); 1007 if (cb->state != RDMA_READ_COMPLETE) { 1008 PRINTF(cb, 1009 "wait for RDMA_READ_COMPLETE state %d\n", 1010 cb->state); 1011 break; 1012 } 1013 DEBUG_LOG(cb, "server received read complete\n"); 1014 1015 /* Display data in recv buf */ 1016 if (cb->verbose) { 1017 if (strlen(cb->rdma_buf) > 128) { 1018 char msgbuf[128]; 1019 1020 strlcpy(msgbuf, cb->rdma_buf, sizeof(msgbuf)); 1021 PRINTF(cb, "server ping data stripped: %s\n", 1022 msgbuf); 1023 } else 1024 PRINTF(cb, "server ping data: %s\n", 1025 cb->rdma_buf); 1026 } 1027 1028 /* Tell client to continue */ 1029 if (cb->server && cb->server_invalidate) { 1030 cb->sq_wr.ex.invalidate_rkey = cb->remote_rkey; 1031 cb->sq_wr.opcode = IB_WR_SEND_WITH_INV; 1032 DEBUG_LOG(cb, "send-w-inv rkey 0x%x\n", cb->remote_rkey); 1033 } 1034 ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr); 1035 if (ret) { 1036 PRINTF(cb, "post send error %d\n", ret); 1037 break; 1038 } 1039 DEBUG_LOG(cb, "server posted go ahead\n"); 1040 1041 /* Wait for client's RDMA STAG/TO/Len */ 1042 wait_event_interruptible(cb->sem, cb->state >= RDMA_WRITE_ADV); 1043 if (cb->state != RDMA_WRITE_ADV) { 1044 PRINTF(cb, 1045 "wait for RDMA_WRITE_ADV state %d\n", 1046 cb->state); 1047 break; 1048 } 1049 DEBUG_LOG(cb, "server received sink adv\n"); 1050 1051 /* RDMA Write echo data */ 1052 cb->rdma_sq_wr.opcode = IB_WR_RDMA_WRITE; 1053 cb->rdma_sq_wr.wr.rdma.rkey = cb->remote_rkey; 1054 cb->rdma_sq_wr.wr.rdma.remote_addr = cb->remote_addr; 1055 cb->rdma_sq_wr.sg_list->length = strlen(cb->rdma_buf) + 1; 1056 if (cb->local_dma_lkey) 1057 cb->rdma_sgl.lkey = cb->qp->device->local_dma_lkey; 1058 else 1059 cb->rdma_sgl.lkey = krping_rdma_rkey(cb, cb->rdma_dma_addr, 0); 1060 1061 DEBUG_LOG(cb, "rdma write from lkey %x laddr %llx len %d\n", 1062 cb->rdma_sq_wr.sg_list->lkey, 1063 (unsigned long long)cb->rdma_sq_wr.sg_list->addr, 1064 cb->rdma_sq_wr.sg_list->length); 1065 1066 ret = ib_post_send(cb->qp, &cb->rdma_sq_wr, &bad_wr); 1067 if (ret) { 1068 PRINTF(cb, "post send error %d\n", ret); 1069 break; 1070 } 1071 1072 /* Wait for completion */ 1073 ret = wait_event_interruptible(cb->sem, cb->state >= 1074 RDMA_WRITE_COMPLETE); 1075 if (cb->state != RDMA_WRITE_COMPLETE) { 1076 PRINTF(cb, 1077 "wait for RDMA_WRITE_COMPLETE state %d\n", 1078 cb->state); 1079 break; 1080 } 1081 DEBUG_LOG(cb, "server rdma write complete \n"); 1082 1083 cb->state = CONNECTED; 1084 1085 /* Tell client to begin again */ 1086 if (cb->server && cb->server_invalidate) { 1087 cb->sq_wr.ex.invalidate_rkey = cb->remote_rkey; 1088 cb->sq_wr.opcode = IB_WR_SEND_WITH_INV; 1089 DEBUG_LOG(cb, "send-w-inv rkey 0x%x\n", cb->remote_rkey); 1090 } 1091 ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr); 1092 if (ret) { 1093 PRINTF(cb, "post send error %d\n", ret); 1094 break; 1095 } 1096 DEBUG_LOG(cb, "server posted go ahead\n"); 1097 } 1098 } 1099 1100 static void rlat_test(struct krping_cb *cb) 1101 { 1102 int scnt; 1103 int iters = cb->count; 1104 struct timeval start_tv, stop_tv; 1105 int ret; 1106 struct ib_wc wc; 1107 struct ib_send_wr *bad_wr; 1108 int ne; 1109 1110 scnt = 0; 1111 cb->rdma_sq_wr.opcode = IB_WR_RDMA_READ; 1112 cb->rdma_sq_wr.wr.rdma.rkey = cb->remote_rkey; 1113 cb->rdma_sq_wr.wr.rdma.remote_addr = cb->remote_addr; 1114 cb->rdma_sq_wr.sg_list->length = cb->size; 1115 1116 microtime(&start_tv); 1117 if (!cb->poll) { 1118 cb->state = RDMA_READ_ADV; 1119 ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP); 1120 } 1121 while (scnt < iters) { 1122 1123 cb->state = RDMA_READ_ADV; 1124 ret = ib_post_send(cb->qp, &cb->rdma_sq_wr, &bad_wr); 1125 if (ret) { 1126 PRINTF(cb, 1127 "Couldn't post send: ret=%d scnt %d\n", 1128 ret, scnt); 1129 return; 1130 } 1131 1132 do { 1133 if (!cb->poll) { 1134 wait_event_interruptible(cb->sem, 1135 cb->state != RDMA_READ_ADV); 1136 if (cb->state == RDMA_READ_COMPLETE) { 1137 ne = 1; 1138 ib_req_notify_cq(cb->cq, 1139 IB_CQ_NEXT_COMP); 1140 } else { 1141 ne = -1; 1142 } 1143 } else 1144 ne = ib_poll_cq(cb->cq, 1, &wc); 1145 if (cb->state == ERROR) { 1146 PRINTF(cb, 1147 "state == ERROR...bailing scnt %d\n", 1148 scnt); 1149 return; 1150 } 1151 } while (ne == 0); 1152 1153 if (ne < 0) { 1154 PRINTF(cb, "poll CQ failed %d\n", ne); 1155 return; 1156 } 1157 if (cb->poll && wc.status != IB_WC_SUCCESS) { 1158 PRINTF(cb, "Completion wth error at %s:\n", 1159 cb->server ? "server" : "client"); 1160 PRINTF(cb, "Failed status %d: wr_id %d\n", 1161 wc.status, (int) wc.wr_id); 1162 return; 1163 } 1164 ++scnt; 1165 } 1166 microtime(&stop_tv); 1167 1168 if (stop_tv.tv_usec < start_tv.tv_usec) { 1169 stop_tv.tv_usec += 1000000; 1170 stop_tv.tv_sec -= 1; 1171 } 1172 1173 PRINTF(cb, "delta sec %lu delta usec %lu iter %d size %d\n", 1174 stop_tv.tv_sec - start_tv.tv_sec, 1175 stop_tv.tv_usec - start_tv.tv_usec, 1176 scnt, cb->size); 1177 } 1178 1179 static void wlat_test(struct krping_cb *cb) 1180 { 1181 int ccnt, scnt, rcnt; 1182 int iters=cb->count; 1183 volatile char *poll_buf = (char *) cb->start_buf; 1184 char *buf = (char *)cb->rdma_buf; 1185 struct timeval start_tv, stop_tv; 1186 cycles_t *post_cycles_start, *post_cycles_stop; 1187 cycles_t *poll_cycles_start, *poll_cycles_stop; 1188 cycles_t *last_poll_cycles_start; 1189 cycles_t sum_poll = 0, sum_post = 0, sum_last_poll = 0; 1190 int i; 1191 int cycle_iters = 1000; 1192 1193 ccnt = 0; 1194 scnt = 0; 1195 rcnt = 0; 1196 1197 post_cycles_start = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL); 1198 if (!post_cycles_start) { 1199 PRINTF(cb, "%s kmalloc failed\n", __FUNCTION__); 1200 return; 1201 } 1202 post_cycles_stop = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL); 1203 if (!post_cycles_stop) { 1204 PRINTF(cb, "%s kmalloc failed\n", __FUNCTION__); 1205 return; 1206 } 1207 poll_cycles_start = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL); 1208 if (!poll_cycles_start) { 1209 PRINTF(cb, "%s kmalloc failed\n", __FUNCTION__); 1210 return; 1211 } 1212 poll_cycles_stop = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL); 1213 if (!poll_cycles_stop) { 1214 PRINTF(cb, "%s kmalloc failed\n", __FUNCTION__); 1215 return; 1216 } 1217 last_poll_cycles_start = kmalloc(cycle_iters * sizeof(cycles_t), 1218 GFP_KERNEL); 1219 if (!last_poll_cycles_start) { 1220 PRINTF(cb, "%s kmalloc failed\n", __FUNCTION__); 1221 return; 1222 } 1223 cb->rdma_sq_wr.opcode = IB_WR_RDMA_WRITE; 1224 cb->rdma_sq_wr.wr.rdma.rkey = cb->remote_rkey; 1225 cb->rdma_sq_wr.wr.rdma.remote_addr = cb->remote_addr; 1226 cb->rdma_sq_wr.sg_list->length = cb->size; 1227 1228 if (cycle_iters > iters) 1229 cycle_iters = iters; 1230 microtime(&start_tv); 1231 while (scnt < iters || ccnt < iters || rcnt < iters) { 1232 1233 /* Wait till buffer changes. */ 1234 if (rcnt < iters && !(scnt < 1 && !cb->server)) { 1235 ++rcnt; 1236 while (*poll_buf != (char)rcnt) { 1237 if (cb->state == ERROR) { 1238 PRINTF(cb, 1239 "state = ERROR, bailing\n"); 1240 return; 1241 } 1242 } 1243 } 1244 1245 if (scnt < iters) { 1246 struct ib_send_wr *bad_wr; 1247 1248 *buf = (char)scnt+1; 1249 if (scnt < cycle_iters) 1250 post_cycles_start[scnt] = get_cycles(); 1251 if (ib_post_send(cb->qp, &cb->rdma_sq_wr, &bad_wr)) { 1252 PRINTF(cb, 1253 "Couldn't post send: scnt=%d\n", 1254 scnt); 1255 return; 1256 } 1257 if (scnt < cycle_iters) 1258 post_cycles_stop[scnt] = get_cycles(); 1259 scnt++; 1260 } 1261 1262 if (ccnt < iters) { 1263 struct ib_wc wc; 1264 int ne; 1265 1266 if (ccnt < cycle_iters) 1267 poll_cycles_start[ccnt] = get_cycles(); 1268 do { 1269 if (ccnt < cycle_iters) 1270 last_poll_cycles_start[ccnt] = 1271 get_cycles(); 1272 ne = ib_poll_cq(cb->cq, 1, &wc); 1273 } while (ne == 0); 1274 if (ccnt < cycle_iters) 1275 poll_cycles_stop[ccnt] = get_cycles(); 1276 ++ccnt; 1277 1278 if (ne < 0) { 1279 PRINTF(cb, "poll CQ failed %d\n", ne); 1280 return; 1281 } 1282 if (wc.status != IB_WC_SUCCESS) { 1283 PRINTF(cb, 1284 "Completion wth error at %s:\n", 1285 cb->server ? "server" : "client"); 1286 PRINTF(cb, 1287 "Failed status %d: wr_id %d\n", 1288 wc.status, (int) wc.wr_id); 1289 PRINTF(cb, 1290 "scnt=%d, rcnt=%d, ccnt=%d\n", 1291 scnt, rcnt, ccnt); 1292 return; 1293 } 1294 } 1295 } 1296 microtime(&stop_tv); 1297 1298 if (stop_tv.tv_usec < start_tv.tv_usec) { 1299 stop_tv.tv_usec += 1000000; 1300 stop_tv.tv_sec -= 1; 1301 } 1302 1303 for (i=0; i < cycle_iters; i++) { 1304 sum_post += post_cycles_stop[i] - post_cycles_start[i]; 1305 sum_poll += poll_cycles_stop[i] - poll_cycles_start[i]; 1306 sum_last_poll += poll_cycles_stop[i]-last_poll_cycles_start[i]; 1307 } 1308 PRINTF(cb, 1309 "delta sec %lu delta usec %lu iter %d size %d cycle_iters %d" 1310 " sum_post %llu sum_poll %llu sum_last_poll %llu\n", 1311 stop_tv.tv_sec - start_tv.tv_sec, 1312 stop_tv.tv_usec - start_tv.tv_usec, 1313 scnt, cb->size, cycle_iters, 1314 (unsigned long long)sum_post, (unsigned long long)sum_poll, 1315 (unsigned long long)sum_last_poll); 1316 kfree(post_cycles_start); 1317 kfree(post_cycles_stop); 1318 kfree(poll_cycles_start); 1319 kfree(poll_cycles_stop); 1320 kfree(last_poll_cycles_start); 1321 } 1322 1323 static void bw_test(struct krping_cb *cb) 1324 { 1325 int ccnt, scnt, rcnt; 1326 int iters=cb->count; 1327 struct timeval start_tv, stop_tv; 1328 cycles_t *post_cycles_start, *post_cycles_stop; 1329 cycles_t *poll_cycles_start, *poll_cycles_stop; 1330 cycles_t *last_poll_cycles_start; 1331 cycles_t sum_poll = 0, sum_post = 0, sum_last_poll = 0; 1332 int i; 1333 int cycle_iters = 1000; 1334 1335 ccnt = 0; 1336 scnt = 0; 1337 rcnt = 0; 1338 1339 post_cycles_start = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL); 1340 if (!post_cycles_start) { 1341 PRINTF(cb, "%s kmalloc failed\n", __FUNCTION__); 1342 return; 1343 } 1344 post_cycles_stop = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL); 1345 if (!post_cycles_stop) { 1346 PRINTF(cb, "%s kmalloc failed\n", __FUNCTION__); 1347 return; 1348 } 1349 poll_cycles_start = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL); 1350 if (!poll_cycles_start) { 1351 PRINTF(cb, "%s kmalloc failed\n", __FUNCTION__); 1352 return; 1353 } 1354 poll_cycles_stop = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL); 1355 if (!poll_cycles_stop) { 1356 PRINTF(cb, "%s kmalloc failed\n", __FUNCTION__); 1357 return; 1358 } 1359 last_poll_cycles_start = kmalloc(cycle_iters * sizeof(cycles_t), 1360 GFP_KERNEL); 1361 if (!last_poll_cycles_start) { 1362 PRINTF(cb, "%s kmalloc failed\n", __FUNCTION__); 1363 return; 1364 } 1365 cb->rdma_sq_wr.opcode = IB_WR_RDMA_WRITE; 1366 cb->rdma_sq_wr.wr.rdma.rkey = cb->remote_rkey; 1367 cb->rdma_sq_wr.wr.rdma.remote_addr = cb->remote_addr; 1368 cb->rdma_sq_wr.sg_list->length = cb->size; 1369 1370 if (cycle_iters > iters) 1371 cycle_iters = iters; 1372 microtime(&start_tv); 1373 while (scnt < iters || ccnt < iters) { 1374 1375 while (scnt < iters && scnt - ccnt < cb->txdepth) { 1376 struct ib_send_wr *bad_wr; 1377 1378 if (scnt < cycle_iters) 1379 post_cycles_start[scnt] = get_cycles(); 1380 if (ib_post_send(cb->qp, &cb->rdma_sq_wr, &bad_wr)) { 1381 PRINTF(cb, 1382 "Couldn't post send: scnt=%d\n", 1383 scnt); 1384 return; 1385 } 1386 if (scnt < cycle_iters) 1387 post_cycles_stop[scnt] = get_cycles(); 1388 ++scnt; 1389 } 1390 1391 if (ccnt < iters) { 1392 int ne; 1393 struct ib_wc wc; 1394 1395 if (ccnt < cycle_iters) 1396 poll_cycles_start[ccnt] = get_cycles(); 1397 do { 1398 if (ccnt < cycle_iters) 1399 last_poll_cycles_start[ccnt] = 1400 get_cycles(); 1401 ne = ib_poll_cq(cb->cq, 1, &wc); 1402 } while (ne == 0); 1403 if (ccnt < cycle_iters) 1404 poll_cycles_stop[ccnt] = get_cycles(); 1405 ccnt += 1; 1406 1407 if (ne < 0) { 1408 PRINTF(cb, "poll CQ failed %d\n", ne); 1409 return; 1410 } 1411 if (wc.status != IB_WC_SUCCESS) { 1412 PRINTF(cb, 1413 "Completion wth error at %s:\n", 1414 cb->server ? "server" : "client"); 1415 PRINTF(cb, 1416 "Failed status %d: wr_id %d\n", 1417 wc.status, (int) wc.wr_id); 1418 return; 1419 } 1420 } 1421 } 1422 microtime(&stop_tv); 1423 1424 if (stop_tv.tv_usec < start_tv.tv_usec) { 1425 stop_tv.tv_usec += 1000000; 1426 stop_tv.tv_sec -= 1; 1427 } 1428 1429 for (i=0; i < cycle_iters; i++) { 1430 sum_post += post_cycles_stop[i] - post_cycles_start[i]; 1431 sum_poll += poll_cycles_stop[i] - poll_cycles_start[i]; 1432 sum_last_poll += poll_cycles_stop[i]-last_poll_cycles_start[i]; 1433 } 1434 PRINTF(cb, 1435 "delta sec %lu delta usec %lu iter %d size %d cycle_iters %d" 1436 " sum_post %llu sum_poll %llu sum_last_poll %llu\n", 1437 stop_tv.tv_sec - start_tv.tv_sec, 1438 stop_tv.tv_usec - start_tv.tv_usec, 1439 scnt, cb->size, cycle_iters, 1440 (unsigned long long)sum_post, (unsigned long long)sum_poll, 1441 (unsigned long long)sum_last_poll); 1442 kfree(post_cycles_start); 1443 kfree(post_cycles_stop); 1444 kfree(poll_cycles_start); 1445 kfree(poll_cycles_stop); 1446 kfree(last_poll_cycles_start); 1447 } 1448 1449 static void krping_rlat_test_server(struct krping_cb *cb) 1450 { 1451 struct ib_send_wr *bad_wr; 1452 struct ib_wc wc; 1453 int ret; 1454 1455 /* Spin waiting for client's Start STAG/TO/Len */ 1456 while (cb->state < RDMA_READ_ADV) { 1457 krping_cq_event_handler(cb->cq, cb); 1458 } 1459 1460 /* Send STAG/TO/Len to client */ 1461 krping_format_send(cb, cb->start_dma_addr); 1462 ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr); 1463 if (ret) { 1464 PRINTF(cb, "post send error %d\n", ret); 1465 return; 1466 } 1467 1468 /* Spin waiting for send completion */ 1469 while ((ret = ib_poll_cq(cb->cq, 1, &wc) == 0)); 1470 if (ret < 0) { 1471 PRINTF(cb, "poll error %d\n", ret); 1472 return; 1473 } 1474 if (wc.status) { 1475 PRINTF(cb, "send completiong error %d\n", wc.status); 1476 return; 1477 } 1478 1479 wait_event_interruptible(cb->sem, cb->state == ERROR); 1480 } 1481 1482 static void krping_wlat_test_server(struct krping_cb *cb) 1483 { 1484 struct ib_send_wr *bad_wr; 1485 struct ib_wc wc; 1486 int ret; 1487 1488 /* Spin waiting for client's Start STAG/TO/Len */ 1489 while (cb->state < RDMA_READ_ADV) { 1490 krping_cq_event_handler(cb->cq, cb); 1491 } 1492 1493 /* Send STAG/TO/Len to client */ 1494 krping_format_send(cb, cb->start_dma_addr); 1495 ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr); 1496 if (ret) { 1497 PRINTF(cb, "post send error %d\n", ret); 1498 return; 1499 } 1500 1501 /* Spin waiting for send completion */ 1502 while ((ret = ib_poll_cq(cb->cq, 1, &wc) == 0)); 1503 if (ret < 0) { 1504 PRINTF(cb, "poll error %d\n", ret); 1505 return; 1506 } 1507 if (wc.status) { 1508 PRINTF(cb, "send completiong error %d\n", wc.status); 1509 return; 1510 } 1511 1512 wlat_test(cb); 1513 wait_event_interruptible(cb->sem, cb->state == ERROR); 1514 } 1515 1516 static void krping_bw_test_server(struct krping_cb *cb) 1517 { 1518 struct ib_send_wr *bad_wr; 1519 struct ib_wc wc; 1520 int ret; 1521 1522 /* Spin waiting for client's Start STAG/TO/Len */ 1523 while (cb->state < RDMA_READ_ADV) { 1524 krping_cq_event_handler(cb->cq, cb); 1525 } 1526 1527 /* Send STAG/TO/Len to client */ 1528 krping_format_send(cb, cb->start_dma_addr); 1529 ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr); 1530 if (ret) { 1531 PRINTF(cb, "post send error %d\n", ret); 1532 return; 1533 } 1534 1535 /* Spin waiting for send completion */ 1536 while ((ret = ib_poll_cq(cb->cq, 1, &wc) == 0)); 1537 if (ret < 0) { 1538 PRINTF(cb, "poll error %d\n", ret); 1539 return; 1540 } 1541 if (wc.status) { 1542 PRINTF(cb, "send completiong error %d\n", wc.status); 1543 return; 1544 } 1545 1546 if (cb->duplex) 1547 bw_test(cb); 1548 wait_event_interruptible(cb->sem, cb->state == ERROR); 1549 } 1550 1551 static int fastreg_supported(struct krping_cb *cb) 1552 { 1553 struct ib_device *dev = cb->child_cm_id->device; 1554 struct ib_device_attr attr; 1555 int ret; 1556 1557 ret = ib_query_device(dev, &attr); 1558 if (ret) { 1559 PRINTF(cb, "ib_query_device failed ret %d\n", ret); 1560 return 0; 1561 } 1562 if (!(attr.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS)) { 1563 PRINTF(cb, "Fastreg not supported - device_cap_flags 0x%x\n", 1564 attr.device_cap_flags); 1565 return 0; 1566 } 1567 DEBUG_LOG(cb, "Fastreg supported - device_cap_flags 0x%x\n", 1568 attr.device_cap_flags); 1569 return 1; 1570 } 1571 1572 static int krping_bind_server(struct krping_cb *cb) 1573 { 1574 struct sockaddr_in sin; 1575 int ret; 1576 1577 memset(&sin, 0, sizeof(sin)); 1578 sin.sin_len = sizeof sin; 1579 sin.sin_family = AF_INET; 1580 sin.sin_addr.s_addr = cb->addr.s_addr; 1581 sin.sin_port = cb->port; 1582 1583 ret = rdma_bind_addr(cb->cm_id, (struct sockaddr *) &sin); 1584 if (ret) { 1585 PRINTF(cb, "rdma_bind_addr error %d\n", ret); 1586 return ret; 1587 } 1588 DEBUG_LOG(cb, "rdma_bind_addr successful\n"); 1589 1590 DEBUG_LOG(cb, "rdma_listen\n"); 1591 ret = rdma_listen(cb->cm_id, 3); 1592 if (ret) { 1593 PRINTF(cb, "rdma_listen failed: %d\n", ret); 1594 return ret; 1595 } 1596 1597 wait_event_interruptible(cb->sem, cb->state >= CONNECT_REQUEST); 1598 if (cb->state != CONNECT_REQUEST) { 1599 PRINTF(cb, "wait for CONNECT_REQUEST state %d\n", 1600 cb->state); 1601 return -1; 1602 } 1603 1604 if (cb->mem == FASTREG && !fastreg_supported(cb)) 1605 return -EINVAL; 1606 1607 return 0; 1608 } 1609 1610 static void krping_run_server(struct krping_cb *cb) 1611 { 1612 struct ib_recv_wr *bad_wr; 1613 int ret; 1614 1615 ret = krping_bind_server(cb); 1616 if (ret) 1617 return; 1618 1619 ret = krping_setup_qp(cb, cb->child_cm_id); 1620 if (ret) { 1621 PRINTF(cb, "setup_qp failed: %d\n", ret); 1622 goto err0; 1623 } 1624 1625 ret = krping_setup_buffers(cb); 1626 if (ret) { 1627 PRINTF(cb, "krping_setup_buffers failed: %d\n", ret); 1628 goto err1; 1629 } 1630 1631 ret = ib_post_recv(cb->qp, &cb->rq_wr, &bad_wr); 1632 if (ret) { 1633 PRINTF(cb, "ib_post_recv failed: %d\n", ret); 1634 goto err2; 1635 } 1636 1637 ret = krping_accept(cb); 1638 if (ret) { 1639 PRINTF(cb, "connect error %d\n", ret); 1640 goto err2; 1641 } 1642 1643 if (cb->wlat) 1644 krping_wlat_test_server(cb); 1645 else if (cb->rlat) 1646 krping_rlat_test_server(cb); 1647 else if (cb->bw) 1648 krping_bw_test_server(cb); 1649 else 1650 krping_test_server(cb); 1651 rdma_disconnect(cb->child_cm_id); 1652 err2: 1653 krping_free_buffers(cb); 1654 err1: 1655 krping_free_qp(cb); 1656 err0: 1657 rdma_destroy_id(cb->child_cm_id); 1658 } 1659 1660 static void krping_test_client(struct krping_cb *cb) 1661 { 1662 int ping, start, cc, i, ret; 1663 struct ib_send_wr *bad_wr; 1664 unsigned char c; 1665 1666 start = 65; 1667 for (ping = 0; !cb->count || ping < cb->count; ping++) { 1668 cb->state = RDMA_READ_ADV; 1669 1670 /* Put some ascii text in the buffer. */ 1671 cc = sprintf(cb->start_buf, "rdma-ping-%d: ", ping); 1672 for (i = cc, c = start; i < cb->size; i++) { 1673 cb->start_buf[i] = c; 1674 c++; 1675 if (c > 122) 1676 c = 65; 1677 } 1678 start++; 1679 if (start > 122) 1680 start = 65; 1681 cb->start_buf[cb->size - 1] = 0; 1682 1683 krping_format_send(cb, cb->start_dma_addr); 1684 if (cb->state == ERROR) { 1685 PRINTF(cb, "krping_format_send failed\n"); 1686 break; 1687 } 1688 ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr); 1689 if (ret) { 1690 PRINTF(cb, "post send error %d\n", ret); 1691 break; 1692 } 1693 1694 /* Wait for server to ACK */ 1695 wait_event_interruptible(cb->sem, cb->state >= RDMA_WRITE_ADV); 1696 if (cb->state != RDMA_WRITE_ADV) { 1697 PRINTF(cb, 1698 "wait for RDMA_WRITE_ADV state %d\n", 1699 cb->state); 1700 break; 1701 } 1702 1703 krping_format_send(cb, cb->rdma_dma_addr); 1704 ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr); 1705 if (ret) { 1706 PRINTF(cb, "post send error %d\n", ret); 1707 break; 1708 } 1709 1710 /* Wait for the server to say the RDMA Write is complete. */ 1711 wait_event_interruptible(cb->sem, 1712 cb->state >= RDMA_WRITE_COMPLETE); 1713 if (cb->state != RDMA_WRITE_COMPLETE) { 1714 PRINTF(cb, 1715 "wait for RDMA_WRITE_COMPLETE state %d\n", 1716 cb->state); 1717 break; 1718 } 1719 1720 if (cb->validate) 1721 if (memcmp(cb->start_buf, cb->rdma_buf, cb->size)) { 1722 PRINTF(cb, "data mismatch!\n"); 1723 break; 1724 } 1725 1726 if (cb->verbose) { 1727 if (strlen(cb->rdma_buf) > 128) { 1728 char msgbuf[128]; 1729 1730 strlcpy(msgbuf, cb->rdma_buf, sizeof(msgbuf)); 1731 PRINTF(cb, "ping data stripped: %s\n", 1732 msgbuf); 1733 } else 1734 PRINTF(cb, "ping data: %s\n", cb->rdma_buf); 1735 } 1736 #ifdef SLOW_KRPING 1737 wait_event_interruptible_timeout(cb->sem, cb->state == ERROR, HZ); 1738 #endif 1739 } 1740 } 1741 1742 static void krping_rlat_test_client(struct krping_cb *cb) 1743 { 1744 struct ib_send_wr *bad_wr; 1745 struct ib_wc wc; 1746 int ret; 1747 1748 cb->state = RDMA_READ_ADV; 1749 1750 /* Send STAG/TO/Len to client */ 1751 krping_format_send(cb, cb->start_dma_addr); 1752 if (cb->state == ERROR) { 1753 PRINTF(cb, "krping_format_send failed\n"); 1754 return; 1755 } 1756 ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr); 1757 if (ret) { 1758 PRINTF(cb, "post send error %d\n", ret); 1759 return; 1760 } 1761 1762 /* Spin waiting for send completion */ 1763 while ((ret = ib_poll_cq(cb->cq, 1, &wc) == 0)); 1764 if (ret < 0) { 1765 PRINTF(cb, "poll error %d\n", ret); 1766 return; 1767 } 1768 if (wc.status) { 1769 PRINTF(cb, "send completion error %d\n", wc.status); 1770 return; 1771 } 1772 1773 /* Spin waiting for server's Start STAG/TO/Len */ 1774 while (cb->state < RDMA_WRITE_ADV) { 1775 krping_cq_event_handler(cb->cq, cb); 1776 } 1777 1778 #if 0 1779 { 1780 int i; 1781 struct timeval start, stop; 1782 time_t sec; 1783 suseconds_t usec; 1784 unsigned long long elapsed; 1785 struct ib_wc wc; 1786 struct ib_send_wr *bad_wr; 1787 int ne; 1788 1789 cb->rdma_sq_wr.opcode = IB_WR_RDMA_WRITE; 1790 cb->rdma_sq_wr.wr.rdma.rkey = cb->remote_rkey; 1791 cb->rdma_sq_wr.wr.rdma.remote_addr = cb->remote_addr; 1792 cb->rdma_sq_wr.sg_list->length = 0; 1793 cb->rdma_sq_wr.num_sge = 0; 1794 1795 microtime(&start); 1796 for (i=0; i < 100000; i++) { 1797 if (ib_post_send(cb->qp, &cb->rdma_sq_wr, &bad_wr)) { 1798 PRINTF(cb, "Couldn't post send\n"); 1799 return; 1800 } 1801 do { 1802 ne = ib_poll_cq(cb->cq, 1, &wc); 1803 } while (ne == 0); 1804 if (ne < 0) { 1805 PRINTF(cb, "poll CQ failed %d\n", ne); 1806 return; 1807 } 1808 if (wc.status != IB_WC_SUCCESS) { 1809 PRINTF(cb, "Completion wth error at %s:\n", 1810 cb->server ? "server" : "client"); 1811 PRINTF(cb, "Failed status %d: wr_id %d\n", 1812 wc.status, (int) wc.wr_id); 1813 return; 1814 } 1815 } 1816 microtime(&stop); 1817 1818 if (stop.tv_usec < start.tv_usec) { 1819 stop.tv_usec += 1000000; 1820 stop.tv_sec -= 1; 1821 } 1822 sec = stop.tv_sec - start.tv_sec; 1823 usec = stop.tv_usec - start.tv_usec; 1824 elapsed = sec * 1000000 + usec; 1825 PRINTF(cb, "0B-write-lat iters 100000 usec %llu\n", elapsed); 1826 } 1827 #endif 1828 1829 rlat_test(cb); 1830 } 1831 1832 static void krping_wlat_test_client(struct krping_cb *cb) 1833 { 1834 struct ib_send_wr *bad_wr; 1835 struct ib_wc wc; 1836 int ret; 1837 1838 cb->state = RDMA_READ_ADV; 1839 1840 /* Send STAG/TO/Len to client */ 1841 krping_format_send(cb, cb->start_dma_addr); 1842 if (cb->state == ERROR) { 1843 PRINTF(cb, "krping_format_send failed\n"); 1844 return; 1845 } 1846 ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr); 1847 if (ret) { 1848 PRINTF(cb, "post send error %d\n", ret); 1849 return; 1850 } 1851 1852 /* Spin waiting for send completion */ 1853 while ((ret = ib_poll_cq(cb->cq, 1, &wc) == 0)); 1854 if (ret < 0) { 1855 PRINTF(cb, "poll error %d\n", ret); 1856 return; 1857 } 1858 if (wc.status) { 1859 PRINTF(cb, "send completion error %d\n", wc.status); 1860 return; 1861 } 1862 1863 /* Spin waiting for server's Start STAG/TO/Len */ 1864 while (cb->state < RDMA_WRITE_ADV) { 1865 krping_cq_event_handler(cb->cq, cb); 1866 } 1867 1868 wlat_test(cb); 1869 } 1870 1871 static void krping_bw_test_client(struct krping_cb *cb) 1872 { 1873 struct ib_send_wr *bad_wr; 1874 struct ib_wc wc; 1875 int ret; 1876 1877 cb->state = RDMA_READ_ADV; 1878 1879 /* Send STAG/TO/Len to client */ 1880 krping_format_send(cb, cb->start_dma_addr); 1881 if (cb->state == ERROR) { 1882 PRINTF(cb, "krping_format_send failed\n"); 1883 return; 1884 } 1885 ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr); 1886 if (ret) { 1887 PRINTF(cb, "post send error %d\n", ret); 1888 return; 1889 } 1890 1891 /* Spin waiting for send completion */ 1892 while ((ret = ib_poll_cq(cb->cq, 1, &wc) == 0)); 1893 if (ret < 0) { 1894 PRINTF(cb, "poll error %d\n", ret); 1895 return; 1896 } 1897 if (wc.status) { 1898 PRINTF(cb, "send completion error %d\n", wc.status); 1899 return; 1900 } 1901 1902 /* Spin waiting for server's Start STAG/TO/Len */ 1903 while (cb->state < RDMA_WRITE_ADV) { 1904 krping_cq_event_handler(cb->cq, cb); 1905 } 1906 1907 bw_test(cb); 1908 } 1909 1910 static void krping_fr_test(struct krping_cb *cb) 1911 { 1912 struct ib_fast_reg_page_list *pl; 1913 struct ib_send_wr fr, inv, *bad; 1914 struct ib_wc wc; 1915 u8 key = 0; 1916 struct ib_mr *mr; 1917 int i; 1918 int ret; 1919 int size = cb->size; 1920 int plen = (((size - 1) & PAGE_MASK) + PAGE_SIZE) >> PAGE_SHIFT; 1921 time_t start; 1922 int count = 0; 1923 int scnt = 0; 1924 1925 pl = ib_alloc_fast_reg_page_list(cb->qp->device, plen); 1926 if (IS_ERR(pl)) { 1927 PRINTF(cb, "ib_alloc_fast_reg_page_list failed %ld\n", PTR_ERR(pl)); 1928 return; 1929 } 1930 1931 mr = ib_alloc_fast_reg_mr(cb->pd, plen); 1932 if (IS_ERR(mr)) { 1933 PRINTF(cb, "ib_alloc_fast_reg_mr failed %ld\n", PTR_ERR(pl)); 1934 goto err1; 1935 } 1936 1937 for (i=0; i<plen; i++) 1938 pl->page_list[i] = 0xcafebabe | i; 1939 1940 memset(&fr, 0, sizeof fr); 1941 fr.opcode = IB_WR_FAST_REG_MR; 1942 fr.wr.fast_reg.page_shift = PAGE_SHIFT; 1943 fr.wr.fast_reg.length = size; 1944 fr.wr.fast_reg.page_list = pl; 1945 fr.wr.fast_reg.page_list_len = plen; 1946 fr.wr.fast_reg.iova_start = 0; 1947 fr.wr.fast_reg.access_flags = IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE; 1948 fr.next = &inv; 1949 memset(&inv, 0, sizeof inv); 1950 inv.opcode = IB_WR_LOCAL_INV; 1951 inv.send_flags = IB_SEND_SIGNALED; 1952 1953 DEBUG_LOG(cb, "fr_test: stag index 0x%x plen %u size %u depth %u\n", mr->rkey >> 8, plen, cb->size, cb->txdepth); 1954 start = time_uptime; 1955 while (1) { 1956 if ((time_uptime - start) >= 9) { 1957 DEBUG_LOG(cb, "fr_test: pausing 1 second! count %u latest size %u plen %u\n", count, size, plen); 1958 wait_event_interruptible(cb->sem, cb->state == ERROR); 1959 if (cb->state == ERROR) 1960 break; 1961 start = time_uptime; 1962 } 1963 while (scnt < (cb->txdepth>>1)) { 1964 ib_update_fast_reg_key(mr, ++key); 1965 fr.wr.fast_reg.rkey = mr->rkey; 1966 inv.ex.invalidate_rkey = mr->rkey; 1967 size = arc4random() % cb->size; 1968 if (size == 0) 1969 size = cb->size; 1970 plen = (((size - 1) & PAGE_MASK) + PAGE_SIZE) >> PAGE_SHIFT; 1971 fr.wr.fast_reg.length = size; 1972 fr.wr.fast_reg.page_list_len = plen; 1973 ret = ib_post_send(cb->qp, &fr, &bad); 1974 if (ret) { 1975 PRINTF(cb, "ib_post_send failed %d\n", ret); 1976 goto err2; 1977 } 1978 scnt++; 1979 } 1980 1981 do { 1982 ret = ib_poll_cq(cb->cq, 1, &wc); 1983 if (ret < 0) { 1984 PRINTF(cb, "ib_poll_cq failed %d\n", ret); 1985 goto err2; 1986 } 1987 if (ret == 1) { 1988 if (wc.status) { 1989 PRINTF(cb, "completion error %u\n", wc.status); 1990 goto err2; 1991 } 1992 count++; 1993 scnt--; 1994 } 1995 else if (krping_sigpending()) { 1996 PRINTF(cb, "signal!\n"); 1997 goto err2; 1998 } 1999 } while (ret == 1); 2000 } 2001 err2: 2002 #if 0 2003 DEBUG_LOG(cb, "sleeping 1 second\n"); 2004 wait_event_interruptible_timeout(cb->sem, cb->state == ERROR, HZ); 2005 #endif 2006 DEBUG_LOG(cb, "draining the cq...\n"); 2007 do { 2008 ret = ib_poll_cq(cb->cq, 1, &wc); 2009 if (ret < 0) { 2010 PRINTF(cb, "ib_poll_cq failed %d\n", ret); 2011 break; 2012 } 2013 if (ret == 1) { 2014 if (wc.status) { 2015 PRINTF(cb, "completion error %u opcode %u\n", wc.status, wc.opcode); 2016 } 2017 } 2018 } while (ret == 1); 2019 DEBUG_LOG(cb, "fr_test: done!\n"); 2020 ib_dereg_mr(mr); 2021 err1: 2022 ib_free_fast_reg_page_list(pl); 2023 } 2024 2025 static int krping_connect_client(struct krping_cb *cb) 2026 { 2027 struct rdma_conn_param conn_param; 2028 int ret; 2029 2030 memset(&conn_param, 0, sizeof conn_param); 2031 conn_param.responder_resources = 1; 2032 conn_param.initiator_depth = 1; 2033 conn_param.retry_count = 10; 2034 2035 ret = rdma_connect(cb->cm_id, &conn_param); 2036 if (ret) { 2037 PRINTF(cb, "rdma_connect error %d\n", ret); 2038 return ret; 2039 } 2040 2041 wait_event_interruptible(cb->sem, cb->state >= CONNECTED); 2042 if (cb->state == ERROR) { 2043 PRINTF(cb, "wait for CONNECTED state %d\n", cb->state); 2044 return -1; 2045 } 2046 2047 DEBUG_LOG(cb, "rdma_connect successful\n"); 2048 return 0; 2049 } 2050 2051 static int krping_bind_client(struct krping_cb *cb) 2052 { 2053 struct sockaddr_in sin; 2054 int ret; 2055 2056 memset(&sin, 0, sizeof(sin)); 2057 sin.sin_len = sizeof sin; 2058 sin.sin_family = AF_INET; 2059 sin.sin_addr.s_addr = cb->addr.s_addr; 2060 sin.sin_port = cb->port; 2061 2062 ret = rdma_resolve_addr(cb->cm_id, NULL, (struct sockaddr *) &sin, 2063 2000); 2064 if (ret) { 2065 PRINTF(cb, "rdma_resolve_addr error %d\n", ret); 2066 return ret; 2067 } 2068 2069 wait_event_interruptible(cb->sem, cb->state >= ROUTE_RESOLVED); 2070 if (cb->state != ROUTE_RESOLVED) { 2071 PRINTF(cb, 2072 "addr/route resolution did not resolve: state %d\n", 2073 cb->state); 2074 return -EINTR; 2075 } 2076 2077 if (cb->mem == FASTREG && !fastreg_supported(cb)) 2078 return -EINVAL; 2079 2080 DEBUG_LOG(cb, "rdma_resolve_addr - rdma_resolve_route successful\n"); 2081 return 0; 2082 } 2083 2084 static void krping_run_client(struct krping_cb *cb) 2085 { 2086 struct ib_recv_wr *bad_wr; 2087 int ret; 2088 2089 ret = krping_bind_client(cb); 2090 if (ret) 2091 return; 2092 2093 ret = krping_setup_qp(cb, cb->cm_id); 2094 if (ret) { 2095 PRINTF(cb, "setup_qp failed: %d\n", ret); 2096 return; 2097 } 2098 2099 ret = krping_setup_buffers(cb); 2100 if (ret) { 2101 PRINTF(cb, "krping_setup_buffers failed: %d\n", ret); 2102 goto err1; 2103 } 2104 2105 ret = ib_post_recv(cb->qp, &cb->rq_wr, &bad_wr); 2106 if (ret) { 2107 PRINTF(cb, "ib_post_recv failed: %d\n", ret); 2108 goto err2; 2109 } 2110 2111 ret = krping_connect_client(cb); 2112 if (ret) { 2113 PRINTF(cb, "connect error %d\n", ret); 2114 goto err2; 2115 } 2116 2117 if (cb->wlat) 2118 krping_wlat_test_client(cb); 2119 else if (cb->rlat) 2120 krping_rlat_test_client(cb); 2121 else if (cb->bw) 2122 krping_bw_test_client(cb); 2123 else if (cb->frtest) 2124 krping_fr_test(cb); 2125 else 2126 krping_test_client(cb); 2127 rdma_disconnect(cb->cm_id); 2128 err2: 2129 krping_free_buffers(cb); 2130 err1: 2131 krping_free_qp(cb); 2132 } 2133 2134 int krping_doit(char *cmd, void *cookie) 2135 { 2136 struct krping_cb *cb; 2137 int op; 2138 int ret = 0; 2139 char *optarg; 2140 unsigned long optint; 2141 2142 cb = kzalloc(sizeof(*cb), GFP_KERNEL); 2143 if (!cb) 2144 return -ENOMEM; 2145 2146 mutex_lock(&krping_mutex); 2147 list_add_tail(&cb->list, &krping_cbs); 2148 mutex_unlock(&krping_mutex); 2149 2150 cb->cookie = cookie; 2151 cb->server = -1; 2152 cb->state = IDLE; 2153 cb->size = 64; 2154 cb->txdepth = RPING_SQ_DEPTH; 2155 cb->mem = DMA; 2156 init_waitqueue_head(&cb->sem); 2157 2158 while ((op = krping_getopt("krping", &cmd, krping_opts, NULL, &optarg, 2159 &optint)) != 0) { 2160 switch (op) { 2161 case 'a': 2162 cb->addr_str = optarg; 2163 DEBUG_LOG(cb, "ipaddr (%s)\n", optarg); 2164 if (!inet_aton(optarg, &cb->addr)) { 2165 PRINTF(cb, "bad addr string %s\n", 2166 optarg); 2167 ret = EINVAL; 2168 } 2169 break; 2170 case 'p': 2171 cb->port = htons(optint); 2172 DEBUG_LOG(cb, "port %d\n", (int)optint); 2173 break; 2174 case 'P': 2175 cb->poll = 1; 2176 DEBUG_LOG(cb, "server\n"); 2177 break; 2178 case 's': 2179 cb->server = 1; 2180 DEBUG_LOG(cb, "server\n"); 2181 break; 2182 case 'c': 2183 cb->server = 0; 2184 DEBUG_LOG(cb, "client\n"); 2185 break; 2186 case 'S': 2187 cb->size = optint; 2188 if ((cb->size < 1) || 2189 (cb->size > RPING_BUFSIZE)) { 2190 PRINTF(cb, "Invalid size %d " 2191 "(valid range is 1 to %d)\n", 2192 cb->size, RPING_BUFSIZE); 2193 ret = EINVAL; 2194 } else 2195 DEBUG_LOG(cb, "size %d\n", (int)optint); 2196 break; 2197 case 'C': 2198 cb->count = optint; 2199 if (cb->count < 0) { 2200 PRINTF(cb, "Invalid count %d\n", 2201 cb->count); 2202 ret = EINVAL; 2203 } else 2204 DEBUG_LOG(cb, "count %d\n", (int) cb->count); 2205 break; 2206 case 'v': 2207 cb->verbose++; 2208 DEBUG_LOG(cb, "verbose\n"); 2209 break; 2210 case 'V': 2211 cb->validate++; 2212 DEBUG_LOG(cb, "validate data\n"); 2213 break; 2214 case 'l': 2215 cb->wlat++; 2216 break; 2217 case 'L': 2218 cb->rlat++; 2219 break; 2220 case 'B': 2221 cb->bw++; 2222 break; 2223 case 'd': 2224 cb->duplex++; 2225 break; 2226 case 'm': 2227 if (!strncmp(optarg, "dma", 3)) 2228 cb->mem = DMA; 2229 else if (!strncmp(optarg, "fastreg", 7)) 2230 cb->mem = FASTREG; 2231 else if (!strncmp(optarg, "mw", 2)) 2232 cb->mem = MW; 2233 else if (!strncmp(optarg, "mr", 2)) 2234 cb->mem = MR; 2235 else { 2236 PRINTF(cb, "unknown mem mode %s. " 2237 "Must be dma, fastreg, mw, or mr\n", 2238 optarg); 2239 ret = -EINVAL; 2240 break; 2241 } 2242 break; 2243 case 'I': 2244 cb->server_invalidate = 1; 2245 break; 2246 case 'T': 2247 cb->txdepth = optint; 2248 DEBUG_LOG(cb, "txdepth %d\n", (int) cb->txdepth); 2249 break; 2250 case 'Z': 2251 cb->local_dma_lkey = 1; 2252 DEBUG_LOG(cb, "using local dma lkey\n"); 2253 break; 2254 case 'R': 2255 cb->read_inv = 1; 2256 DEBUG_LOG(cb, "using read-with-inv\n"); 2257 break; 2258 case 'f': 2259 cb->frtest = 1; 2260 DEBUG_LOG(cb, "fast-reg test!\n"); 2261 break; 2262 default: 2263 PRINTF(cb, "unknown opt %s\n", optarg); 2264 ret = -EINVAL; 2265 break; 2266 } 2267 } 2268 if (ret) 2269 goto out; 2270 2271 if (cb->server == -1) { 2272 PRINTF(cb, "must be either client or server\n"); 2273 ret = -EINVAL; 2274 goto out; 2275 } 2276 2277 if (cb->server && cb->frtest) { 2278 PRINTF(cb, "must be client to run frtest\n"); 2279 ret = -EINVAL; 2280 goto out; 2281 } 2282 2283 if ((cb->frtest + cb->bw + cb->rlat + cb->wlat) > 1) { 2284 PRINTF(cb, "Pick only one test: fr, bw, rlat, wlat\n"); 2285 ret = -EINVAL; 2286 goto out; 2287 } 2288 2289 if (cb->server_invalidate && cb->mem != FASTREG) { 2290 PRINTF(cb, "server_invalidate only valid with fastreg mem_mode\n"); 2291 ret = -EINVAL; 2292 goto out; 2293 } 2294 2295 if (cb->read_inv && cb->mem != FASTREG) { 2296 PRINTF(cb, "read_inv only valid with fastreg mem_mode\n"); 2297 ret = -EINVAL; 2298 goto out; 2299 } 2300 2301 if (cb->mem != MR && (cb->wlat || cb->rlat || cb->bw)) { 2302 PRINTF(cb, "wlat, rlat, and bw tests only support mem_mode MR\n"); 2303 ret = -EINVAL; 2304 goto out; 2305 } 2306 2307 cb->cm_id = rdma_create_id(krping_cma_event_handler, cb, RDMA_PS_TCP, IB_QPT_RC); 2308 if (IS_ERR(cb->cm_id)) { 2309 ret = PTR_ERR(cb->cm_id); 2310 PRINTF(cb, "rdma_create_id error %d\n", ret); 2311 goto out; 2312 } 2313 DEBUG_LOG(cb, "created cm_id %p\n", cb->cm_id); 2314 2315 if (cb->server) 2316 krping_run_server(cb); 2317 else 2318 krping_run_client(cb); 2319 2320 DEBUG_LOG(cb, "destroy cm_id %p\n", cb->cm_id); 2321 rdma_destroy_id(cb->cm_id); 2322 out: 2323 mutex_lock(&krping_mutex); 2324 list_del(&cb->list); 2325 mutex_unlock(&krping_mutex); 2326 kfree(cb); 2327 return ret; 2328 } 2329 2330 void 2331 krping_walk_cb_list(void (*f)(struct krping_stats *, void *), void *arg) 2332 { 2333 struct krping_cb *cb; 2334 2335 mutex_lock(&krping_mutex); 2336 list_for_each_entry(cb, &krping_cbs, list) 2337 (*f)(cb->pd ? &cb->stats : NULL, arg); 2338 mutex_unlock(&krping_mutex); 2339 } 2340 2341 void krping_init(void) 2342 { 2343 2344 mutex_init(&krping_mutex); 2345 } 2346