/* * Copyright (c) 2005 Ammasso, Inc. All rights reserved. * Copyright (c) 2006-2009 Open Grid Computing, Inc. All rights reserved. * Copyright (c) 2019 Pensando Systems, Inc. All rights reserved. * * This software is available to you under a choice of one of two * licenses. You may choose to be licensed under the terms of the GNU * General Public License (GPL) Version 2, available from the file * COPYING in the main directory of this source tree, or the * OpenIB.org BSD license below: * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: * * - Redistributions of source code must retain the above * copyright notice, this list of conditions and the following * disclaimer. * * - Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials * provided with the distribution. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "krping.h" #include "getopt.h" MODULE_AUTHOR("Steve Wise"); MODULE_DESCRIPTION("RDMA ping server"); MODULE_LICENSE("Dual BSD/GPL"); enum mem_type { DMA = 1, REG = 2, }; static const struct krping_option krping_opts[] = { {"count", OPT_INT, 'C'}, {"size", OPT_INT, 'S'}, {"addr", OPT_STRING, 'a'}, {"addr6", OPT_STRING, 'A'}, {"port", OPT_INT, 'p'}, {"verbose", OPT_NOPARAM, 'v'}, {"validate", OPT_NOPARAM, 'V'}, {"server", OPT_NOPARAM, 's'}, {"client", OPT_NOPARAM, 'c'}, {"mem_mode", OPT_STRING, 'm'}, {"server_inv", OPT_NOPARAM, 'I'}, {"wlat", OPT_NOPARAM, 'l'}, {"rlat", OPT_NOPARAM, 'L'}, {"bw", OPT_NOPARAM, 'B'}, {"duplex", OPT_NOPARAM, 'd'}, {"tos", OPT_INT, 't'}, {"txdepth", OPT_INT, 'T'}, {"poll", OPT_NOPARAM, 'P'}, {"local_dma_lkey", OPT_NOPARAM, 'Z'}, {"read_inv", OPT_NOPARAM, 'R'}, {"fr", OPT_NOPARAM, 'f'}, {"wait", OPT_INT, 'w'}, {"fragments", OPT_INT, 'F'}, {NULL, 0, 0} }; #define htonll(x) cpu_to_be64((x)) #define ntohll(x) cpu_to_be64((x)) static DEFINE_MUTEX(krping_mutex); /* * List of running krping threads. */ static LIST_HEAD(krping_cbs); static u32 krping_next_index = 1; /* * Invoke like this, one on each side, using the server's address on * the RDMA device (iw%d): * * /bin/echo server,port=9999,addr=192.168.69.142,validate > /proc/krping * /bin/echo client,port=9999,addr=192.168.69.142,validate > /proc/krping * /bin/echo client,port=9999,addr6=2001:db8:0:f101::1,validate > /proc/krping * * krping "ping/pong" loop: * client sends source rkey/addr/len * server receives source rkey/add/len * server rdma reads "ping" data from source * server sends "go ahead" on rdma read completion * client sends sink rkey/addr/len * server receives sink rkey/addr/len * server rdma writes "pong" data to sink * server sends "go ahead" on rdma write completion * */ /* * These states are used to signal events between the completion handler * and the main client or server thread. * * Once CONNECTED, they cycle through RDMA_READ_ADV, RDMA_WRITE_ADV, * and RDMA_WRITE_COMPLETE for each ping. */ enum test_state { IDLE = 1, CONNECT_REQUEST, ADDR_RESOLVED, ROUTE_RESOLVED, CONNECTED, CONN_HSHAKE_RCVD, CONN_HSHAKE_SENT, CONN_HSHAKE_DONE, RDMA_READ_ADV, RDMA_READ_COMPLETE, RDMA_WRITE_ADV, RDMA_WRITE_COMPLETE, DONE, DONE_RCVD, DONE_SENT, DONE_DONE, DISCONNECTED, ERROR }; struct krping_rdma_info { uint64_t buf; uint32_t rkey; uint32_t size; uint64_t pad[2]; }; /* * Default max buffer size for IO... */ #define RPING_BUFSIZE 128*1024 #define RPING_SQ_DEPTH 64 #define MAX_SGE_CNT 32 /* * Control block struct. */ struct krping_cb { struct list_head list; u32 index; int server; /* 0 iff client */ struct ib_cq *cq; struct ib_pd *pd; struct ib_qp *qp; enum mem_type mem; struct ib_mr *dma_mr; struct ib_fast_reg_page_list *page_list; int page_list_len; struct ib_reg_wr reg_mr_wr; struct ib_send_wr invalidate_wr; struct ib_mr *reg_mr; int server_invalidate; int read_inv; u8 key; struct ib_recv_wr rq_wr; /* recv work request record */ struct ib_sge recv_sgl; /* recv single SGE */ struct krping_rdma_info recv_buf __aligned(16); /* malloc'd buffer */ u64 recv_dma_addr; DEFINE_DMA_UNMAP_ADDR(recv_mapping); struct ib_send_wr sq_wr; /* send work requrest record */ struct ib_sge send_sgl[MAX_SGE_CNT]; struct krping_rdma_info send_buf __aligned(16); /* single send buf */ u64 send_dma_addr; DEFINE_DMA_UNMAP_ADDR(send_mapping); struct ib_rdma_wr rdma_sq_wr; /* rdma work request record */ struct ib_sge rdma_sgl; /* rdma single SGE */ char *rdma_buf; /* used as rdma sink */ u64 rdma_dma_addr; DEFINE_DMA_UNMAP_ADDR(rdma_mapping); struct ib_mr *rdma_mr; uint32_t remote_rkey; /* remote guys RKEY */ uint64_t remote_addr; /* remote guys TO */ uint32_t remote_len; /* remote guys LEN */ char *start_buf; /* rdma read src */ u64 start_dma_addr; DEFINE_DMA_UNMAP_ADDR(start_mapping); struct ib_mr *start_mr; enum test_state state; /* used for cond/signalling */ spinlock_t state_lock; wait_queue_head_t sem; struct krping_stats stats; uint16_t port; /* dst port in NBO */ u8 addr[16] __aligned(8); /* dst addr in NBO */ char *addr_str; /* dst addr string */ uint8_t addr_type; /* ADDR_FAMILY - IPv4/V6 */ uint16_t scope_id; /* IPv6 scope */ int verbose; /* verbose logging */ int count; /* ping count */ int size; /* ping data size */ int validate; /* validate ping data */ int ping; /* run basic ping-pong test */ int wlat; /* run wlat test */ int rlat; /* run rlat test */ int bw; /* run bw test */ int duplex; /* run bw full duplex test */ int poll; /* poll or block for rlat test */ int txdepth; /* SQ depth */ int local_dma_lkey; /* use 0 for lkey */ int frtest; /* reg test */ int tos; /* type of service */ int wait; /* server timeout for connect */ int fragments; /* fragments used for initial SEND */ int scnt; int rcnt; int ccnt; int frcnt; int fcnt; /* CM stuff */ struct rdma_cm_id *cm_id; /* connection on client side, */ /* listener on server side. */ struct rdma_cm_id *child_cm_id; /* connection on server side */ }; static const char * krping_state_str(enum test_state state) { switch (state) { case IDLE: return "IDLE"; case CONNECT_REQUEST: return "CONNECT_REQUEST"; case ADDR_RESOLVED: return "ADDR_RESOLVED"; case ROUTE_RESOLVED: return "ROUTE_RESOLVED"; case CONNECTED: return "CONNECTED"; case CONN_HSHAKE_RCVD: return "CONN_HSHAKE_RCVD"; case CONN_HSHAKE_SENT: return "CONN_HSHAKE_SENT"; case CONN_HSHAKE_DONE: return "CONN_HSHAKE_DONE"; case RDMA_READ_ADV: return "RDMA_READ_ADV"; case RDMA_READ_COMPLETE: return "RDMA_READ_COMPLETE"; case RDMA_WRITE_ADV: return "RDMA_WRITE_ADV"; case RDMA_WRITE_COMPLETE: return "RDMA_WRITE_COMPLETE"; case DONE: return "DONE"; case DONE_RCVD: return "DONE_RCVD"; case DONE_SENT: return "DONE_SENT"; case DONE_DONE: return "DONE_DONE"; case DISCONNECTED: return "DISCONNECTED"; case ERROR: return "ERROR"; default: return "UNKNOWN"; } } static inline void krping_to_state_locked(struct krping_cb *cb, enum test_state new_state) { if (cb->state != new_state && cb->state != DISCONNECTED && cb->state != ERROR) { DEBUG_LOG("%s transition %s -> %s\n", cb->server ? "server" : "client", krping_state_str(cb->state), krping_state_str(new_state)); cb->state = new_state; } } static void krping_to_state(struct krping_cb *cb, enum test_state new_state) { spin_lock(&cb->state_lock); krping_to_state_locked(cb, new_state); spin_unlock(&cb->state_lock); } static inline struct ib_mr * krping_get_dma_mr(struct ib_pd *pd, int mr_access_flags) { struct ib_mr *mr; int err; err = ib_check_mr_access(mr_access_flags); if (err) return ERR_PTR(err); #ifdef LINUX_DEV_OPS if (!pd->device->ops.get_dma_mr) #else if (!pd->device->get_dma_mr) #endif return ERR_PTR(-ENOSYS); #ifdef LINUX_DEV_OPS mr = pd->device->ops.get_dma_mr(pd, mr_access_flags); #else mr = pd->device->get_dma_mr(pd, mr_access_flags); #endif if (IS_ERR(mr)) return ERR_CAST(mr); mr->device = pd->device; mr->pd = pd; mr->uobject = NULL; mr->need_inval = false; atomic_inc(&pd->usecnt); return (mr); } static int krping_cma_event_handler(struct rdma_cm_id *cma_id, struct rdma_cm_event *event) { int ret; struct krping_cb *cb = cma_id->context; DEBUG_LOG("cma_event type %d cma_id %p (%s)\n", event->event, cma_id, (cma_id == cb->cm_id) ? "parent" : "child"); switch (event->event) { case RDMA_CM_EVENT_ADDR_RESOLVED: krping_to_state(cb, ADDR_RESOLVED); ret = rdma_resolve_route(cma_id, 2000); if (ret) { printk(KERN_ERR PFX "rdma_resolve_route error %d\n", ret); wake_up_interruptible(&cb->sem); } break; case RDMA_CM_EVENT_ROUTE_RESOLVED: krping_to_state(cb, ROUTE_RESOLVED); wake_up_interruptible(&cb->sem); break; case RDMA_CM_EVENT_CONNECT_REQUEST: if (cb->state != IDLE) { printk(KERN_ERR PFX "rejecting cma %p in state %s\n", cma_id, krping_state_str(cb->state)); rdma_reject(cma_id, NULL, 0); break; } krping_to_state(cb, CONNECT_REQUEST); cb->child_cm_id = cma_id; DEBUG_LOG("child_cm_id %p\n", cb->child_cm_id); wake_up_interruptible(&cb->sem); break; case RDMA_CM_EVENT_ESTABLISHED: krping_to_state(cb, CONNECTED); wake_up_interruptible(&cb->sem); break; case RDMA_CM_EVENT_ADDR_ERROR: case RDMA_CM_EVENT_ROUTE_ERROR: case RDMA_CM_EVENT_CONNECT_ERROR: case RDMA_CM_EVENT_UNREACHABLE: case RDMA_CM_EVENT_REJECTED: printk(KERN_ERR PFX "cma event %d, error %d\n", event->event, event->status); krping_to_state(cb, ERROR); wake_up_interruptible(&cb->sem); break; case RDMA_CM_EVENT_DISCONNECTED: krping_to_state(cb, DISCONNECTED); wake_up_interruptible(&cb->sem); break; case RDMA_CM_EVENT_DEVICE_REMOVAL: printk(KERN_ERR PFX "cma detected device removal!!!!\n"); krping_to_state(cb, ERROR); wake_up_interruptible(&cb->sem); break; default: printk(KERN_ERR PFX "oof bad type!\n"); wake_up_interruptible(&cb->sem); break; } return 0; } /* * Ping-pong test (server side) */ static void server_recv(struct krping_cb *cb) { cb->remote_rkey = ntohl(cb->recv_buf.rkey); cb->remote_addr = ntohll(cb->recv_buf.buf); cb->remote_len = ntohl(cb->recv_buf.size); DEBUG_LOG("Received rkey %x addr %llx len %d from peer\n", cb->remote_rkey, (unsigned long long)cb->remote_addr, cb->remote_len); } /* * Ping-pong test (server side) * CONNECTED -> RDMA_READ_ADV * RDMA_READ_COMPLETE -> RDMA_WRITE_ADV * RDMA_WRITE_COMPLETE -> RDMA_READ_ADV */ static int server_recv_state(struct krping_cb *cb) { int ret = 0; spin_lock(&cb->state_lock); if (cb->state == CONNECTED || cb->state == RDMA_WRITE_COMPLETE) krping_to_state_locked(cb, RDMA_READ_ADV); else if (cb->state == RDMA_READ_COMPLETE) krping_to_state_locked(cb, RDMA_WRITE_ADV); else { DEBUG_LOG("didn't expect state %s", krping_state_str(cb->state)); ret = -EINVAL; } spin_unlock(&cb->state_lock); return ret; } /* * Ping-pong test (client side) * RDMA_READ_ADV -> RDMA_WRITE_ADV * RDMA_WRITE_ADV -> RDMA_WRITE_COMPLETE */ static int client_recv_state(struct krping_cb *cb) { int ret = 0; spin_lock(&cb->state_lock); if (cb->state == RDMA_READ_ADV) krping_to_state_locked(cb, RDMA_WRITE_ADV); else if (cb->state == RDMA_WRITE_ADV) krping_to_state_locked(cb, RDMA_WRITE_COMPLETE); else { DEBUG_LOG("didn't expect state %s", krping_state_str(cb->state)); ret = -EINVAL; } spin_unlock(&cb->state_lock); return ret; } /* * This is the CQ event handler for the basic ping-pong test * Write CQE -> RDMA_WRITE_COMPLETE * Read CQE -> RDMA_READ_COMPLETE * Recv CQE -> server_recv()/client_recv() */ static void krping_cq_event_handler(struct ib_cq *cq, void *ctx) { struct krping_cb *cb = ctx; struct ib_wc wc; WRCONST struct ib_recv_wr *bad_wr; int ret; BUG_ON(cb->cq != cq); if (cb->state >= DONE) { if (cb->state == ERROR) printk(KERN_ERR PFX "cq completion in ERROR state\n"); else DEBUG_LOG("cq completion in %s state\n", krping_state_str(cb->state)); return; } ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP); while ((ret = ib_poll_cq(cb->cq, 1, &wc)) == 1) { if (wc.status) { if (wc.status == IB_WC_WR_FLUSH_ERR) { DEBUG_LOG("cq flushed\n"); continue; } else { printk(KERN_ERR PFX "cq completion failed with wr_id "PRIx64 " status %d opcode %d vendor_err %x\n", (uintmax_t)wc.wr_id, wc.status, wc.opcode, wc.vendor_err); goto error; } } switch (wc.opcode) { case IB_WC_SEND: DEBUG_LOG2("send completion\n"); cb->stats.send_bytes += sizeof cb->send_buf; cb->stats.send_msgs++; break; case IB_WC_RDMA_WRITE: DEBUG_LOG2("rdma write completion\n"); cb->stats.write_bytes += cb->rdma_sq_wr.wr.sg_list->length; cb->stats.write_msgs++; krping_to_state(cb, RDMA_WRITE_COMPLETE); wake_up_interruptible(&cb->sem); break; case IB_WC_RDMA_READ: DEBUG_LOG2("rdma read completion\n"); cb->stats.read_bytes += cb->rdma_sq_wr.wr.sg_list->length; cb->stats.read_msgs++; krping_to_state(cb, RDMA_READ_COMPLETE); wake_up_interruptible(&cb->sem); break; case IB_WC_RECV: DEBUG_LOG2("recv completion\n"); cb->stats.recv_bytes += sizeof(cb->recv_buf); cb->stats.recv_msgs++; if (wc.byte_len != sizeof(cb->recv_buf)) { printk(KERN_ERR PFX "received bogus size %d\n", wc.byte_len); goto error; } if (cb->server) server_recv(cb); ret = ib_post_recv(cb->qp, &cb->rq_wr, &bad_wr); if (ret) { printk(KERN_ERR PFX "post recv error: %d\n", ret); goto error; } ret = cb->server ? server_recv_state(cb) : client_recv_state(cb); if (ret) { printk(KERN_ERR PFX "recv state error: %d\n", ret); goto error; } wake_up_interruptible(&cb->sem); break; default: printk(KERN_ERR PFX "unexpected opcode %d in cq_event\n", wc.opcode); goto error; } } if (ret) { printk(KERN_ERR PFX "poll error %d\n", ret); goto error; } return; error: krping_to_state(cb, ERROR); wake_up_interruptible(&cb->sem); } /* * bw/wlat/rlat test * CONNECTED -> CONN_HSHAKE_SENT * CONN_HSHAKE_RCVD -> CONN_HSHAKE_DONE * DONE -> DONE_SENT * DONE_RCVD -> DONE_DONE */ static int polled_send(struct krping_cb *cb, struct ib_wc *wc) { int ret = 0; spin_lock(&cb->state_lock); if (cb->state == CONNECTED) krping_to_state_locked(cb, CONN_HSHAKE_SENT); else if (cb->state == CONN_HSHAKE_RCVD) krping_to_state_locked(cb, CONN_HSHAKE_DONE); else if (cb->state == DONE) krping_to_state_locked(cb, DONE_SENT); else if (cb->state == DONE_RCVD) krping_to_state_locked(cb, DONE_DONE); else ret = -EINVAL; spin_unlock(&cb->state_lock); return ret; } /* * bw/wlat/rlat test * CONNECTED -> CONN_HSHAKE_RCVD * CONN_HSHAKE_SENT -> CONN_HSHAKE_DONE * DONE -> DONE_RCVD * DONE_SENT -> DONE_DONE */ static int polled_recv(struct krping_cb *cb, struct ib_wc *wc) { int ret = 0; if (wc->byte_len != sizeof(cb->recv_buf)) { printk(KERN_ERR PFX "received bogus data, size %d\n", wc->byte_len); return -1; } if (cb->remote_addr == 0) { cb->remote_rkey = ntohl(cb->recv_buf.rkey); cb->remote_addr = ntohll(cb->recv_buf.buf); cb->remote_len = ntohl(cb->recv_buf.size); DEBUG_LOG("Received rkey %x addr %llx len %d from peer\n", cb->remote_rkey, (unsigned long long)cb->remote_addr, cb->remote_len); } spin_lock(&cb->state_lock); if (cb->state == CONNECTED) krping_to_state_locked(cb, CONN_HSHAKE_RCVD); else if (cb->state == CONN_HSHAKE_SENT) krping_to_state_locked(cb, CONN_HSHAKE_DONE); else if (cb->state == DONE) krping_to_state_locked(cb, DONE_RCVD); else if (cb->state == DONE_SENT) krping_to_state_locked(cb, DONE_DONE); else ret = -EINVAL; spin_unlock(&cb->state_lock); return ret; } /* * This is the CQ event handler for the rlat test in event-driven mode. * It also used for the other bandwidth and latency tests for handshakes. */ static void krping_cq_bwlat_event_handler(struct ib_cq *cq, void *ctx) { struct krping_cb *cb = ctx; struct ib_wc wc; WRCONST struct ib_recv_wr *bad_wr; int ret; BUG_ON(cb->cq != cq); if (cb->rlat && !cb->poll && cb->state == RDMA_READ_ADV) { /* Test body will poll off the completion */ krping_to_state(cb, RDMA_READ_COMPLETE); wake_up_interruptible(&cb->sem); return; } while ((ret = ib_poll_cq(cb->cq, 1, &wc)) == 1) { if (wc.status) { if (wc.status == IB_WC_WR_FLUSH_ERR) { DEBUG_LOG("cq flushed\n"); continue; } else { printk(KERN_ERR PFX "cq completion failed with wr_id "PRIx64 " status %d opcode %d vendor_err %x\n", (uintmax_t)wc.wr_id, wc.status, wc.opcode, wc.vendor_err); goto error; } } switch (wc.opcode) { case IB_WC_SEND: DEBUG_LOG2("send completion\n"); cb->stats.send_bytes += sizeof cb->send_buf; cb->stats.send_msgs++; ret = polled_send(cb, &wc); if (ret) { printk(KERN_ERR PFX "send wc error: %d\n", ret); goto error; } break; case IB_WC_RECV: DEBUG_LOG2("recv completion\n"); cb->stats.recv_bytes += sizeof cb->recv_buf; cb->stats.recv_msgs++; ret = polled_recv(cb, &wc); if (ret) { printk(KERN_ERR PFX "recv wc error: %d\n", ret); goto error; } ret = ib_post_recv(cb->qp, &cb->rq_wr, &bad_wr); if (ret) { printk(KERN_ERR PFX "post recv error: %d\n", ret); goto error; } break; default: printk(KERN_ERR PFX "unexpected opcode %d in bwlat_event\n", wc.opcode); goto error; } } if (ret) { printk(KERN_ERR PFX "poll error %d\n", ret); goto error; } wake_up_interruptible(&cb->sem); return; error: krping_to_state(cb, ERROR); wake_up_interruptible(&cb->sem); } /* * This is the server handshake function used in the bw/wlat/rlat tests */ static int krping_handshake_server(struct krping_cb *cb) { WRCONST struct ib_send_wr *bad_wr; int ret; cb->send_buf.buf = htonll(cb->start_dma_addr); cb->send_buf.rkey = htonl(cb->dma_mr->rkey); cb->send_buf.size = htonl(cb->size); /* Arm CQ event handler for the receive */ ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP); /* Wait for a handshake message from the peer */ wait_event_interruptible(cb->sem, cb->state >= CONN_HSHAKE_RCVD); if (cb->state != CONN_HSHAKE_RCVD) { printk(KERN_ERR PFX "wait for CONN_HSHAKE_RCVD ended in %s\n", krping_state_str(cb->state)); return -ECONNRESET; } /* Arm CQ event handler for the send */ ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP); /* Post the send */ ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr); if (ret) { printk(KERN_ERR PFX "post send error %d\n", ret); krping_to_state(cb, ERROR); return -ECONNRESET; } /* Wait for the send to complete */ wait_event_interruptible(cb->sem, cb->state >= CONN_HSHAKE_DONE); if (cb->state != CONN_HSHAKE_DONE) { printk(KERN_ERR PFX "wait for CONN_HSHAKE_DONE ended in %s\n", krping_state_str(cb->state)); return -ECONNRESET; } return 0; } /* * This is the client handshake function used in the bw/wlat/rlat tests */ static int krping_handshake_client(struct krping_cb *cb) { WRCONST struct ib_send_wr *bad_wr; int ret; cb->send_buf.buf = htonll(cb->start_dma_addr); cb->send_buf.rkey = htonl(cb->dma_mr->rkey); cb->send_buf.size = htonl(cb->size); /* Arm CQ event handler for the send completion */ ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP); /* Post the send */ ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr); if (ret) { printk(KERN_ERR PFX "post send error %d\n", ret); krping_to_state(cb, ERROR); return -ECONNRESET; } /* Wait for a completion (send or handshake from the peer) */ wait_event_interruptible(cb->sem, cb->state >= CONN_HSHAKE_RCVD); if (cb->state < CONN_HSHAKE_DONE) { /* Arm CQ event handler for the other completion */ ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP); wait_event_interruptible(cb->sem, cb->state >= CONN_HSHAKE_DONE); } if (cb->state != CONN_HSHAKE_DONE) { printk(KERN_ERR PFX "wait for CONN_HSHAKE_DONE ended in %s\n", krping_state_str(cb->state)); return -ECONNRESET; } return 0; } static int krping_accept(struct krping_cb *cb) { struct rdma_conn_param conn_param; int ret; DEBUG_LOG("accepting client connection request\n"); memset(&conn_param, 0, sizeof conn_param); conn_param.responder_resources = 1; conn_param.initiator_depth = 1; ret = rdma_accept(cb->child_cm_id, &conn_param); if (ret) { printk(KERN_ERR PFX "rdma_accept error: %d\n", ret); krping_to_state(cb, ERROR); return ret; } wait_event_interruptible(cb->sem, cb->state >= CONNECTED); if (cb->state >= DISCONNECTED) { printk(KERN_ERR PFX "wait for CONNECTED ended in %s\n", krping_state_str(cb->state)); return -1; } return 0; } static void krping_setup_wr(struct krping_cb *cb) { size_t sgl_len; int i; cb->recv_sgl.addr = cb->recv_dma_addr; cb->recv_sgl.length = sizeof cb->recv_buf; if (cb->mem == DMA) cb->recv_sgl.lkey = cb->dma_mr->lkey; else cb->recv_sgl.lkey = cb->pd->local_dma_lkey; cb->rq_wr.sg_list = &cb->recv_sgl; cb->rq_wr.num_sge = 1; for (i = 0, sgl_len = 0; i < cb->fragments; i++) { cb->send_sgl[i].addr = cb->send_dma_addr + sgl_len; cb->send_sgl[i].length = (sizeof cb->send_buf) / cb->fragments; if (cb->mem == DMA) cb->send_sgl[i].lkey = cb->dma_mr->lkey; else cb->send_sgl[i].lkey = cb->pd->local_dma_lkey; sgl_len += cb->send_sgl[i].length; } cb->send_sgl[cb->fragments-1].length += sizeof cb->send_buf - sgl_len; cb->sq_wr.opcode = IB_WR_SEND; cb->sq_wr.send_flags = IB_SEND_SIGNALED; cb->sq_wr.sg_list = cb->send_sgl; cb->sq_wr.num_sge = cb->fragments; if (cb->server || cb->wlat || cb->rlat || cb->bw) { cb->rdma_sgl.addr = cb->rdma_dma_addr; cb->rdma_sq_wr.wr.send_flags = IB_SEND_SIGNALED; cb->rdma_sq_wr.wr.sg_list = &cb->rdma_sgl; cb->rdma_sq_wr.wr.num_sge = 1; } if (cb->mem == REG) { /* * A chain of 2 WRs, INVALIDATE_MR + REG_MR. * both unsignaled. The client uses them to reregister * the rdma buffers with a new key each iteration. */ cb->reg_mr_wr.wr.opcode = IB_WR_REG_MR; cb->reg_mr_wr.mr = cb->reg_mr; cb->invalidate_wr.next = &cb->reg_mr_wr.wr; cb->invalidate_wr.opcode = IB_WR_LOCAL_INV; } } static int krping_setup_buffers(struct krping_cb *cb) { int ret; DEBUG_LOG(PFX "krping_setup_buffers called on cb %p\n", cb); cb->recv_dma_addr = ib_dma_map_single(cb->pd->device, &cb->recv_buf, sizeof(cb->recv_buf), DMA_BIDIRECTIONAL); dma_unmap_addr_set(cb, recv_mapping, cb->recv_dma_addr); cb->send_dma_addr = ib_dma_map_single(cb->pd->device, &cb->send_buf, sizeof(cb->send_buf), DMA_BIDIRECTIONAL); dma_unmap_addr_set(cb, send_mapping, cb->send_dma_addr); if (cb->mem == DMA) { cb->dma_mr = krping_get_dma_mr(cb->pd, IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ | IB_ACCESS_REMOTE_WRITE); if (IS_ERR(cb->dma_mr)) { DEBUG_LOG(PFX "reg_dmamr failed\n"); ret = PTR_ERR(cb->dma_mr); goto bail; } } cb->rdma_buf = ib_dma_alloc_coherent(cb->pd->device, cb->size, &cb->rdma_dma_addr, GFP_KERNEL); if (!cb->rdma_buf) { DEBUG_LOG(PFX "rdma_buf allocation failed\n"); ret = -ENOMEM; goto bail; } dma_unmap_addr_set(cb, rdma_mapping, cb->rdma_dma_addr); if (cb->mem == REG) { cb->page_list_len = (((cb->size - 1) & PAGE_MASK) + PAGE_SIZE) >> PAGE_SHIFT; cb->reg_mr = ib_alloc_mr(cb->pd, IB_MR_TYPE_MEM_REG, cb->page_list_len); if (IS_ERR(cb->reg_mr)) { ret = PTR_ERR(cb->reg_mr); DEBUG_LOG(PFX "recv_buf reg_mr failed %d\n", ret); goto bail; } DEBUG_LOG(PFX "reg rkey 0x%x page_list_len %u\n", cb->reg_mr->rkey, cb->page_list_len); } if (!cb->server || cb->wlat || cb->rlat || cb->bw) { cb->start_buf = ib_dma_alloc_coherent(cb->pd->device, cb->size, &cb->start_dma_addr, GFP_KERNEL); if (!cb->start_buf) { DEBUG_LOG(PFX "start_buf malloc failed\n"); ret = -ENOMEM; goto bail; } dma_unmap_addr_set(cb, start_mapping, cb->start_dma_addr); } krping_setup_wr(cb); DEBUG_LOG(PFX "allocated & registered buffers...\n"); return 0; bail: if (cb->reg_mr && !IS_ERR(cb->reg_mr)) ib_dereg_mr(cb->reg_mr); if (cb->rdma_mr && !IS_ERR(cb->rdma_mr)) ib_dereg_mr(cb->rdma_mr); if (cb->dma_mr && !IS_ERR(cb->dma_mr)) ib_dereg_mr(cb->dma_mr); if (cb->rdma_buf) { ib_dma_free_coherent(cb->pd->device, cb->size, cb->rdma_buf, cb->rdma_dma_addr); } if (cb->start_buf) { ib_dma_free_coherent(cb->pd->device, cb->size, cb->start_buf, cb->start_dma_addr); } return ret; } static void krping_free_buffers(struct krping_cb *cb) { DEBUG_LOG("krping_free_buffers called on cb %p\n", cb); if (cb->dma_mr) ib_dereg_mr(cb->dma_mr); if (cb->rdma_mr) ib_dereg_mr(cb->rdma_mr); if (cb->start_mr) ib_dereg_mr(cb->start_mr); if (cb->reg_mr) ib_dereg_mr(cb->reg_mr); dma_unmap_single(cb->pd->device->dma_device, dma_unmap_addr(cb, recv_mapping), sizeof(cb->recv_buf), DMA_BIDIRECTIONAL); dma_unmap_single(cb->pd->device->dma_device, dma_unmap_addr(cb, send_mapping), sizeof(cb->send_buf), DMA_BIDIRECTIONAL); ib_dma_free_coherent(cb->pd->device, cb->size, cb->rdma_buf, cb->rdma_dma_addr); if (cb->start_buf) { ib_dma_free_coherent(cb->pd->device, cb->size, cb->start_buf, cb->start_dma_addr); } } static int krping_create_qp(struct krping_cb *cb) { struct ib_qp_init_attr init_attr; int ret; memset(&init_attr, 0, sizeof(init_attr)); init_attr.cap.max_send_wr = cb->txdepth; init_attr.cap.max_recv_wr = 2; /* For flush_qp() */ init_attr.cap.max_send_wr++; init_attr.cap.max_recv_wr++; init_attr.cap.max_recv_sge = 1; init_attr.cap.max_send_sge = cb->fragments; init_attr.qp_type = IB_QPT_RC; init_attr.send_cq = cb->cq; init_attr.recv_cq = cb->cq; init_attr.sq_sig_type = IB_SIGNAL_REQ_WR; if (cb->server) { ret = rdma_create_qp(cb->child_cm_id, cb->pd, &init_attr); if (!ret) cb->qp = cb->child_cm_id->qp; } else { ret = rdma_create_qp(cb->cm_id, cb->pd, &init_attr); if (!ret) cb->qp = cb->cm_id->qp; } return ret; } static void krping_free_qp(struct krping_cb *cb) { if (cb->server) rdma_destroy_qp(cb->child_cm_id); else rdma_destroy_qp(cb->cm_id); ib_destroy_cq(cb->cq); ib_dealloc_pd(cb->pd); } static int krping_setup_qp(struct krping_cb *cb, struct rdma_cm_id *cm_id) { int ret; struct ib_cq_init_attr attr = {0}; cb->pd = krping_alloc_pd(cm_id->device); if (IS_ERR(cb->pd)) { printk(KERN_ERR PFX "ib_alloc_pd failed\n"); return PTR_ERR(cb->pd); } DEBUG_LOG("created pd %p\n", cb->pd); strlcpy(cb->stats.name, cb->pd->device->name, sizeof(cb->stats.name)); attr.cqe = cb->txdepth * 2; attr.comp_vector = 0; if (cb->ping) cb->cq = ib_create_cq(cm_id->device, krping_cq_event_handler, NULL, cb, &attr); else cb->cq = ib_create_cq(cm_id->device, krping_cq_bwlat_event_handler, NULL, cb, &attr); if (IS_ERR(cb->cq)) { printk(KERN_ERR PFX "ib_create_cq failed\n"); ret = PTR_ERR(cb->cq); goto err1; } DEBUG_LOG("created cq %p\n", cb->cq); ret = krping_create_qp(cb); if (ret) { printk(KERN_ERR PFX "krping_create_qp failed: %d\n", ret); goto err2; } DEBUG_LOG("created qp %p\n", cb->qp); return 0; err2: ib_destroy_cq(cb->cq); err1: ib_dealloc_pd(cb->pd); return ret; } /* * return the (possibly rebound) rkey for the rdma buffer. * REG mode: invalidate and rebind via reg wr. * other modes: just return the mr rkey. */ static u32 krping_rdma_rkey(struct krping_cb *cb, u64 buf, int post_inv) { u32 rkey; WRCONST struct ib_send_wr *bad_wr; int ret; struct scatterlist sg = {0}; if (cb->mem == DMA) { rkey = cb->dma_mr->rkey; return rkey; } cb->invalidate_wr.ex.invalidate_rkey = cb->reg_mr->rkey; /* * Update the reg key. */ ib_update_fast_reg_key(cb->reg_mr, ++cb->key); cb->reg_mr_wr.key = cb->reg_mr->rkey; /* * Update the reg WR with new buf info. */ if (buf == (u64)cb->start_dma_addr) cb->reg_mr_wr.access = IB_ACCESS_REMOTE_READ; else cb->reg_mr_wr.access = IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE; sg_init_table(&sg, 1); sg_dma_address(&sg) = buf; sg_dma_len(&sg) = cb->size; ret = ib_map_mr_sg(cb->reg_mr, &sg, 1, NULL, PAGE_SIZE); BUG_ON(ret <= 0 || ret > cb->page_list_len); DEBUG_LOG2(PFX "post_inv = %d, reg_mr new rkey 0x%x pgsz %u len %llu" " iova_start %llx\n", post_inv, cb->reg_mr_wr.key, cb->reg_mr->page_size, (unsigned long long)cb->reg_mr->length, (unsigned long long)cb->reg_mr->iova); if (post_inv) ret = ib_post_send(cb->qp, &cb->invalidate_wr, &bad_wr); else ret = ib_post_send(cb->qp, &cb->reg_mr_wr.wr, &bad_wr); if (ret) { printk(KERN_ERR PFX "post send error %d\n", ret); krping_to_state(cb, ERROR); } rkey = cb->reg_mr->rkey; return rkey; } static void krping_format_send(struct krping_cb *cb, u64 buf) { struct krping_rdma_info *info = &cb->send_buf; u32 rkey; /* * Client side will do reg or mw bind before * advertising the rdma buffer. Server side * sends have no data. */ if (!cb->server) { rkey = krping_rdma_rkey(cb, buf, !cb->server_invalidate); info->buf = htonll(buf); info->rkey = htonl(rkey); info->size = htonl(cb->size); DEBUG_LOG("RDMA addr %llx rkey %x len %d\n", (unsigned long long)buf, rkey, cb->size); } } /* * Ping-pong test server * CONNECTED -> RDMA_READ_ADV -> RDMA_READ_COMPLETE -> * RDMA_WRITE_ADV -> RDMA_WRITE_COMPLETE -> CONNECTED */ static int krping_test_server(struct krping_cb *cb) { WRCONST struct ib_send_wr *bad_wr; struct ib_send_wr inv; int ret; int ping = 0; bool fence = true; /* Start CQ event handler */ ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP); /* Server should start in CONNECTED */ while (1) { /* Wait for client's Start STAG/TO/Len */ wait_event_interruptible(cb->sem, cb->state >= RDMA_READ_ADV); if (cb->state != RDMA_READ_ADV) { if (cb->state < DONE) printk(KERN_ERR PFX "wait for RDMA_READ_ADV ended in %s\n", krping_state_str(cb->state)); break; } DEBUG_LOG("server received sink adv, iter is %s\n", fence ? "fenced" : "not fenced"); if (fence) { cb->sq_wr.send_flags |= IB_SEND_FENCE; cb->rdma_sq_wr.wr.send_flags |= IB_SEND_FENCE; cb->reg_mr_wr.wr.send_flags |= IB_SEND_FENCE; } else { cb->sq_wr.send_flags &= ~IB_SEND_FENCE; cb->rdma_sq_wr.wr.send_flags &= ~IB_SEND_FENCE; cb->reg_mr_wr.wr.send_flags &= ~IB_SEND_FENCE; } fence = !fence; cb->rdma_sq_wr.rkey = cb->remote_rkey; cb->rdma_sq_wr.remote_addr = cb->remote_addr; cb->rdma_sq_wr.wr.sg_list->length = cb->remote_len; cb->rdma_sgl.lkey = krping_rdma_rkey(cb, cb->rdma_dma_addr, !cb->read_inv); cb->rdma_sq_wr.wr.next = NULL; /* Issue RDMA Read. */ if (cb->read_inv) cb->rdma_sq_wr.wr.opcode = IB_WR_RDMA_READ_WITH_INV; else { cb->rdma_sq_wr.wr.opcode = IB_WR_RDMA_READ; if (cb->mem == REG) { /* * Immediately follow the read with a * fenced LOCAL_INV. */ cb->rdma_sq_wr.wr.next = &inv; memset(&inv, 0, sizeof inv); inv.opcode = IB_WR_LOCAL_INV; inv.ex.invalidate_rkey = cb->reg_mr->rkey; inv.send_flags = IB_SEND_FENCE; } } ret = ib_post_send(cb->qp, &cb->rdma_sq_wr.wr, &bad_wr); if (ret) { printk(KERN_ERR PFX "post send error %d\n", ret); break; } cb->rdma_sq_wr.wr.next = NULL; DEBUG_LOG2("server posted rdma read req \n"); /* Wait for read completion */ wait_event_interruptible(cb->sem, cb->state >= RDMA_READ_COMPLETE); if (cb->state != RDMA_READ_COMPLETE) { printk(KERN_ERR PFX "wait for RDMA_READ_COMPLETE ended in %s\n", krping_state_str(cb->state)); break; } DEBUG_LOG2("server received read complete\n"); /* Display data in recv buf */ if (cb->verbose) printk(KERN_INFO PFX "server ping data (64B max): |%.64s|\n", cb->rdma_buf); /* Tell client to continue */ if (cb->server_invalidate) { cb->sq_wr.ex.invalidate_rkey = cb->remote_rkey; cb->sq_wr.opcode = IB_WR_SEND_WITH_INV; DEBUG_LOG2("send-w-inv rkey 0x%x\n", cb->remote_rkey); } ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr); if (ret) { printk(KERN_ERR PFX "post send error %d\n", ret); break; } DEBUG_LOG2("server posted go ahead\n"); /* Wait for client's RDMA STAG/TO/Len */ wait_event_interruptible(cb->sem, cb->state >= RDMA_WRITE_ADV); if (cb->state != RDMA_WRITE_ADV) { printk(KERN_ERR PFX "wait for RDMA_WRITE_ADV ended in %s\n", krping_state_str(cb->state)); break; } DEBUG_LOG2("server received sink adv\n"); /* RDMA Write echo data */ cb->rdma_sq_wr.wr.opcode = IB_WR_RDMA_WRITE; cb->rdma_sq_wr.rkey = cb->remote_rkey; cb->rdma_sq_wr.remote_addr = cb->remote_addr; cb->rdma_sq_wr.wr.sg_list->length = cb->remote_len; if (cb->local_dma_lkey) cb->rdma_sgl.lkey = cb->pd->local_dma_lkey; else cb->rdma_sgl.lkey = krping_rdma_rkey(cb, cb->rdma_dma_addr, 0); DEBUG_LOG2("rdma write from lkey %x laddr %llx len %d\n", cb->rdma_sq_wr.wr.sg_list->lkey, (unsigned long long)cb->rdma_sq_wr.wr.sg_list->addr, cb->rdma_sq_wr.wr.sg_list->length); ret = ib_post_send(cb->qp, &cb->rdma_sq_wr.wr, &bad_wr); if (ret) { printk(KERN_ERR PFX "post send error %d\n", ret); break; } /* Wait for completion */ ret = wait_event_interruptible(cb->sem, cb->state >= RDMA_WRITE_COMPLETE); if (cb->state != RDMA_WRITE_COMPLETE) { printk(KERN_ERR PFX "wait for RDMA_WRITE_COMPLETE ended in %s\n", krping_state_str(cb->state)); break; } DEBUG_LOG2("server rdma write complete \n"); krping_to_state(cb, CONNECTED); /* Tell client to begin again */ if (cb->server_invalidate) { cb->sq_wr.ex.invalidate_rkey = cb->remote_rkey; cb->sq_wr.opcode = IB_WR_SEND_WITH_INV; DEBUG_LOG2("send-w-inv rkey 0x%x\n", cb->remote_rkey); } ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr); if (ret) { printk(KERN_ERR PFX "post send error %d\n", ret); break; } DEBUG_LOG2("server posted go ahead, ping=%d\n", ping); ping++; } return (ping == cb->count || (!cb->count && cb->state == DISCONNECTED)) ? 0 : -EIO; } /* * Read latency test body * CONN_HSHAKE_DONE -> RDMA_READ_ADV -> RDMA_READ_COMPLETE -> DONE (events) * CONN_HSHAKE_DONE -> DONE (polled) * CONN_HSHAKE_DONE -> ERROR */ static int rlat_test(struct krping_cb *cb) { int iters = cb->count; struct timespec64 start_ts, stop_ts, delta_ts; int ret; struct ib_wc wc; WRCONST struct ib_send_wr *bad_wr; int ne; DEBUG_LOG("%s starting rlat test%s\n", cb->server ? "server" : "client", cb->poll ? "(polled)" : ""); cb->rdma_sq_wr.wr.opcode = IB_WR_RDMA_READ; cb->rdma_sq_wr.rkey = cb->remote_rkey; cb->rdma_sq_wr.remote_addr = cb->remote_addr; cb->rdma_sq_wr.wr.sg_list->length = cb->size; ktime_get_ts64(&start_ts); while (cb->scnt < iters) { ret = ib_post_send(cb->qp, &cb->rdma_sq_wr.wr, &bad_wr); if (ret) { printk(KERN_ERR PFX "Couldn't post send: ret=%d scnt %d\n", ret, cb->scnt); goto error; } do { if (!cb->poll) { krping_to_state(cb, RDMA_READ_ADV); ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP); wait_event_interruptible(cb->sem, cb->state != RDMA_READ_ADV); if (cb->state != RDMA_READ_COMPLETE) goto error; } ne = ib_poll_cq(cb->cq, 1, &wc); if (cb->state >= DISCONNECTED) { printk(KERN_ERR PFX "state %s, scnt %d\n", krping_state_str(cb->state), cb->scnt); goto error; } } while (ne == 0); if (ne < 0) { printk(KERN_ERR PFX "poll failed %d\n", ne); goto error; } if (wc.status != IB_WC_SUCCESS) { printk(KERN_ERR PFX "completion with error at %s:\n", cb->server ? "server" : "client"); printk(KERN_ERR PFX "failed status %d: wr_id %d\n", wc.status, (int) wc.wr_id); goto error; } if (wc.opcode != IB_WC_RDMA_READ) { printk(KERN_ERR PFX "unexpected opcode %d in rlat\n", wc.opcode); goto error; } cb->stats.read_bytes += cb->rdma_sq_wr.wr.sg_list->length; cb->stats.read_msgs++; cb->scnt++; } ktime_get_ts64(&stop_ts); delta_ts = timespec64_sub(stop_ts, start_ts); printk(KERN_INFO PFX "delta sec %lu delta usec %lu iter %d size %d\n", (unsigned long)delta_ts.tv_sec, (unsigned long)delta_ts.tv_nsec / 1000, cb->scnt, cb->size); krping_to_state(cb, DONE); cleanup: return (cb->stats.read_msgs == cb->count) ? 0 : -EIO; error: krping_to_state(cb, ERROR); goto cleanup; } /* * Write latency test body * CONN_HSHAKE_DONE -> DONE * CONN_HSHAKE_DONE -> ERROR */ static int wlat_test(struct krping_cb *cb) { int iters = cb->count; volatile char *poll_buf = (char *) cb->start_buf; char *buf = (char *)cb->rdma_buf; struct timespec64 start_ts, stop_ts, delta_ts; cycles_t *post_cycles_start = NULL; cycles_t *post_cycles_stop = NULL; cycles_t *poll_cycles_start = NULL; cycles_t *poll_cycles_stop = NULL; cycles_t *last_poll_cycles_start = NULL; cycles_t sum_poll = 0, sum_post = 0, sum_last_poll = 0; int i; int cycle_iters = 1000; size_t msize = cycle_iters * sizeof(cycles_t); DEBUG_LOG("%s starting wlat test\n", cb->server ? "server" : "client"); post_cycles_start = kmalloc(msize, GFP_KERNEL); if (!post_cycles_start) { printk(KERN_ERR PFX "%s kmalloc failed\n", __FUNCTION__); goto error; } post_cycles_stop = kmalloc(msize, GFP_KERNEL); if (!post_cycles_stop) { printk(KERN_ERR PFX "%s kmalloc failed\n", __FUNCTION__); goto error; } poll_cycles_start = kmalloc(msize, GFP_KERNEL); if (!poll_cycles_start) { printk(KERN_ERR PFX "%s kmalloc failed\n", __FUNCTION__); goto error; } poll_cycles_stop = kmalloc(msize, GFP_KERNEL); if (!poll_cycles_stop) { printk(KERN_ERR PFX "%s kmalloc failed\n", __FUNCTION__); goto error; } last_poll_cycles_start = kmalloc(msize, GFP_KERNEL); if (!last_poll_cycles_start) { printk(KERN_ERR PFX "%s kmalloc failed\n", __FUNCTION__); goto error; } cb->rdma_sq_wr.wr.opcode = IB_WR_RDMA_WRITE; cb->rdma_sq_wr.rkey = cb->remote_rkey; cb->rdma_sq_wr.remote_addr = cb->remote_addr; cb->rdma_sq_wr.wr.sg_list->length = cb->size; if (cycle_iters > iters) cycle_iters = iters; ktime_get_ts64(&start_ts); while (cb->scnt < iters || cb->ccnt < iters || cb->rcnt < iters) { /* Wait till buffer changes. */ if (cb->rcnt < iters && !(cb->scnt < 1 && cb->server)) { cb->rcnt++; while (*poll_buf != (char)cb->rcnt) { if (signal_pending(current)) { printk(KERN_ERR PFX "signal!\n"); goto error; } if (cb->state >= DISCONNECTED) { printk(KERN_ERR PFX "state %s, bailing\n", krping_state_str(cb->state)); goto cleanup; } } } if (cb->scnt < iters) { WRCONST struct ib_send_wr *bad_wr; *buf = (char)cb->scnt + 1; if (cb->scnt < cycle_iters) post_cycles_start[cb->scnt] = get_cycles(); if (ib_post_send(cb->qp, &cb->rdma_sq_wr.wr, &bad_wr)) { printk(KERN_ERR PFX "couldn't post send: scnt=%d\n", cb->scnt); goto error; } if (cb->scnt < cycle_iters) post_cycles_stop[cb->scnt] = get_cycles(); cb->scnt++; } if (cb->ccnt < iters) { struct ib_wc wc; int ne; if (cb->ccnt < cycle_iters) poll_cycles_start[cb->ccnt] = get_cycles(); do { if (cb->state >= DISCONNECTED) { printk(KERN_ERR PFX "state %s, scnt %d\n", krping_state_str(cb->state), cb->scnt); goto cleanup; } if (cb->ccnt < cycle_iters) last_poll_cycles_start[cb->ccnt] = get_cycles(); ne = ib_poll_cq(cb->cq, 1, &wc); } while (ne == 0); if (cb->ccnt < cycle_iters) poll_cycles_stop[cb->ccnt] = get_cycles(); cb->ccnt++; if (ne < 0) { printk(KERN_ERR PFX "poll cq failed %d\n", ne); goto error; } if (wc.status != IB_WC_SUCCESS) { printk(KERN_ERR PFX "completion with error at %s:\n", cb->server ? "server" : "client"); printk(KERN_ERR PFX "failed status %d: wr_id %d\n", wc.status, (int) wc.wr_id); printk(KERN_ERR PFX "scnt=%d, rcnt=%d, ccnt=%d\n", cb->scnt, cb->rcnt, cb->ccnt); goto error; } cb->stats.write_bytes += cb->rdma_sq_wr.wr.sg_list->length; cb->stats.write_msgs++; } } ktime_get_ts64(&stop_ts); delta_ts = timespec64_sub(stop_ts, start_ts); for (i = 0; i < cycle_iters; i++) { sum_post += post_cycles_stop[i] - post_cycles_start[i]; sum_poll += poll_cycles_stop[i] - poll_cycles_start[i]; sum_last_poll += poll_cycles_stop[i]-last_poll_cycles_start[i]; } printk(KERN_INFO PFX "delta sec %lu delta usec %lu iter %d size %d cycle_iters %d" " sum_post %llu sum_poll %llu sum_last_poll %llu\n", (unsigned long)delta_ts.tv_sec, (unsigned long)delta_ts.tv_nsec / 1000, cb->scnt, cb->size, cycle_iters, (unsigned long long)sum_post, (unsigned long long)sum_poll, (unsigned long long)sum_last_poll); krping_to_state(cb, DONE); cleanup: kfree(post_cycles_start); kfree(post_cycles_stop); kfree(poll_cycles_start); kfree(poll_cycles_stop); kfree(last_poll_cycles_start); return (cb->stats.write_msgs == cb->count) ? 0 : -EIO; error: krping_to_state(cb, ERROR); goto cleanup; } /* * Bandwidth test body * CONN_HSHAKE_DONE -> DONE * CONN_HSHAKE_DONE -> DONE_RCVD (if End msg arrives from client) * CONN_HSHAKE_DONE -> ERROR */ static int bw_test(struct krping_cb *cb) { struct ib_wc wc; int ne; int iters = cb->count; struct timespec64 start_ts, stop_ts, delta_ts; cycles_t *post_cycles_start = NULL; cycles_t *post_cycles_stop = NULL; cycles_t *poll_cycles_start = NULL; cycles_t *poll_cycles_stop = NULL; cycles_t *last_poll_cycles_start = NULL; cycles_t sum_poll = 0, sum_post = 0, sum_last_poll = 0; int i; int cycle_iters = 1000; size_t msize = cycle_iters * sizeof(cycles_t); DEBUG_LOG("%s starting bw test\n", cb->server ? "server" : "client"); post_cycles_start = kmalloc(msize, GFP_KERNEL); if (!post_cycles_start) { printk(KERN_ERR PFX "%s kmalloc failed\n", __FUNCTION__); goto error; } post_cycles_stop = kmalloc(msize, GFP_KERNEL); if (!post_cycles_stop) { printk(KERN_ERR PFX "%s kmalloc failed\n", __FUNCTION__); goto error; } poll_cycles_start = kmalloc(msize, GFP_KERNEL); if (!poll_cycles_start) { printk(KERN_ERR PFX "%s kmalloc failed\n", __FUNCTION__); goto error; } poll_cycles_stop = kmalloc(msize, GFP_KERNEL); if (!poll_cycles_stop) { printk(KERN_ERR PFX "%s kmalloc failed\n", __FUNCTION__); goto error; } last_poll_cycles_start = kmalloc(msize, GFP_KERNEL); if (!last_poll_cycles_start) { printk(KERN_ERR PFX "%s kmalloc failed\n", __FUNCTION__); goto error; } cb->rdma_sq_wr.wr.opcode = IB_WR_RDMA_WRITE; cb->rdma_sq_wr.rkey = cb->remote_rkey; cb->rdma_sq_wr.remote_addr = cb->remote_addr; cb->rdma_sq_wr.wr.sg_list->length = cb->size; if (cycle_iters > iters) cycle_iters = iters; ktime_get_ts64(&start_ts); while (cb->scnt < iters || cb->ccnt < iters) { while (cb->scnt < iters && cb->scnt - cb->ccnt < cb->txdepth) { WRCONST struct ib_send_wr *bad_wr; if (cb->scnt < cycle_iters) post_cycles_start[cb->scnt] = get_cycles(); if (ib_post_send(cb->qp, &cb->rdma_sq_wr.wr, &bad_wr)) { printk(KERN_ERR PFX "couldn't post send: scnt=%d\n", cb->scnt); goto error; } if (cb->scnt < cycle_iters) post_cycles_stop[cb->scnt] = get_cycles(); cb->scnt++; } if (cb->ccnt >= iters) continue; if (cb->ccnt < cycle_iters) poll_cycles_start[cb->ccnt] = get_cycles(); do { if (cb->state >= DISCONNECTED) { printk(KERN_ERR PFX "state %s, scnt %d\n", krping_state_str(cb->state), cb->scnt); goto cleanup; } if (cb->ccnt < cycle_iters) last_poll_cycles_start[cb->ccnt] = get_cycles(); ne = ib_poll_cq(cb->cq, 1, &wc); if (ne == 1 && wc.status == IB_WC_SUCCESS && wc.opcode == IB_WC_RECV) { /* Special handling for End handshake */ cb->stats.recv_bytes += sizeof(cb->recv_buf); cb->stats.recv_msgs++; if (!cb->server) { printk(KERN_ERR PFX "unexpected client end msg " "in state %s\n", krping_state_str(cb->state)); printk(KERN_ERR PFX "scnt=%d, ccnt=%d\n", cb->scnt, cb->ccnt); goto error; } krping_to_state(cb, DONE_RCVD); /* Keep polling */ ne = 0; } } while (ne == 0); if (ne < 0) { printk(KERN_ERR PFX "poll cq failed %d\n", ne); goto error; } if (wc.status != IB_WC_SUCCESS) { printk(KERN_ERR PFX "completion with error at %s:\n", cb->server ? "server" : "client"); printk(KERN_ERR PFX "failed status %d: wr_id %d\n", wc.status, (int) wc.wr_id); printk(KERN_ERR PFX "scnt=%d, ccnt=%d\n", cb->scnt, cb->ccnt); goto error; } cb->stats.write_bytes += cb->rdma_sq_wr.wr.sg_list->length; cb->stats.write_msgs++; if (cb->ccnt < cycle_iters) poll_cycles_stop[cb->ccnt] = get_cycles(); cb->ccnt++; } ktime_get_ts64(&stop_ts); delta_ts = timespec64_sub(stop_ts, start_ts); for (i = 0; i < cycle_iters; i++) { sum_post += post_cycles_stop[i] - post_cycles_start[i]; sum_poll += poll_cycles_stop[i] - poll_cycles_start[i]; sum_last_poll += poll_cycles_stop[i]-last_poll_cycles_start[i]; } printk(KERN_INFO PFX "delta sec %lu delta usec %lu iter %d size %d cycle_iters %d" " sum_post %llu sum_poll %llu sum_last_poll %llu\n", (unsigned long)delta_ts.tv_sec, (unsigned long)delta_ts.tv_nsec / 1000, cb->scnt, cb->size, cycle_iters, (unsigned long long)sum_post, (unsigned long long)sum_poll, (unsigned long long)sum_last_poll); spin_lock(&cb->state_lock); if (cb->state < DONE_RCVD) krping_to_state_locked(cb, DONE); spin_unlock(&cb->state_lock); cleanup: kfree(post_cycles_start); kfree(post_cycles_stop); kfree(poll_cycles_start); kfree(poll_cycles_stop); kfree(last_poll_cycles_start); return (cb->stats.write_msgs == cb->count) ? 0 : -EIO; error: krping_to_state(cb, ERROR); goto cleanup; } static int krping_rlat_test_server(struct krping_cb *cb) { int ret; /* Exchange STAG/TO/Len with client */ ret = krping_handshake_server(cb); if (ret) return ret; /* * In the rlat test, the server does nothing. It will sit and wait * for the client to disconnect. */ wait_event_interruptible(cb->sem, cb->state >= DISCONNECTED); return 0; } static int krping_wlat_test_server(struct krping_cb *cb) { int ret; /* Exchange STAG/TO/Len with client */ ret = krping_handshake_server(cb); if (ret) return ret; /* * In the wlat test, both sides do writes. After finishing the server * will wait for the client to disconnect. */ ret = wlat_test(cb); wait_event_interruptible(cb->sem, cb->state >= DISCONNECTED); return ret; } static int krping_bw_test_server(struct krping_cb *cb) { WRCONST struct ib_send_wr *bad_wr; int ret; /* Exchange STAG/TO/Len with client */ ret = krping_handshake_server(cb); if (ret) return ret; /* * In the bw test, the server only sends data if the duplex option is * selected. After one or both sides finish they will handshake before * the client disconnects. */ if (cb->duplex) { ret = bw_test(cb); } else { krping_to_state(cb, DONE); ret = 0; } /* Spin waiting for client's End */ ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP); wait_event_interruptible(cb->sem, cb->state >= DONE_RCVD); if (cb->state >= DISCONNECTED) return -ECONNRESET; /* Arm CQ event handler for the send completion */ ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP); /* Post the send */ ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr); if (ret) { printk(KERN_ERR PFX "post send error %d\n", ret); krping_to_state(cb, ERROR); return -ECONNRESET; } /* Wait for a completion */ wait_event_interruptible(cb->sem, cb->state >= DONE_DONE); return ret; } static int reg_supported(struct ib_device *dev) { u64 needed_flags = IB_DEVICE_MEM_MGT_EXTENSIONS; if ((dev->attrs.device_cap_flags & needed_flags) != needed_flags) { printk(KERN_ERR PFX "fastreg not supported - device_cap_flags 0x%llx\n", (unsigned long long)dev->attrs.device_cap_flags); return 0; } DEBUG_LOG("fastreg supported - device_cap_flags 0x%llx\n", (unsigned long long)dev->attrs.device_cap_flags); return 1; } static void fill_sockaddr(struct sockaddr_storage *sin, struct krping_cb *cb) { memset(sin, 0, sizeof(*sin)); if (cb->addr_type == AF_INET) { struct sockaddr_in *sin4 = (struct sockaddr_in *)sin; #ifdef HAVE_SINLEN sin4->sin_len = sizeof(*sin4); #endif sin4->sin_family = AF_INET; memcpy((void *)&sin4->sin_addr.s_addr, cb->addr, 4); sin4->sin_port = cb->port; } else if (cb->addr_type == AF_INET6) { struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin; #ifdef HAVE_SINLEN sin6->sin6_len = sizeof(*sin6); #endif sin6->sin6_family = AF_INET6; memcpy((void *)&sin6->sin6_addr, cb->addr, 16); sin6->sin6_port = cb->port; sin6->sin6_scope_id = cb->scope_id; } } static int krping_bind_server(struct krping_cb *cb) { struct sockaddr_storage sin; int ret; fill_sockaddr(&sin, cb); ret = rdma_bind_addr(cb->cm_id, (struct sockaddr *)&sin); if (ret) { printk(KERN_ERR PFX "rdma_bind_addr error %d\n", ret); krping_to_state(cb, ERROR); return ret; } DEBUG_LOG("rdma_bind_addr successful\n"); DEBUG_LOG("rdma_listen\n"); ret = rdma_listen(cb->cm_id, 3); if (ret) { printk(KERN_ERR PFX "rdma_listen failed: %d\n", ret); krping_to_state(cb, ERROR); return ret; } if (cb->wait) wait_event_interruptible_timeout(cb->sem, cb->state >= CONNECT_REQUEST, cb->wait * HZ); else wait_event_interruptible(cb->sem, cb->state >= CONNECT_REQUEST); if (cb->state != CONNECT_REQUEST) { printk(KERN_ERR PFX "wait for CONNECT_REQUEST ended in %s\n", krping_state_str(cb->state)); return -ENOTCONN; } if ((cb->ping || cb->frtest) && !reg_supported(cb->child_cm_id->device)) return -EINVAL; return 0; } static int krping_run_server(struct krping_cb *cb) { WRCONST struct ib_recv_wr *bad_wr; int ret; ret = krping_bind_server(cb); if (ret) goto err0; ret = krping_setup_qp(cb, cb->child_cm_id); if (ret) { printk(KERN_ERR PFX "setup_qp failed: %d\n", ret); krping_to_state(cb, ERROR); goto err0; } ret = krping_setup_buffers(cb); if (ret) { printk(KERN_ERR PFX "krping_setup_buffers failed: %d\n", ret); krping_to_state(cb, ERROR); goto err1; } ret = ib_post_recv(cb->qp, &cb->rq_wr, &bad_wr); if (ret) { printk(KERN_ERR PFX "ib_post_recv failed: %d\n", ret); krping_to_state(cb, ERROR); goto err2; } ret = krping_accept(cb); if (ret) { printk(KERN_ERR PFX "connect error %d\n", ret); goto err2; } if (cb->wlat) ret = krping_wlat_test_server(cb); else if (cb->rlat) ret = krping_rlat_test_server(cb); else if (cb->bw) ret = krping_bw_test_server(cb); else ret = krping_test_server(cb); DEBUG_LOG("server initiating disconnect\n"); rdma_disconnect(cb->child_cm_id); err2: krping_free_buffers(cb); err1: krping_free_qp(cb); err0: if (cb->child_cm_id) { DEBUG_LOG("destroying child_cm_id %p\n", cb->child_cm_id); rdma_destroy_id(cb->child_cm_id); } return ret; } static int krping_test_client(struct krping_cb *cb) { int ping, start, cc, i, ret; WRCONST struct ib_send_wr *bad_wr; unsigned char c; bool fence = true; /* Start CQ event handler */ ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP); start = 65; /* 'A' */ for (ping = 0; !cb->count || ping < cb->count; ping++) { krping_to_state(cb, RDMA_READ_ADV); /* Put some ascii text in the buffer. */ cc = sprintf(cb->start_buf, "rdma-ping-%d: ", ping); for (i = cc, c = start; i < cb->size; i++) { cb->start_buf[i] = c; c++; if (c > 122) c = 65; } start++; if (start > 122) start = 65; cb->start_buf[cb->size - 1] = 0; DEBUG_LOG("client starting, iter is %s\n", fence ? "fenced" : "not fenced"); if (fence) { cb->sq_wr.send_flags |= IB_SEND_FENCE; cb->reg_mr_wr.wr.send_flags |= IB_SEND_FENCE; } else { cb->sq_wr.send_flags &= ~IB_SEND_FENCE; cb->reg_mr_wr.wr.send_flags &= ~IB_SEND_FENCE; } fence = !fence; krping_format_send(cb, cb->start_dma_addr); if (cb->state == ERROR) { printk(KERN_ERR PFX "krping_format_send failed\n"); break; } ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr); if (ret) { printk(KERN_ERR PFX "post send error %d\n", ret); break; } /* Wait for server to ACK */ wait_event_interruptible(cb->sem, cb->state >= RDMA_WRITE_ADV); if (cb->state != RDMA_WRITE_ADV) { printk(KERN_ERR PFX "wait for RDMA_WRITE_ADV ended in %s\n", krping_state_str(cb->state)); break; } krping_format_send(cb, cb->rdma_dma_addr); ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr); if (ret) { printk(KERN_ERR PFX "post send error %d\n", ret); break; } /* Wait for the server to say the RDMA Write is complete. */ wait_event_interruptible(cb->sem, cb->state >= RDMA_WRITE_COMPLETE); if (cb->state != RDMA_WRITE_COMPLETE) { printk(KERN_ERR PFX "wait for RDMA_WRITE_COMPLETE ended in %s\n", krping_state_str(cb->state)); break; } if (cb->validate) if (memcmp(cb->start_buf, cb->rdma_buf, cb->size)) { printk(KERN_ERR PFX "data mismatch!\n"); break; } if (cb->verbose) printk(KERN_INFO PFX "ping data (64B max): |%.64s|\n", cb->rdma_buf); } return (ping == cb->count || (!cb->count && cb->state == DISCONNECTED)) ? 0 : -EIO; } static int krping_rlat_test_client(struct krping_cb *cb) { int ret; /* Exchange STAG/TO/Len with server */ ret = krping_handshake_client(cb); if (ret) return ret; /* * In the rlat test, the client reads data and then disconnects. */ return rlat_test(cb); } static int krping_wlat_test_client(struct krping_cb *cb) { int ret; /* Exchange STAG/TO/Len with server */ ret = krping_handshake_client(cb); if (ret) return ret; /* * In the wlat test, both sides do writes. After finishing the client * will disconnect. */ return wlat_test(cb); } static int krping_bw_test_client(struct krping_cb *cb) { WRCONST struct ib_send_wr *bad_wr; int ret; /* Exchange STAG/TO/Len with server */ ret = krping_handshake_client(cb); if (ret) return ret; /* * In the bw test, the client always sends data. After one or both * sides finish they will handshake before the client disconnects. */ ret = bw_test(cb); /* Arm CQ event handler for the send completion */ ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP); /* Post the send */ ret = ib_post_send(cb->qp, &cb->sq_wr, &bad_wr); if (ret) { printk(KERN_ERR PFX "post send error %d\n", ret); krping_to_state(cb, ERROR); return -ECONNRESET; } /* Wait for a completion (send or handshake from the peer) */ wait_event_interruptible(cb->sem, cb->state >= DONE_RCVD); if (cb->state < DONE_DONE) { /* Arm CQ event handler for the other completion */ ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP); wait_event_interruptible(cb->sem, cb->state >= DONE_DONE); } return ret; } /* * Manual qp flush test */ static void flush_qp(struct krping_cb *cb) { struct ib_send_wr wr = { 0 }; WRCONST struct ib_send_wr *bad; struct ib_recv_wr recv_wr = { 0 }; WRCONST struct ib_recv_wr *recv_bad; struct ib_wc wc; int ret; rdma_disconnect(cb->cm_id); DEBUG_LOG("disconnected!\n"); wr.opcode = IB_WR_SEND; wr.wr_id = 0xdeadbeefcafebabe; ret = ib_post_send(cb->qp, &wr, &bad); if (ret) { printk(KERN_ERR PFX "%s post_send failed ret %d\n", __func__, ret); return; } recv_wr.wr_id = 0xcafebabedeadbeef; ret = ib_post_recv(cb->qp, &recv_wr, &recv_bad); if (ret) { printk(KERN_ERR PFX "%s post_recv failed ret %d\n", __func__, ret); return; } /* poll until the flush WRs complete */ do { ret = ib_poll_cq(cb->cq, 1, &wc); if (ret < 0) { printk(KERN_ERR PFX "ib_poll_cq failed %d\n", ret); return; } if (ret == 0) continue; cb->ccnt++; if (wc.wr_id == 0xdeadbeefcafebabe || wc.wr_id == 0xcafebabedeadbeef) cb->fcnt++; } while (cb->fcnt != 2 && cb->state < DISCONNECTED); DEBUG_LOG("qp_flushed! ccnt %u\n", cb->ccnt); } /* * Fast registration test (client only) * CONNECTED -> DONE */ static int krping_fr_test(struct krping_cb *cb) { struct ib_send_wr inv; WRCONST struct ib_send_wr *bad; struct ib_reg_wr fr; struct ib_wc wc; u8 key = 0; struct ib_mr *mr; int ret; int size = cb->size; int plen = (((size - 1) & PAGE_MASK) + PAGE_SIZE) >> PAGE_SHIFT; unsigned long start; struct scatterlist sg = {0}; mr = ib_alloc_mr(cb->pd, IB_MR_TYPE_MEM_REG, plen); if (IS_ERR(mr)) { printk(KERN_ERR PFX "ib_alloc_mr failed %ld\n", PTR_ERR(mr)); return -ENOMEM; } sg_dma_address(&sg) = (dma_addr_t)0xcafebabe0000ULL; sg_dma_len(&sg) = size; ret = ib_map_mr_sg(mr, &sg, 1, NULL, PAGE_SIZE); if (ret <= 0) { printk(KERN_ERR PFX "ib_map_mr_sge err %d\n", ret); goto error; } memset(&fr, 0, sizeof fr); fr.wr.opcode = IB_WR_REG_MR; fr.access = IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE; fr.mr = mr; fr.wr.next = &inv; memset(&inv, 0, sizeof inv); inv.opcode = IB_WR_LOCAL_INV; inv.send_flags = IB_SEND_SIGNALED; DEBUG_LOG("fr_test: stag index 0x%x plen %u size %u depth %u\n", mr->rkey >> 8, plen, cb->size, cb->txdepth); start = get_seconds(); while (!cb->count || cb->frcnt < cb->count) { if (signal_pending(current)) { printk(KERN_ERR PFX "signal!\n"); break; } if ((get_seconds() - start) >= 9) { DEBUG_LOG("fr_test: pausing 1 second! " "count %u latest size %u plen %u\n", cb->frcnt, size, plen); wait_event_interruptible_timeout(cb->sem, cb->state >= DONE, HZ); if (cb->state >= DISCONNECTED) break; start = get_seconds(); } while (cb->scnt < (cb->txdepth >> 1)) { ib_update_fast_reg_key(mr, ++key); fr.key = mr->rkey; inv.ex.invalidate_rkey = mr->rkey; size = prandom_u32() % cb->size; if (size == 0) size = cb->size; sg_dma_len(&sg) = size; ret = ib_map_mr_sg(mr, &sg, 1, NULL, PAGE_SIZE); if (ret <= 0) { printk(KERN_ERR PFX "ib_map_mr_sge err %d\n", ret); goto error; } ret = ib_post_send(cb->qp, &fr.wr, &bad); if (ret) { printk(KERN_ERR PFX "ib_post_send failed %d\n", ret); goto error; } cb->scnt++; } ret = ib_poll_cq(cb->cq, 1, &wc); if (ret < 0) { printk(KERN_ERR PFX "ib_poll_cq failed %d\n", ret); goto error; } if (ret == 1) { if (wc.status) { printk(KERN_ERR PFX "completion error %u\n", wc.status); goto error; } cb->frcnt++; cb->scnt--; } } krping_to_state(cb, DONE); cleanup: flush_qp(cb); ib_dereg_mr(mr); if (cb->frcnt != cb->count) { printk(KERN_ERR PFX "completed %d/%d fr\n", cb->frcnt, cb->count); return -EIO; } return 0; error: krping_to_state(cb, ERROR); goto cleanup; } static int krping_connect_client(struct krping_cb *cb) { struct rdma_conn_param conn_param; int ret; memset(&conn_param, 0, sizeof conn_param); conn_param.responder_resources = 1; conn_param.initiator_depth = 1; conn_param.retry_count = 10; ret = rdma_connect(cb->cm_id, &conn_param); if (ret) { printk(KERN_ERR PFX "rdma_connect error %d\n", ret); krping_to_state(cb, ERROR); return ret; } wait_event_interruptible(cb->sem, cb->state >= CONNECTED); if (cb->state >= DISCONNECTED) { printk(KERN_ERR PFX "wait for CONNECTED ended in %s\n", krping_state_str(cb->state)); return -1; } DEBUG_LOG("rdma_connect successful\n"); return 0; } static int krping_bind_client(struct krping_cb *cb) { struct sockaddr_storage sin; int ret; fill_sockaddr(&sin, cb); ret = rdma_resolve_addr(cb->cm_id, NULL, (struct sockaddr *)&sin, 2000); if (ret) { printk(KERN_ERR PFX "rdma_resolve_addr error %d\n", ret); krping_to_state(cb, ERROR); return ret; } wait_event_interruptible(cb->sem, cb->state >= ROUTE_RESOLVED); if (cb->state != ROUTE_RESOLVED) { printk(KERN_ERR PFX "addr/route resolution ended in %s\n", krping_state_str(cb->state)); return -EINTR; } if ((cb->ping || cb->frtest) && !reg_supported(cb->cm_id->device)) return -EINVAL; DEBUG_LOG("rdma_resolve_addr - rdma_resolve_route successful\n"); return 0; } static int krping_run_client(struct krping_cb *cb) { WRCONST struct ib_recv_wr *bad_wr; int ret; /* set type of service, if any */ if (cb->tos != 0) rdma_set_service_type(cb->cm_id, cb->tos); ret = krping_bind_client(cb); if (ret) goto err0; ret = krping_setup_qp(cb, cb->cm_id); if (ret) { printk(KERN_ERR PFX "setup_qp failed: %d\n", ret); krping_to_state(cb, ERROR); goto err0; } ret = krping_setup_buffers(cb); if (ret) { printk(KERN_ERR PFX "krping_setup_buffers failed: %d\n", ret); krping_to_state(cb, ERROR); goto err1; } ret = ib_post_recv(cb->qp, &cb->rq_wr, &bad_wr); if (ret) { printk(KERN_ERR PFX "ib_post_recv failed: %d\n", ret); krping_to_state(cb, ERROR); goto err2; } ret = krping_connect_client(cb); if (ret) { printk(KERN_ERR PFX "connect error %d\n", ret); goto err2; } if (cb->wlat) ret = krping_wlat_test_client(cb); else if (cb->rlat) ret = krping_rlat_test_client(cb); else if (cb->bw) ret = krping_bw_test_client(cb); else if (cb->frtest) ret = krping_fr_test(cb); else ret = krping_test_client(cb); DEBUG_LOG("client initiating disconnect\n"); rdma_disconnect(cb->cm_id); err2: krping_free_buffers(cb); err1: krping_free_qp(cb); err0: return ret; } static uint16_t krping_get_ipv6_scope_id(char *name) { struct net_device *ndev; uint16_t retval; ndev = dev_get_by_name(&init_net, name); if (ndev == NULL) return (0); retval = ndev->ifindex; dev_put(ndev); return (retval); } int krping_doit(char *cmd) { struct krping_stats *stats; struct krping_cb *cb; int op; int ret = 0; char *optarg; char *scope; unsigned long optint; cb = kzalloc(sizeof(*cb), GFP_KERNEL); if (!cb) return -ENOMEM; cb->server = -1; cb->state = IDLE; cb->size = 64; cb->txdepth = RPING_SQ_DEPTH; cb->mem = REG; cb->ping = 1; cb->fragments = 1; spin_lock_init(&cb->state_lock); init_waitqueue_head(&cb->sem); while ((op = krping_getopt("krping", &cmd, krping_opts, NULL, &optarg, &optint)) != 0) { switch (op) { case 'a': cb->addr_str = optarg; cb->addr_type = AF_INET; if (in4_pton(optarg, -1, cb->addr, -1, NULL) != 1) { printk(KERN_ERR PFX "bad addr string %s\n", optarg); ret = EINVAL; } break; case 'A': cb->addr_str = optarg; cb->addr_type = AF_INET6; /* extract scope ID, if any */ scope = strstr(optarg, "%"); if (scope != NULL) { *scope++ = 0; cb->scope_id = krping_get_ipv6_scope_id(scope); } /* extract IPv6 network address */ if (in6_pton(optarg, -1, cb->addr, -1, NULL) != 1) { printk(KERN_ERR PFX "bad addr string %s\n", optarg); ret = EINVAL; } break; case 'p': cb->port = htons(optint); break; case 'P': cb->poll = 1; break; case 's': cb->server = 1; break; case 'c': cb->server = 0; break; case 'S': cb->size = optint; if ((cb->size < 1) || (cb->size > RPING_BUFSIZE)) { printk(KERN_ERR PFX "Invalid size %d " "(valid range is 1 to %d)\n", cb->size, RPING_BUFSIZE); ret = EINVAL; } break; case 'C': cb->count = optint; if (cb->count < 0) { printk(KERN_ERR PFX "Invalid count %d\n", cb->count); ret = EINVAL; } break; case 'v': cb->verbose++; break; case 'V': cb->validate++; break; case 'l': cb->wlat++; cb->ping = 0; break; case 'L': cb->rlat++; cb->ping = 0; break; case 'B': cb->bw++; cb->ping = 0; break; case 'd': cb->duplex++; break; case 'm': if (!strncmp(optarg, "dma", 3)) cb->mem = DMA; else cb->mem = REG; break; case 'I': cb->server_invalidate = 1; break; case 't': cb->tos = optint; break; case 'T': cb->txdepth = optint; break; case 'Z': cb->local_dma_lkey = 1; break; case 'R': cb->read_inv = 1; break; case 'f': cb->frtest = 1; cb->ping = 0; break; case 'w': cb->wait = optint; break; case 'F': cb->fragments = optint; if (cb->fragments < 1 || cb->fragments > MAX_SGE_CNT) { printk(KERN_ERR PFX "Invalid fragments %d\n", cb->fragments); ret = EINVAL; } break; default: printk(KERN_ERR PFX "unknown opt %s\n", optarg); ret = -EINVAL; break; } } if (ret) goto out; if (cb->server == -1) { printk(KERN_ERR PFX "must be either client or server\n"); ret = -EINVAL; goto out; } if (cb->server && cb->frtest) { printk(KERN_ERR PFX "must be client to run frtest\n"); ret = -EINVAL; goto out; } if ((cb->frtest + cb->bw + cb->rlat + cb->wlat) > 1) { printk(KERN_ERR PFX "pick only one test: fr, bw, rlat, wlat\n"); ret = -EINVAL; goto out; } if (cb->server_invalidate && cb->mem != REG) { printk(KERN_ERR PFX "server_invalidate only valid with mem_mode=reg\n"); ret = -EINVAL; goto out; } if (cb->read_inv && cb->mem != REG) { printk(KERN_ERR PFX "read_inv only valid with mem_mode=reg\n"); ret = -EINVAL; goto out; } if (cb->mem != DMA && (cb->wlat || cb->rlat || cb->bw)) { printk(KERN_ERR PFX "wlat, rlat, and bw tests only support mem_mode=dma\n"); ret = -EINVAL; goto out; } if (!cb->server && cb->wait) { printk(KERN_ERR PFX "wait only valid on server\n"); ret = -EINVAL; goto out; } cb->cm_id = rdma_create_id(&init_net, krping_cma_event_handler, cb, RDMA_PS_TCP, IB_QPT_RC); if (IS_ERR(cb->cm_id)) { ret = PTR_ERR(cb->cm_id); printk(KERN_ERR PFX "rdma_create_id error %d\n", ret); goto out; } mutex_lock(&krping_mutex); cb->index = krping_next_index++; list_add_tail(&cb->list, &krping_cbs); mutex_unlock(&krping_mutex); DEBUG_LOG("created index %u cm_id %p\n", cb->index, cb->cm_id); if (cb->server) ret = krping_run_server(cb); else ret = krping_run_client(cb); DEBUG_LOG("destroying index %u cm_id %p\n", cb->index, cb->cm_id); rdma_destroy_id(cb->cm_id); mutex_lock(&krping_mutex); list_del(&cb->list); mutex_unlock(&krping_mutex); stats = &cb->stats; printk(KERN_INFO PFX "stats_out %u %s %d " "Sn %llu %llu Rc %llu %llu W %llu %llu R %llu %llu\n", cb->index, stats->name, ret, stats->send_bytes, stats->send_msgs, stats->recv_bytes, stats->recv_msgs, stats->write_bytes, stats->write_msgs, stats->read_bytes, stats->read_msgs); out: spin_lock_destroy(&cb->state_lock); kfree(cb); return ret; } void krping_walk_cb_list(void (*f)(u32, struct krping_stats *, void *), void *arg) { struct krping_cb *cb; mutex_lock(&krping_mutex); list_for_each_entry(cb, &krping_cbs, list) (*f)(cb->index, cb->pd ? &cb->stats : NULL, arg); mutex_unlock(&krping_mutex); }