2 * Copyright (c) 2005 Ammasso, Inc. All rights reserved.
3 * Copyright (c) 2006-2009 Open Grid Computing, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
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>
44 #include <linux/device.h>
45 #include <linux/pci.h>
46 #include <linux/sched.h>
48 #include <asm/atomic.h>
50 #include <rdma/ib_verbs.h>
51 #include <rdma/rdma_cm.h>
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)
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);
66 static __inline uint64_t
70 __asm __volatile("rdtsc" : "=a" (low), "=d" (high));
71 return (low | ((u_int64_t)high << 32));
74 typedef uint64_t cycles_t;
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'},
106 #define htonll(x) cpu_to_be64((x))
107 #define ntohll(x) cpu_to_be64((x))
109 static struct mutex krping_mutex;
112 * List of running krping threads.
114 static LIST_HEAD(krping_cbs);
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
130 * These states are used to signal events between the completion handler
131 * and the main client or server thread.
133 * Once CONNECTED, they cycle through RDMA_READ_ADV, RDMA_WRITE_ADV,
134 * and RDMA_WRITE_COMPLETE for each ping.
149 struct krping_rdma_info {
156 * Default max buffer size for IO...
158 #define RPING_BUFSIZE 128*1024
159 #define RPING_SQ_DEPTH 64
162 * Control block struct.
166 int server; /* 0 iff client */
172 struct ib_mr *dma_mr;
174 struct ib_fast_reg_page_list *page_list;
176 struct ib_send_wr fastreg_wr;
177 struct ib_send_wr invalidate_wr;
178 struct ib_mr *fastreg_mr;
179 int server_invalidate;
184 struct ib_mw_bind bind_attr;
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 */
190 DECLARE_PCI_UNMAP_ADDR(recv_mapping)
191 struct ib_mr *recv_mr;
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 */
197 DECLARE_PCI_UNMAP_ADDR(send_mapping)
198 struct ib_mr *send_mr;
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 */
204 DECLARE_PCI_UNMAP_ADDR(rdma_mapping)
205 struct ib_mr *rdma_mr;
207 uint32_t remote_rkey; /* remote guys RKEY */
208 uint64_t remote_addr; /* remote guys TO */
209 uint32_t remote_len; /* remote guys LEN */
211 char *start_buf; /* rdma read src */
213 DECLARE_PCI_UNMAP_ADDR(start_mapping)
214 struct ib_mr *start_mr;
216 enum test_state state; /* used for cond/signalling */
217 wait_queue_head_t sem;
218 struct krping_stats stats;
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 */
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;
243 static int krping_cma_event_handler(struct rdma_cm_id *cma_id,
244 struct rdma_cm_event *event)
247 struct krping_cb *cb = cma_id->context;
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");
252 switch (event->event) {
253 case RDMA_CM_EVENT_ADDR_RESOLVED:
254 cb->state = ADDR_RESOLVED;
255 ret = rdma_resolve_route(cma_id, 2000);
257 PRINTF(cb, "rdma_resolve_route error %d\n", ret);
258 wake_up_interruptible(&cb->sem);
262 case RDMA_CM_EVENT_ROUTE_RESOLVED:
263 cb->state = ROUTE_RESOLVED;
264 wake_up_interruptible(&cb->sem);
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);
274 case RDMA_CM_EVENT_ESTABLISHED:
275 DEBUG_LOG(cb, "ESTABLISHED\n");
277 cb->state = CONNECTED;
279 wake_up_interruptible(&cb->sem);
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,
290 wake_up_interruptible(&cb->sem);
293 case RDMA_CM_EVENT_DISCONNECTED:
294 PRINTF(cb, "DISCONNECT EVENT...\n");
296 wake_up_interruptible(&cb->sem);
299 case RDMA_CM_EVENT_DEVICE_REMOVAL:
300 PRINTF(cb, "cma detected device removal!!!!\n");
304 PRINTF(cb, "oof bad type!\n");
305 wake_up_interruptible(&cb->sem);
311 static int server_recv(struct krping_cb *cb, struct ib_wc *wc)
313 if (wc->byte_len != sizeof(cb->recv_buf)) {
314 PRINTF(cb, "Received bogus data, size %d\n",
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,
326 if (cb->state <= CONNECTED || cb->state == RDMA_WRITE_COMPLETE)
327 cb->state = RDMA_READ_ADV;
329 cb->state = RDMA_WRITE_ADV;
334 static int client_recv(struct krping_cb *cb, struct ib_wc *wc)
336 if (wc->byte_len != sizeof(cb->recv_buf)) {
337 PRINTF(cb, "Received bogus data, size %d\n",
342 if (cb->state == RDMA_READ_ADV)
343 cb->state = RDMA_WRITE_ADV;
345 cb->state = RDMA_WRITE_COMPLETE;
350 static void krping_cq_event_handler(struct ib_cq *cq, void *ctx)
352 struct krping_cb *cb = ctx;
354 struct ib_recv_wr *bad_wr;
357 BUG_ON(cb->cq != cq);
358 if (cb->state == ERROR) {
359 PRINTF(cb, "cq completion in ERROR state\n");
363 PRINTF(cb, "cq completion event in frtest!\n");
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) {
370 if (wc.status == IB_WC_WR_FLUSH_ERR) {
371 DEBUG_LOG(cb, "cq flushed\n");
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);
383 DEBUG_LOG(cb, "send completion\n");
384 cb->stats.send_bytes += cb->send_sgl.length;
385 cb->stats.send_msgs++;
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);
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);
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);
411 ret = cb->server ? server_recv(cb, &wc) :
412 client_recv(cb, &wc);
414 PRINTF(cb, "recv wc error: %d\n", ret);
418 ret = ib_post_recv(cb->qp, &cb->rq_wr, &bad_wr);
420 PRINTF(cb, "post recv error: %d\n",
424 wake_up_interruptible(&cb->sem);
429 "%s:%d Unexpected opcode %d, Shutting down\n",
430 __func__, __LINE__, wc.opcode);
435 PRINTF(cb, "poll error %d\n", ret);
441 wake_up_interruptible(&cb->sem);
444 static int krping_accept(struct krping_cb *cb)
446 struct rdma_conn_param conn_param;
449 DEBUG_LOG(cb, "accepting client connection request\n");
451 memset(&conn_param, 0, sizeof conn_param);
452 conn_param.responder_resources = 1;
453 conn_param.initiator_depth = 1;
455 ret = rdma_accept(cb->child_cm_id, &conn_param);
457 PRINTF(cb, "rdma_accept error: %d\n", ret);
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",
472 static void krping_setup_wr(struct krping_cb *cb)
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;
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;
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;
492 cb->send_sgl.lkey = cb->send_mr->lkey;
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;
499 if (cb->server || cb->wlat || cb->rlat || cb->bw) {
500 cb->rdma_sgl.addr = cb->rdma_dma_addr;
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;
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.
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;
522 cb->invalidate_wr.next = &cb->fastreg_wr;
523 cb->invalidate_wr.opcode = IB_WR_LOCAL_INV;
526 cb->bind_attr.wr_id = 0xabbaabba;
527 cb->bind_attr.send_flags = 0; /* unsignaled */
528 cb->bind_attr.bind_info.length = cb->size;
535 static int krping_setup_buffers(struct krping_cb *cb)
538 struct ib_phys_buf buf;
541 DEBUG_LOG(cb, "krping_setup_buffers called on cb %p\n", cb);
543 cb->recv_dma_addr = dma_map_single(cb->pd->device->dma_device,
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),
550 pci_unmap_addr_set(cb, send_mapping, cb->send_dma_addr);
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);
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,
567 iovbase = cb->recv_dma_addr;
568 cb->recv_mr = ib_reg_phys_mr(cb->pd, &buf, 1,
569 IB_ACCESS_LOCAL_WRITE,
572 if (IS_ERR(cb->recv_mr)) {
573 DEBUG_LOG(cb, "recv_buf reg_mr failed\n");
574 ret = PTR_ERR(cb->recv_mr);
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,
582 iovbase = cb->send_dma_addr;
583 cb->send_mr = ib_reg_phys_mr(cb->pd, &buf, 1,
586 if (IS_ERR(cb->send_mr)) {
587 DEBUG_LOG(cb, "send_buf reg_mr failed\n");
588 ret = PTR_ERR(cb->send_mr);
594 cb->rdma_buf = kmalloc(cb->size, GFP_KERNEL);
596 DEBUG_LOG(cb, "rdma_buf malloc failed\n");
601 cb->rdma_dma_addr = dma_map_single(cb->pd->device->dma_device,
602 cb->rdma_buf, cb->size,
604 pci_unmap_addr_set(cb, rdma_mapping, cb->rdma_dma_addr);
605 if (cb->mem != DMA) {
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(
613 if (IS_ERR(cb->page_list)) {
614 DEBUG_LOG(cb, "recv_buf reg_mr failed\n");
615 ret = PTR_ERR(cb->page_list);
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);
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);
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);
636 DEBUG_LOG(cb, "mw rkey 0x%x\n", cb->mw->rkey);
639 buf.addr = cb->rdma_dma_addr;
641 iovbase = cb->rdma_dma_addr;
642 cb->rdma_mr = ib_reg_phys_mr(cb->pd, &buf, 1,
643 IB_ACCESS_LOCAL_WRITE|
644 IB_ACCESS_REMOTE_READ|
645 IB_ACCESS_REMOTE_WRITE,
647 if (IS_ERR(cb->rdma_mr)) {
648 DEBUG_LOG(cb, "rdma_buf reg_mr failed\n");
649 ret = PTR_ERR(cb->rdma_mr);
652 DEBUG_LOG(cb, "rdma buf dma_addr %llx size %d mr rkey 0x%x\n",
653 buf.addr, (int)buf.size, cb->rdma_mr->rkey);
662 if (!cb->server || cb->wlat || cb->rlat || cb->bw) {
664 cb->start_buf = kmalloc(cb->size, GFP_KERNEL);
665 if (!cb->start_buf) {
666 DEBUG_LOG(cb, "start_buf malloc failed\n");
671 cb->start_dma_addr = dma_map_single(cb->pd->device->dma_device,
672 cb->start_buf, cb->size,
674 pci_unmap_addr_set(cb, start_mapping, cb->start_dma_addr);
676 if (cb->mem == MR || cb->mem == MW) {
677 unsigned flags = IB_ACCESS_REMOTE_READ;
679 if (cb->wlat || cb->rlat || cb->bw) {
680 flags |= IB_ACCESS_LOCAL_WRITE |
681 IB_ACCESS_REMOTE_WRITE;
684 buf.addr = cb->start_dma_addr;
686 DEBUG_LOG(cb, "start buf dma_addr %llx size %d\n",
687 buf.addr, (int)buf.size);
688 iovbase = cb->start_dma_addr;
689 cb->start_mr = ib_reg_phys_mr(cb->pd, &buf, 1,
693 if (IS_ERR(cb->start_mr)) {
694 DEBUG_LOG(cb, "start_buf reg_mr failed\n");
695 ret = PTR_ERR(cb->start_mr);
702 DEBUG_LOG(cb, "allocated & registered buffers...\n");
705 if (cb->fastreg_mr && !IS_ERR(cb->fastreg_mr))
706 ib_dereg_mr(cb->fastreg_mr);
707 if (cb->mw && !IS_ERR(cb->mw))
708 ib_dealloc_mw(cb->mw);
709 if (cb->rdma_mr && !IS_ERR(cb->rdma_mr))
710 ib_dereg_mr(cb->rdma_mr);
711 if (cb->page_list && !IS_ERR(cb->page_list))
712 ib_free_fast_reg_page_list(cb->page_list);
713 if (cb->dma_mr && !IS_ERR(cb->dma_mr))
714 ib_dereg_mr(cb->dma_mr);
715 if (cb->recv_mr && !IS_ERR(cb->recv_mr))
716 ib_dereg_mr(cb->recv_mr);
717 if (cb->send_mr && !IS_ERR(cb->send_mr))
718 ib_dereg_mr(cb->send_mr);
722 kfree(cb->start_buf);
726 static void krping_free_buffers(struct krping_cb *cb)
728 DEBUG_LOG(cb, "krping_free_buffers called on cb %p\n", cb);
731 ib_dereg_mr(cb->dma_mr);
733 ib_dereg_mr(cb->send_mr);
735 ib_dereg_mr(cb->recv_mr);
737 ib_dereg_mr(cb->rdma_mr);
739 ib_dereg_mr(cb->start_mr);
741 ib_dereg_mr(cb->fastreg_mr);
743 ib_dealloc_mw(cb->mw);
745 dma_unmap_single(cb->pd->device->dma_device,
746 pci_unmap_addr(cb, recv_mapping),
747 sizeof(cb->recv_buf), DMA_BIDIRECTIONAL);
748 dma_unmap_single(cb->pd->device->dma_device,
749 pci_unmap_addr(cb, send_mapping),
750 sizeof(cb->send_buf), DMA_BIDIRECTIONAL);
751 dma_unmap_single(cb->pd->device->dma_device,
752 pci_unmap_addr(cb, rdma_mapping),
753 cb->size, DMA_BIDIRECTIONAL);
756 dma_unmap_single(cb->pd->device->dma_device,
757 pci_unmap_addr(cb, start_mapping),
758 cb->size, DMA_BIDIRECTIONAL);
759 kfree(cb->start_buf);
763 static int krping_create_qp(struct krping_cb *cb)
765 struct ib_qp_init_attr init_attr;
768 memset(&init_attr, 0, sizeof(init_attr));
769 init_attr.cap.max_send_wr = cb->txdepth;
770 init_attr.cap.max_recv_wr = 2;
771 init_attr.cap.max_recv_sge = 1;
772 init_attr.cap.max_send_sge = 1;
773 init_attr.qp_type = IB_QPT_RC;
774 init_attr.send_cq = cb->cq;
775 init_attr.recv_cq = cb->cq;
776 init_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
779 ret = rdma_create_qp(cb->child_cm_id, cb->pd, &init_attr);
781 cb->qp = cb->child_cm_id->qp;
783 ret = rdma_create_qp(cb->cm_id, cb->pd, &init_attr);
785 cb->qp = cb->cm_id->qp;
791 static void krping_free_qp(struct krping_cb *cb)
793 ib_destroy_qp(cb->qp);
794 ib_destroy_cq(cb->cq);
795 ib_dealloc_pd(cb->pd);
798 static int krping_setup_qp(struct krping_cb *cb, struct rdma_cm_id *cm_id)
801 cb->pd = ib_alloc_pd(cm_id->device);
802 if (IS_ERR(cb->pd)) {
803 PRINTF(cb, "ib_alloc_pd failed\n");
804 return PTR_ERR(cb->pd);
806 DEBUG_LOG(cb, "created pd %p\n", cb->pd);
808 strlcpy(cb->stats.name, cb->pd->device->name, sizeof(cb->stats.name));
810 cb->cq = ib_create_cq(cm_id->device, krping_cq_event_handler, NULL,
811 cb, cb->txdepth * 2, 0);
812 if (IS_ERR(cb->cq)) {
813 PRINTF(cb, "ib_create_cq failed\n");
814 ret = PTR_ERR(cb->cq);
817 DEBUG_LOG(cb, "created cq %p\n", cb->cq);
819 if (!cb->wlat && !cb->rlat && !cb->bw && !cb->frtest) {
820 ret = ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP);
822 PRINTF(cb, "ib_create_cq failed\n");
827 ret = krping_create_qp(cb);
829 PRINTF(cb, "krping_create_qp failed: %d\n", ret);
832 DEBUG_LOG(cb, "created qp %p\n", cb->qp);
835 ib_destroy_cq(cb->cq);
837 ib_dealloc_pd(cb->pd);
842 * return the (possibly rebound) rkey for the rdma buffer.
843 * FASTREG mode: invalidate and rebind via fastreg wr.
844 * MW mode: rebind the MW.
845 * other modes: just return the mr rkey.
847 static u32 krping_rdma_rkey(struct krping_cb *cb, u64 buf, int post_inv)
849 u32 rkey = 0xffffffff;
851 struct ib_send_wr *bad_wr;
857 cb->invalidate_wr.ex.invalidate_rkey = cb->fastreg_mr->rkey;
860 * Update the fastreg key.
862 ib_update_fast_reg_key(cb->fastreg_mr, ++cb->key);
863 cb->fastreg_wr.wr.fast_reg.rkey = cb->fastreg_mr->rkey;
866 * Update the fastreg WR with new buf info.
868 if (buf == (u64)cb->start_dma_addr)
869 cb->fastreg_wr.wr.fast_reg.access_flags = IB_ACCESS_REMOTE_READ;
871 cb->fastreg_wr.wr.fast_reg.access_flags = IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE;
872 cb->fastreg_wr.wr.fast_reg.iova_start = buf;
873 p = (u64)(buf & PAGE_MASK);
874 for (i=0; i < cb->fastreg_wr.wr.fast_reg.page_list_len;
875 i++, p += PAGE_SIZE) {
876 cb->page_list->page_list[i] = p;
877 DEBUG_LOG(cb, "page_list[%d] 0x%llx\n", i, p);
880 DEBUG_LOG(cb, "post_inv = %d, fastreg new rkey 0x%x shift %u len %u"
881 " iova_start %llx page_list_len %u\n",
883 cb->fastreg_wr.wr.fast_reg.rkey,
884 cb->fastreg_wr.wr.fast_reg.page_shift,
885 cb->fastreg_wr.wr.fast_reg.length,
886 cb->fastreg_wr.wr.fast_reg.iova_start,
887 cb->fastreg_wr.wr.fast_reg.page_list_len);
890 ret = ib_post_send(cb->qp, &cb->invalidate_wr, &bad_wr);
892 ret = ib_post_send(cb->qp, &cb->fastreg_wr, &bad_wr);
894 PRINTF(cb, "post send error %d\n", ret);
897 rkey = cb->fastreg_mr->rkey;
901 * Update the MW with new buf info.
903 if (buf == (u64)cb->start_dma_addr) {
904 cb->bind_attr.bind_info.mw_access_flags = IB_ACCESS_REMOTE_READ;
905 cb->bind_attr.bind_info.mr = cb->start_mr;
907 cb->bind_attr.bind_info.mw_access_flags = IB_ACCESS_REMOTE_WRITE;
908 cb->bind_attr.bind_info.mr = cb->rdma_mr;
910 cb->bind_attr.bind_info.addr = buf;
911 DEBUG_LOG(cb, "binding mw rkey 0x%x to buf %llx mr rkey 0x%x\n",
912 cb->mw->rkey, buf, cb->bind_attr.bind_info.mr->rkey);
913 ret = ib_bind_mw(cb->qp, cb->mw, &cb->bind_attr);
915 PRINTF(cb, "bind mw error %d\n", ret);
921 if (buf == (u64)cb->start_dma_addr)
922 rkey = cb->start_mr->rkey;
924 rkey = cb->rdma_mr->rkey;
927 rkey = cb->dma_mr->rkey;
930 PRINTF(cb, "%s:%d case ERROR\n", __func__, __LINE__);
937 static void krping_format_send(struct krping_cb *cb, u64 buf)
939 struct krping_rdma_info *info = &cb->send_buf;
943 * Client side will do fastreg or mw bind before
944 * advertising the rdma buffer. Server side
945 * sends have no data.
947 if (!cb->server || cb->wlat || cb->rlat || cb->bw) {
948 rkey = krping_rdma_rkey(cb, buf, !cb->server_invalidate);
949 info->buf = htonll(buf);
950 info->rkey = htonl(rkey);
951 info->size = htonl(cb->size);
952 DEBUG_LOG(cb, "RDMA addr %llx rkey %x len %d\n",
953 (unsigned long long)buf, rkey, cb->size);
957 static void krping_test_server(struct krping_cb *cb)
959 struct ib_send_wr *bad_wr, inv;
963 /* Wait for client's Start STAG/TO/Len */
964 wait_event_interruptible(cb->sem, cb->state >= RDMA_READ_ADV);
965 if (cb->state != RDMA_READ_ADV) {
966 PRINTF(cb, "wait for RDMA_READ_ADV state %d\n",
971 DEBUG_LOG(cb, "server received sink adv\n");
973 cb->rdma_sq_wr.wr.rdma.rkey = cb->remote_rkey;
974 cb->rdma_sq_wr.wr.rdma.remote_addr = cb->remote_addr;
975 cb->rdma_sq_wr.sg_list->length = cb->remote_len;
976 cb->rdma_sgl.lkey = krping_rdma_rkey(cb, cb->rdma_dma_addr, 1);
977 cb->rdma_sq_wr.next = NULL;
979 /* Issue RDMA Read. */
981 cb->rdma_sq_wr.opcode = IB_WR_RDMA_READ_WITH_INV;
984 cb->rdma_sq_wr.opcode = IB_WR_RDMA_READ;
985 if (cb->mem == FASTREG) {
987 * Immediately follow the read with a
990 cb->rdma_sq_wr.next = &inv;
991 memset(&inv, 0, sizeof inv);
992 inv.opcode = IB_WR_LOCAL_INV;
993 inv.ex.invalidate_rkey = cb->fastreg_mr->rkey;
994 inv.send_flags = IB_SEND_FENCE;
998 ret = ib_post_send(cb->qp, &cb->rdma_sq_wr, &bad_wr);
1000 PRINTF(cb, "post send error %d\n", ret);
1003 cb->rdma_sq_wr.next = NULL;
1005 DEBUG_LOG(cb, "server posted rdma read req \n");
1007 /* Wait for read completion */
1008 wait_event_interruptible(cb->sem,
1009 cb->state >= RDMA_READ_COMPLETE);
1010 if (cb->state != RDMA_READ_COMPLETE) {
1012 "wait for RDMA_READ_COMPLETE state %d\n",
1016 DEBUG_LOG(cb, "server received read complete\n");
1018 /* Display data in recv buf */
1020 if (strlen(cb->rdma_buf) > 128) {
1023 strlcpy(msgbuf, cb->rdma_buf, sizeof(msgbuf));
1024 PRINTF(cb, "server ping data stripped: %s\n",
1027 PRINTF(cb, "server ping data: %s\n",
1031 /* Tell client to continue */
1032 if (cb->server && cb->server_invalidate) {
1033 cb->sq_wr.ex.invalidate_rkey = cb->remote_rkey;
1034 cb->sq_wr.opcode = IB_WR_SEND_WITH_INV;
1035 DEBUG_LOG(cb, "send-w-inv rkey 0x%x\n", cb->remote_rkey);
1037 ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr);
1039 PRINTF(cb, "post send error %d\n", ret);
1042 DEBUG_LOG(cb, "server posted go ahead\n");
1044 /* Wait for client's RDMA STAG/TO/Len */
1045 wait_event_interruptible(cb->sem, cb->state >= RDMA_WRITE_ADV);
1046 if (cb->state != RDMA_WRITE_ADV) {
1048 "wait for RDMA_WRITE_ADV state %d\n",
1052 DEBUG_LOG(cb, "server received sink adv\n");
1054 /* RDMA Write echo data */
1055 cb->rdma_sq_wr.opcode = IB_WR_RDMA_WRITE;
1056 cb->rdma_sq_wr.wr.rdma.rkey = cb->remote_rkey;
1057 cb->rdma_sq_wr.wr.rdma.remote_addr = cb->remote_addr;
1058 cb->rdma_sq_wr.sg_list->length = strlen(cb->rdma_buf) + 1;
1059 if (cb->local_dma_lkey)
1060 cb->rdma_sgl.lkey = cb->qp->device->local_dma_lkey;
1062 cb->rdma_sgl.lkey = krping_rdma_rkey(cb, cb->rdma_dma_addr, 0);
1064 DEBUG_LOG(cb, "rdma write from lkey %x laddr %llx len %d\n",
1065 cb->rdma_sq_wr.sg_list->lkey,
1066 (unsigned long long)cb->rdma_sq_wr.sg_list->addr,
1067 cb->rdma_sq_wr.sg_list->length);
1069 ret = ib_post_send(cb->qp, &cb->rdma_sq_wr, &bad_wr);
1071 PRINTF(cb, "post send error %d\n", ret);
1075 /* Wait for completion */
1076 ret = wait_event_interruptible(cb->sem, cb->state >=
1077 RDMA_WRITE_COMPLETE);
1078 if (cb->state != RDMA_WRITE_COMPLETE) {
1080 "wait for RDMA_WRITE_COMPLETE state %d\n",
1084 DEBUG_LOG(cb, "server rdma write complete \n");
1086 cb->state = CONNECTED;
1088 /* Tell client to begin again */
1089 if (cb->server && cb->server_invalidate) {
1090 cb->sq_wr.ex.invalidate_rkey = cb->remote_rkey;
1091 cb->sq_wr.opcode = IB_WR_SEND_WITH_INV;
1092 DEBUG_LOG(cb, "send-w-inv rkey 0x%x\n", cb->remote_rkey);
1094 ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr);
1096 PRINTF(cb, "post send error %d\n", ret);
1099 DEBUG_LOG(cb, "server posted go ahead\n");
1103 static void rlat_test(struct krping_cb *cb)
1106 int iters = cb->count;
1107 struct timeval start_tv, stop_tv;
1110 struct ib_send_wr *bad_wr;
1114 cb->rdma_sq_wr.opcode = IB_WR_RDMA_READ;
1115 cb->rdma_sq_wr.wr.rdma.rkey = cb->remote_rkey;
1116 cb->rdma_sq_wr.wr.rdma.remote_addr = cb->remote_addr;
1117 cb->rdma_sq_wr.sg_list->length = cb->size;
1119 microtime(&start_tv);
1121 cb->state = RDMA_READ_ADV;
1122 ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP);
1124 while (scnt < iters) {
1126 cb->state = RDMA_READ_ADV;
1127 ret = ib_post_send(cb->qp, &cb->rdma_sq_wr, &bad_wr);
1130 "Couldn't post send: ret=%d scnt %d\n",
1137 wait_event_interruptible(cb->sem,
1138 cb->state != RDMA_READ_ADV);
1139 if (cb->state == RDMA_READ_COMPLETE) {
1141 ib_req_notify_cq(cb->cq,
1147 ne = ib_poll_cq(cb->cq, 1, &wc);
1148 if (cb->state == ERROR) {
1150 "state == ERROR...bailing scnt %d\n",
1157 PRINTF(cb, "poll CQ failed %d\n", ne);
1160 if (cb->poll && wc.status != IB_WC_SUCCESS) {
1161 PRINTF(cb, "Completion wth error at %s:\n",
1162 cb->server ? "server" : "client");
1163 PRINTF(cb, "Failed status %d: wr_id %d\n",
1164 wc.status, (int) wc.wr_id);
1169 microtime(&stop_tv);
1171 if (stop_tv.tv_usec < start_tv.tv_usec) {
1172 stop_tv.tv_usec += 1000000;
1173 stop_tv.tv_sec -= 1;
1176 PRINTF(cb, "delta sec %lu delta usec %lu iter %d size %d\n",
1177 stop_tv.tv_sec - start_tv.tv_sec,
1178 stop_tv.tv_usec - start_tv.tv_usec,
1182 static void wlat_test(struct krping_cb *cb)
1184 int ccnt, scnt, rcnt;
1185 int iters=cb->count;
1186 volatile char *poll_buf = (char *) cb->start_buf;
1187 char *buf = (char *)cb->rdma_buf;
1188 struct timeval start_tv, stop_tv;
1189 cycles_t *post_cycles_start, *post_cycles_stop;
1190 cycles_t *poll_cycles_start, *poll_cycles_stop;
1191 cycles_t *last_poll_cycles_start;
1192 cycles_t sum_poll = 0, sum_post = 0, sum_last_poll = 0;
1194 int cycle_iters = 1000;
1200 post_cycles_start = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL);
1201 if (!post_cycles_start) {
1202 PRINTF(cb, "%s kmalloc failed\n", __FUNCTION__);
1205 post_cycles_stop = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL);
1206 if (!post_cycles_stop) {
1207 PRINTF(cb, "%s kmalloc failed\n", __FUNCTION__);
1210 poll_cycles_start = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL);
1211 if (!poll_cycles_start) {
1212 PRINTF(cb, "%s kmalloc failed\n", __FUNCTION__);
1215 poll_cycles_stop = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL);
1216 if (!poll_cycles_stop) {
1217 PRINTF(cb, "%s kmalloc failed\n", __FUNCTION__);
1220 last_poll_cycles_start = kmalloc(cycle_iters * sizeof(cycles_t),
1222 if (!last_poll_cycles_start) {
1223 PRINTF(cb, "%s kmalloc failed\n", __FUNCTION__);
1226 cb->rdma_sq_wr.opcode = IB_WR_RDMA_WRITE;
1227 cb->rdma_sq_wr.wr.rdma.rkey = cb->remote_rkey;
1228 cb->rdma_sq_wr.wr.rdma.remote_addr = cb->remote_addr;
1229 cb->rdma_sq_wr.sg_list->length = cb->size;
1231 if (cycle_iters > iters)
1232 cycle_iters = iters;
1233 microtime(&start_tv);
1234 while (scnt < iters || ccnt < iters || rcnt < iters) {
1236 /* Wait till buffer changes. */
1237 if (rcnt < iters && !(scnt < 1 && !cb->server)) {
1239 while (*poll_buf != (char)rcnt) {
1240 if (cb->state == ERROR) {
1242 "state = ERROR, bailing\n");
1249 struct ib_send_wr *bad_wr;
1251 *buf = (char)scnt+1;
1252 if (scnt < cycle_iters)
1253 post_cycles_start[scnt] = get_cycles();
1254 if (ib_post_send(cb->qp, &cb->rdma_sq_wr, &bad_wr)) {
1256 "Couldn't post send: scnt=%d\n",
1260 if (scnt < cycle_iters)
1261 post_cycles_stop[scnt] = get_cycles();
1269 if (ccnt < cycle_iters)
1270 poll_cycles_start[ccnt] = get_cycles();
1272 if (ccnt < cycle_iters)
1273 last_poll_cycles_start[ccnt] =
1275 ne = ib_poll_cq(cb->cq, 1, &wc);
1277 if (ccnt < cycle_iters)
1278 poll_cycles_stop[ccnt] = get_cycles();
1282 PRINTF(cb, "poll CQ failed %d\n", ne);
1285 if (wc.status != IB_WC_SUCCESS) {
1287 "Completion wth error at %s:\n",
1288 cb->server ? "server" : "client");
1290 "Failed status %d: wr_id %d\n",
1291 wc.status, (int) wc.wr_id);
1293 "scnt=%d, rcnt=%d, ccnt=%d\n",
1299 microtime(&stop_tv);
1301 if (stop_tv.tv_usec < start_tv.tv_usec) {
1302 stop_tv.tv_usec += 1000000;
1303 stop_tv.tv_sec -= 1;
1306 for (i=0; i < cycle_iters; i++) {
1307 sum_post += post_cycles_stop[i] - post_cycles_start[i];
1308 sum_poll += poll_cycles_stop[i] - poll_cycles_start[i];
1309 sum_last_poll += poll_cycles_stop[i]-last_poll_cycles_start[i];
1312 "delta sec %lu delta usec %lu iter %d size %d cycle_iters %d"
1313 " sum_post %llu sum_poll %llu sum_last_poll %llu\n",
1314 stop_tv.tv_sec - start_tv.tv_sec,
1315 stop_tv.tv_usec - start_tv.tv_usec,
1316 scnt, cb->size, cycle_iters,
1317 (unsigned long long)sum_post, (unsigned long long)sum_poll,
1318 (unsigned long long)sum_last_poll);
1319 kfree(post_cycles_start);
1320 kfree(post_cycles_stop);
1321 kfree(poll_cycles_start);
1322 kfree(poll_cycles_stop);
1323 kfree(last_poll_cycles_start);
1326 static void bw_test(struct krping_cb *cb)
1328 int ccnt, scnt, rcnt;
1329 int iters=cb->count;
1330 struct timeval start_tv, stop_tv;
1331 cycles_t *post_cycles_start, *post_cycles_stop;
1332 cycles_t *poll_cycles_start, *poll_cycles_stop;
1333 cycles_t *last_poll_cycles_start;
1334 cycles_t sum_poll = 0, sum_post = 0, sum_last_poll = 0;
1336 int cycle_iters = 1000;
1342 post_cycles_start = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL);
1343 if (!post_cycles_start) {
1344 PRINTF(cb, "%s kmalloc failed\n", __FUNCTION__);
1347 post_cycles_stop = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL);
1348 if (!post_cycles_stop) {
1349 PRINTF(cb, "%s kmalloc failed\n", __FUNCTION__);
1352 poll_cycles_start = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL);
1353 if (!poll_cycles_start) {
1354 PRINTF(cb, "%s kmalloc failed\n", __FUNCTION__);
1357 poll_cycles_stop = kmalloc(cycle_iters * sizeof(cycles_t), GFP_KERNEL);
1358 if (!poll_cycles_stop) {
1359 PRINTF(cb, "%s kmalloc failed\n", __FUNCTION__);
1362 last_poll_cycles_start = kmalloc(cycle_iters * sizeof(cycles_t),
1364 if (!last_poll_cycles_start) {
1365 PRINTF(cb, "%s kmalloc failed\n", __FUNCTION__);
1368 cb->rdma_sq_wr.opcode = IB_WR_RDMA_WRITE;
1369 cb->rdma_sq_wr.wr.rdma.rkey = cb->remote_rkey;
1370 cb->rdma_sq_wr.wr.rdma.remote_addr = cb->remote_addr;
1371 cb->rdma_sq_wr.sg_list->length = cb->size;
1373 if (cycle_iters > iters)
1374 cycle_iters = iters;
1375 microtime(&start_tv);
1376 while (scnt < iters || ccnt < iters) {
1378 while (scnt < iters && scnt - ccnt < cb->txdepth) {
1379 struct ib_send_wr *bad_wr;
1381 if (scnt < cycle_iters)
1382 post_cycles_start[scnt] = get_cycles();
1383 if (ib_post_send(cb->qp, &cb->rdma_sq_wr, &bad_wr)) {
1385 "Couldn't post send: scnt=%d\n",
1389 if (scnt < cycle_iters)
1390 post_cycles_stop[scnt] = get_cycles();
1398 if (ccnt < cycle_iters)
1399 poll_cycles_start[ccnt] = get_cycles();
1401 if (ccnt < cycle_iters)
1402 last_poll_cycles_start[ccnt] =
1404 ne = ib_poll_cq(cb->cq, 1, &wc);
1406 if (ccnt < cycle_iters)
1407 poll_cycles_stop[ccnt] = get_cycles();
1411 PRINTF(cb, "poll CQ failed %d\n", ne);
1414 if (wc.status != IB_WC_SUCCESS) {
1416 "Completion wth error at %s:\n",
1417 cb->server ? "server" : "client");
1419 "Failed status %d: wr_id %d\n",
1420 wc.status, (int) wc.wr_id);
1425 microtime(&stop_tv);
1427 if (stop_tv.tv_usec < start_tv.tv_usec) {
1428 stop_tv.tv_usec += 1000000;
1429 stop_tv.tv_sec -= 1;
1432 for (i=0; i < cycle_iters; i++) {
1433 sum_post += post_cycles_stop[i] - post_cycles_start[i];
1434 sum_poll += poll_cycles_stop[i] - poll_cycles_start[i];
1435 sum_last_poll += poll_cycles_stop[i]-last_poll_cycles_start[i];
1438 "delta sec %lu delta usec %lu iter %d size %d cycle_iters %d"
1439 " sum_post %llu sum_poll %llu sum_last_poll %llu\n",
1440 stop_tv.tv_sec - start_tv.tv_sec,
1441 stop_tv.tv_usec - start_tv.tv_usec,
1442 scnt, cb->size, cycle_iters,
1443 (unsigned long long)sum_post, (unsigned long long)sum_poll,
1444 (unsigned long long)sum_last_poll);
1445 kfree(post_cycles_start);
1446 kfree(post_cycles_stop);
1447 kfree(poll_cycles_start);
1448 kfree(poll_cycles_stop);
1449 kfree(last_poll_cycles_start);
1452 static void krping_rlat_test_server(struct krping_cb *cb)
1454 struct ib_send_wr *bad_wr;
1458 /* Spin waiting for client's Start STAG/TO/Len */
1459 while (cb->state < RDMA_READ_ADV) {
1460 krping_cq_event_handler(cb->cq, cb);
1463 /* Send STAG/TO/Len to client */
1464 krping_format_send(cb, cb->start_dma_addr);
1465 ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr);
1467 PRINTF(cb, "post send error %d\n", ret);
1471 /* Spin waiting for send completion */
1472 while ((ret = ib_poll_cq(cb->cq, 1, &wc) == 0));
1474 PRINTF(cb, "poll error %d\n", ret);
1478 PRINTF(cb, "send completiong error %d\n", wc.status);
1482 wait_event_interruptible(cb->sem, cb->state == ERROR);
1485 static void krping_wlat_test_server(struct krping_cb *cb)
1487 struct ib_send_wr *bad_wr;
1491 /* Spin waiting for client's Start STAG/TO/Len */
1492 while (cb->state < RDMA_READ_ADV) {
1493 krping_cq_event_handler(cb->cq, cb);
1496 /* Send STAG/TO/Len to client */
1497 krping_format_send(cb, cb->start_dma_addr);
1498 ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr);
1500 PRINTF(cb, "post send error %d\n", ret);
1504 /* Spin waiting for send completion */
1505 while ((ret = ib_poll_cq(cb->cq, 1, &wc) == 0));
1507 PRINTF(cb, "poll error %d\n", ret);
1511 PRINTF(cb, "send completiong error %d\n", wc.status);
1516 wait_event_interruptible(cb->sem, cb->state == ERROR);
1519 static void krping_bw_test_server(struct krping_cb *cb)
1521 struct ib_send_wr *bad_wr;
1525 /* Spin waiting for client's Start STAG/TO/Len */
1526 while (cb->state < RDMA_READ_ADV) {
1527 krping_cq_event_handler(cb->cq, cb);
1530 /* Send STAG/TO/Len to client */
1531 krping_format_send(cb, cb->start_dma_addr);
1532 ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr);
1534 PRINTF(cb, "post send error %d\n", ret);
1538 /* Spin waiting for send completion */
1539 while ((ret = ib_poll_cq(cb->cq, 1, &wc) == 0));
1541 PRINTF(cb, "poll error %d\n", ret);
1545 PRINTF(cb, "send completiong error %d\n", wc.status);
1551 wait_event_interruptible(cb->sem, cb->state == ERROR);
1554 static int fastreg_supported(struct krping_cb *cb)
1556 struct ib_device *dev = cb->child_cm_id->device;
1557 struct ib_device_attr attr;
1560 ret = ib_query_device(dev, &attr);
1562 PRINTF(cb, "ib_query_device failed ret %d\n", ret);
1565 if (!(attr.device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS)) {
1566 PRINTF(cb, "Fastreg not supported - device_cap_flags 0x%x\n",
1567 attr.device_cap_flags);
1570 DEBUG_LOG(cb, "Fastreg supported - device_cap_flags 0x%x\n",
1571 attr.device_cap_flags);
1575 static int krping_bind_server(struct krping_cb *cb)
1577 struct sockaddr_in sin;
1580 memset(&sin, 0, sizeof(sin));
1581 sin.sin_len = sizeof sin;
1582 sin.sin_family = AF_INET;
1583 sin.sin_addr.s_addr = cb->addr.s_addr;
1584 sin.sin_port = cb->port;
1586 ret = rdma_bind_addr(cb->cm_id, (struct sockaddr *) &sin);
1588 PRINTF(cb, "rdma_bind_addr error %d\n", ret);
1591 DEBUG_LOG(cb, "rdma_bind_addr successful\n");
1593 DEBUG_LOG(cb, "rdma_listen\n");
1594 ret = rdma_listen(cb->cm_id, 3);
1596 PRINTF(cb, "rdma_listen failed: %d\n", ret);
1600 wait_event_interruptible(cb->sem, cb->state >= CONNECT_REQUEST);
1601 if (cb->state != CONNECT_REQUEST) {
1602 PRINTF(cb, "wait for CONNECT_REQUEST state %d\n",
1607 if (cb->mem == FASTREG && !fastreg_supported(cb))
1613 static void krping_run_server(struct krping_cb *cb)
1615 struct ib_recv_wr *bad_wr;
1618 ret = krping_bind_server(cb);
1622 ret = krping_setup_qp(cb, cb->child_cm_id);
1624 PRINTF(cb, "setup_qp failed: %d\n", ret);
1628 ret = krping_setup_buffers(cb);
1630 PRINTF(cb, "krping_setup_buffers failed: %d\n", ret);
1634 ret = ib_post_recv(cb->qp, &cb->rq_wr, &bad_wr);
1636 PRINTF(cb, "ib_post_recv failed: %d\n", ret);
1640 ret = krping_accept(cb);
1642 PRINTF(cb, "connect error %d\n", ret);
1647 krping_wlat_test_server(cb);
1649 krping_rlat_test_server(cb);
1651 krping_bw_test_server(cb);
1653 krping_test_server(cb);
1654 rdma_disconnect(cb->child_cm_id);
1656 krping_free_buffers(cb);
1660 rdma_destroy_id(cb->child_cm_id);
1663 static void krping_test_client(struct krping_cb *cb)
1665 int ping, start, cc, i, ret;
1666 struct ib_send_wr *bad_wr;
1670 for (ping = 0; !cb->count || ping < cb->count; ping++) {
1671 cb->state = RDMA_READ_ADV;
1673 /* Put some ascii text in the buffer. */
1674 cc = sprintf(cb->start_buf, "rdma-ping-%d: ", ping);
1675 for (i = cc, c = start; i < cb->size; i++) {
1676 cb->start_buf[i] = c;
1684 cb->start_buf[cb->size - 1] = 0;
1686 krping_format_send(cb, cb->start_dma_addr);
1687 if (cb->state == ERROR) {
1688 PRINTF(cb, "krping_format_send failed\n");
1691 ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr);
1693 PRINTF(cb, "post send error %d\n", ret);
1697 /* Wait for server to ACK */
1698 wait_event_interruptible(cb->sem, cb->state >= RDMA_WRITE_ADV);
1699 if (cb->state != RDMA_WRITE_ADV) {
1701 "wait for RDMA_WRITE_ADV state %d\n",
1706 krping_format_send(cb, cb->rdma_dma_addr);
1707 ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr);
1709 PRINTF(cb, "post send error %d\n", ret);
1713 /* Wait for the server to say the RDMA Write is complete. */
1714 wait_event_interruptible(cb->sem,
1715 cb->state >= RDMA_WRITE_COMPLETE);
1716 if (cb->state != RDMA_WRITE_COMPLETE) {
1718 "wait for RDMA_WRITE_COMPLETE state %d\n",
1724 if (memcmp(cb->start_buf, cb->rdma_buf, cb->size)) {
1725 PRINTF(cb, "data mismatch!\n");
1730 if (strlen(cb->rdma_buf) > 128) {
1733 strlcpy(msgbuf, cb->rdma_buf, sizeof(msgbuf));
1734 PRINTF(cb, "ping data stripped: %s\n",
1737 PRINTF(cb, "ping data: %s\n", cb->rdma_buf);
1740 wait_event_interruptible_timeout(cb->sem, cb->state == ERROR, HZ);
1745 static void krping_rlat_test_client(struct krping_cb *cb)
1747 struct ib_send_wr *bad_wr;
1751 cb->state = RDMA_READ_ADV;
1753 /* Send STAG/TO/Len to client */
1754 krping_format_send(cb, cb->start_dma_addr);
1755 if (cb->state == ERROR) {
1756 PRINTF(cb, "krping_format_send failed\n");
1759 ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr);
1761 PRINTF(cb, "post send error %d\n", ret);
1765 /* Spin waiting for send completion */
1766 while ((ret = ib_poll_cq(cb->cq, 1, &wc) == 0));
1768 PRINTF(cb, "poll error %d\n", ret);
1772 PRINTF(cb, "send completion error %d\n", wc.status);
1776 /* Spin waiting for server's Start STAG/TO/Len */
1777 while (cb->state < RDMA_WRITE_ADV) {
1778 krping_cq_event_handler(cb->cq, cb);
1784 struct timeval start, stop;
1787 unsigned long long elapsed;
1789 struct ib_send_wr *bad_wr;
1792 cb->rdma_sq_wr.opcode = IB_WR_RDMA_WRITE;
1793 cb->rdma_sq_wr.wr.rdma.rkey = cb->remote_rkey;
1794 cb->rdma_sq_wr.wr.rdma.remote_addr = cb->remote_addr;
1795 cb->rdma_sq_wr.sg_list->length = 0;
1796 cb->rdma_sq_wr.num_sge = 0;
1799 for (i=0; i < 100000; i++) {
1800 if (ib_post_send(cb->qp, &cb->rdma_sq_wr, &bad_wr)) {
1801 PRINTF(cb, "Couldn't post send\n");
1805 ne = ib_poll_cq(cb->cq, 1, &wc);
1808 PRINTF(cb, "poll CQ failed %d\n", ne);
1811 if (wc.status != IB_WC_SUCCESS) {
1812 PRINTF(cb, "Completion wth error at %s:\n",
1813 cb->server ? "server" : "client");
1814 PRINTF(cb, "Failed status %d: wr_id %d\n",
1815 wc.status, (int) wc.wr_id);
1821 if (stop.tv_usec < start.tv_usec) {
1822 stop.tv_usec += 1000000;
1825 sec = stop.tv_sec - start.tv_sec;
1826 usec = stop.tv_usec - start.tv_usec;
1827 elapsed = sec * 1000000 + usec;
1828 PRINTF(cb, "0B-write-lat iters 100000 usec %llu\n", elapsed);
1835 static void krping_wlat_test_client(struct krping_cb *cb)
1837 struct ib_send_wr *bad_wr;
1841 cb->state = RDMA_READ_ADV;
1843 /* Send STAG/TO/Len to client */
1844 krping_format_send(cb, cb->start_dma_addr);
1845 if (cb->state == ERROR) {
1846 PRINTF(cb, "krping_format_send failed\n");
1849 ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr);
1851 PRINTF(cb, "post send error %d\n", ret);
1855 /* Spin waiting for send completion */
1856 while ((ret = ib_poll_cq(cb->cq, 1, &wc) == 0));
1858 PRINTF(cb, "poll error %d\n", ret);
1862 PRINTF(cb, "send completion error %d\n", wc.status);
1866 /* Spin waiting for server's Start STAG/TO/Len */
1867 while (cb->state < RDMA_WRITE_ADV) {
1868 krping_cq_event_handler(cb->cq, cb);
1874 static void krping_bw_test_client(struct krping_cb *cb)
1876 struct ib_send_wr *bad_wr;
1880 cb->state = RDMA_READ_ADV;
1882 /* Send STAG/TO/Len to client */
1883 krping_format_send(cb, cb->start_dma_addr);
1884 if (cb->state == ERROR) {
1885 PRINTF(cb, "krping_format_send failed\n");
1888 ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr);
1890 PRINTF(cb, "post send error %d\n", ret);
1894 /* Spin waiting for send completion */
1895 while ((ret = ib_poll_cq(cb->cq, 1, &wc) == 0));
1897 PRINTF(cb, "poll error %d\n", ret);
1901 PRINTF(cb, "send completion error %d\n", wc.status);
1905 /* Spin waiting for server's Start STAG/TO/Len */
1906 while (cb->state < RDMA_WRITE_ADV) {
1907 krping_cq_event_handler(cb->cq, cb);
1913 static void krping_fr_test(struct krping_cb *cb)
1915 struct ib_fast_reg_page_list *pl;
1916 struct ib_send_wr fr, inv, *bad;
1922 int size = cb->size;
1923 int plen = (((size - 1) & PAGE_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
1928 pl = ib_alloc_fast_reg_page_list(cb->qp->device, plen);
1930 PRINTF(cb, "ib_alloc_fast_reg_page_list failed %ld\n", PTR_ERR(pl));
1934 mr = ib_alloc_fast_reg_mr(cb->pd, plen);
1936 PRINTF(cb, "ib_alloc_fast_reg_mr failed %ld\n", PTR_ERR(pl));
1940 for (i=0; i<plen; i++)
1941 pl->page_list[i] = 0xcafebabe | i;
1943 memset(&fr, 0, sizeof fr);
1944 fr.opcode = IB_WR_FAST_REG_MR;
1945 fr.wr.fast_reg.page_shift = PAGE_SHIFT;
1946 fr.wr.fast_reg.length = size;
1947 fr.wr.fast_reg.page_list = pl;
1948 fr.wr.fast_reg.page_list_len = plen;
1949 fr.wr.fast_reg.iova_start = 0;
1950 fr.wr.fast_reg.access_flags = IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE;
1952 memset(&inv, 0, sizeof inv);
1953 inv.opcode = IB_WR_LOCAL_INV;
1954 inv.send_flags = IB_SEND_SIGNALED;
1956 DEBUG_LOG(cb, "fr_test: stag index 0x%x plen %u size %u depth %u\n", mr->rkey >> 8, plen, cb->size, cb->txdepth);
1957 start = time_uptime;
1959 if ((time_uptime - start) >= 9) {
1960 DEBUG_LOG(cb, "fr_test: pausing 1 second! count %u latest size %u plen %u\n", count, size, plen);
1961 wait_event_interruptible(cb->sem, cb->state == ERROR);
1962 if (cb->state == ERROR)
1964 start = time_uptime;
1966 while (scnt < (cb->txdepth>>1)) {
1967 ib_update_fast_reg_key(mr, ++key);
1968 fr.wr.fast_reg.rkey = mr->rkey;
1969 inv.ex.invalidate_rkey = mr->rkey;
1970 size = arc4random() % cb->size;
1973 plen = (((size - 1) & PAGE_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
1974 fr.wr.fast_reg.length = size;
1975 fr.wr.fast_reg.page_list_len = plen;
1976 ret = ib_post_send(cb->qp, &fr, &bad);
1978 PRINTF(cb, "ib_post_send failed %d\n", ret);
1985 ret = ib_poll_cq(cb->cq, 1, &wc);
1987 PRINTF(cb, "ib_poll_cq failed %d\n", ret);
1992 PRINTF(cb, "completion error %u\n", wc.status);
1998 else if (krping_sigpending()) {
1999 PRINTF(cb, "signal!\n");
2006 DEBUG_LOG(cb, "sleeping 1 second\n");
2007 wait_event_interruptible_timeout(cb->sem, cb->state == ERROR, HZ);
2009 DEBUG_LOG(cb, "draining the cq...\n");
2011 ret = ib_poll_cq(cb->cq, 1, &wc);
2013 PRINTF(cb, "ib_poll_cq failed %d\n", ret);
2018 PRINTF(cb, "completion error %u opcode %u\n", wc.status, wc.opcode);
2022 DEBUG_LOG(cb, "fr_test: done!\n");
2025 ib_free_fast_reg_page_list(pl);
2028 static int krping_connect_client(struct krping_cb *cb)
2030 struct rdma_conn_param conn_param;
2033 memset(&conn_param, 0, sizeof conn_param);
2034 conn_param.responder_resources = 1;
2035 conn_param.initiator_depth = 1;
2036 conn_param.retry_count = 10;
2038 ret = rdma_connect(cb->cm_id, &conn_param);
2040 PRINTF(cb, "rdma_connect error %d\n", ret);
2044 wait_event_interruptible(cb->sem, cb->state >= CONNECTED);
2045 if (cb->state == ERROR) {
2046 PRINTF(cb, "wait for CONNECTED state %d\n", cb->state);
2050 DEBUG_LOG(cb, "rdma_connect successful\n");
2054 static int krping_bind_client(struct krping_cb *cb)
2056 struct sockaddr_in sin;
2059 memset(&sin, 0, sizeof(sin));
2060 sin.sin_len = sizeof sin;
2061 sin.sin_family = AF_INET;
2062 sin.sin_addr.s_addr = cb->addr.s_addr;
2063 sin.sin_port = cb->port;
2065 ret = rdma_resolve_addr(cb->cm_id, NULL, (struct sockaddr *) &sin,
2068 PRINTF(cb, "rdma_resolve_addr error %d\n", ret);
2072 wait_event_interruptible(cb->sem, cb->state >= ROUTE_RESOLVED);
2073 if (cb->state != ROUTE_RESOLVED) {
2075 "addr/route resolution did not resolve: state %d\n",
2080 if (cb->mem == FASTREG && !fastreg_supported(cb))
2083 DEBUG_LOG(cb, "rdma_resolve_addr - rdma_resolve_route successful\n");
2087 static void krping_run_client(struct krping_cb *cb)
2089 struct ib_recv_wr *bad_wr;
2092 ret = krping_bind_client(cb);
2096 ret = krping_setup_qp(cb, cb->cm_id);
2098 PRINTF(cb, "setup_qp failed: %d\n", ret);
2102 ret = krping_setup_buffers(cb);
2104 PRINTF(cb, "krping_setup_buffers failed: %d\n", ret);
2108 ret = ib_post_recv(cb->qp, &cb->rq_wr, &bad_wr);
2110 PRINTF(cb, "ib_post_recv failed: %d\n", ret);
2114 ret = krping_connect_client(cb);
2116 PRINTF(cb, "connect error %d\n", ret);
2121 krping_wlat_test_client(cb);
2123 krping_rlat_test_client(cb);
2125 krping_bw_test_client(cb);
2126 else if (cb->frtest)
2129 krping_test_client(cb);
2130 rdma_disconnect(cb->cm_id);
2132 krping_free_buffers(cb);
2137 int krping_doit(char *cmd, void *cookie)
2139 struct krping_cb *cb;
2143 unsigned long optint;
2145 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
2149 mutex_lock(&krping_mutex);
2150 list_add_tail(&cb->list, &krping_cbs);
2151 mutex_unlock(&krping_mutex);
2153 cb->cookie = cookie;
2157 cb->txdepth = RPING_SQ_DEPTH;
2159 init_waitqueue_head(&cb->sem);
2161 while ((op = krping_getopt("krping", &cmd, krping_opts, NULL, &optarg,
2165 cb->addr_str = optarg;
2166 DEBUG_LOG(cb, "ipaddr (%s)\n", optarg);
2167 if (!inet_aton(optarg, &cb->addr)) {
2168 PRINTF(cb, "bad addr string %s\n",
2174 cb->port = htons(optint);
2175 DEBUG_LOG(cb, "port %d\n", (int)optint);
2179 DEBUG_LOG(cb, "server\n");
2183 DEBUG_LOG(cb, "server\n");
2187 DEBUG_LOG(cb, "client\n");
2191 if ((cb->size < 1) ||
2192 (cb->size > RPING_BUFSIZE)) {
2193 PRINTF(cb, "Invalid size %d "
2194 "(valid range is 1 to %d)\n",
2195 cb->size, RPING_BUFSIZE);
2198 DEBUG_LOG(cb, "size %d\n", (int)optint);
2202 if (cb->count < 0) {
2203 PRINTF(cb, "Invalid count %d\n",
2207 DEBUG_LOG(cb, "count %d\n", (int) cb->count);
2211 DEBUG_LOG(cb, "verbose\n");
2215 DEBUG_LOG(cb, "validate data\n");
2230 if (!strncmp(optarg, "dma", 3))
2232 else if (!strncmp(optarg, "fastreg", 7))
2234 else if (!strncmp(optarg, "mw", 2))
2236 else if (!strncmp(optarg, "mr", 2))
2239 PRINTF(cb, "unknown mem mode %s. "
2240 "Must be dma, fastreg, mw, or mr\n",
2247 cb->server_invalidate = 1;
2250 cb->txdepth = optint;
2251 DEBUG_LOG(cb, "txdepth %d\n", (int) cb->txdepth);
2254 cb->local_dma_lkey = 1;
2255 DEBUG_LOG(cb, "using local dma lkey\n");
2259 DEBUG_LOG(cb, "using read-with-inv\n");
2263 DEBUG_LOG(cb, "fast-reg test!\n");
2266 PRINTF(cb, "unknown opt %s\n", optarg);
2274 if (cb->server == -1) {
2275 PRINTF(cb, "must be either client or server\n");
2280 if (cb->server && cb->frtest) {
2281 PRINTF(cb, "must be client to run frtest\n");
2286 if ((cb->frtest + cb->bw + cb->rlat + cb->wlat) > 1) {
2287 PRINTF(cb, "Pick only one test: fr, bw, rlat, wlat\n");
2292 if (cb->server_invalidate && cb->mem != FASTREG) {
2293 PRINTF(cb, "server_invalidate only valid with fastreg mem_mode\n");
2298 if (cb->read_inv && cb->mem != FASTREG) {
2299 PRINTF(cb, "read_inv only valid with fastreg mem_mode\n");
2304 if (cb->mem != MR && (cb->wlat || cb->rlat || cb->bw)) {
2305 PRINTF(cb, "wlat, rlat, and bw tests only support mem_mode MR\n");
2310 cb->cm_id = rdma_create_id(krping_cma_event_handler, cb, RDMA_PS_TCP, IB_QPT_RC);
2311 if (IS_ERR(cb->cm_id)) {
2312 ret = PTR_ERR(cb->cm_id);
2313 PRINTF(cb, "rdma_create_id error %d\n", ret);
2316 DEBUG_LOG(cb, "created cm_id %p\n", cb->cm_id);
2319 krping_run_server(cb);
2321 krping_run_client(cb);
2323 DEBUG_LOG(cb, "destroy cm_id %p\n", cb->cm_id);
2324 rdma_destroy_id(cb->cm_id);
2326 mutex_lock(&krping_mutex);
2327 list_del(&cb->list);
2328 mutex_unlock(&krping_mutex);
2334 krping_walk_cb_list(void (*f)(struct krping_stats *, void *), void *arg)
2336 struct krping_cb *cb;
2338 mutex_lock(&krping_mutex);
2339 list_for_each_entry(cb, &krping_cbs, list)
2340 (*f)(cb->pd ? &cb->stats : NULL, arg);
2341 mutex_unlock(&krping_mutex);
2344 void krping_init(void)
2347 mutex_init(&krping_mutex);