blob: 808b3c52427a28647a0a5724b0563c86adbe42e3 [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
Jeff Laytonf895b252014-11-17 16:58:04 -050058#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040059# 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;
Chuck Leverd5440e22014-11-08 20:14:53 -050076 int xprt_rdma_pad_optimize = 1;
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040077
Jeff Laytonf895b252014-11-17 16:58:04 -050078#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -040079
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{
Chuck Lever5abefb82015-01-21 11:02:37 -0500203 struct rpcrdma_xprt *r_xprt = container_of(work, struct rpcrdma_xprt,
204 rx_connect_worker.work);
205 struct rpc_xprt *xprt = &r_xprt->rx_xprt;
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400206 int rc = 0;
207
Trond Myklebustd19751e2012-09-11 17:21:25 -0400208 xprt_clear_connected(xprt);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400209
Trond Myklebustd19751e2012-09-11 17:21:25 -0400210 dprintk("RPC: %s: %sconnect\n", __func__,
211 r_xprt->rx_ep.rep_connected != 0 ? "re" : "");
212 rc = rpcrdma_ep_connect(&r_xprt->rx_ep, &r_xprt->rx_ia);
213 if (rc)
214 xprt_wake_pending_tasks(xprt, rc);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400215
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400216 dprintk("RPC: %s: exit\n", __func__);
217 xprt_clear_connecting(xprt);
218}
219
220/*
221 * xprt_rdma_destroy
222 *
223 * Destroy the xprt.
224 * Free all memory associated with the object, including its own.
225 * NOTE: none of the *destroy methods free memory for their top-level
226 * objects, even though they may have allocated it (they do free
227 * private memory). It's up to the caller to handle it. In this
228 * case (RDMA transport), all structure memory is inlined with the
229 * struct rpcrdma_xprt.
230 */
231static void
232xprt_rdma_destroy(struct rpc_xprt *xprt)
233{
234 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400235
236 dprintk("RPC: %s: called\n", __func__);
237
Chuck Lever5abefb82015-01-21 11:02:37 -0500238 cancel_delayed_work_sync(&r_xprt->rx_connect_worker);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400239
240 xprt_clear_connected(xprt);
241
242 rpcrdma_buffer_destroy(&r_xprt->rx_buf);
Chuck Lever7f1d5412014-05-28 10:33:16 -0400243 rpcrdma_ep_destroy(&r_xprt->rx_ep, &r_xprt->rx_ia);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400244 rpcrdma_ia_close(&r_xprt->rx_ia);
245
246 xprt_rdma_free_addresses(xprt);
247
Pavel Emelyanove204e622010-09-29 16:03:13 +0400248 xprt_free(xprt);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400249
250 dprintk("RPC: %s: returning\n", __func__);
251
252 module_put(THIS_MODULE);
253}
254
Trond Myklebust2881ae72007-12-20 16:03:54 -0500255static const struct rpc_timeout xprt_rdma_default_timeout = {
256 .to_initval = 60 * HZ,
257 .to_maxval = 60 * HZ,
258};
259
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400260/**
261 * xprt_setup_rdma - Set up transport to use RDMA
262 *
263 * @args: rpc transport arguments
264 */
265static struct rpc_xprt *
266xprt_setup_rdma(struct xprt_create *args)
267{
268 struct rpcrdma_create_data_internal cdata;
269 struct rpc_xprt *xprt;
270 struct rpcrdma_xprt *new_xprt;
271 struct rpcrdma_ep *new_ep;
272 struct sockaddr_in *sin;
273 int rc;
274
275 if (args->addrlen > sizeof(xprt->addr)) {
276 dprintk("RPC: %s: address too large\n", __func__);
277 return ERR_PTR(-EBADF);
278 }
279
Pavel Emelyanov37aa2132010-09-29 16:05:43 +0400280 xprt = xprt_alloc(args->net, sizeof(struct rpcrdma_xprt),
Trond Myklebustd9ba1312011-07-17 18:11:30 -0400281 xprt_rdma_slot_table_entries,
Pavel Emelyanovbd1722d2010-09-29 16:02:43 +0400282 xprt_rdma_slot_table_entries);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400283 if (xprt == NULL) {
284 dprintk("RPC: %s: couldn't allocate rpcrdma_xprt\n",
285 __func__);
286 return ERR_PTR(-ENOMEM);
287 }
288
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400289 /* 60 second timeout, no retries */
Trond Myklebustba7392b2007-12-20 16:03:55 -0500290 xprt->timeout = &xprt_rdma_default_timeout;
Chuck Leverbfaee092014-05-28 10:34:32 -0400291 xprt->bind_timeout = RPCRDMA_BIND_TO;
292 xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
293 xprt->idle_timeout = RPCRDMA_IDLE_DISC_TO;
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400294
295 xprt->resvport = 0; /* privileged port not needed */
296 xprt->tsh_size = 0; /* RPC-RDMA handles framing */
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400297 xprt->ops = &xprt_rdma_procs;
298
299 /*
300 * Set up RDMA-specific connect data.
301 */
302
303 /* Put server RDMA address in local cdata */
304 memcpy(&cdata.addr, args->dstaddr, args->addrlen);
305
306 /* Ensure xprt->addr holds valid server TCP (not RDMA)
307 * address, for any side protocols which peek at it */
308 xprt->prot = IPPROTO_TCP;
309 xprt->addrlen = args->addrlen;
310 memcpy(&xprt->addr, &cdata.addr, xprt->addrlen);
311
312 sin = (struct sockaddr_in *)&cdata.addr;
313 if (ntohs(sin->sin_port) != 0)
314 xprt_set_bound(xprt);
315
Harvey Harrison21454aa2008-10-31 00:54:56 -0700316 dprintk("RPC: %s: %pI4:%u\n",
317 __func__, &sin->sin_addr.s_addr, ntohs(sin->sin_port));
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400318
319 /* Set max requests */
320 cdata.max_requests = xprt->max_reqs;
321
322 /* Set some length limits */
323 cdata.rsize = RPCRDMA_MAX_SEGS * PAGE_SIZE; /* RDMA write max */
324 cdata.wsize = RPCRDMA_MAX_SEGS * PAGE_SIZE; /* RDMA read max */
325
326 cdata.inline_wsize = xprt_rdma_max_inline_write;
327 if (cdata.inline_wsize > cdata.wsize)
328 cdata.inline_wsize = cdata.wsize;
329
330 cdata.inline_rsize = xprt_rdma_max_inline_read;
331 if (cdata.inline_rsize > cdata.rsize)
332 cdata.inline_rsize = cdata.rsize;
333
334 cdata.padding = xprt_rdma_inline_write_padding;
335
336 /*
337 * Create new transport instance, which includes initialized
338 * o ia
339 * o endpoint
340 * o buffers
341 */
342
343 new_xprt = rpcx_to_rdmax(xprt);
344
345 rc = rpcrdma_ia_open(new_xprt, (struct sockaddr *) &cdata.addr,
346 xprt_rdma_memreg_strategy);
347 if (rc)
348 goto out1;
349
350 /*
351 * initialize and create ep
352 */
353 new_xprt->rx_data = cdata;
354 new_ep = &new_xprt->rx_ep;
355 new_ep->rep_remote_addr = cdata.addr;
356
357 rc = rpcrdma_ep_create(&new_xprt->rx_ep,
358 &new_xprt->rx_ia, &new_xprt->rx_data);
359 if (rc)
360 goto out2;
361
362 /*
363 * Allocate pre-registered send and receive buffers for headers and
364 * any inline data. Also specify any padding which will be provided
365 * from a preregistered zero buffer.
366 */
Chuck Leverac920d02015-01-21 11:03:44 -0500367 rc = rpcrdma_buffer_create(new_xprt);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400368 if (rc)
369 goto out3;
370
371 /*
372 * Register a callback for connection events. This is necessary because
373 * connection loss notification is async. We also catch connection loss
374 * when reaping receives.
375 */
Chuck Lever5abefb82015-01-21 11:02:37 -0500376 INIT_DELAYED_WORK(&new_xprt->rx_connect_worker,
377 xprt_rdma_connect_worker);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400378
379 xprt_rdma_format_addresses(xprt);
Chuck Lever43e95982014-07-29 17:23:34 -0400380 xprt->max_payload = rpcrdma_max_payload(new_xprt);
381 dprintk("RPC: %s: transport data payload maximum: %zu bytes\n",
382 __func__, xprt->max_payload);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400383
384 if (!try_module_get(THIS_MODULE))
385 goto out4;
386
387 return xprt;
388
389out4:
390 xprt_rdma_free_addresses(xprt);
391 rc = -EINVAL;
392out3:
Chuck Lever7f1d5412014-05-28 10:33:16 -0400393 rpcrdma_ep_destroy(new_ep, &new_xprt->rx_ia);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400394out2:
395 rpcrdma_ia_close(&new_xprt->rx_ia);
396out1:
Pavel Emelyanove204e622010-09-29 16:03:13 +0400397 xprt_free(xprt);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400398 return ERR_PTR(rc);
399}
400
401/*
402 * Close a connection, during shutdown or timeout/reconnect
403 */
404static void
405xprt_rdma_close(struct rpc_xprt *xprt)
406{
407 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
408
409 dprintk("RPC: %s: closing\n", __func__);
Tom Talpey08ca0dc2008-10-10 11:32:34 -0400410 if (r_xprt->rx_ep.rep_connected > 0)
411 xprt->reestablish_timeout = 0;
Trond Myklebust62da3b22007-11-06 18:44:20 -0500412 xprt_disconnect_done(xprt);
Chuck Lever282191c2014-07-29 17:25:55 -0400413 rpcrdma_ep_disconnect(&r_xprt->rx_ep, &r_xprt->rx_ia);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400414}
415
416static void
417xprt_rdma_set_port(struct rpc_xprt *xprt, u16 port)
418{
419 struct sockaddr_in *sap;
420
421 sap = (struct sockaddr_in *)&xprt->addr;
422 sap->sin_port = htons(port);
423 sap = (struct sockaddr_in *)&rpcx_to_rdmad(xprt).addr;
424 sap->sin_port = htons(port);
425 dprintk("RPC: %s: %u\n", __func__, port);
426}
427
428static void
Trond Myklebust1b092092013-01-08 09:26:49 -0500429xprt_rdma_connect(struct rpc_xprt *xprt, struct rpc_task *task)
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400430{
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400431 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
432
Trond Myklebust0b9e7942010-04-16 16:41:57 -0400433 if (r_xprt->rx_ep.rep_connected != 0) {
434 /* Reconnect */
Chuck Lever5abefb82015-01-21 11:02:37 -0500435 schedule_delayed_work(&r_xprt->rx_connect_worker,
436 xprt->reestablish_timeout);
Trond Myklebust0b9e7942010-04-16 16:41:57 -0400437 xprt->reestablish_timeout <<= 1;
Chuck Leverbfaee092014-05-28 10:34:32 -0400438 if (xprt->reestablish_timeout > RPCRDMA_MAX_REEST_TO)
439 xprt->reestablish_timeout = RPCRDMA_MAX_REEST_TO;
440 else if (xprt->reestablish_timeout < RPCRDMA_INIT_REEST_TO)
441 xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
Trond Myklebust0b9e7942010-04-16 16:41:57 -0400442 } else {
Chuck Lever5abefb82015-01-21 11:02:37 -0500443 schedule_delayed_work(&r_xprt->rx_connect_worker, 0);
Trond Myklebust0b9e7942010-04-16 16:41:57 -0400444 if (!RPC_IS_ASYNC(task))
Chuck Lever5abefb82015-01-21 11:02:37 -0500445 flush_delayed_work(&r_xprt->rx_connect_worker);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400446 }
447}
448
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400449/*
450 * The RDMA allocate/free functions need the task structure as a place
451 * to hide the struct rpcrdma_req, which is necessary for the actual send/recv
452 * sequence. For this reason, the recv buffers are attached to send
453 * buffers for portions of the RPC. Note that the RPC layer allocates
454 * both send and receive buffers in the same call. We may register
455 * the receive buffer portion when using reply chunks.
456 */
457static void *
458xprt_rdma_allocate(struct rpc_task *task, size_t size)
459{
Trond Myklebusta4f08352013-01-08 09:10:21 -0500460 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400461 struct rpcrdma_req *req, *nreq;
462
463 req = rpcrdma_buffer_get(&rpcx_to_rdmax(xprt)->rx_buf);
Chuck Leverc977dea2014-05-28 10:35:06 -0400464 if (req == NULL)
465 return NULL;
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400466
467 if (size > req->rl_size) {
468 dprintk("RPC: %s: size %zd too large for buffer[%zd]: "
469 "prog %d vers %d proc %d\n",
470 __func__, size, req->rl_size,
471 task->tk_client->cl_prog, task->tk_client->cl_vers,
472 task->tk_msg.rpc_proc->p_proc);
473 /*
474 * Outgoing length shortage. Our inline write max must have
475 * been configured to perform direct i/o.
476 *
477 * This is therefore a large metadata operation, and the
478 * allocate call was made on the maximum possible message,
479 * e.g. containing long filename(s) or symlink data. In
480 * fact, while these metadata operations *might* carry
481 * large outgoing payloads, they rarely *do*. However, we
482 * have to commit to the request here, so reallocate and
483 * register it now. The data path will never require this
484 * reallocation.
485 *
486 * If the allocation or registration fails, the RPC framework
487 * will (doggedly) retry.
488 */
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400489 if (task->tk_flags & RPC_TASK_SWAPPER)
490 nreq = kmalloc(sizeof *req + size, GFP_ATOMIC);
491 else
492 nreq = kmalloc(sizeof *req + size, GFP_NOFS);
493 if (nreq == NULL)
494 goto outfail;
495
496 if (rpcrdma_register_internal(&rpcx_to_rdmax(xprt)->rx_ia,
497 nreq->rl_base, size + sizeof(struct rpcrdma_req)
498 - offsetof(struct rpcrdma_req, rl_base),
499 &nreq->rl_handle, &nreq->rl_iov)) {
500 kfree(nreq);
501 goto outfail;
502 }
503 rpcx_to_rdmax(xprt)->rx_stats.hardway_register_count += size;
504 nreq->rl_size = size;
505 nreq->rl_niovs = 0;
506 nreq->rl_nchunks = 0;
507 nreq->rl_buffer = (struct rpcrdma_buffer *)req;
508 nreq->rl_reply = req->rl_reply;
509 memcpy(nreq->rl_segments,
510 req->rl_segments, sizeof nreq->rl_segments);
511 /* flag the swap with an unused field */
512 nreq->rl_iov.length = 0;
513 req->rl_reply = NULL;
514 req = nreq;
515 }
516 dprintk("RPC: %s: size %zd, request 0x%p\n", __func__, size, req);
Tom Talpey575448b2008-10-09 15:00:40 -0400517 req->rl_connect_cookie = 0; /* our reserved value */
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400518 return req->rl_xdr_buf;
519
520outfail:
521 rpcrdma_buffer_put(req);
522 rpcx_to_rdmax(xprt)->rx_stats.failed_marshal_count++;
523 return NULL;
524}
525
526/*
527 * This function returns all RDMA resources to the pool.
528 */
529static void
530xprt_rdma_free(void *buffer)
531{
532 struct rpcrdma_req *req;
533 struct rpcrdma_xprt *r_xprt;
534 struct rpcrdma_rep *rep;
535 int i;
536
537 if (buffer == NULL)
538 return;
539
540 req = container_of(buffer, struct rpcrdma_req, rl_xdr_buf[0]);
Tom Talpeyee1a2c52008-02-27 15:04:26 -0500541 if (req->rl_iov.length == 0) { /* see allocate above */
542 r_xprt = container_of(((struct rpcrdma_req *) req->rl_buffer)->rl_buffer,
543 struct rpcrdma_xprt, rx_buf);
544 } else
545 r_xprt = container_of(req->rl_buffer, struct rpcrdma_xprt, rx_buf);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400546 rep = req->rl_reply;
547
548 dprintk("RPC: %s: called on 0x%p%s\n",
549 __func__, rep, (rep && rep->rr_func) ? " (with waiter)" : "");
550
551 /*
Chuck Leverb45ccfd2014-05-28 10:32:34 -0400552 * Finish the deregistration. The process is considered
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400553 * complete when the rr_func vector becomes NULL - this
554 * was put in place during rpcrdma_reply_handler() - the wait
555 * call below will not block if the dereg is "done". If
556 * interrupted, our framework will clean up.
557 */
558 for (i = 0; req->rl_nchunks;) {
559 --req->rl_nchunks;
560 i += rpcrdma_deregister_external(
Chuck Lever13c9ff82014-05-28 10:33:08 -0400561 &req->rl_segments[i], r_xprt);
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400562 }
563
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400564 if (req->rl_iov.length == 0) { /* see allocate above */
565 struct rpcrdma_req *oreq = (struct rpcrdma_req *)req->rl_buffer;
566 oreq->rl_reply = req->rl_reply;
567 (void) rpcrdma_deregister_internal(&r_xprt->rx_ia,
568 req->rl_handle,
569 &req->rl_iov);
570 kfree(req);
571 req = oreq;
572 }
573
574 /* Put back request+reply buffers */
575 rpcrdma_buffer_put(req);
576}
577
578/*
579 * send_request invokes the meat of RPC RDMA. It must do the following:
580 * 1. Marshal the RPC request into an RPC RDMA request, which means
581 * putting a header in front of data, and creating IOVs for RDMA
582 * from those in the request.
583 * 2. In marshaling, detect opportunities for RDMA, and use them.
584 * 3. Post a recv message to set up asynch completion, then send
585 * the request (rpcrdma_ep_post).
586 * 4. No partial sends are possible in the RPC-RDMA protocol (as in UDP).
587 */
588
589static int
590xprt_rdma_send_request(struct rpc_task *task)
591{
592 struct rpc_rqst *rqst = task->tk_rqstp;
Trond Myklebusta4f08352013-01-08 09:10:21 -0500593 struct rpc_xprt *xprt = rqst->rq_xprt;
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400594 struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
595 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
Chuck Lever6ab59942014-07-29 17:23:43 -0400596 int rc = 0;
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400597
Chuck Lever6ab59942014-07-29 17:23:43 -0400598 if (req->rl_niovs == 0)
Chuck Leverc93c6222014-05-28 10:35:14 -0400599 rc = rpcrdma_marshal_req(rqst);
Chuck Lever467c9672014-11-08 20:14:29 -0500600 else if (r_xprt->rx_ia.ri_memreg_strategy != RPCRDMA_ALLPHYSICAL)
Chuck Lever6ab59942014-07-29 17:23:43 -0400601 rc = rpcrdma_marshal_chunks(rqst, 0);
602 if (rc < 0)
603 goto failed_marshal;
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400604
605 if (req->rl_reply == NULL) /* e.g. reconnection */
606 rpcrdma_recv_buffer_get(req);
607
608 if (req->rl_reply) {
609 req->rl_reply->rr_func = rpcrdma_reply_handler;
610 /* this need only be done once, but... */
611 req->rl_reply->rr_xprt = xprt;
612 }
613
Tom Talpey575448b2008-10-09 15:00:40 -0400614 /* Must suppress retransmit to maintain credits */
615 if (req->rl_connect_cookie == xprt->connect_cookie)
616 goto drop_connection;
617 req->rl_connect_cookie = xprt->connect_cookie;
618
619 if (rpcrdma_ep_post(&r_xprt->rx_ia, &r_xprt->rx_ep, req))
620 goto drop_connection;
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400621
Trond Myklebustd60dbb22010-05-13 12:51:49 -0400622 rqst->rq_xmit_bytes_sent += rqst->rq_snd_buf.len;
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400623 rqst->rq_bytes_sent = 0;
624 return 0;
Tom Talpey575448b2008-10-09 15:00:40 -0400625
Chuck Leverc93c6222014-05-28 10:35:14 -0400626failed_marshal:
627 r_xprt->rx_stats.failed_marshal_count++;
628 dprintk("RPC: %s: rpcrdma_marshal_req failed, status %i\n",
629 __func__, rc);
630 if (rc == -EIO)
631 return -EIO;
Tom Talpey575448b2008-10-09 15:00:40 -0400632drop_connection:
633 xprt_disconnect_done(xprt);
634 return -ENOTCONN; /* implies disconnect */
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400635}
636
637static void xprt_rdma_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
638{
639 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
640 long idle_time = 0;
641
642 if (xprt_connected(xprt))
643 idle_time = (long)(jiffies - xprt->last_used) / HZ;
644
645 seq_printf(seq,
646 "\txprt:\trdma %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu "
647 "%lu %lu %lu %Lu %Lu %Lu %Lu %lu %lu %lu\n",
648
649 0, /* need a local port? */
650 xprt->stat.bind_count,
651 xprt->stat.connect_count,
652 xprt->stat.connect_time,
653 idle_time,
654 xprt->stat.sends,
655 xprt->stat.recvs,
656 xprt->stat.bad_xids,
657 xprt->stat.req_u,
658 xprt->stat.bklog_u,
659
660 r_xprt->rx_stats.read_chunk_count,
661 r_xprt->rx_stats.write_chunk_count,
662 r_xprt->rx_stats.reply_chunk_count,
663 r_xprt->rx_stats.total_rdma_request,
664 r_xprt->rx_stats.total_rdma_reply,
665 r_xprt->rx_stats.pullup_copy_count,
666 r_xprt->rx_stats.fixup_copy_count,
667 r_xprt->rx_stats.hardway_register_count,
668 r_xprt->rx_stats.failed_marshal_count,
669 r_xprt->rx_stats.bad_reply_count);
670}
671
672/*
673 * Plumbing for rpc transport switch and kernel module
674 */
675
676static struct rpc_xprt_ops xprt_rdma_procs = {
Chuck Levere7ce7102014-05-28 10:34:57 -0400677 .reserve_xprt = xprt_reserve_xprt_cong,
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400678 .release_xprt = xprt_release_xprt_cong, /* sunrpc/xprt.c */
Trond Myklebustf39c1bf2012-09-07 11:08:50 -0400679 .alloc_slot = xprt_alloc_slot,
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400680 .release_request = xprt_release_rqst_cong, /* ditto */
681 .set_retrans_timeout = xprt_set_retrans_timeout_def, /* ditto */
682 .rpcbind = rpcb_getport_async, /* sunrpc/rpcb_clnt.c */
683 .set_port = xprt_rdma_set_port,
684 .connect = xprt_rdma_connect,
685 .buf_alloc = xprt_rdma_allocate,
686 .buf_free = xprt_rdma_free,
687 .send_request = xprt_rdma_send_request,
688 .close = xprt_rdma_close,
689 .destroy = xprt_rdma_destroy,
690 .print_stats = xprt_rdma_print_stats
691};
692
693static struct xprt_class xprt_rdma = {
694 .list = LIST_HEAD_INIT(xprt_rdma.list),
695 .name = "rdma",
696 .owner = THIS_MODULE,
697 .ident = XPRT_TRANSPORT_RDMA,
698 .setup = xprt_setup_rdma,
699};
700
701static void __exit xprt_rdma_cleanup(void)
702{
703 int rc;
704
Chuck Lever3a0799a2014-03-12 12:51:39 -0400705 dprintk("RPCRDMA Module Removed, deregister RPC RDMA transport\n");
Jeff Laytonf895b252014-11-17 16:58:04 -0500706#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400707 if (sunrpc_table_header) {
708 unregister_sysctl_table(sunrpc_table_header);
709 sunrpc_table_header = NULL;
710 }
711#endif
712 rc = xprt_unregister_transport(&xprt_rdma);
713 if (rc)
714 dprintk("RPC: %s: xprt_unregister returned %i\n",
715 __func__, rc);
716}
717
718static int __init xprt_rdma_init(void)
719{
720 int rc;
721
722 rc = xprt_register_transport(&xprt_rdma);
723
724 if (rc)
725 return rc;
726
Chuck Lever3a0799a2014-03-12 12:51:39 -0400727 dprintk("RPCRDMA Module Init, register RPC RDMA transport\n");
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400728
Chuck Lever3a0799a2014-03-12 12:51:39 -0400729 dprintk("Defaults:\n");
730 dprintk("\tSlots %d\n"
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400731 "\tMaxInlineRead %d\n\tMaxInlineWrite %d\n",
732 xprt_rdma_slot_table_entries,
733 xprt_rdma_max_inline_read, xprt_rdma_max_inline_write);
Chuck Lever3a0799a2014-03-12 12:51:39 -0400734 dprintk("\tPadding %d\n\tMemreg %d\n",
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400735 xprt_rdma_inline_write_padding, xprt_rdma_memreg_strategy);
736
Jeff Laytonf895b252014-11-17 16:58:04 -0500737#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
\"Talpey, Thomas\f58851e2007-09-10 13:50:12 -0400738 if (!sunrpc_table_header)
739 sunrpc_table_header = register_sysctl_table(sunrpc_table);
740#endif
741 return 0;
742}
743
744module_init(xprt_rdma_init);
745module_exit(xprt_rdma_cleanup);