blob: 187894b4c33f31df2763b61a181935a4cf782265 [file] [log] [blame]
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -04001/*
2 * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the BSD-type
8 * license below:
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided
20 * with the distribution.
21 *
22 * Neither the name of the Network Appliance, Inc. nor the names of
23 * its contributors may be used to endorse or promote products
24 * derived from this software without specific prior written
25 * permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
40/*
41 * transport.c
42 *
43 * This file contains the top-level implementation of an RPC RDMA
44 * transport.
45 *
46 * Naming convention: functions beginning with xprt_ are part of the
47 * transport switch. All others are RPC RDMA internal.
48 */
49
50#include <linux/module.h>
51#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040053#include <linux/seq_file.h>
Jeff Layton59766872013-02-04 12:50:00 -050054#include <linux/sunrpc/addr.h>
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040055
56#include "xprt_rdma.h"
57
58#ifdef RPC_DEBUG
59# define RPCDBG_FACILITY RPCDBG_TRANS
60#endif
61
62MODULE_LICENSE("Dual BSD/GPL");
63
64MODULE_DESCRIPTION("RPC/RDMA Transport for Linux kernel NFS");
65MODULE_AUTHOR("Network Appliance, Inc.");
66
67/*
68 * tunables
69 */
70
71static unsigned int xprt_rdma_slot_table_entries = RPCRDMA_DEF_SLOT_TABLE;
72static unsigned int xprt_rdma_max_inline_read = RPCRDMA_DEF_INLINE;
73static unsigned int xprt_rdma_max_inline_write = RPCRDMA_DEF_INLINE;
74static unsigned int xprt_rdma_inline_write_padding;
Tom Talpey3197d3092008-10-09 15:00:20 -040075static unsigned int xprt_rdma_memreg_strategy = RPCRDMA_FRMR;
Tom Talpey9191ca32008-10-09 15:01:11 -040076 int xprt_rdma_pad_optimize = 0;
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040077
78#ifdef RPC_DEBUG
79
80static unsigned int min_slot_table_size = RPCRDMA_MIN_SLOT_TABLE;
81static unsigned int max_slot_table_size = RPCRDMA_MAX_SLOT_TABLE;
82static unsigned int zero;
83static unsigned int max_padding = PAGE_SIZE;
84static unsigned int min_memreg = RPCRDMA_BOUNCEBUFFERS;
85static unsigned int max_memreg = RPCRDMA_LAST - 1;
86
87static struct ctl_table_header *sunrpc_table_header;
88
Joe Perchesfe2c6332013-06-11 23:04:25 -070089static struct ctl_table xr_tunables_table[] = {
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040090 {
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040091 .procname = "rdma_slot_table_entries",
92 .data = &xprt_rdma_slot_table_entries,
93 .maxlen = sizeof(unsigned int),
94 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -080095 .proc_handler = proc_dointvec_minmax,
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040096 .extra1 = &min_slot_table_size,
97 .extra2 = &max_slot_table_size
98 },
99 {
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400100 .procname = "rdma_max_inline_read",
101 .data = &xprt_rdma_max_inline_read,
102 .maxlen = sizeof(unsigned int),
103 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800104 .proc_handler = proc_dointvec,
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400105 },
106 {
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400107 .procname = "rdma_max_inline_write",
108 .data = &xprt_rdma_max_inline_write,
109 .maxlen = sizeof(unsigned int),
110 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800111 .proc_handler = proc_dointvec,
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400112 },
113 {
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400114 .procname = "rdma_inline_write_padding",
115 .data = &xprt_rdma_inline_write_padding,
116 .maxlen = sizeof(unsigned int),
117 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800118 .proc_handler = proc_dointvec_minmax,
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400119 .extra1 = &zero,
120 .extra2 = &max_padding,
121 },
122 {
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400123 .procname = "rdma_memreg_strategy",
124 .data = &xprt_rdma_memreg_strategy,
125 .maxlen = sizeof(unsigned int),
126 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800127 .proc_handler = proc_dointvec_minmax,
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400128 .extra1 = &min_memreg,
129 .extra2 = &max_memreg,
130 },
131 {
Tom Talpey9191ca32008-10-09 15:01:11 -0400132 .procname = "rdma_pad_optimize",
133 .data = &xprt_rdma_pad_optimize,
134 .maxlen = sizeof(unsigned int),
135 .mode = 0644,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800136 .proc_handler = proc_dointvec,
Tom Talpey9191ca32008-10-09 15:01:11 -0400137 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800138 { },
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400139};
140
Joe Perchesfe2c6332013-06-11 23:04:25 -0700141static struct ctl_table sunrpc_table[] = {
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400142 {
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400143 .procname = "sunrpc",
144 .mode = 0555,
145 .child = xr_tunables_table
146 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800147 { },
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400148};
149
150#endif
151
Chuck Leverbfaee092014-05-28 10:34:32 -0400152#define RPCRDMA_BIND_TO (60U * HZ)
153#define RPCRDMA_INIT_REEST_TO (5U * HZ)
154#define RPCRDMA_MAX_REEST_TO (30U * HZ)
155#define RPCRDMA_IDLE_DISC_TO (5U * 60 * HZ)
156
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400157static struct rpc_xprt_ops xprt_rdma_procs; /* forward reference */
158
159static void
160xprt_rdma_format_addresses(struct rpc_xprt *xprt)
161{
Chuck Leverc877b842009-08-09 15:09:36 -0400162 struct sockaddr *sap = (struct sockaddr *)
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400163 &rpcx_to_rdmad(xprt).addr;
Chuck Leverc877b842009-08-09 15:09:36 -0400164 struct sockaddr_in *sin = (struct sockaddr_in *)sap;
165 char buf[64];
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400166
Chuck Leverc877b842009-08-09 15:09:36 -0400167 (void)rpc_ntop(sap, buf, sizeof(buf));
168 xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400169
Joe Perches81160e662010-03-08 12:15:59 -0800170 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400171 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400172
173 xprt->address_strings[RPC_DISPLAY_PROTO] = "rdma";
174
Joe Perches81160e662010-03-08 12:15:59 -0800175 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
Chuck Leverc877b842009-08-09 15:09:36 -0400176 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400177
Joe Perches81160e662010-03-08 12:15:59 -0800178 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
Chuck Leverc877b842009-08-09 15:09:36 -0400179 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400180
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400181 /* netid */
182 xprt->address_strings[RPC_DISPLAY_NETID] = "rdma";
183}
184
185static void
186xprt_rdma_free_addresses(struct rpc_xprt *xprt)
187{
Chuck Lever33e01dc2008-01-14 12:32:20 -0500188 unsigned int i;
189
190 for (i = 0; i < RPC_DISPLAY_MAX; i++)
191 switch (i) {
192 case RPC_DISPLAY_PROTO:
193 case RPC_DISPLAY_NETID:
194 continue;
195 default:
196 kfree(xprt->address_strings[i]);
197 }
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400198}
199
200static void
201xprt_rdma_connect_worker(struct work_struct *work)
202{
203 struct rpcrdma_xprt *r_xprt =
204 container_of(work, struct rpcrdma_xprt, rdma_connect.work);
205 struct rpc_xprt *xprt = &r_xprt->xprt;
206 int rc = 0;
207
Trond Myklebustd19751e2012-09-11 17:21:25 -0400208 current->flags |= PF_FSTRANS;
209 xprt_clear_connected(xprt);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400210
Trond Myklebustd19751e2012-09-11 17:21:25 -0400211 dprintk("RPC: %s: %sconnect\n", __func__,
212 r_xprt->rx_ep.rep_connected != 0 ? "re" : "");
213 rc = rpcrdma_ep_connect(&r_xprt->rx_ep, &r_xprt->rx_ia);
214 if (rc)
215 xprt_wake_pending_tasks(xprt, rc);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400216
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400217 dprintk("RPC: %s: exit\n", __func__);
218 xprt_clear_connecting(xprt);
Jeff Layton5cf02d02012-07-23 13:58:51 -0400219 current->flags &= ~PF_FSTRANS;
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400220}
221
222/*
223 * xprt_rdma_destroy
224 *
225 * Destroy the xprt.
226 * Free all memory associated with the object, including its own.
227 * NOTE: none of the *destroy methods free memory for their top-level
228 * objects, even though they may have allocated it (they do free
229 * private memory). It's up to the caller to handle it. In this
230 * case (RDMA transport), all structure memory is inlined with the
231 * struct rpcrdma_xprt.
232 */
233static void
234xprt_rdma_destroy(struct rpc_xprt *xprt)
235{
236 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400237
238 dprintk("RPC: %s: called\n", __func__);
239
Tejun Heoa25e7582010-10-15 17:49:27 +0200240 cancel_delayed_work_sync(&r_xprt->rdma_connect);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400241
242 xprt_clear_connected(xprt);
243
244 rpcrdma_buffer_destroy(&r_xprt->rx_buf);
Chuck Lever7f1d5412014-05-28 10:33:16 -0400245 rpcrdma_ep_destroy(&r_xprt->rx_ep, &r_xprt->rx_ia);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400246 rpcrdma_ia_close(&r_xprt->rx_ia);
247
248 xprt_rdma_free_addresses(xprt);
249
Pavel Emelyanove204e622010-09-29 16:03:13 +0400250 xprt_free(xprt);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400251
252 dprintk("RPC: %s: returning\n", __func__);
253
254 module_put(THIS_MODULE);
255}
256
Trond Myklebust2881ae72007-12-20 16:03:54 -0500257static const struct rpc_timeout xprt_rdma_default_timeout = {
258 .to_initval = 60 * HZ,
259 .to_maxval = 60 * HZ,
260};
261
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400262/**
263 * xprt_setup_rdma - Set up transport to use RDMA
264 *
265 * @args: rpc transport arguments
266 */
267static struct rpc_xprt *
268xprt_setup_rdma(struct xprt_create *args)
269{
270 struct rpcrdma_create_data_internal cdata;
271 struct rpc_xprt *xprt;
272 struct rpcrdma_xprt *new_xprt;
273 struct rpcrdma_ep *new_ep;
274 struct sockaddr_in *sin;
275 int rc;
276
277 if (args->addrlen > sizeof(xprt->addr)) {
278 dprintk("RPC: %s: address too large\n", __func__);
279 return ERR_PTR(-EBADF);
280 }
281
Pavel Emelyanov37aa2132010-09-29 16:05:43 +0400282 xprt = xprt_alloc(args->net, sizeof(struct rpcrdma_xprt),
Trond Myklebustd9ba1312011-07-17 18:11:30 -0400283 xprt_rdma_slot_table_entries,
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +0400284 xprt_rdma_slot_table_entries);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400285 if (xprt == NULL) {
286 dprintk("RPC: %s: couldn't allocate rpcrdma_xprt\n",
287 __func__);
288 return ERR_PTR(-ENOMEM);
289 }
290
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400291 /* 60 second timeout, no retries */
Trond Myklebustba7392b2007-12-20 16:03:55 -0500292 xprt->timeout = &xprt_rdma_default_timeout;
Chuck Leverbfaee092014-05-28 10:34:32 -0400293 xprt->bind_timeout = RPCRDMA_BIND_TO;
294 xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
295 xprt->idle_timeout = RPCRDMA_IDLE_DISC_TO;
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400296
297 xprt->resvport = 0; /* privileged port not needed */
298 xprt->tsh_size = 0; /* RPC-RDMA handles framing */
299 xprt->max_payload = RPCRDMA_MAX_DATA_SEGS * PAGE_SIZE;
300 xprt->ops = &xprt_rdma_procs;
301
302 /*
303 * Set up RDMA-specific connect data.
304 */
305
306 /* Put server RDMA address in local cdata */
307 memcpy(&cdata.addr, args->dstaddr, args->addrlen);
308
309 /* Ensure xprt->addr holds valid server TCP (not RDMA)
310 * address, for any side protocols which peek at it */
311 xprt->prot = IPPROTO_TCP;
312 xprt->addrlen = args->addrlen;
313 memcpy(&xprt->addr, &cdata.addr, xprt->addrlen);
314
315 sin = (struct sockaddr_in *)&cdata.addr;
316 if (ntohs(sin->sin_port) != 0)
317 xprt_set_bound(xprt);
318
Harvey Harrison21454aa2008-10-31 00:54:56 -0700319 dprintk("RPC: %s: %pI4:%u\n",
320 __func__, &sin->sin_addr.s_addr, ntohs(sin->sin_port));
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400321
322 /* Set max requests */
323 cdata.max_requests = xprt->max_reqs;
324
325 /* Set some length limits */
326 cdata.rsize = RPCRDMA_MAX_SEGS * PAGE_SIZE; /* RDMA write max */
327 cdata.wsize = RPCRDMA_MAX_SEGS * PAGE_SIZE; /* RDMA read max */
328
329 cdata.inline_wsize = xprt_rdma_max_inline_write;
330 if (cdata.inline_wsize > cdata.wsize)
331 cdata.inline_wsize = cdata.wsize;
332
333 cdata.inline_rsize = xprt_rdma_max_inline_read;
334 if (cdata.inline_rsize > cdata.rsize)
335 cdata.inline_rsize = cdata.rsize;
336
337 cdata.padding = xprt_rdma_inline_write_padding;
338
339 /*
340 * Create new transport instance, which includes initialized
341 * o ia
342 * o endpoint
343 * o buffers
344 */
345
346 new_xprt = rpcx_to_rdmax(xprt);
347
348 rc = rpcrdma_ia_open(new_xprt, (struct sockaddr *) &cdata.addr,
349 xprt_rdma_memreg_strategy);
350 if (rc)
351 goto out1;
352
353 /*
354 * initialize and create ep
355 */
356 new_xprt->rx_data = cdata;
357 new_ep = &new_xprt->rx_ep;
358 new_ep->rep_remote_addr = cdata.addr;
359
360 rc = rpcrdma_ep_create(&new_xprt->rx_ep,
361 &new_xprt->rx_ia, &new_xprt->rx_data);
362 if (rc)
363 goto out2;
364
365 /*
366 * Allocate pre-registered send and receive buffers for headers and
367 * any inline data. Also specify any padding which will be provided
368 * from a preregistered zero buffer.
369 */
370 rc = rpcrdma_buffer_create(&new_xprt->rx_buf, new_ep, &new_xprt->rx_ia,
371 &new_xprt->rx_data);
372 if (rc)
373 goto out3;
374
375 /*
376 * Register a callback for connection events. This is necessary because
377 * connection loss notification is async. We also catch connection loss
378 * when reaping receives.
379 */
380 INIT_DELAYED_WORK(&new_xprt->rdma_connect, xprt_rdma_connect_worker);
381 new_ep->rep_func = rpcrdma_conn_func;
382 new_ep->rep_xprt = xprt;
383
384 xprt_rdma_format_addresses(xprt);
385
386 if (!try_module_get(THIS_MODULE))
387 goto out4;
388
389 return xprt;
390
391out4:
392 xprt_rdma_free_addresses(xprt);
393 rc = -EINVAL;
394out3:
Chuck Lever7f1d5412014-05-28 10:33:16 -0400395 rpcrdma_ep_destroy(new_ep, &new_xprt->rx_ia);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400396out2:
397 rpcrdma_ia_close(&new_xprt->rx_ia);
398out1:
Pavel Emelyanove204e622010-09-29 16:03:13 +0400399 xprt_free(xprt);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400400 return ERR_PTR(rc);
401}
402
403/*
404 * Close a connection, during shutdown or timeout/reconnect
405 */
406static void
407xprt_rdma_close(struct rpc_xprt *xprt)
408{
409 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
410
411 dprintk("RPC: %s: closing\n", __func__);
Tom Talpey08ca0dc2008-10-10 11:32:34 -0400412 if (r_xprt->rx_ep.rep_connected > 0)
413 xprt->reestablish_timeout = 0;
Trond Myklebust62da3b22007-11-06 18:44:20 -0500414 xprt_disconnect_done(xprt);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400415 (void) rpcrdma_ep_disconnect(&r_xprt->rx_ep, &r_xprt->rx_ia);
416}
417
418static void
419xprt_rdma_set_port(struct rpc_xprt *xprt, u16 port)
420{
421 struct sockaddr_in *sap;
422
423 sap = (struct sockaddr_in *)&xprt->addr;
424 sap->sin_port = htons(port);
425 sap = (struct sockaddr_in *)&rpcx_to_rdmad(xprt).addr;
426 sap->sin_port = htons(port);
427 dprintk("RPC: %s: %u\n", __func__, port);
428}
429
430static void
Trond Myklebust1b092092013-01-08 09:26:49 -0500431xprt_rdma_connect(struct rpc_xprt *xprt, struct rpc_task *task)
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400432{
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400433 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
434
Trond Myklebust0b9e7942010-04-16 16:41:57 -0400435 if (r_xprt->rx_ep.rep_connected != 0) {
436 /* Reconnect */
437 schedule_delayed_work(&r_xprt->rdma_connect,
438 xprt->reestablish_timeout);
439 xprt->reestablish_timeout <<= 1;
Chuck Leverbfaee092014-05-28 10:34:32 -0400440 if (xprt->reestablish_timeout > RPCRDMA_MAX_REEST_TO)
441 xprt->reestablish_timeout = RPCRDMA_MAX_REEST_TO;
442 else if (xprt->reestablish_timeout < RPCRDMA_INIT_REEST_TO)
443 xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
Trond Myklebust0b9e7942010-04-16 16:41:57 -0400444 } else {
445 schedule_delayed_work(&r_xprt->rdma_connect, 0);
446 if (!RPC_IS_ASYNC(task))
Tejun Heoa25e7582010-10-15 17:49:27 +0200447 flush_delayed_work(&r_xprt->rdma_connect);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400448 }
449}
450
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400451/*
452 * The RDMA allocate/free functions need the task structure as a place
453 * to hide the struct rpcrdma_req, which is necessary for the actual send/recv
454 * sequence. For this reason, the recv buffers are attached to send
455 * buffers for portions of the RPC. Note that the RPC layer allocates
456 * both send and receive buffers in the same call. We may register
457 * the receive buffer portion when using reply chunks.
458 */
459static void *
460xprt_rdma_allocate(struct rpc_task *task, size_t size)
461{
Trond Myklebusta4f08352013-01-08 09:10:21 -0500462 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400463 struct rpcrdma_req *req, *nreq;
464
465 req = rpcrdma_buffer_get(&rpcx_to_rdmax(xprt)->rx_buf);
466 BUG_ON(NULL == req);
467
468 if (size > req->rl_size) {
469 dprintk("RPC: %s: size %zd too large for buffer[%zd]: "
470 "prog %d vers %d proc %d\n",
471 __func__, size, req->rl_size,
472 task->tk_client->cl_prog, task->tk_client->cl_vers,
473 task->tk_msg.rpc_proc->p_proc);
474 /*
475 * Outgoing length shortage. Our inline write max must have
476 * been configured to perform direct i/o.
477 *
478 * This is therefore a large metadata operation, and the
479 * allocate call was made on the maximum possible message,
480 * e.g. containing long filename(s) or symlink data. In
481 * fact, while these metadata operations *might* carry
482 * large outgoing payloads, they rarely *do*. However, we
483 * have to commit to the request here, so reallocate and
484 * register it now. The data path will never require this
485 * reallocation.
486 *
487 * If the allocation or registration fails, the RPC framework
488 * will (doggedly) retry.
489 */
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400490 if (task->tk_flags & RPC_TASK_SWAPPER)
491 nreq = kmalloc(sizeof *req + size, GFP_ATOMIC);
492 else
493 nreq = kmalloc(sizeof *req + size, GFP_NOFS);
494 if (nreq == NULL)
495 goto outfail;
496
497 if (rpcrdma_register_internal(&rpcx_to_rdmax(xprt)->rx_ia,
498 nreq->rl_base, size + sizeof(struct rpcrdma_req)
499 - offsetof(struct rpcrdma_req, rl_base),
500 &nreq->rl_handle, &nreq->rl_iov)) {
501 kfree(nreq);
502 goto outfail;
503 }
504 rpcx_to_rdmax(xprt)->rx_stats.hardway_register_count += size;
505 nreq->rl_size = size;
506 nreq->rl_niovs = 0;
507 nreq->rl_nchunks = 0;
508 nreq->rl_buffer = (struct rpcrdma_buffer *)req;
509 nreq->rl_reply = req->rl_reply;
510 memcpy(nreq->rl_segments,
511 req->rl_segments, sizeof nreq->rl_segments);
512 /* flag the swap with an unused field */
513 nreq->rl_iov.length = 0;
514 req->rl_reply = NULL;
515 req = nreq;
516 }
517 dprintk("RPC: %s: size %zd, request 0x%p\n", __func__, size, req);
Tom Talpey575448b2008-10-09 15:00:40 -0400518 req->rl_connect_cookie = 0; /* our reserved value */
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400519 return req->rl_xdr_buf;
520
521outfail:
522 rpcrdma_buffer_put(req);
523 rpcx_to_rdmax(xprt)->rx_stats.failed_marshal_count++;
524 return NULL;
525}
526
527/*
528 * This function returns all RDMA resources to the pool.
529 */
530static void
531xprt_rdma_free(void *buffer)
532{
533 struct rpcrdma_req *req;
534 struct rpcrdma_xprt *r_xprt;
535 struct rpcrdma_rep *rep;
536 int i;
537
538 if (buffer == NULL)
539 return;
540
541 req = container_of(buffer, struct rpcrdma_req, rl_xdr_buf[0]);
Tom Talpeyee1a2c52008-02-27 15:04:26 -0500542 if (req->rl_iov.length == 0) { /* see allocate above */
543 r_xprt = container_of(((struct rpcrdma_req *) req->rl_buffer)->rl_buffer,
544 struct rpcrdma_xprt, rx_buf);
545 } else
546 r_xprt = container_of(req->rl_buffer, struct rpcrdma_xprt, rx_buf);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400547 rep = req->rl_reply;
548
549 dprintk("RPC: %s: called on 0x%p%s\n",
550 __func__, rep, (rep && rep->rr_func) ? " (with waiter)" : "");
551
552 /*
Chuck Leverb45ccfd2014-05-28 10:32:34 -0400553 * Finish the deregistration. The process is considered
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400554 * complete when the rr_func vector becomes NULL - this
555 * was put in place during rpcrdma_reply_handler() - the wait
556 * call below will not block if the dereg is "done". If
557 * interrupted, our framework will clean up.
558 */
559 for (i = 0; req->rl_nchunks;) {
560 --req->rl_nchunks;
561 i += rpcrdma_deregister_external(
Chuck Lever13c9ff82014-05-28 10:33:08 -0400562 &req->rl_segments[i], r_xprt);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400563 }
564
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400565 if (req->rl_iov.length == 0) { /* see allocate above */
566 struct rpcrdma_req *oreq = (struct rpcrdma_req *)req->rl_buffer;
567 oreq->rl_reply = req->rl_reply;
568 (void) rpcrdma_deregister_internal(&r_xprt->rx_ia,
569 req->rl_handle,
570 &req->rl_iov);
571 kfree(req);
572 req = oreq;
573 }
574
575 /* Put back request+reply buffers */
576 rpcrdma_buffer_put(req);
577}
578
579/*
580 * send_request invokes the meat of RPC RDMA. It must do the following:
581 * 1. Marshal the RPC request into an RPC RDMA request, which means
582 * putting a header in front of data, and creating IOVs for RDMA
583 * from those in the request.
584 * 2. In marshaling, detect opportunities for RDMA, and use them.
585 * 3. Post a recv message to set up asynch completion, then send
586 * the request (rpcrdma_ep_post).
587 * 4. No partial sends are possible in the RPC-RDMA protocol (as in UDP).
588 */
589
590static int
591xprt_rdma_send_request(struct rpc_task *task)
592{
593 struct rpc_rqst *rqst = task->tk_rqstp;
Trond Myklebusta4f08352013-01-08 09:10:21 -0500594 struct rpc_xprt *xprt = rqst->rq_xprt;
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400595 struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
596 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
597
598 /* marshal the send itself */
599 if (req->rl_niovs == 0 && rpcrdma_marshal_req(rqst) != 0) {
600 r_xprt->rx_stats.failed_marshal_count++;
601 dprintk("RPC: %s: rpcrdma_marshal_req failed\n",
602 __func__);
603 return -EIO;
604 }
605
606 if (req->rl_reply == NULL) /* e.g. reconnection */
607 rpcrdma_recv_buffer_get(req);
608
609 if (req->rl_reply) {
610 req->rl_reply->rr_func = rpcrdma_reply_handler;
611 /* this need only be done once, but... */
612 req->rl_reply->rr_xprt = xprt;
613 }
614
Tom Talpey575448b2008-10-09 15:00:40 -0400615 /* Must suppress retransmit to maintain credits */
616 if (req->rl_connect_cookie == xprt->connect_cookie)
617 goto drop_connection;
618 req->rl_connect_cookie = xprt->connect_cookie;
619
620 if (rpcrdma_ep_post(&r_xprt->rx_ia, &r_xprt->rx_ep, req))
621 goto drop_connection;
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400622
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400623 rqst->rq_xmit_bytes_sent += rqst->rq_snd_buf.len;
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400624 rqst->rq_bytes_sent = 0;
625 return 0;
Tom Talpey575448b2008-10-09 15:00:40 -0400626
627drop_connection:
628 xprt_disconnect_done(xprt);
629 return -ENOTCONN; /* implies disconnect */
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400630}
631
632static void xprt_rdma_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
633{
634 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
635 long idle_time = 0;
636
637 if (xprt_connected(xprt))
638 idle_time = (long)(jiffies - xprt->last_used) / HZ;
639
640 seq_printf(seq,
641 "\txprt:\trdma %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu "
642 "%lu %lu %lu %Lu %Lu %Lu %Lu %lu %lu %lu\n",
643
644 0, /* need a local port? */
645 xprt->stat.bind_count,
646 xprt->stat.connect_count,
647 xprt->stat.connect_time,
648 idle_time,
649 xprt->stat.sends,
650 xprt->stat.recvs,
651 xprt->stat.bad_xids,
652 xprt->stat.req_u,
653 xprt->stat.bklog_u,
654
655 r_xprt->rx_stats.read_chunk_count,
656 r_xprt->rx_stats.write_chunk_count,
657 r_xprt->rx_stats.reply_chunk_count,
658 r_xprt->rx_stats.total_rdma_request,
659 r_xprt->rx_stats.total_rdma_reply,
660 r_xprt->rx_stats.pullup_copy_count,
661 r_xprt->rx_stats.fixup_copy_count,
662 r_xprt->rx_stats.hardway_register_count,
663 r_xprt->rx_stats.failed_marshal_count,
664 r_xprt->rx_stats.bad_reply_count);
665}
666
667/*
668 * Plumbing for rpc transport switch and kernel module
669 */
670
671static struct rpc_xprt_ops xprt_rdma_procs = {
Chuck Levere7ce7102014-05-28 10:34:57 -0400672 .reserve_xprt = xprt_reserve_xprt_cong,
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400673 .release_xprt = xprt_release_xprt_cong, /* sunrpc/xprt.c */
Trond Myklebustf39c1bf2012-09-07 11:08:50 -0400674 .alloc_slot = xprt_alloc_slot,
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400675 .release_request = xprt_release_rqst_cong, /* ditto */
676 .set_retrans_timeout = xprt_set_retrans_timeout_def, /* ditto */
677 .rpcbind = rpcb_getport_async, /* sunrpc/rpcb_clnt.c */
678 .set_port = xprt_rdma_set_port,
679 .connect = xprt_rdma_connect,
680 .buf_alloc = xprt_rdma_allocate,
681 .buf_free = xprt_rdma_free,
682 .send_request = xprt_rdma_send_request,
683 .close = xprt_rdma_close,
684 .destroy = xprt_rdma_destroy,
685 .print_stats = xprt_rdma_print_stats
686};
687
688static struct xprt_class xprt_rdma = {
689 .list = LIST_HEAD_INIT(xprt_rdma.list),
690 .name = "rdma",
691 .owner = THIS_MODULE,
692 .ident = XPRT_TRANSPORT_RDMA,
693 .setup = xprt_setup_rdma,
694};
695
696static void __exit xprt_rdma_cleanup(void)
697{
698 int rc;
699
Chuck Lever3a0799a2014-03-12 12:51:39 -0400700 dprintk("RPCRDMA Module Removed, deregister RPC RDMA transport\n");
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400701#ifdef RPC_DEBUG
702 if (sunrpc_table_header) {
703 unregister_sysctl_table(sunrpc_table_header);
704 sunrpc_table_header = NULL;
705 }
706#endif
707 rc = xprt_unregister_transport(&xprt_rdma);
708 if (rc)
709 dprintk("RPC: %s: xprt_unregister returned %i\n",
710 __func__, rc);
711}
712
713static int __init xprt_rdma_init(void)
714{
715 int rc;
716
717 rc = xprt_register_transport(&xprt_rdma);
718
719 if (rc)
720 return rc;
721
Chuck Lever3a0799a2014-03-12 12:51:39 -0400722 dprintk("RPCRDMA Module Init, register RPC RDMA transport\n");
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400723
Chuck Lever3a0799a2014-03-12 12:51:39 -0400724 dprintk("Defaults:\n");
725 dprintk("\tSlots %d\n"
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400726 "\tMaxInlineRead %d\n\tMaxInlineWrite %d\n",
727 xprt_rdma_slot_table_entries,
728 xprt_rdma_max_inline_read, xprt_rdma_max_inline_write);
Chuck Lever3a0799a2014-03-12 12:51:39 -0400729 dprintk("\tPadding %d\n\tMemreg %d\n",
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400730 xprt_rdma_inline_write_padding, xprt_rdma_memreg_strategy);
731
732#ifdef RPC_DEBUG
733 if (!sunrpc_table_header)
734 sunrpc_table_header = register_sysctl_table(sunrpc_table);
735#endif
736 return 0;
737}
738
739module_init(xprt_rdma_init);
740module_exit(xprt_rdma_cleanup);