[openib-general] [PATCH] rdma_krping test tool

Steve Wise swise at aoot.com
Wed Mar 8 13:43:13 PST 2006


Sean,  

If you're interested, this is a port of user-mode rping.c as a kernel
module to implement the same rping protocol.  It loads as rdma_krping.ko
and uses the kernel mode CMA and VERBS to implement the rping
protocol.  

After the module loads, you use /proc/krping to start a rping thread in
kernel mode.  Like this: 

# echo "server,port=9999,addr=192.168.69.142,validate" > /proc/krping  

One the client side:

# echo "client,port=9999,addr=192.168.69.142,validate" > /proc/krping  

The test will run in the context of this write to the proc entry.  Thus
doing multiple commands to /proc/krping will start multiple threads.
The write blocks until the command completes or it is interrupted.
Also, reading from /proc/krping will give I/O statistics for each
running thread.

I've tested it over amso and mthca.  And I tested it against the
user-mode rping tool too.  So I ran krping<->krping and rping<->krping.

If you think this has utility, then please add it to
gen2/utils/src/linux-kernel/infiniband/util/krping.

Signed-off-by: Steve Wise <swise at opengridcomputing.com>

Index: krping/Kconfig
===================================================================
--- krping/Kconfig	(revision 0)
+++ krping/Kconfig	(revision 0)
@@ -0,0 +1,6 @@
+config KRPING
+	tristate "Kernel RDMA Ping Module"
+	depends on INFINIBAND 
+	---help---
+	  Test module implementing a RDMA ping client/server.
+
Index: krping/getopt.c
===================================================================
--- krping/getopt.c	(revision 0)
+++ krping/getopt.c	(revision 0)
@@ -0,0 +1,75 @@
+/*
+ * lifted from fs/ncpfs/getopt.c
+ */
+#include <linux/kernel.h>
+#include <linux/string.h>
+
+#include <asm/errno.h>
+
+#include "getopt.h"
+
+/**
+ *	krping_getopt - option parser
+ *	@caller: name of the caller, for error messages
+ *	@options: the options string
+ *	@opts: an array of &struct option entries controlling parser operations
+ *	@optopt: output; will contain the current option
+ *	@optarg: output; will contain the value (if one exists)
+ *	@flag: output; may be NULL; should point to a long for or'ing flags
+ *	@value: output; may be NULL; will be overwritten with the integer value
+ *		of the current argument.
+ *
+ *	Helper to parse options on the format used by mount ("a=b,c=d,e,f").
+ *	Returns opts->val if a matching entry in the 'opts' array is found,
+ *	0 when no more tokens are found, -1 if an error is encountered.
+ */
+int krping_getopt(const char *caller, char **options, 
+		  const struct krping_option *opts, char **optopt, 
+		  char **optarg, unsigned long *value)
+{
+	char *token;
+	char *val;
+
+	do {
+		if ((token = strsep(options, ",")) == NULL)
+			return 0;
+	} while (*token == '\0');
+	if (optopt)
+		*optopt = token;
+
+	if ((val = strchr (token, '=')) != NULL) {
+		*val++ = 0;
+	}
+	*optarg = val;
+	for (; opts->name; opts++) {
+		if (!strcmp(opts->name, token)) {
+			if (!val) {
+				if (opts->has_arg & OPT_NOPARAM) {
+					return opts->val;
+				}
+				printk(KERN_INFO "%s: the %s option requires "
+				       "an argument\n", caller, token);
+				return -EINVAL;
+			}
+			if (opts->has_arg & OPT_INT) {
+				char* v;
+
+				*value = simple_strtoul(val, &v, 0);
+				if (!*v) {
+					return opts->val;
+				}
+				printk(KERN_INFO "%s: invalid numeric value "
+				       "in %s=%s\n", caller, token, val);
+				return -EDOM;
+			}
+			if (opts->has_arg & OPT_STRING) {
+				return opts->val;
+			}
+			printk(KERN_INFO "%s: unexpected argument %s to the "
+			       "%s option\n", caller, val, token);
+			return -EINVAL;
+		}
+	}
+	printk(KERN_INFO "%s: Unrecognized mount option %s\n", caller, token);
+	return -EOPNOTSUPP;
+}
Index: krping/getopt.h
===================================================================
--- krping/getopt.h	(revision 0)
+++ krping/getopt.h	(revision 0)
@@ -0,0 +1,19 @@
+/*
+ * lifted from fs/ncpfs/getopt.c
+ */
+#ifndef _KRPING_GETOPT_H
+#define _KRPING_GETOPT_H
+
+#define OPT_NOPARAM	1
+#define OPT_INT		2
+#define OPT_STRING	4
+struct krping_option {
+	const char *name;
+	unsigned int has_arg;
+	int val;
+};
+
+extern int krping_getopt(const char *caller, char **options, const struct krping_option *opts,
+		      char **optopt, char **optarg, unsigned long *value);
+
+#endif /* _KRPING_GETOPT_H */
Index: krping/krping.c
===================================================================
--- krping/krping.c	(revision 0)
+++ krping/krping.c	(revision 0)
@@ -0,0 +1,1129 @@
+/*
+ * Copyright (c) 2005 Ammasso, Inc. All rights reserved.
+ * Copyright (c) 2006 Open Grid Computing, 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 <linux/version.h>
+#include <linux/module.h>
+#include <linux/moduleparam.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/err.h>
+#include <linux/string.h>
+#include <linux/parser.h>
+#include <linux/proc_fs.h>
+#include <linux/inet.h>
+#include <linux/list.h>
+#include <linux/in.h>
+#include <linux/device.h>
+#include <linux/pci.h>
+
+#include <asm/atomic.h>
+#include <asm/pci.h>
+
+#include <rdma/ib_verbs.h>
+#include <rdma/rdma_cm.h>
+
+#include "getopt.h"
+
+#define PFX "krping: "
+
+static int debug = 0;
+module_param(debug, int, 0);
+MODULE_PARM_DESC(debug, "Debug level (0=none, 1=all)");
+#define DEBUG_LOG if (debug) printk
+
+MODULE_AUTHOR("Steve Wise");
+MODULE_DESCRIPTION("RDMA ping server");
+MODULE_LICENSE("Dual BSD/GPL");
+
+static const struct krping_option krping_opts[] = {
+	{"count", OPT_INT, 'C'},
+	{"size", OPT_INT, 'S'},
+	{"addr", OPT_STRING, 'a'},
+	{"port", OPT_INT, 'p'},
+	{"verbose", OPT_NOPARAM, 'v'},
+	{"validate", OPT_NOPARAM, 'V'},
+	{"server", OPT_NOPARAM, 's'},
+	{"client", OPT_NOPARAM, 'c'},
+	{NULL, 0, 0}
+};
+
+struct krping_stats {
+	u64 send_bytes;
+	u64 send_msgs;
+	u64 recv_bytes;
+	u64 recv_msgs;
+	u64 write_bytes;
+	u64 write_msgs;
+	u64 read_bytes;
+	u64 read_msgs;
+};
+
+static DECLARE_MUTEX(krping_mutex);
+
+/*
+ * List of running krping threads.
+ */
+static LIST_HEAD(krping_cbs);
+
+static struct proc_dir_entry *krping_proc;
+
+/*
+ * 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
+ * 	<repeat loop>
+ */
+
+/*
+ * 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,
+	RDMA_READ_ADV,
+	RDMA_READ_COMPLETE,
+	RDMA_WRITE_ADV,
+	RDMA_WRITE_COMPLETE,
+	ERROR
+};
+
+struct krping_rdma_info {
+	uint64_t buf;
+	uint32_t rkey;
+	uint32_t size;
+};
+
+/*
+ * Default max buffer size for IO...
+ */
+#define RPING_BUFSIZE 64*1024
+#define RPING_SQ_DEPTH 16
+
+/*
+ * Control block struct.
+ */
+struct krping_cb {
+	int server;			/* 0 iff client */
+	struct ib_cq *cq;
+	struct ib_pd *pd;
+	struct ib_qp *qp;
+	struct ib_mr *dma_mr;
+
+	struct ib_recv_wr rq_wr;	/* recv work request record */
+	struct ib_sge recv_sgl;		/* recv single SGE */
+	struct krping_rdma_info recv_buf;/* malloc'd buffer */
+	DECLARE_PCI_UNMAP_ADDR(recv_mapping)
+
+	struct ib_send_wr sq_wr;	/* send work requrest record */
+	struct ib_sge send_sgl;
+	struct krping_rdma_info send_buf;/* single send buf */
+	DECLARE_PCI_UNMAP_ADDR(send_mapping)
+
+	struct ib_send_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_addr;
+	DECLARE_PCI_UNMAP_ADDR(rdma_mapping)
+
+	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_addr;
+	DECLARE_PCI_UNMAP_ADDR(start_mapping)
+
+	enum test_state state;		/* used for cond/signalling */
+	wait_queue_head_t sem;
+	struct krping_stats stats;
+
+	uint16_t port;			/* dst port in NBO */
+	uint32_t addr;			/* dst addr in NBO */
+	char *addr_str;			/* dst addr string */
+	int verbose;			/* verbose logging */
+	int count;			/* ping count */
+	int size;			/* ping data size */
+	int validate;			/* validate ping data */
+
+	/* CM stuff */
+	struct rdma_cm_id *cm_id;	/* connection on client side,*/
+					/* listener on service side. */
+	struct rdma_cm_id *child_cm_id;	/* connection on server side */
+	struct list_head list;	
+};
+
+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:
+		cb->state = 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:
+		cb->state = ROUTE_RESOLVED;
+		wake_up_interruptible(&cb->sem);
+		break;
+
+	case RDMA_CM_EVENT_CONNECT_REQUEST:
+		cb->state = CONNECT_REQUEST;
+		cb->child_cm_id = cma_id;
+		DEBUG_LOG("child cma %p\n", cb->child_cm_id);
+		wake_up_interruptible(&cb->sem);
+		break;
+
+	case RDMA_CM_EVENT_ESTABLISHED:
+		DEBUG_LOG("ESTABLISHED\n");
+		if (!cb->server) {
+			cb->state = 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);
+		wake_up_interruptible(&cb->sem);
+		break;
+
+	case RDMA_CM_EVENT_DISCONNECTED:
+		printk(KERN_ERR PFX "DISCONNECT EVENT...\n");
+		cb->state = ERROR;
+		wake_up_interruptible(&cb->sem);
+		break;
+
+	case RDMA_CM_EVENT_DEVICE_REMOVAL:
+		printk(KERN_ERR PFX "cma detected device removal!!!!\n");
+		break;
+
+	default:
+		printk(KERN_ERR PFX "oof bad type!\n");
+		wake_up_interruptible(&cb->sem);
+		break;
+	}
+	return 0;
+}
+
+static int server_recv(struct krping_cb *cb, struct ib_wc *wc)
+{
+	if (wc->byte_len != sizeof(cb->recv_buf)) {
+		printk(KERN_ERR PFX "Received bogus data, size %d\n", 
+		       wc->byte_len);
+		return -1;
+	}
+
+	cb->remote_rkey = cb->recv_buf.rkey;
+	cb->remote_addr = cb->recv_buf.buf;
+	cb->remote_len  = cb->recv_buf.size;
+	DEBUG_LOG("Received rkey %x addr %llx len %d from peer\n",
+		  cb->remote_rkey, cb->remote_addr, cb->remote_len);
+
+	if (cb->state <= CONNECTED || cb->state == RDMA_WRITE_COMPLETE)
+		cb->state = RDMA_READ_ADV;
+	else
+		cb->state = RDMA_WRITE_ADV;
+
+	return 0;
+}
+
+static int client_recv(struct krping_cb *cb, struct ib_wc *wc)
+{
+	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->state == RDMA_READ_ADV)
+		cb->state = RDMA_WRITE_ADV;
+	else
+		cb->state = RDMA_WRITE_COMPLETE;
+
+	return 0;
+}
+
+static void krping_cq_event_handler(struct ib_cq *cq, void *ctx)
+{
+	struct krping_cb *cb = ctx;
+	struct ib_wc wc;
+	struct ib_recv_wr *bad_wr;
+	int ret;
+
+	BUG_ON(cb->cq != cq);
+	if (cb->state == ERROR) {
+		printk(KERN_ERR PFX "cq completion in ERROR state\n");
+		return;
+	}
+	ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP);
+	while ((ret = ib_poll_cq(cb->cq, 1, &wc)) == 1) {
+		if (wc.status) {
+			printk(KERN_ERR PFX "cq completion failed status %d\n",
+				wc.status);
+			goto error;
+		}
+
+		switch (wc.opcode) {
+		case IB_WC_SEND:
+			DEBUG_LOG("send completion\n");
+			cb->stats.send_bytes += cb->send_sgl.length;
+			cb->stats.send_msgs++;
+			break;
+
+		case IB_WC_RDMA_WRITE:
+			DEBUG_LOG("rdma write completion\n");
+			cb->stats.write_bytes += cb->rdma_sq_wr.sg_list->length;
+			cb->stats.write_msgs++;
+			cb->state = RDMA_WRITE_COMPLETE;
+			wake_up_interruptible(&cb->sem);
+			break;
+
+		case IB_WC_RDMA_READ:
+			DEBUG_LOG("rdma read completion\n");
+			cb->stats.read_bytes += cb->rdma_sq_wr.sg_list->length;
+			cb->stats.read_msgs++;
+			cb->state = RDMA_READ_COMPLETE;
+			wake_up_interruptible(&cb->sem);
+			break;
+
+		case IB_WC_RECV:
+			DEBUG_LOG("recv completion\n");
+			cb->stats.recv_bytes += sizeof(cb->recv_buf);
+			cb->stats.recv_msgs++;
+			ret = cb->server ? server_recv(cb, &wc) :
+					   client_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;
+			}
+			wake_up_interruptible(&cb->sem);
+			break;
+
+		default:
+			DEBUG_LOG("unknown!!!!! completion\n");
+			goto error;
+		}
+	}
+	if (ret) {
+		printk(KERN_ERR PFX "poll error %d\n", ret);
+		goto error;
+	}
+	return;
+error:
+	cb->state = ERROR;
+	wake_up_interruptible(&cb->sem);
+}
+
+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);
+		return ret;
+	}
+
+	wait_event_interruptible(cb->sem, cb->state >= CONNECTED);
+	if (cb->state == ERROR) {
+		printk(KERN_ERR PFX "wait for CONNECTED state %d\n", cb->state);
+		return -1;
+	}
+	return 0;
+}
+
+static void krping_setup_wr(struct krping_cb *cb)
+{
+	cb->recv_sgl.addr = dma_map_single(cb->pd->device->dma_device, 
+					   &cb->recv_buf, 
+					   sizeof(cb->recv_buf), DMA_BIDIRECTIONAL);
+	pci_unmap_addr_set(cb, recv_mapping, cb->recv_sgl.addr);
+	cb->recv_sgl.length = sizeof cb->recv_buf;
+	cb->recv_sgl.lkey = cb->dma_mr->lkey;
+	cb->rq_wr.sg_list = &cb->recv_sgl;
+	cb->rq_wr.num_sge = 1;
+
+	cb->send_sgl.addr = dma_map_single(cb->pd->device->dma_device, 
+					   &cb->send_buf, sizeof(cb->recv_buf),
+					   DMA_BIDIRECTIONAL);
+	pci_unmap_addr_set(cb, send_mapping, cb->send_sgl.addr);
+	cb->send_sgl.length = sizeof cb->send_buf;
+	cb->send_sgl.lkey = cb->dma_mr->lkey;
+
+	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 = 1;
+
+	cb->rdma_addr = dma_map_single(cb->pd->device->dma_device, 
+				       cb->rdma_buf, cb->size, 
+				       DMA_BIDIRECTIONAL);
+	pci_unmap_addr_set(cb, rdma_mapping, cb->rdma_addr);
+	cb->rdma_sgl.addr = cb->rdma_addr;
+	cb->rdma_sgl.lkey = cb->dma_mr->lkey;
+	cb->rdma_sq_wr.send_flags = IB_SEND_SIGNALED;
+	cb->rdma_sq_wr.sg_list = &cb->rdma_sgl;
+	cb->rdma_sq_wr.num_sge = 1;
+
+	if (!cb->server) {
+		cb->start_addr = dma_map_single(cb->pd->device->dma_device, 
+					   cb->start_buf, cb->size, 
+					   DMA_BIDIRECTIONAL);
+		pci_unmap_addr_set(cb, start_mapping, cb->start_addr);
+	}
+}
+
+static int krping_setup_buffers(struct krping_cb *cb)
+{
+	int ret;
+
+	DEBUG_LOG("krping_setup_buffers called on cb %p\n", cb);
+
+	cb->dma_mr = ib_get_dma_mr(cb->pd, IB_ACCESS_LOCAL_WRITE|
+					   IB_ACCESS_REMOTE_READ|
+				           IB_ACCESS_REMOTE_WRITE);
+	if (IS_ERR(cb->dma_mr)) {
+		printk(KERN_ERR PFX "recv_buf reg_mr failed\n");
+		return PTR_ERR(cb->dma_mr);
+	}
+
+	cb->rdma_buf = kmalloc(cb->size, GFP_KERNEL);
+	if (!cb->rdma_buf) {
+		printk(KERN_ERR PFX "rdma_buf malloc failed\n");
+		ret = -ENOMEM;
+		goto err1;
+	}
+
+	if (!cb->server) {
+		cb->start_buf = kmalloc(cb->size, GFP_KERNEL);
+		if (!cb->start_buf) {
+			printk(KERN_ERR PFX "start_buf malloc failed\n");
+			ret = -ENOMEM;
+			goto err2;
+		}
+	}
+
+	krping_setup_wr(cb);
+	DEBUG_LOG("allocated & registered buffers...\n");
+	return 0;
+err2:
+	kfree(cb->rdma_buf);
+err1:
+	ib_dereg_mr(cb->dma_mr);
+	return ret;
+}
+
+static void krping_free_buffers(struct krping_cb *cb)
+{
+	DEBUG_LOG("krping_free_buffers called on cb %p\n", cb);
+	
+	dma_unmap_single(cb->pd->device->dma_device,
+			 pci_unmap_addr(cb, recv_mapping),
+			 sizeof(cb->recv_buf), DMA_BIDIRECTIONAL);
+	dma_unmap_single(cb->pd->device->dma_device,
+			 pci_unmap_addr(cb, send_mapping),
+			 sizeof(cb->send_buf), DMA_BIDIRECTIONAL);
+	dma_unmap_single(cb->pd->device->dma_device,
+			 pci_unmap_addr(cb, rdma_mapping),
+			 cb->size, DMA_BIDIRECTIONAL);
+	kfree(cb->rdma_buf);
+	if (!cb->server) {
+		dma_unmap_single(cb->pd->device->dma_device,
+			 pci_unmap_addr(cb, start_mapping),
+			 cb->size, DMA_BIDIRECTIONAL);
+		kfree(cb->start_buf);
+	}
+	ib_dereg_mr(cb->dma_mr);
+}
+
+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 = RPING_SQ_DEPTH;
+	init_attr.cap.max_recv_wr = 2;
+	init_attr.cap.max_recv_sge = 1;
+	init_attr.cap.max_send_sge = 1;
+	init_attr.qp_type = IB_QPT_RC;
+	init_attr.send_cq = cb->cq;
+	init_attr.recv_cq = cb->cq;
+
+	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)
+{
+	ib_destroy_qp(cb->qp);
+	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;
+
+	cb->pd = ib_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);
+
+	cb->cq = ib_create_cq(cm_id->device, krping_cq_event_handler, NULL,
+			      cb, RPING_SQ_DEPTH * 2);
+	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 = ib_req_notify_cq(cb->cq, IB_CQ_NEXT_COMP);
+	if (ret) {
+		printk(KERN_ERR PFX "ib_create_cq failed\n");
+		goto err2;
+	}
+
+	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;
+}
+
+static void krping_format_send(struct krping_cb *cb, u64 buf, 
+			       struct ib_mr *mr)
+{
+	struct krping_rdma_info *info = &cb->send_buf;
+
+	info->buf = buf;
+	info->rkey = mr->rkey;
+	info->size = cb->size;
+
+	DEBUG_LOG("RDMA addr %llx rkey %x len %d\n",
+		  info->buf, info->rkey, info->size);
+}
+
+static void krping_test_server(struct krping_cb *cb)
+{
+	struct ib_send_wr *bad_wr;
+	int ret;
+
+	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) {
+			printk(KERN_ERR PFX "wait for RDMA_READ_ADV state %d\n",
+				cb->state);
+			break;
+		}
+
+		DEBUG_LOG("server received sink adv\n");
+
+		/* Issue RDMA Read. */
+		cb->rdma_sq_wr.opcode = IB_WR_RDMA_READ;
+		cb->rdma_sq_wr.wr.rdma.rkey = cb->remote_rkey;
+		cb->rdma_sq_wr.wr.rdma.remote_addr = cb->remote_addr;
+		cb->rdma_sq_wr.sg_list->length = cb->remote_len;
+
+		ret = ib_post_send(cb->qp, &cb->rdma_sq_wr, &bad_wr);
+		if (ret) {
+			printk(KERN_ERR PFX "post send error %d\n", ret);
+			break;
+		}
+		DEBUG_LOG("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 state %d\n",
+			       cb->state);
+			break;
+		}
+		DEBUG_LOG("server received read complete\n");
+
+		/* Display data in recv buf */
+		if (cb->verbose)
+			printk("server ping data: %s\n", cb->rdma_buf);
+
+		/* Tell client to continue */
+		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_LOG("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 state %d\n",
+			       cb->state);
+			break;
+		}
+		DEBUG_LOG("server received sink adv\n");
+
+		/* RDMA Write echo data */
+		cb->rdma_sq_wr.opcode = IB_WR_RDMA_WRITE;
+		cb->rdma_sq_wr.wr.rdma.rkey = cb->remote_rkey;
+		cb->rdma_sq_wr.wr.rdma.remote_addr = cb->remote_addr;
+		cb->rdma_sq_wr.sg_list->length = strlen(cb->rdma_buf) + 1;
+		DEBUG_LOG("rdma write from lkey %x laddr %llx len %d\n",
+			  cb->rdma_sq_wr.sg_list->lkey,
+			  cb->rdma_sq_wr.sg_list->addr,
+			  cb->rdma_sq_wr.sg_list->length);
+
+		ret = ib_post_send(cb->qp, &cb->rdma_sq_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 state %d\n",
+			       cb->state);
+			break;
+		}
+		DEBUG_LOG("server rdma write complete \n");
+
+		cb->state = CONNECTED;
+
+		/* Tell client to begin again */
+		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_LOG("server posted go ahead\n");
+	}
+}
+
+static int krping_bind_server(struct krping_cb *cb)
+{
+	struct sockaddr_in sin;
+	int ret;
+
+	memset(&sin, 0, sizeof(sin));
+	sin.sin_family = AF_INET;
+	sin.sin_addr.s_addr = cb->addr;
+	sin.sin_port = cb->port;
+
+	ret = rdma_bind_addr(cb->cm_id, (struct sockaddr *) &sin);
+	if (ret) {
+		printk(KERN_ERR PFX "rdma_bind_addr error %d\n", ret);
+		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);
+		return ret;
+	}
+
+	wait_event_interruptible(cb->sem, cb->state >= CONNECT_REQUEST);
+	if (cb->state != CONNECT_REQUEST) {
+		printk(KERN_ERR PFX "wait for CONNECT_REQUEST state %d\n",
+			cb->state);
+		return -1;
+	}
+
+	return 0;
+}
+
+static void krping_run_server(struct krping_cb *cb)
+{
+	struct ib_recv_wr *bad_wr;
+	int ret;
+
+	ret = krping_bind_server(cb);
+	if (ret)
+		return;
+
+	ret = krping_setup_qp(cb, cb->child_cm_id);
+	if (ret) {
+		printk(KERN_ERR PFX "setup_qp failed: %d\n", ret);
+		return;
+	}
+
+	ret = krping_setup_buffers(cb);
+	if (ret) {
+		printk(KERN_ERR PFX "krping_setup_buffers failed: %d\n", ret);
+		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);
+		goto err2;
+	}
+
+	ret = krping_accept(cb);
+	if (ret) {
+		printk(KERN_ERR PFX "connect error %d\n", ret);
+		goto err2;
+	}
+
+	krping_test_server(cb);
+	rdma_disconnect(cb->child_cm_id);
+	rdma_destroy_id(cb->child_cm_id);
+err2:
+	krping_free_buffers(cb);
+err1:
+	krping_free_qp(cb);
+}
+
+static void krping_test_client(struct krping_cb *cb)
+{
+	int ping, start, cc, i, ret;
+	struct ib_send_wr *bad_wr;
+	unsigned char c;
+
+	start = 65;
+	for (ping = 0; !cb->count || ping < cb->count; ping++) {
+		cb->state = 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;
+
+		krping_format_send(cb, cb->start_addr, cb->dma_mr);
+		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 state %d\n",
+			       cb->state);
+			break;
+		}
+
+		krping_format_send(cb, cb->rdma_addr, cb->dma_mr);
+		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 state %d\n",
+			       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("ping data: %s\n", cb->rdma_buf);
+	}
+}
+
+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);
+		return ret;
+	}
+
+	wait_event_interruptible(cb->sem, cb->state >= CONNECTED);
+	if (cb->state == ERROR) {
+		printk(KERN_ERR PFX "wait for CONNECTED state %d\n", cb->state);
+		return -1;
+	}
+
+	DEBUG_LOG("rmda_connect successful\n");
+	return 0;
+}
+
+static int krping_bind_client(struct krping_cb *cb)
+{
+	struct sockaddr_in sin;
+	int ret;
+
+	memset(&sin, 0, sizeof(sin));
+	sin.sin_family = AF_INET;
+	sin.sin_addr.s_addr = cb->addr;
+	sin.sin_port = cb->port;
+
+	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);
+		return ret;
+	}
+
+	wait_event_interruptible(cb->sem, cb->state >= ROUTE_RESOLVED);
+	if (cb->state != ROUTE_RESOLVED) {
+		printk(KERN_ERR PFX 
+		       "waiting for addr/route resolution state %d\n",
+		       cb->state);
+		return ret;
+	}
+
+	DEBUG_LOG("rdma_resolve_addr - rdma_resolve_route successful\n");
+	return 0;
+}
+
+static void krping_run_client(struct krping_cb *cb)
+{
+	struct ib_recv_wr *bad_wr;
+	int ret;
+
+	ret = krping_bind_client(cb);
+	if (ret)
+		return;
+
+	ret = krping_setup_qp(cb, cb->cm_id);
+	if (ret) {
+		printk(KERN_ERR PFX "setup_qp failed: %d\n", ret);
+		return;
+	}
+
+	ret = krping_setup_buffers(cb);
+	if (ret) {
+		printk(KERN_ERR PFX "krping_setup_buffers failed: %d\n", ret);
+		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);
+		goto err2;
+	}
+
+	ret = krping_connect_client(cb);
+	if (ret) {
+		printk(KERN_ERR PFX "connect error %d\n", ret);
+		goto err2;
+	}
+
+	krping_test_client(cb);
+	rdma_disconnect(cb->cm_id);
+err2:
+	krping_free_buffers(cb);
+err1:
+	krping_free_qp(cb);
+}
+
+int krping_doit(char *cmd)
+{
+	struct krping_cb *cb;
+	int op;
+	int ret = 0;
+	char *optarg;
+	unsigned long optint;
+
+	cb = kzalloc(sizeof(*cb), GFP_KERNEL);
+	if (!cb)
+		return -ENOMEM;
+
+	down(&krping_mutex);
+	list_add_tail(&cb->list, &krping_cbs);
+	up(&krping_mutex);
+
+	cb->server = -1;
+	cb->state = IDLE;
+	cb->size = 64;
+	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 = in_aton(optarg);
+			DEBUG_LOG("ipaddr (%s)\n", optarg);
+			break;
+		case 'p':
+			cb->port = htons(optint);
+			DEBUG_LOG("port %d\n", (int)optint);
+			break;
+		case 's':
+			cb->server = 1;
+			DEBUG_LOG("server\n");
+			break;
+		case 'c':
+			cb->server = 0;
+			DEBUG_LOG("client\n");
+			break;
+		case 'S':
+			cb->size = optint;
+			if ((cb->size < 1) ||
+			    (cb->size > (RPING_BUFSIZE - 1))) {
+				printk(KERN_ERR PFX "Invalid size %d "
+				       "(valid range is 1 to %d)\n",
+				       cb->size, RPING_BUFSIZE);
+				ret = EINVAL;
+			} else
+				DEBUG_LOG("size %d\n", (int)optint);
+			break;
+		case 'C':
+			cb->count = optint;
+			if (cb->count < 0) {
+				printk(KERN_ERR PFX "Invalid count %d\n",
+					cb->count);
+				ret = EINVAL;
+			} else
+				DEBUG_LOG("count %d\n", (int) cb->count);
+			break;
+		case 'v':
+			cb->verbose++;
+			DEBUG_LOG("verbose\n");
+			break;
+		case 'V':
+			cb->validate++;
+			DEBUG_LOG("validate data\n");
+			break;
+		case 'd':
+			debug++;
+			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;
+	}
+
+	cb->cm_id = rdma_create_id(krping_cma_event_handler, cb, RDMA_PS_TCP);
+	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;
+	}
+	DEBUG_LOG("created cm_id %p\n", cb->cm_id);
+
+	if (cb->server)
+		krping_run_server(cb);
+	else
+		krping_run_client(cb);
+
+	DEBUG_LOG("destroy cm_id %p\n", cb->cm_id);
+	rdma_destroy_id(cb->cm_id);
+out:
+	down(&krping_mutex);
+	list_del(&cb->list);
+	up(&krping_mutex);
+	kfree(cb);
+	return ret;
+}
+
+/*
+ * Read proc returns stats for each device.
+ */
+static int krping_read_proc(char *page, char **start, off_t off, int count,
+			    int *eof, void *data)
+{
+	struct krping_cb *cb;
+	int cc = 0;
+	char *cp = page;
+	int num = 1;
+
+	DEBUG_LOG(KERN_INFO PFX "proc read called...\n");
+	*cp = 0;
+	down(&krping_mutex);
+	list_for_each_entry(cb, &krping_cbs, list) {
+		cc = sprintf(cp,
+			     "%d-%s %lld %lld %lld %lld %lld %lld %lld %lld\n",
+			     num++, cb->pd->device->name, cb->stats.send_bytes,
+			     cb->stats.send_msgs, cb->stats.recv_bytes,
+			     cb->stats.recv_msgs, cb->stats.write_bytes,
+			     cb->stats.write_msgs,
+			     cb->stats.read_bytes,
+			     cb->stats.read_msgs);
+		cp += cc;
+	}
+	up(&krping_mutex);
+	*eof = 1;
+	return strlen(page);
+}
+
+/*
+ * Write proc is used to start a ping client or server.
+ */
+static int krping_write_proc(struct file *file, const char *buffer,
+			     unsigned long count, void *data)
+{
+	char *cmd;
+	int rc;
+
+	cmd = kmalloc(count, GFP_KERNEL);
+	if (cmd == NULL) {
+		printk(KERN_ERR PFX "kmalloc failure\n");
+		return -ENOMEM;
+	}
+	if (copy_from_user(cmd, buffer, count)) {
+		return -EFAULT;
+	}
+
+	/*
+	 * remove the \n.
+	 */
+	cmd[count - 1] = 0;
+	DEBUG_LOG(KERN_INFO PFX "proc write |%s|\n", cmd);
+	rc = krping_doit(cmd);
+	kfree(cmd);
+	if (rc)
+		return rc;
+	else
+		return (int) count;
+}
+
+static int __init krping_init(void)
+{
+	DEBUG_LOG("krping_init\n");
+	krping_proc = create_proc_entry("krping", 0666, NULL);
+	if (krping_proc == NULL) {
+		printk(KERN_ERR PFX "cannot create /proc/krping\n");
+		return -ENOMEM;
+	}
+	krping_proc->owner = THIS_MODULE;
+	krping_proc->read_proc = krping_read_proc;
+	krping_proc->write_proc = krping_write_proc;
+	return 0;
+}
+
+static void __exit krping_exit(void)
+{
+	DEBUG_LOG("krping_exit\n");
+	remove_proc_entry("krping", NULL);
+}
+
+module_init(krping_init);
+module_exit(krping_exit);
Index: krping/Makefile
===================================================================
--- krping/Makefile	(revision 0)
+++ krping/Makefile	(revision 0)
@@ -0,0 +1,4 @@
+EXTRA_CFLAGS += -Idrivers/infiniband/include 
+
+obj-$(CONFIG_KRPING)		+= rdma_krping.o
+rdma_krping-y			:= getopt.o krping.o




More information about the general mailing list