git: 80bee4d83637 - main - nfsclrdma: Add code to support client side NFS over RDMA

From: Rick Macklem <rmacklem_at_FreeBSD.org>
Date: Thu, 26 Nov 2026 14:06:27 UTC
The branch main has been updated by rmacklem:

URL: https://cgit.FreeBSD.org/src/commit/?id=80bee4d836376e71e429baed8553cb6630385170

commit 80bee4d836376e71e429baed8553cb6630385170
Author:     Rick Macklem <rmacklem@FreeBSD.org>
AuthorDate: 2026-11-26 14:02:57 +0000
Commit:     Rick Macklem <rmacklem@FreeBSD.org>
CommitDate: 2026-11-26 14:02:57 +0000

    nfsclrdma: Add code to support client side NFS over RDMA
    
    These functions started life as sys/rpc/clnt_vc.c and
    a couple of files in the Linux kernel that are dual
    licensed (the same as the OFED stack).  They have now
    been heavily modified to work with the FreeBSD NFS client
    and krpc.  NFS over RDMA is based on RFC-8166 and RFC-8267.
    I have not added my name to the copyrights.  Should I do so?
    
    One outstanding issue is that I see "mlx5_1: WARN: dump_cge.."
    failures for the ConnectX4 NICs I have available for testing,
    if I allow more than 8 concurrent RPCs or an I/O size greater
    than 64Kbytes.  Testing by others have not seen this issue
    with newer Mellanox NICs, so the current code sets a limit of
    8 concurrent RPCs and a 64Kbyte I/O size for ConnectX4 to
    avoid the failures.
    
    The build glue and man page updates will be done as
    separate commits, once reviewed.  There are also three
    patches authored by Vinicius Ferrao to be committed
    to the code after this commit.
    
    Thanks go to Vinicius Ferrao for implementing the server
    NFS over RDMA code to test against.  The Mellanox folk
    helped out with a lot of testing and the Netperf Cluster
    folk were able to provide access to the Netperf cluster
    so that I could test the code.  Thanks go to them, as well.
    
    The code has only been tested using Mellanox ConnectX4 or newer
    NICs at this time, although I hope to do some testing
    using an Intel 810 NIC soon.
    
    There is still work to be done related to O_DIRECT open(2)s
    and pNFS, but those will be future commits.
    
    Reviewed by:    Vinicius Ferrao <ferrao@versatushpc.com.br>
    Tested by:      Wafa Hamzah <wafah@nvidia.com>
    Discussed with: markj
    MFC after:      1 month
    Differential Revision:  https://reviews.freebsd.org/D59278
---
 .../drivers/infiniband/ulp/nfsclrdma/clnt_rdma.c   | 1887 ++++++++++++++++++++
 .../drivers/infiniband/ulp/nfsclrdma/xprt_rdma.h   |   86 +
 .../drivers/infiniband/ulp/nfsclrdma/xprt_verbs.c  | 1498 ++++++++++++++++
 3 files changed, 3471 insertions(+)

diff --git a/sys/ofed/drivers/infiniband/ulp/nfsclrdma/clnt_rdma.c b/sys/ofed/drivers/infiniband/ulp/nfsclrdma/clnt_rdma.c
new file mode 100644
index 000000000000..1c2075493e3e
--- /dev/null
+++ b/sys/ofed/drivers/infiniband/ulp/nfsclrdma/clnt_rdma.c
@@ -0,0 +1,1887 @@
+/*	$NetBSD: clnt_vc.c,v 1.4 2000/07/14 08:40:42 fvdl Exp $	*/
+
+/*-
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ * Copyright (c) 2009, Sun Microsystems, Inc.
+ * All rights reserved.
+ *
+ * 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.
+ * - Neither the name of Sun Microsystems, Inc. nor the names of its
+ *   contributors may be used to endorse or promote products derived
+ *   from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * clnt_tcp.c, Implements a TCP/IP based, client side RPC.
+ *
+ * Copyright (C) 1984, Sun Microsystems, Inc.
+ *
+ * TCP based RPC supports 'batched calls'.
+ * A sequence of calls may be batched-up in a send buffer.  The rpc call
+ * return immediately to the client even though the call was not necessarily
+ * sent.  The batching occurs if the results' xdr routine is NULL (0) AND
+ * the rpc timeout value is zero (see clnt.h, rpc).
+ *
+ * Clients should NOT casually batch calls that in fact return results; that is,
+ * the server side should be aware that a call is batched and not produce any
+ * return message.  Batched calls that produce many result messages can
+ * deadlock (netlock) the client and the server....
+ *
+ * Now go hang yourself.
+ */
+
+/*
+ * The code in this file handles RPC over RDMA as described by
+ * RFC-8166 and RFC-8267 and is loosely based on code that the above
+ * comment discusses.
+ */
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/kthread.h>
+#include <sys/ktls.h>
+#include <sys/lock.h>
+#include <sys/malloc.h>
+#include <sys/mbuf.h>
+#include <sys/module.h>
+#include <sys/mutex.h>
+#include <sys/pcpu.h>
+#include <sys/proc.h>
+#include <sys/protosw.h>
+#include <sys/socket.h>
+#include <sys/socketvar.h>
+#include <sys/sx.h>
+#include <sys/syslog.h>
+#include <sys/time.h>
+#include <sys/uio.h>
+
+#include <vm/vm.h>
+#include <vm/vm_page.h>
+
+#include <net/vnet.h>
+
+#include <netinet/tcp.h>
+
+#include <rpc/rpc.h>
+#include <rpc/rpc_com.h>
+#include <rpc/krpc.h>
+#include <rpc/clntrdma.h>
+
+/*
+ * This structure stores the internals for an RDMA connection endpoint.
+ */
+struct crdma_data {
+	int		crdma_threads;	/* # of threads in clnt_rdma_call */
+	int		crdma_upcallrefs; /* Cnt of upcalls in progress */
+	bool_t		crdma_closing;	/* TRUE if we are closing */
+	bool_t		crdma_closed;	/* TRUE if we are closed */
+	struct rpcrdma_xprt crdma_xp;	/* RDMA endpoint */
+	bool_t		crdma_closeit;	/* close it on destroy */
+	struct timeval	crdma_wait;	/* wait interval in milliseconds */
+	struct sockaddr_storage	crdma_addr; /* remote addr */
+	struct rpc_err	crdma_error;
+	uint32_t	crdma_xid;
+	uint32_t	crdma_prog;
+	uint32_t	crdma_vers;
+	char		crdma_mcallc[MCALL_MSG_SIZE]; /* call msg */
+	size_t		crdma_mpos;	/* pos after marshal */
+	const char	*crdma_waitchan;
+	int		crdma_waitflag;
+	struct mbuf	*crdma_record;	/* current reply record */
+	size_t		crdma_record_resid; /* how much left of reply to read */
+	bool_t		crdma_record_eor; /* true if reading last fragment */
+	struct ct_request_list crdma_pending;
+	SVCXPRT		*crdma_backchannelxprt; /* xprt for backchannel */
+	struct mbuf	*crdma_raw;	/* Raw mbufs recv'd */
+	uint32_t	crdma_small_reply; /* Max small reply M_PROTO8 */
+	uint32_t	crdma_max_io;	/* Max size of an I/O M_PROTO9 */
+	uint32_t	crdma_readcnt;	/* Count of RPCs with a Read RDMA */
+};
+#define	crdma_lock	crdma_xp.mtx
+
+MALLOC_DECLARE(M_RPCRDMA);
+
+SYSCTL_DECL(_kern_rpc);
+SYSCTL_NODE(_kern_rpc, OID_AUTO, rdma, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
+    "RPC over RDMA");
+int rpcrdma_debuglevel = 0;
+SYSCTL_INT(_kern_rpc_rdma, OID_AUTO, debuglevel, CTLFLAG_RW,
+    &rpcrdma_debuglevel, 0, "Debug level for RPC over RDMA client");
+
+u_int rpcrdma_maxrpc = RPCRDMA_IO_NUMBUFS - 8;
+static int
+sysctl_maxrpc(SYSCTL_HANDLER_ARGS)
+{
+	int error, newmaxrpc;
+
+	newmaxrpc = rpcrdma_maxrpc;
+	error = sysctl_handle_int(oidp, &newmaxrpc, 0, req);
+	if (error != 0 || req->newptr == NULL)
+		return (error);
+	if (newmaxrpc < 4 || newmaxrpc > RPCRDMA_IO_NUMBUFS - 8)
+		return (EINVAL);
+	rpcrdma_maxrpc = newmaxrpc;
+	return (0);
+}
+SYSCTL_PROC(_kern_rpc_rdma, OID_AUTO, maxrpc,
+    CTLTYPE_UINT | CTLFLAG_MPSAFE | CTLFLAG_RW, 0, sizeof(rpcrdma_maxrpc),
+    sysctl_maxrpc, "IU", "Maximum # of concurrent rpcs");
+
+struct cmessage {
+        struct cmsghdr cmsg;
+        struct cmsgcred cmcred;
+};
+
+static enum clnt_stat clnt_rdma_call(CLIENT *, struct rpc_callextra *,
+    rpcproc_t, struct mbuf *, struct mbuf **, struct timeval);
+static void clnt_rdma_geterr(CLIENT *, struct rpc_err *);
+static bool_t clnt_rdma_freeres(CLIENT *, xdrproc_t, void *);
+static void clnt_rdma_abort(CLIENT *);
+static bool_t clnt_rdma_control(CLIENT *, u_int, void *);
+static void clnt_rdma_close(CLIENT *);
+static void clnt_rdma_destroy(CLIENT *);
+static bool_t time_not_ok(struct timeval *);
+static void clnt_rdma_upcall(struct rpcrdma_xprt *xp, struct mbuf *);
+static bool_t clnt_rdma_bcksend(SVCXPRT *, struct mbuf *);
+
+static const struct clnt_ops clnt_rdma_ops = {
+	.cl_call =	clnt_rdma_call,
+	.cl_abort =	clnt_rdma_abort,
+	.cl_geterr =	clnt_rdma_geterr,
+	.cl_freeres =	clnt_rdma_freeres,
+	.cl_close =	clnt_rdma_close,
+	.cl_destroy =	clnt_rdma_destroy,
+	.cl_control =	clnt_rdma_control
+};
+
+
+/*
+ * Create a client handle for an RDMA connection.
+ */
+CLIENT *
+clnt_rdma_create(
+	struct sockaddr *raddr,		/* servers address */
+	const rpcprog_t prog,		/* program number */
+	const rpcvers_t vers,		/* version number */
+	int intrflag,			/* interruptible */
+	uint32_t small_reply,		/* Max. medium reply */
+	uint32_t max_io,		/* size of largest RPC I/O */
+	uint32_t cbslots,		/* Max # of callbacks */
+	struct rpc_err *err)		/* Return error and stat */
+{
+	CLIENT *cl;			/* client handle */
+	struct crdma_data *crdma = NULL;	/* client handle */
+	struct timeval now;
+	int error;
+	static uint32_t disrupt;
+
+	KASSERT(raddr->sa_family != AF_LOCAL,
+	    ("%s: kernel RPC over unix(4) not supported", __func__));
+
+	if (disrupt == 0)
+		disrupt = (uint32_t)(long)raddr;
+
+	cl = (CLIENT *)mem_alloc(sizeof (*cl));
+	crdma = (struct crdma_data *)mem_alloc(sizeof (*crdma));
+
+	mtx_init(&crdma->crdma_lock, "crdma->crdma_lock", NULL, MTX_DEF);
+	crdma->crdma_threads = 0;
+	crdma->crdma_upcallrefs = 0;
+	crdma->crdma_closing = FALSE;
+	crdma->crdma_closed = FALSE;
+	crdma->crdma_closeit = FALSE;
+	TAILQ_INIT(&crdma->crdma_pending);
+
+	/*
+	 * Set up private data struct
+	 */
+	crdma->crdma_xp.credits = 1;
+	xprt_rdma_init(&crdma->crdma_xp, clnt_rdma_upcall);
+	crdma->crdma_wait.tv_sec = -1;
+	crdma->crdma_wait.tv_usec = -1;
+	memcpy(&crdma->crdma_addr, raddr, raddr->sa_len);
+	crdma->crdma_small_reply = round_page(small_reply);
+	crdma->crdma_max_io = round_page(max_io + 2048);
+
+	/* Attempt to connect.. */
+#ifdef VIMAGE
+	error = xprt_rdma_connect(vnet0, raddr, &crdma->crdma_xp,
+	    RPCRDMA_MAX_SMALL_MSG, cbslots);
+#else
+	error = xprt_rdma_connect(NULL, raddr, &crdma->crdma_xp,
+	    RPCRDMA_MAX_SMALL_MSG, cbslots);
+#endif
+	RPCRDMA_DEBUG(1, "clnt_rdma_create: xprt_rdma_connect=%d\n", error);
+	if (error != 0) {
+		err->re_errno = error;
+		if (error == ENETUNREACH)
+			err->re_status = RPC_SYSTEMERROR;
+		else if (error == EPROTO)
+			err->re_status = RPC_UNKNOWNADDR;
+		else
+			err->re_status = RPC_UNKNOWNPROTO;
+		RPCRDMA_DEBUG(1, "clnt_rdma_create: Fail stat=%d\n",
+		    err->re_status);
+		goto fail;
+	}
+
+	/*
+	 * Initialize the xid.
+	 */
+	getmicrotime(&now);
+	crdma->crdma_xid = ((uint32_t)++disrupt) ^ __RPC_GETXID(&now);
+
+	crdma->crdma_prog = prog;
+	crdma->crdma_vers = vers;
+
+	crdma->crdma_waitchan = "rdmarecv";
+	crdma->crdma_waitflag = 0;
+
+	cl->cl_refs = 1;
+	cl->cl_ops = &clnt_rdma_ops;
+	cl->cl_private = crdma;
+	cl->cl_auth = authnone_create();
+
+	crdma->crdma_raw = NULL;
+	crdma->crdma_record = NULL;
+	crdma->crdma_record_resid = 0;
+	TAILQ_INIT(&crdma->crdma_pending);
+	RPCRDMA_DEBUG(1, "clnt_rdma_create: client=%p\n", cl);
+	return (cl);
+fail:
+	RPCRDMA_DEBUG(1, "clnt_rdma_create: failed err=%d\n", error);
+	free(crdma->crdma_xp.ep, M_RPCRDMA);
+	mtx_destroy(&crdma->crdma_lock);
+	mem_free(crdma, sizeof (struct crdma_data));
+	mem_free(cl, sizeof (CLIENT));
+
+	return ((CLIENT *)NULL);
+}
+
+#define	READLIST	0
+#define	WRITELIST	1
+#define	WRITECHUNK	2
+
+/*
+ * Create an M_EXTPG mbuf list of numpg pages.
+ */
+static struct mbuf *
+_rpc_create_mextpg(int numpg)
+{
+	struct mbuf *m, *mhead, **mprev;
+	int i, j;
+
+	mprev = &mhead;
+	mhead = NULL;
+	for (i = numpg; i > 0; i -= j) {
+		j = MIN(i, MBUF_PEXT_MAX_PGS);
+		m = mb_alloc_ext_plus_pages(j * PAGE_SIZE, M_WAITOK);
+		if (m == NULL) {
+			if (mhead != NULL)
+				m_freem(mhead);
+			return (NULL);
+		}
+		m->m_epg_last_len = PAGE_SIZE;
+		m->m_len = j * PAGE_SIZE;
+		*mprev = m;
+		mprev = &m->m_next;
+	}
+	return (mhead);
+}
+
+/*
+ * Trim the tail of an M_EXTPG mbuf chain created by _rpc_create_mextpg().
+ */
+static void
+_rpc_trim_mextpg(struct mbuf *mextpg, uint32_t newlen)
+{
+	struct mbuf *m, *m2;
+	vm_page_t pg;
+	uint32_t i, j;
+
+	i = 0;
+	for (m = mextpg; m != NULL; m = m->m_next) {
+		/* At last mbuf. */
+		if (i + m->m_len >= newlen)
+			break;
+		i += m->m_len;
+	}
+	if (m == NULL)
+		return;
+
+	/* Throw away any additional mbufs. */
+	m2 = m->m_next;
+	m->m_next = NULL;
+	if (m2 != NULL)
+		m_freem(m2);
+
+	/* Now trim the last mbuf. */
+	newlen -= i;
+	if (newlen == m->m_len)
+		return;
+	i = howmany(newlen, PAGE_SIZE);
+	for (j = i; j < m->m_epg_npgs; j++) {
+		pg = PHYS_TO_VM_PAGE(m->m_epg_pa[j]);
+		vm_page_unwire_noq(pg);
+		vm_page_free(pg);
+	}
+	m->m_epg_npgs = i;
+	m->m_len = newlen;
+	m->m_epg_last_len = newlen % PAGE_SIZE;
+	if (m->m_epg_last_len == 0)
+		m->m_epg_last_len = PAGE_SIZE;
+}
+
+/*
+ * Some M_PROTOnn flags are used in "args" for RDMA chunks.
+ * M_PROTO11 - Indicates that the RPC gets reduced.
+ *             (Read/Write/Symlink for NFS.)
+ * M_PROTO10 - On an mbuf in the list (marked by a M_PROTO11) that has the
+ *             reduction information, such as the pages.  This mbuf is removed
+ *             from the list before being sent.
+ * M_PROTO9  - This RPC needs a large rdma_reply write chunk.
+ * M_PROTO8  - This RPC needs a small sized rdma_reply write chunk.
+ *             (Readdir for NFS.)
+ * M_PROTO7  - This RPC needs a single page rdma_reply write chunk.
+ *             (Readlink for NFS.)
+ * M_PROTO6  - Set in reply to indicate reduction was done.
+ * Except for M_PROTO10, these flags are always set in the first mbuf in "args".
+ *
+ * At this time, only one read or write chunk is provided for an RPC,
+ * which implies only one reduction and no rdma_reply write chunk for
+ * RPCs that have a reduction.
+ */
+static enum clnt_stat
+clnt_rdma_call(
+	CLIENT		*cl,		/* client handle */
+	struct rpc_callextra *ext,	/* call metadata */
+	rpcproc_t	proc,		/* procedure number */
+	struct mbuf	*args,		/* pointer to args */
+	struct mbuf	**resultsp,	/* pointer to results */
+	struct timeval	utimeout)
+{
+	struct crdma_data *crdma = (struct crdma_data *) cl->cl_private;
+	AUTH *auth;
+	struct rpc_err *errp;
+	enum clnt_stat stat;
+	XDR xdrs;
+	struct rpc_msg call_msg;
+	struct rpc_msg reply_msg;
+	uint32_t chunks[3], reply_chunks[3];
+	bool_t bval, falseval, ok;
+	bool readcounted;
+	int nrefreshes = 2;		/* number of times to refresh cred */
+	struct timeval timeout;
+	uint32_t xid, rdma_xid, rdma_vers, rdma_credit, rdma_body, rpos;
+	uint32_t tlen, tlen2, length, handle, reduce_pos, rpchdr_len;
+	uint64_t offset;
+	struct mbuf *m1, *m2, *mreq, *results, *mr, *mreduce, *mreduce_prev;
+	struct ct_request *cr;
+	struct rpcrdma_chunk *reduce_chp, *reply_chp, *request_chp;
+	int error, i, ind, reduce_ind;
+
+	cr = malloc(sizeof(struct ct_request), M_RPC, M_WAITOK);
+
+	ind = -1;
+	reduce_chp = NULL;
+	reply_chp = NULL;
+	request_chp = NULL;
+	reduce_pos = 0;
+	reduce_ind = 0;
+	mr = NULL;
+	mreq = NULL;
+	mreduce = NULL;
+	mreduce_prev = NULL;
+	readcounted = false;
+	chunks[READLIST] = chunks[WRITELIST] = chunks[WRITECHUNK] = 0;
+	mtx_lock(&crdma->crdma_lock);
+
+	if (crdma->crdma_closing || crdma->crdma_closed ||
+	    xprt_rdma_isdisconnected(&crdma->crdma_xp) != 0) {
+		mtx_unlock(&crdma->crdma_lock);
+		free(cr, M_RPC);
+		RPCRDMA_DEBUG(2, "clnt_rdma_call: First closing, return "
+		    "RPC_CANTSEND\n");
+		return (RPC_CANTSEND);
+	}
+	crdma->crdma_threads++;
+
+	if (ext) {
+		auth = ext->rc_auth;
+		errp = &ext->rc_err;
+	} else {
+		auth = cl->cl_auth;
+		errp = &crdma->crdma_error;
+	}
+
+	cr->cr_mrep = NULL;
+	cr->cr_error = 0;
+
+	if (crdma->crdma_wait.tv_usec == -1) {
+		timeout = utimeout;	/* use supplied timeout */
+	} else {
+		timeout = crdma->crdma_wait;	/* use default timeout */
+	}
+
+	/*
+	 * After 15sec of looping, allow it to return RPC_CANTSEND, which will
+	 * cause the clnt_reconnect layer to create a new RDMA connection.
+	 */
+call_again:
+	mtx_assert(&crdma->crdma_lock, MA_OWNED);
+	if (crdma->crdma_closing || crdma->crdma_closed) {
+closing:
+		xprt_rdma_mark_disconnected(&crdma->crdma_xp);
+		mtx_unlock(&crdma->crdma_lock);
+		if (ind >= 0)
+			xprt_rdma_release_send(&crdma->crdma_xp, ind,
+			    reduce_chp, reply_chp, request_chp, RPC_CANTSEND);
+		mtx_lock(&crdma->crdma_lock);
+		crdma->crdma_threads--;
+		wakeup(crdma);
+		mtx_unlock(&crdma->crdma_lock);
+		free(cr, M_RPC);
+		RPCRDMA_DEBUG(2, "clnt_rdma_call: Second closing, return "
+		    "RPC_CANTSEND\n");
+		return (RPC_CANTSEND);
+	}
+
+	crdma->crdma_xid++;
+	xid = crdma->crdma_xid;
+
+	if (ind < 0)
+		ind = xprt_rdma_acquire_buf(&crdma->crdma_xp, 0,
+		    crdma->crdma_xp.maxrpc);
+	RPCRDMA_DEBUG(4, "clnt_rdma_call: buffer xid=0x%x ind=%d maxrpc=%d\n",
+	    xid, ind, crdma->crdma_xp.maxrpc);
+	if (ind < 0)
+		goto closing;
+	mtx_unlock(&crdma->crdma_lock);
+
+	m2 = NULL;
+	if ((args->m_flags & M_PROTO9) != 0 && reply_chp == NULL) {
+		/* Create a large rdma_reply chunk, as required. */
+		tlen = howmany(crdma->crdma_max_io, PAGE_SIZE);
+		m1 = _rpc_create_mextpg(tlen);
+		reply_chp = xprt_rdma_create_chunk(&crdma->crdma_xp, tlen,
+		    NULL, m1, true, ind);
+		if (reply_chp != NULL)
+			chunks[WRITECHUNK] = reply_chp->num_segment;
+		else
+			goto cannotchunk;
+		RPCRDMA_DEBUG(4, "clnt_rdma_call: Large (M_PROTO9) reply chunk"
+		    " reply_chp=%p num_pg=%d\n", reply_chp, tlen);
+	} else if ((args->m_flags & M_PROTO8) != 0 && reply_chp == NULL) {
+		/* Create a small (Readdir) sized rdma_reply chunk. */
+		tlen = howmany(crdma->crdma_small_reply, PAGE_SIZE);
+		m1 = _rpc_create_mextpg(tlen);
+		reply_chp = xprt_rdma_create_chunk(&crdma->crdma_xp, tlen,
+		    NULL, m1, true, ind);
+		if (reply_chp != NULL)
+			chunks[WRITECHUNK] = reply_chp->num_segment;
+		else
+			goto cannotchunk;
+
+		RPCRDMA_DEBUG(4, "clnt_rdma_call: Small (M_PROTO8) reply chunk"
+		    " reply_chp=%p num_pg=%d\n", reply_chp, tlen);
+	} else if ((args->m_flags & M_PROTO7) != 0 && reply_chp == NULL) {
+		/* Create a single page sized rdma_reply chunk. (Readlink) */
+		m1 = _rpc_create_mextpg(1);
+		reply_chp = xprt_rdma_create_chunk(&crdma->crdma_xp, 1,
+		    NULL, m1, true, ind);
+		if (reply_chp != NULL)
+			chunks[WRITECHUNK] = reply_chp->num_segment;
+		else
+			goto cannotchunk;
+		RPCRDMA_DEBUG(4, "clnt_rdma_call: Single page (M_PROTO7) reply"
+		    " chunk reply_chp=%p num_pg=%d\n", reply_chp, tlen);
+	}
+	if ((args->m_flags & M_PROTO11) != 0 && reduce_chp == NULL) {
+		KASSERT((args->m_flags & M_PROTO10) == 0,
+		    ("clnt_rdma_call: first args mbuf marked M_PROTO10"));
+		RPCRDMA_DEBUG(4, "clnt_rdma_call: M_PROTO11 to be reduced\n");
+		/*
+		 * This RPC needs to be reduced.  The M_PROTO10 mbuf provides
+		 * the information to create the chunk for reduction.
+		 */
+		mreduce_prev = NULL;
+		for (mreduce = args; mreduce != NULL &&
+		    (mreduce->m_flags & M_PROTO10) == 0;
+		     mreduce = mreduce->m_next)
+			mreduce_prev = mreduce;
+		if (mreduce != NULL && mreduce_prev != NULL) {
+			struct rpcrdma_reduce_pg *rb;
+
+			/* Use mreduce mbuf to create chunk. */
+			rb = mtod(mreduce, struct rpcrdma_reduce_pg *);
+			reduce_chp = xprt_rdma_create_chunk(&crdma->crdma_xp,
+			    0, rb, NULL, (rb->into_mem != 0) ? true : false,
+			    ind);
+			if (reduce_chp != NULL) {
+				if (rb->into_mem != 0)
+					reduce_ind = WRITELIST;
+				else
+					reduce_ind = READLIST;
+				chunks[reduce_ind] = reduce_chp->num_segment;
+				tlen = rb->len;
+				reduce_pos = rb->pos;
+			} else
+				goto cannotchunk;
+			RPCRDMA_DEBUG(4, "clnt_rdma_call: reduction pos=%d "
+			    "len=%d into_mem=%d reduce_chp=%p\n", reduce_pos,
+			    tlen, rb->into_mem, reduce_chp);
+		} else
+			printf("clnt_rdma_call: Bogus M_PROTO10\n");
+	}
+
+	rdma_vers = 1;
+	rdma_credit = crdma->crdma_xp.maxrpc;
+	rdma_body = RDMA_MSG;
+
+	/* Wait until read cnt < maxreads. */
+	mtx_lock(&crdma->crdma_lock);
+	if (chunks[READLIST] > 0) {
+		while (crdma->crdma_readcnt >= crdma->crdma_xp.maxreads)
+			(void)msleep(&crdma->crdma_readcnt, &crdma->crdma_lock,
+			    PVFS, "crmrd", hz);
+		crdma->crdma_readcnt++;
+		readcounted = true;
+	}
+	mtx_unlock(&crdma->crdma_lock);
+
+	/*
+	 * Create the RPC header now in m2, so we can size it and adjust the
+	 * rdma_readlist positions.
+	 */
+	m2 = m_get(M_WAITOK, MT_DATA);
+	m2->m_len = 0;
+	xdrmbuf_create(&xdrs, m2, XDR_ENCODE);
+	/*
+	 * Put the RPC header in the XDR stream.
+	 */
+	call_msg.rm_xid = xid;
+	call_msg.rm_direction = CALL;
+	call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
+	call_msg.rm_call.cb_prog = crdma->crdma_prog;
+	call_msg.rm_call.cb_vers = crdma->crdma_vers;
+	if (! xdr_callhdr(&xdrs, &call_msg))
+		goto cannotencode;
+	/* Keep the caller's metadata linked on every failure and retry. */
+	if (mreduce != NULL && mreduce_prev != NULL)
+		mreduce_prev->m_next = mreduce->m_next;
+	m1 = m_copym(args, 0, M_COPYALL, M_WAITOK);
+	if (mreduce != NULL && mreduce_prev != NULL) {
+		/* Put mreduce back in args list so that a retry will work. */
+		mreduce->m_next = mreduce_prev->m_next;
+		mreduce_prev->m_next = mreduce;
+	}
+	if (m1 == NULL)
+		goto cannotencode;
+
+	rpchdr_len = m_length(m1, NULL);   /* Set to length of the NFS args. */
+	if ((! XDR_PUTINT32(&xdrs, &proc)) ||
+	    (! AUTH_MARSHALL(auth, xid, &xdrs, m1))) {
+cannotencode:
+		if (m2)
+			m_freem(m2);
+		m2 = NULL;
+		if (mreq)
+			m_freem(mreq);
+		mreq = NULL;
+		errp->re_status = stat = RPC_CANTENCODEARGS;
+		RPCRDMA_DEBUG(1, "xprt_rdma_cal: cannot encode args\n");
+		mtx_lock(&crdma->crdma_lock);
+		goto out;
+	}
+	tlen2 = m_length(m2, NULL);
+	rpchdr_len = tlen2 - rpchdr_len;   /* Subtract out the NFS args len. */
+	RPCRDMA_DEBUG(4, "clnt_rdma_call: RPC header length=%d\n", tlen2);
+	tlen2 += RPCRDMA_HEADER_LEN(chunks[READLIST], chunks[WRITELIST],
+	    chunks[WRITECHUNK]);
+	RPCRDMA_DEBUG(4, "clnt_rdma_call: RPC header plus RDMA header "
+	    "length=%d\n", tlen2);
+
+	/*
+	 * If the RPC request message is too large
+	 * create a large enough buffer and copy the mbuf
+	 * list into it.
+	 */
+	if (rdma_body == RDMA_MSG && tlen2 > RPCRDMA_MAX_INLINE) {
+		struct rpcrdma_reduce_pg *rb;
+
+		KASSERT(request_chp == NULL, ("clnt_rdma_call: request_chp not "
+		    "NULL"));
+		tlen2 -= RPCRDMA_HEADER_LEN(chunks[READLIST], chunks[WRITELIST],
+		    chunks[WRITECHUNK]);
+		if (reduce_chp != NULL) {
+			printf("clnt_rdma_call: Request msg too big with chunk"
+			    " %d\n", tlen2);
+			goto cannotencode;
+		}
+		RPCRDMA_DEBUG(4, "clnt_rdma_call: Large request len=%d\n",
+		    tlen2);
+		mr = rpc_reduce_pg(NULL, tlen2, 0, false);
+		rb = mtod(mr, struct rpcrdma_reduce_pg *);
+		request_chp = xprt_rdma_create_chunk(&crdma->crdma_xp,
+		    0, rb, NULL, false, ind);
+		if (request_chp != NULL) {
+			rpc_copy_mbuf_to_rb(m2, rb);
+			m_freem(m2);
+			m2 = NULL;
+			chunks[READLIST] = request_chp->num_segment;
+			rdma_body = RDMA_NOMSG;
+			RPCRDMA_DEBUG(4, "clnt_rdma_call: Got request_chp, "
+			    "num_segment=%d\n", request_chp->num_segment);
+		} else {
+cannotchunk:
+			if (m2)
+				m_freem(m2);
+			m2 = NULL;
+			errp->re_status = stat = RPC_CANTSEND;
+			RPCRDMA_DEBUG(1, "xprt_rdma_cal: cannot create "
+			    "chunk\n");
+			mtx_lock(&crdma->crdma_lock);
+			goto out;
+		}
+		RPCRDMA_DEBUG(1, "clnt_rdma_call: Creating large RPC request "
+		    "chunk length=%d request_chp=%p\n", tlen2, request_chp);
+	}
+
+	mreq = m_gethdr(M_WAITOK, MT_DATA);
+	mreq->m_len = 0;
+	xdrmbuf_create(&xdrs, mreq, XDR_ENCODE);
+
+	errp->re_status = stat = RPC_SUCCESS;
+
+	/*
+	 * Put the RDMA header in the XDR stream.
+	 */
+	falseval = FALSE;
+	bval = (chunks[READLIST] > 0) ? TRUE : FALSE;
+	if ((! XDR_PUTINT32(&xdrs, &xid)) ||
+	    (! XDR_PUTINT32(&xdrs, &rdma_vers)) ||
+	    (! XDR_PUTINT32(&xdrs, &rdma_credit)) ||
+	    (! XDR_PUTINT32(&xdrs, &rdma_body)) ||
+	    (! XDR_PUTINT32(&xdrs, &bval)))
+		goto cannotencode;
+	RPCRDMA_DEBUG(4, "clnt_rdma_call: xid=0x%x vers=%d credits=%d "
+	    "rdma_body=%d readlist=%d\n", xid, rdma_vers, rdma_credit,
+	    rdma_body, bval);
+	if (bval) {
+		struct rpcrdma_chunk *tchp;
+
+		KASSERT(reduce_pos > 0 || rdma_body == RDMA_NOMSG,
+		    ("clnt_rdma_call: reduce_pos zero for RDMA_MSG"));
+		rpos = reduce_pos + rpchdr_len;
+		tchp = reduce_chp;
+		if (rdma_body == RDMA_NOMSG) {
+			rpos = 0;	/* 0 for a long call message. */
+			tchp = request_chp;
+		}
+		bval = TRUE;
+		for (i = 0; i < chunks[READLIST]; i++) {
+			if (i == chunks[READLIST] - 1)
+				bval = FALSE;
+			if ((! XDR_PUTINT32(&xdrs, &rpos)) ||
+			    (! XDR_PUTINT32(&xdrs, &tchp->handle[i])) ||
+			    (! XDR_PUTINT32(&xdrs, &tchp->length[i])) ||
+			    (! xdr_uint64_t(&xdrs, &tchp->offset[i])) ||
+			    (! XDR_PUTINT32(&xdrs, &bval)))
+				goto cannotencode;
+			RPCRDMA_DEBUG(4, "clnt_rdma_call: readlist chunk "
+			    "handle=0x%x length=%d offset=%ju numseg=%d\n",
+			    tchp->handle[i], tchp->length[i],
+			    (uintmax_t)tchp->offset[i], chunks[READLIST]);
+		}
+		RPCRDMA_DEBUG(2, "clnt_rdma_call: Readlist rdma_body=%d "
+		    "numseg=%d\n", rdma_body, chunks[READLIST]);
+	}
+	bval = (chunks[WRITELIST] > 0) ? TRUE : FALSE;
+	if (bval) {
+		if ((! XDR_PUTINT32(&xdrs, &bval)) ||
+		    (! XDR_PUTINT32(&xdrs, &chunks[WRITELIST])))
+			goto cannotencode;
+		for (i = 0; i < chunks[WRITELIST]; i++) {
+			if ((! XDR_PUTINT32(&xdrs, &reduce_chp->handle[i])) ||
+			    (! XDR_PUTINT32(&xdrs, &reduce_chp->length[i])) ||
+			    (! xdr_uint64_t(&xdrs, &reduce_chp->offset[i])))
+				goto cannotencode;
+			RPCRDMA_DEBUG(4, "clnt_rdma_call: writelist chunk "
+			    "handle=0x%x length=%d offset=%ju numseg=%d\n",
+			    reduce_chp->handle[i], reduce_chp->length[i],
+			    (uintmax_t)reduce_chp->offset[i],
+			    chunks[WRITELIST]);
+		}
+		if (! XDR_PUTINT32(&xdrs, &falseval))
+			goto cannotencode;
+		RPCRDMA_DEBUG(2, "clnt_rdma_call: Writelist numseg=%d\n",
+		    chunks[WRITELIST]);
+	} else {
+		if (! XDR_PUTINT32(&xdrs, &bval))
+			goto cannotencode;
+	}
+	bval = (chunks[WRITECHUNK] > 0) ? TRUE : FALSE;
+	if (bval) {
+		if ((! XDR_PUTINT32(&xdrs, &bval)) ||
+		    (! XDR_PUTINT32(&xdrs, &chunks[WRITECHUNK])))
+			goto cannotencode;
+		for (i = 0; i < chunks[WRITECHUNK]; i++) {
+			if ((! XDR_PUTINT32(&xdrs, &reply_chp->handle[i])) ||
+			    (! XDR_PUTINT32(&xdrs, &reply_chp->length[i])) ||
+			    (! xdr_uint64_t(&xdrs, &reply_chp->offset[i])))
+				goto cannotencode;
+			RPCRDMA_DEBUG(4, "clnt_rdma_call: replylist chunk "
+			    "handle=0x%x length=%d offset=%ju numseg=%d\n",
+			    reply_chp->handle[i], reply_chp->length[i],
+			    (uintmax_t)reply_chp->offset[i],
+			    chunks[WRITECHUNK]);
+		}
+		RPCRDMA_DEBUG(2, "clnt_rdma_call: Replylist numseg=%d\n",
+		    chunks[WRITECHUNK]);
+	} else {
+		if (! XDR_PUTINT32(&xdrs, &bval))
+			goto cannotencode;
+	}
+	RPCRDMA_DEBUG(1, "clnt_rdma_call: RDMA header len=%d rdma_body=%d\n",
+	    m_length(mreq, NULL), rdma_body);
+	if (rdma_body == RDMA_MSG) {
+		if (! XDR_PUTMBUF(&xdrs, m2))
+			goto cannotencode;
+		m2 = NULL;
+	}
+	mreq->m_pkthdr.len = m_length(mreq, NULL);
+	RPCRDMA_DEBUG(1, "clnt_rdma_call: RDMA header plus RPC message "
+	    "len=%d\n", mreq->m_pkthdr.len);
+
+	cr->cr_xid = xid;
+	mtx_lock(&crdma->crdma_lock);
+	/*
+	 * Check to see if the other end has already started to close down
+	 * the connection. The upcall will have set crdma_error.re_status
+	 * to RPC_CANTRECV if this is the case.
+	 * If the other end starts to close down the connection after this
+	 * point, it will be detected later when cr_error is checked,
+	 * since the request is in the crdma_pending queue.
+	 */
+	if (crdma->crdma_error.re_status == RPC_CANTRECV) {
+		if (errp != &crdma->crdma_error) {
+			errp->re_errno = crdma->crdma_error.re_errno;
+			errp->re_status = RPC_CANTRECV;
+		}
+		stat = RPC_CANTRECV;
+		RPCRDMA_DEBUG(1, "clnt_rdma_call: return RPC_CANTRECV\n");
+		goto out;
+	}
+
+	TAILQ_INSERT_TAIL(&crdma->crdma_pending, cr, cr_link);
+	mtx_unlock(&crdma->crdma_lock);
+
+	/*
+	 * Send the message on the RDMA payload stream.
+	 */
+	error = xprt_rdma_send(&crdma->crdma_xp, mreq, ind);
+	RPCRDMA_DEBUG(1, "clnt_rdma_call: xprt_rdma_send index=%d error=%d\n",
+	    ind, error);
+	mreq = NULL;
+	reply_msg.acpted_rply.ar_verf.oa_flavor = AUTH_NULL;
+	reply_msg.acpted_rply.ar_verf.oa_base = cr->cr_verf;
+	reply_msg.acpted_rply.ar_verf.oa_length = 0;
+	reply_msg.acpted_rply.ar_results.where = NULL;
+	reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
+
+	mtx_lock(&crdma->crdma_lock);
+	if (error) {
+		TAILQ_REMOVE(&crdma->crdma_pending, cr, cr_link);
+		errp->re_errno = error;
+		errp->re_status = stat = RPC_CANTSEND;
+		RPCRDMA_DEBUG(1, "clnt_rdma_call: Third RPC_CANTSEND\n");
+		goto out;
+	}
+
+	/*
+	 * Check to see if we got an upcall while waiting for the
+	 * lock. In both these cases, the request has been removed
+	 * from crdma->crdma_pending.
+	 */
+	if (cr->cr_error) {
+		TAILQ_REMOVE(&crdma->crdma_pending, cr, cr_link);
+		errp->re_errno = cr->cr_error;
+		RPCRDMA_DEBUG(1, "clnt_rdma_call: First RPC_CANTRECV\n");
+		errp->re_status = stat = RPC_CANTRECV;
+		goto out;
+	}
+	if (cr->cr_mrep) {
+		TAILQ_REMOVE(&crdma->crdma_pending, cr, cr_link);
+		RPCRDMA_DEBUG(1, "clnt_rdma_call: Got reply to RPC\n");
+		goto got_reply;
+	}
+
+	/*
+	 * Hack to provide rpc-based message passing
+	 */
+	if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
+		TAILQ_REMOVE(&crdma->crdma_pending, cr, cr_link);
+		errp->re_status = stat = RPC_TIMEDOUT;
+		RPCRDMA_DEBUG(1, "clnt_rdma_call: Weird zero timeout\n");
+		goto out;
+	}
+
+	error = msleep(cr, &crdma->crdma_lock, crdma->crdma_waitflag,
+	    crdma->crdma_waitchan, tvtohz(&timeout));
+	RPCRDMA_DEBUG(1, "clnt_rdma_call: msleep=%d\n", error);
+
+	TAILQ_REMOVE(&crdma->crdma_pending, cr, cr_link);
+
+	if (error) {
+		/*
+		 * The sleep returned an error so our request is still
+		 * on the list. Turn the error code into an
+		 * appropriate client status.
+		 */
+		errp->re_errno = error;
+		switch (error) {
+		case EINTR:
+			stat = RPC_INTR;
+			break;
+		case EWOULDBLOCK:
+			stat = RPC_TIMEDOUT;
+			break;
+		default:
+			stat = RPC_CANTRECV;
+		}
+		errp->re_status = stat;
+		RPCRDMA_DEBUG(1, "clnt_rdma_call: msleep error return "
+		    "stat=%d\n", stat);
+		goto out;
+	} else {
+		/*
+		 * We were woken up by the upcall.  If the
+		 * upcall had a receive error, report that,
+		 * otherwise we have a reply.
+		 */
+		if (cr->cr_error) {
+			errp->re_errno = cr->cr_error;
+			errp->re_status = stat = RPC_CANTRECV;
+			RPCRDMA_DEBUG(1, "clnt_rdma_call: Upcall recv=%d, "
+			    "second RPC_CANTRECV\n", errp->re_errno);
+			goto out;
+		}
+	}
+
+got_reply:
+	/*
+	 * Now decode and validate the response. We need to drop the
+	 * lock since xdr_replymsg may end up sleeping in malloc.
+	 */
+	mtx_unlock(&crdma->crdma_lock);
+
+	if (ext && ext->rc_feedback)
+		ext->rc_feedback(FEEDBACK_OK, proc, ext->rc_feedback_arg);
+
+	xdrmbuf_create(&xdrs, cr->cr_mrep, XDR_DECODE);
+	cr->cr_mrep = NULL;
+
+	/* Decode the RDMA header. */
+	if ((! XDR_GETINT32(&xdrs, &rdma_xid)) ||
+	    (! XDR_GETINT32(&xdrs, &rdma_vers)) ||
+	    (! XDR_GETINT32(&xdrs, &rdma_credit)) ||
+	    (! XDR_GETINT32(&xdrs, &rdma_body)))
+		goto cannotdecode;
+	RPCRDMA_DEBUG(2, "clnt_rdma_call: Got reply RDMA header xid=0x%x "
+	    "vers=%d rdma_credit=%d rdma_body=%d\n", rdma_xid, rdma_vers,
+	    rdma_credit, rdma_body);
+	if (xid != rdma_xid || rdma_vers != 1) {
+		printf("clnt_rdma_call: xid=%u rdma_xid=%u rdma_vers=%d\n",
+		    xid, rdma_xid, rdma_vers);
+		goto cannotdecode;
+	}
+	crdma->crdma_xp.credits = rdma_credit;
+	reply_chunks[READLIST] = chunks[READLIST];
+	reply_chunks[WRITELIST] = chunks[WRITELIST];
+	reply_chunks[WRITECHUNK] = chunks[WRITECHUNK];
+	if (rdma_body == RDMA_MSG || rdma_body == RDMA_NOMSG) {
+		/* Check the 3 chunks in the reply. */
+		for (i = 0; i < 3; i++) {
+			tlen = 0;
*** 2558 LINES SKIPPED ***