Commit | Line | Data |
---|---|---|
f58851e6 TT |
1 | /* |
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> | |
52 | #include <linux/seq_file.h> | |
53 | ||
54 | #include "xprt_rdma.h" | |
55 | ||
56 | #ifdef RPC_DEBUG | |
57 | # define RPCDBG_FACILITY RPCDBG_TRANS | |
58 | #endif | |
59 | ||
60 | MODULE_LICENSE("Dual BSD/GPL"); | |
61 | ||
62 | MODULE_DESCRIPTION("RPC/RDMA Transport for Linux kernel NFS"); | |
63 | MODULE_AUTHOR("Network Appliance, Inc."); | |
64 | ||
65 | /* | |
66 | * tunables | |
67 | */ | |
68 | ||
69 | static unsigned int xprt_rdma_slot_table_entries = RPCRDMA_DEF_SLOT_TABLE; | |
70 | static unsigned int xprt_rdma_max_inline_read = RPCRDMA_DEF_INLINE; | |
71 | static unsigned int xprt_rdma_max_inline_write = RPCRDMA_DEF_INLINE; | |
72 | static unsigned int xprt_rdma_inline_write_padding; | |
3197d309 | 73 | static unsigned int xprt_rdma_memreg_strategy = RPCRDMA_FRMR; |
9191ca3b | 74 | int xprt_rdma_pad_optimize = 0; |
f58851e6 TT |
75 | |
76 | #ifdef RPC_DEBUG | |
77 | ||
78 | static unsigned int min_slot_table_size = RPCRDMA_MIN_SLOT_TABLE; | |
79 | static unsigned int max_slot_table_size = RPCRDMA_MAX_SLOT_TABLE; | |
80 | static unsigned int zero; | |
81 | static unsigned int max_padding = PAGE_SIZE; | |
82 | static unsigned int min_memreg = RPCRDMA_BOUNCEBUFFERS; | |
83 | static unsigned int max_memreg = RPCRDMA_LAST - 1; | |
84 | ||
85 | static struct ctl_table_header *sunrpc_table_header; | |
86 | ||
87 | static ctl_table xr_tunables_table[] = { | |
88 | { | |
cfcb43ff | 89 | .ctl_name = CTL_UNNUMBERED, |
f58851e6 TT |
90 | .procname = "rdma_slot_table_entries", |
91 | .data = &xprt_rdma_slot_table_entries, | |
92 | .maxlen = sizeof(unsigned int), | |
93 | .mode = 0644, | |
94 | .proc_handler = &proc_dointvec_minmax, | |
95 | .strategy = &sysctl_intvec, | |
96 | .extra1 = &min_slot_table_size, | |
97 | .extra2 = &max_slot_table_size | |
98 | }, | |
99 | { | |
cfcb43ff | 100 | .ctl_name = CTL_UNNUMBERED, |
f58851e6 TT |
101 | .procname = "rdma_max_inline_read", |
102 | .data = &xprt_rdma_max_inline_read, | |
103 | .maxlen = sizeof(unsigned int), | |
104 | .mode = 0644, | |
105 | .proc_handler = &proc_dointvec, | |
106 | .strategy = &sysctl_intvec, | |
107 | }, | |
108 | { | |
cfcb43ff | 109 | .ctl_name = CTL_UNNUMBERED, |
f58851e6 TT |
110 | .procname = "rdma_max_inline_write", |
111 | .data = &xprt_rdma_max_inline_write, | |
112 | .maxlen = sizeof(unsigned int), | |
113 | .mode = 0644, | |
114 | .proc_handler = &proc_dointvec, | |
115 | .strategy = &sysctl_intvec, | |
116 | }, | |
117 | { | |
cfcb43ff | 118 | .ctl_name = CTL_UNNUMBERED, |
f58851e6 TT |
119 | .procname = "rdma_inline_write_padding", |
120 | .data = &xprt_rdma_inline_write_padding, | |
121 | .maxlen = sizeof(unsigned int), | |
122 | .mode = 0644, | |
123 | .proc_handler = &proc_dointvec_minmax, | |
124 | .strategy = &sysctl_intvec, | |
125 | .extra1 = &zero, | |
126 | .extra2 = &max_padding, | |
127 | }, | |
128 | { | |
cfcb43ff | 129 | .ctl_name = CTL_UNNUMBERED, |
f58851e6 TT |
130 | .procname = "rdma_memreg_strategy", |
131 | .data = &xprt_rdma_memreg_strategy, | |
132 | .maxlen = sizeof(unsigned int), | |
133 | .mode = 0644, | |
134 | .proc_handler = &proc_dointvec_minmax, | |
135 | .strategy = &sysctl_intvec, | |
136 | .extra1 = &min_memreg, | |
137 | .extra2 = &max_memreg, | |
138 | }, | |
9191ca3b TT |
139 | { |
140 | .ctl_name = CTL_UNNUMBERED, | |
141 | .procname = "rdma_pad_optimize", | |
142 | .data = &xprt_rdma_pad_optimize, | |
143 | .maxlen = sizeof(unsigned int), | |
144 | .mode = 0644, | |
145 | .proc_handler = &proc_dointvec, | |
146 | }, | |
f58851e6 TT |
147 | { |
148 | .ctl_name = 0, | |
149 | }, | |
150 | }; | |
151 | ||
152 | static ctl_table sunrpc_table[] = { | |
153 | { | |
154 | .ctl_name = CTL_SUNRPC, | |
155 | .procname = "sunrpc", | |
156 | .mode = 0555, | |
157 | .child = xr_tunables_table | |
158 | }, | |
159 | { | |
160 | .ctl_name = 0, | |
161 | }, | |
162 | }; | |
163 | ||
164 | #endif | |
165 | ||
166 | static struct rpc_xprt_ops xprt_rdma_procs; /* forward reference */ | |
167 | ||
168 | static void | |
169 | xprt_rdma_format_addresses(struct rpc_xprt *xprt) | |
170 | { | |
171 | struct sockaddr_in *addr = (struct sockaddr_in *) | |
172 | &rpcx_to_rdmad(xprt).addr; | |
173 | char *buf; | |
174 | ||
175 | buf = kzalloc(20, GFP_KERNEL); | |
176 | if (buf) | |
177 | snprintf(buf, 20, NIPQUAD_FMT, NIPQUAD(addr->sin_addr.s_addr)); | |
178 | xprt->address_strings[RPC_DISPLAY_ADDR] = buf; | |
179 | ||
180 | buf = kzalloc(8, GFP_KERNEL); | |
181 | if (buf) | |
182 | snprintf(buf, 8, "%u", ntohs(addr->sin_port)); | |
183 | xprt->address_strings[RPC_DISPLAY_PORT] = buf; | |
184 | ||
185 | xprt->address_strings[RPC_DISPLAY_PROTO] = "rdma"; | |
186 | ||
187 | buf = kzalloc(48, GFP_KERNEL); | |
188 | if (buf) | |
189 | snprintf(buf, 48, "addr="NIPQUAD_FMT" port=%u proto=%s", | |
190 | NIPQUAD(addr->sin_addr.s_addr), | |
191 | ntohs(addr->sin_port), "rdma"); | |
192 | xprt->address_strings[RPC_DISPLAY_ALL] = buf; | |
193 | ||
194 | buf = kzalloc(10, GFP_KERNEL); | |
195 | if (buf) | |
196 | snprintf(buf, 10, "%02x%02x%02x%02x", | |
197 | NIPQUAD(addr->sin_addr.s_addr)); | |
198 | xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf; | |
199 | ||
200 | buf = kzalloc(8, GFP_KERNEL); | |
201 | if (buf) | |
202 | snprintf(buf, 8, "%4hx", ntohs(addr->sin_port)); | |
203 | xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf; | |
204 | ||
205 | buf = kzalloc(30, GFP_KERNEL); | |
206 | if (buf) | |
207 | snprintf(buf, 30, NIPQUAD_FMT".%u.%u", | |
208 | NIPQUAD(addr->sin_addr.s_addr), | |
209 | ntohs(addr->sin_port) >> 8, | |
210 | ntohs(addr->sin_port) & 0xff); | |
211 | xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf; | |
212 | ||
213 | /* netid */ | |
214 | xprt->address_strings[RPC_DISPLAY_NETID] = "rdma"; | |
215 | } | |
216 | ||
217 | static void | |
218 | xprt_rdma_free_addresses(struct rpc_xprt *xprt) | |
219 | { | |
33e01dc7 CL |
220 | unsigned int i; |
221 | ||
222 | for (i = 0; i < RPC_DISPLAY_MAX; i++) | |
223 | switch (i) { | |
224 | case RPC_DISPLAY_PROTO: | |
225 | case RPC_DISPLAY_NETID: | |
226 | continue; | |
227 | default: | |
228 | kfree(xprt->address_strings[i]); | |
229 | } | |
f58851e6 TT |
230 | } |
231 | ||
232 | static void | |
233 | xprt_rdma_connect_worker(struct work_struct *work) | |
234 | { | |
235 | struct rpcrdma_xprt *r_xprt = | |
236 | container_of(work, struct rpcrdma_xprt, rdma_connect.work); | |
237 | struct rpc_xprt *xprt = &r_xprt->xprt; | |
238 | int rc = 0; | |
239 | ||
240 | if (!xprt->shutdown) { | |
241 | xprt_clear_connected(xprt); | |
242 | ||
243 | dprintk("RPC: %s: %sconnect\n", __func__, | |
244 | r_xprt->rx_ep.rep_connected != 0 ? "re" : ""); | |
245 | rc = rpcrdma_ep_connect(&r_xprt->rx_ep, &r_xprt->rx_ia); | |
246 | if (rc) | |
247 | goto out; | |
248 | } | |
249 | goto out_clear; | |
250 | ||
251 | out: | |
252 | xprt_wake_pending_tasks(xprt, rc); | |
253 | ||
254 | out_clear: | |
255 | dprintk("RPC: %s: exit\n", __func__); | |
256 | xprt_clear_connecting(xprt); | |
257 | } | |
258 | ||
259 | /* | |
260 | * xprt_rdma_destroy | |
261 | * | |
262 | * Destroy the xprt. | |
263 | * Free all memory associated with the object, including its own. | |
264 | * NOTE: none of the *destroy methods free memory for their top-level | |
265 | * objects, even though they may have allocated it (they do free | |
266 | * private memory). It's up to the caller to handle it. In this | |
267 | * case (RDMA transport), all structure memory is inlined with the | |
268 | * struct rpcrdma_xprt. | |
269 | */ | |
270 | static void | |
271 | xprt_rdma_destroy(struct rpc_xprt *xprt) | |
272 | { | |
273 | struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt); | |
274 | int rc; | |
275 | ||
276 | dprintk("RPC: %s: called\n", __func__); | |
277 | ||
278 | cancel_delayed_work(&r_xprt->rdma_connect); | |
279 | flush_scheduled_work(); | |
280 | ||
281 | xprt_clear_connected(xprt); | |
282 | ||
283 | rpcrdma_buffer_destroy(&r_xprt->rx_buf); | |
284 | rc = rpcrdma_ep_destroy(&r_xprt->rx_ep, &r_xprt->rx_ia); | |
285 | if (rc) | |
286 | dprintk("RPC: %s: rpcrdma_ep_destroy returned %i\n", | |
287 | __func__, rc); | |
288 | rpcrdma_ia_close(&r_xprt->rx_ia); | |
289 | ||
290 | xprt_rdma_free_addresses(xprt); | |
291 | ||
292 | kfree(xprt->slot); | |
293 | xprt->slot = NULL; | |
294 | kfree(xprt); | |
295 | ||
296 | dprintk("RPC: %s: returning\n", __func__); | |
297 | ||
298 | module_put(THIS_MODULE); | |
299 | } | |
300 | ||
2881ae74 TM |
301 | static const struct rpc_timeout xprt_rdma_default_timeout = { |
302 | .to_initval = 60 * HZ, | |
303 | .to_maxval = 60 * HZ, | |
304 | }; | |
305 | ||
f58851e6 TT |
306 | /** |
307 | * xprt_setup_rdma - Set up transport to use RDMA | |
308 | * | |
309 | * @args: rpc transport arguments | |
310 | */ | |
311 | static struct rpc_xprt * | |
312 | xprt_setup_rdma(struct xprt_create *args) | |
313 | { | |
314 | struct rpcrdma_create_data_internal cdata; | |
315 | struct rpc_xprt *xprt; | |
316 | struct rpcrdma_xprt *new_xprt; | |
317 | struct rpcrdma_ep *new_ep; | |
318 | struct sockaddr_in *sin; | |
319 | int rc; | |
320 | ||
321 | if (args->addrlen > sizeof(xprt->addr)) { | |
322 | dprintk("RPC: %s: address too large\n", __func__); | |
323 | return ERR_PTR(-EBADF); | |
324 | } | |
325 | ||
326 | xprt = kzalloc(sizeof(struct rpcrdma_xprt), GFP_KERNEL); | |
327 | if (xprt == NULL) { | |
328 | dprintk("RPC: %s: couldn't allocate rpcrdma_xprt\n", | |
329 | __func__); | |
330 | return ERR_PTR(-ENOMEM); | |
331 | } | |
332 | ||
333 | xprt->max_reqs = xprt_rdma_slot_table_entries; | |
334 | xprt->slot = kcalloc(xprt->max_reqs, | |
335 | sizeof(struct rpc_rqst), GFP_KERNEL); | |
336 | if (xprt->slot == NULL) { | |
f58851e6 TT |
337 | dprintk("RPC: %s: couldn't allocate %d slots\n", |
338 | __func__, xprt->max_reqs); | |
d5cd9787 | 339 | kfree(xprt); |
f58851e6 TT |
340 | return ERR_PTR(-ENOMEM); |
341 | } | |
342 | ||
343 | /* 60 second timeout, no retries */ | |
ba7392bb | 344 | xprt->timeout = &xprt_rdma_default_timeout; |
f58851e6 TT |
345 | xprt->bind_timeout = (60U * HZ); |
346 | xprt->connect_timeout = (60U * HZ); | |
347 | xprt->reestablish_timeout = (5U * HZ); | |
348 | xprt->idle_timeout = (5U * 60 * HZ); | |
349 | ||
350 | xprt->resvport = 0; /* privileged port not needed */ | |
351 | xprt->tsh_size = 0; /* RPC-RDMA handles framing */ | |
352 | xprt->max_payload = RPCRDMA_MAX_DATA_SEGS * PAGE_SIZE; | |
353 | xprt->ops = &xprt_rdma_procs; | |
354 | ||
355 | /* | |
356 | * Set up RDMA-specific connect data. | |
357 | */ | |
358 | ||
359 | /* Put server RDMA address in local cdata */ | |
360 | memcpy(&cdata.addr, args->dstaddr, args->addrlen); | |
361 | ||
362 | /* Ensure xprt->addr holds valid server TCP (not RDMA) | |
363 | * address, for any side protocols which peek at it */ | |
364 | xprt->prot = IPPROTO_TCP; | |
365 | xprt->addrlen = args->addrlen; | |
366 | memcpy(&xprt->addr, &cdata.addr, xprt->addrlen); | |
367 | ||
368 | sin = (struct sockaddr_in *)&cdata.addr; | |
369 | if (ntohs(sin->sin_port) != 0) | |
370 | xprt_set_bound(xprt); | |
371 | ||
372 | dprintk("RPC: %s: %u.%u.%u.%u:%u\n", __func__, | |
373 | NIPQUAD(sin->sin_addr.s_addr), ntohs(sin->sin_port)); | |
374 | ||
375 | /* Set max requests */ | |
376 | cdata.max_requests = xprt->max_reqs; | |
377 | ||
378 | /* Set some length limits */ | |
379 | cdata.rsize = RPCRDMA_MAX_SEGS * PAGE_SIZE; /* RDMA write max */ | |
380 | cdata.wsize = RPCRDMA_MAX_SEGS * PAGE_SIZE; /* RDMA read max */ | |
381 | ||
382 | cdata.inline_wsize = xprt_rdma_max_inline_write; | |
383 | if (cdata.inline_wsize > cdata.wsize) | |
384 | cdata.inline_wsize = cdata.wsize; | |
385 | ||
386 | cdata.inline_rsize = xprt_rdma_max_inline_read; | |
387 | if (cdata.inline_rsize > cdata.rsize) | |
388 | cdata.inline_rsize = cdata.rsize; | |
389 | ||
390 | cdata.padding = xprt_rdma_inline_write_padding; | |
391 | ||
392 | /* | |
393 | * Create new transport instance, which includes initialized | |
394 | * o ia | |
395 | * o endpoint | |
396 | * o buffers | |
397 | */ | |
398 | ||
399 | new_xprt = rpcx_to_rdmax(xprt); | |
400 | ||
401 | rc = rpcrdma_ia_open(new_xprt, (struct sockaddr *) &cdata.addr, | |
402 | xprt_rdma_memreg_strategy); | |
403 | if (rc) | |
404 | goto out1; | |
405 | ||
406 | /* | |
407 | * initialize and create ep | |
408 | */ | |
409 | new_xprt->rx_data = cdata; | |
410 | new_ep = &new_xprt->rx_ep; | |
411 | new_ep->rep_remote_addr = cdata.addr; | |
412 | ||
413 | rc = rpcrdma_ep_create(&new_xprt->rx_ep, | |
414 | &new_xprt->rx_ia, &new_xprt->rx_data); | |
415 | if (rc) | |
416 | goto out2; | |
417 | ||
418 | /* | |
419 | * Allocate pre-registered send and receive buffers for headers and | |
420 | * any inline data. Also specify any padding which will be provided | |
421 | * from a preregistered zero buffer. | |
422 | */ | |
423 | rc = rpcrdma_buffer_create(&new_xprt->rx_buf, new_ep, &new_xprt->rx_ia, | |
424 | &new_xprt->rx_data); | |
425 | if (rc) | |
426 | goto out3; | |
427 | ||
428 | /* | |
429 | * Register a callback for connection events. This is necessary because | |
430 | * connection loss notification is async. We also catch connection loss | |
431 | * when reaping receives. | |
432 | */ | |
433 | INIT_DELAYED_WORK(&new_xprt->rdma_connect, xprt_rdma_connect_worker); | |
434 | new_ep->rep_func = rpcrdma_conn_func; | |
435 | new_ep->rep_xprt = xprt; | |
436 | ||
437 | xprt_rdma_format_addresses(xprt); | |
438 | ||
439 | if (!try_module_get(THIS_MODULE)) | |
440 | goto out4; | |
441 | ||
442 | return xprt; | |
443 | ||
444 | out4: | |
445 | xprt_rdma_free_addresses(xprt); | |
446 | rc = -EINVAL; | |
447 | out3: | |
448 | (void) rpcrdma_ep_destroy(new_ep, &new_xprt->rx_ia); | |
449 | out2: | |
450 | rpcrdma_ia_close(&new_xprt->rx_ia); | |
451 | out1: | |
452 | kfree(xprt->slot); | |
453 | kfree(xprt); | |
454 | return ERR_PTR(rc); | |
455 | } | |
456 | ||
457 | /* | |
458 | * Close a connection, during shutdown or timeout/reconnect | |
459 | */ | |
460 | static void | |
461 | xprt_rdma_close(struct rpc_xprt *xprt) | |
462 | { | |
463 | struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt); | |
464 | ||
465 | dprintk("RPC: %s: closing\n", __func__); | |
62da3b24 | 466 | xprt_disconnect_done(xprt); |
f58851e6 TT |
467 | (void) rpcrdma_ep_disconnect(&r_xprt->rx_ep, &r_xprt->rx_ia); |
468 | } | |
469 | ||
470 | static void | |
471 | xprt_rdma_set_port(struct rpc_xprt *xprt, u16 port) | |
472 | { | |
473 | struct sockaddr_in *sap; | |
474 | ||
475 | sap = (struct sockaddr_in *)&xprt->addr; | |
476 | sap->sin_port = htons(port); | |
477 | sap = (struct sockaddr_in *)&rpcx_to_rdmad(xprt).addr; | |
478 | sap->sin_port = htons(port); | |
479 | dprintk("RPC: %s: %u\n", __func__, port); | |
480 | } | |
481 | ||
482 | static void | |
483 | xprt_rdma_connect(struct rpc_task *task) | |
484 | { | |
485 | struct rpc_xprt *xprt = (struct rpc_xprt *)task->tk_xprt; | |
486 | struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt); | |
487 | ||
488 | if (!xprt_test_and_set_connecting(xprt)) { | |
489 | if (r_xprt->rx_ep.rep_connected != 0) { | |
490 | /* Reconnect */ | |
491 | schedule_delayed_work(&r_xprt->rdma_connect, | |
492 | xprt->reestablish_timeout); | |
493 | } else { | |
494 | schedule_delayed_work(&r_xprt->rdma_connect, 0); | |
495 | if (!RPC_IS_ASYNC(task)) | |
496 | flush_scheduled_work(); | |
497 | } | |
498 | } | |
499 | } | |
500 | ||
501 | static int | |
502 | xprt_rdma_reserve_xprt(struct rpc_task *task) | |
503 | { | |
504 | struct rpc_xprt *xprt = task->tk_xprt; | |
505 | struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt); | |
506 | int credits = atomic_read(&r_xprt->rx_buf.rb_credits); | |
507 | ||
508 | /* == RPC_CWNDSCALE @ init, but *after* setup */ | |
509 | if (r_xprt->rx_buf.rb_cwndscale == 0UL) { | |
510 | r_xprt->rx_buf.rb_cwndscale = xprt->cwnd; | |
511 | dprintk("RPC: %s: cwndscale %lu\n", __func__, | |
512 | r_xprt->rx_buf.rb_cwndscale); | |
513 | BUG_ON(r_xprt->rx_buf.rb_cwndscale <= 0); | |
514 | } | |
515 | xprt->cwnd = credits * r_xprt->rx_buf.rb_cwndscale; | |
516 | return xprt_reserve_xprt_cong(task); | |
517 | } | |
518 | ||
519 | /* | |
520 | * The RDMA allocate/free functions need the task structure as a place | |
521 | * to hide the struct rpcrdma_req, which is necessary for the actual send/recv | |
522 | * sequence. For this reason, the recv buffers are attached to send | |
523 | * buffers for portions of the RPC. Note that the RPC layer allocates | |
524 | * both send and receive buffers in the same call. We may register | |
525 | * the receive buffer portion when using reply chunks. | |
526 | */ | |
527 | static void * | |
528 | xprt_rdma_allocate(struct rpc_task *task, size_t size) | |
529 | { | |
530 | struct rpc_xprt *xprt = task->tk_xprt; | |
531 | struct rpcrdma_req *req, *nreq; | |
532 | ||
533 | req = rpcrdma_buffer_get(&rpcx_to_rdmax(xprt)->rx_buf); | |
534 | BUG_ON(NULL == req); | |
535 | ||
536 | if (size > req->rl_size) { | |
537 | dprintk("RPC: %s: size %zd too large for buffer[%zd]: " | |
538 | "prog %d vers %d proc %d\n", | |
539 | __func__, size, req->rl_size, | |
540 | task->tk_client->cl_prog, task->tk_client->cl_vers, | |
541 | task->tk_msg.rpc_proc->p_proc); | |
542 | /* | |
543 | * Outgoing length shortage. Our inline write max must have | |
544 | * been configured to perform direct i/o. | |
545 | * | |
546 | * This is therefore a large metadata operation, and the | |
547 | * allocate call was made on the maximum possible message, | |
548 | * e.g. containing long filename(s) or symlink data. In | |
549 | * fact, while these metadata operations *might* carry | |
550 | * large outgoing payloads, they rarely *do*. However, we | |
551 | * have to commit to the request here, so reallocate and | |
552 | * register it now. The data path will never require this | |
553 | * reallocation. | |
554 | * | |
555 | * If the allocation or registration fails, the RPC framework | |
556 | * will (doggedly) retry. | |
557 | */ | |
558 | if (rpcx_to_rdmax(xprt)->rx_ia.ri_memreg_strategy == | |
559 | RPCRDMA_BOUNCEBUFFERS) { | |
560 | /* forced to "pure inline" */ | |
561 | dprintk("RPC: %s: too much data (%zd) for inline " | |
562 | "(r/w max %d/%d)\n", __func__, size, | |
563 | rpcx_to_rdmad(xprt).inline_rsize, | |
564 | rpcx_to_rdmad(xprt).inline_wsize); | |
565 | size = req->rl_size; | |
566 | rpc_exit(task, -EIO); /* fail the operation */ | |
567 | rpcx_to_rdmax(xprt)->rx_stats.failed_marshal_count++; | |
568 | goto out; | |
569 | } | |
570 | if (task->tk_flags & RPC_TASK_SWAPPER) | |
571 | nreq = kmalloc(sizeof *req + size, GFP_ATOMIC); | |
572 | else | |
573 | nreq = kmalloc(sizeof *req + size, GFP_NOFS); | |
574 | if (nreq == NULL) | |
575 | goto outfail; | |
576 | ||
577 | if (rpcrdma_register_internal(&rpcx_to_rdmax(xprt)->rx_ia, | |
578 | nreq->rl_base, size + sizeof(struct rpcrdma_req) | |
579 | - offsetof(struct rpcrdma_req, rl_base), | |
580 | &nreq->rl_handle, &nreq->rl_iov)) { | |
581 | kfree(nreq); | |
582 | goto outfail; | |
583 | } | |
584 | rpcx_to_rdmax(xprt)->rx_stats.hardway_register_count += size; | |
585 | nreq->rl_size = size; | |
586 | nreq->rl_niovs = 0; | |
587 | nreq->rl_nchunks = 0; | |
588 | nreq->rl_buffer = (struct rpcrdma_buffer *)req; | |
589 | nreq->rl_reply = req->rl_reply; | |
590 | memcpy(nreq->rl_segments, | |
591 | req->rl_segments, sizeof nreq->rl_segments); | |
592 | /* flag the swap with an unused field */ | |
593 | nreq->rl_iov.length = 0; | |
594 | req->rl_reply = NULL; | |
595 | req = nreq; | |
596 | } | |
597 | dprintk("RPC: %s: size %zd, request 0x%p\n", __func__, size, req); | |
598 | out: | |
575448bd | 599 | req->rl_connect_cookie = 0; /* our reserved value */ |
f58851e6 TT |
600 | return req->rl_xdr_buf; |
601 | ||
602 | outfail: | |
603 | rpcrdma_buffer_put(req); | |
604 | rpcx_to_rdmax(xprt)->rx_stats.failed_marshal_count++; | |
605 | return NULL; | |
606 | } | |
607 | ||
608 | /* | |
609 | * This function returns all RDMA resources to the pool. | |
610 | */ | |
611 | static void | |
612 | xprt_rdma_free(void *buffer) | |
613 | { | |
614 | struct rpcrdma_req *req; | |
615 | struct rpcrdma_xprt *r_xprt; | |
616 | struct rpcrdma_rep *rep; | |
617 | int i; | |
618 | ||
619 | if (buffer == NULL) | |
620 | return; | |
621 | ||
622 | req = container_of(buffer, struct rpcrdma_req, rl_xdr_buf[0]); | |
ee1a2c56 TT |
623 | if (req->rl_iov.length == 0) { /* see allocate above */ |
624 | r_xprt = container_of(((struct rpcrdma_req *) req->rl_buffer)->rl_buffer, | |
625 | struct rpcrdma_xprt, rx_buf); | |
626 | } else | |
627 | r_xprt = container_of(req->rl_buffer, struct rpcrdma_xprt, rx_buf); | |
f58851e6 TT |
628 | rep = req->rl_reply; |
629 | ||
630 | dprintk("RPC: %s: called on 0x%p%s\n", | |
631 | __func__, rep, (rep && rep->rr_func) ? " (with waiter)" : ""); | |
632 | ||
633 | /* | |
634 | * Finish the deregistration. When using mw bind, this was | |
635 | * begun in rpcrdma_reply_handler(). In all other modes, we | |
636 | * do it here, in thread context. The process is considered | |
637 | * complete when the rr_func vector becomes NULL - this | |
638 | * was put in place during rpcrdma_reply_handler() - the wait | |
639 | * call below will not block if the dereg is "done". If | |
640 | * interrupted, our framework will clean up. | |
641 | */ | |
642 | for (i = 0; req->rl_nchunks;) { | |
643 | --req->rl_nchunks; | |
644 | i += rpcrdma_deregister_external( | |
645 | &req->rl_segments[i], r_xprt, NULL); | |
646 | } | |
647 | ||
648 | if (rep && wait_event_interruptible(rep->rr_unbind, !rep->rr_func)) { | |
649 | rep->rr_func = NULL; /* abandon the callback */ | |
650 | req->rl_reply = NULL; | |
651 | } | |
652 | ||
653 | if (req->rl_iov.length == 0) { /* see allocate above */ | |
654 | struct rpcrdma_req *oreq = (struct rpcrdma_req *)req->rl_buffer; | |
655 | oreq->rl_reply = req->rl_reply; | |
656 | (void) rpcrdma_deregister_internal(&r_xprt->rx_ia, | |
657 | req->rl_handle, | |
658 | &req->rl_iov); | |
659 | kfree(req); | |
660 | req = oreq; | |
661 | } | |
662 | ||
663 | /* Put back request+reply buffers */ | |
664 | rpcrdma_buffer_put(req); | |
665 | } | |
666 | ||
667 | /* | |
668 | * send_request invokes the meat of RPC RDMA. It must do the following: | |
669 | * 1. Marshal the RPC request into an RPC RDMA request, which means | |
670 | * putting a header in front of data, and creating IOVs for RDMA | |
671 | * from those in the request. | |
672 | * 2. In marshaling, detect opportunities for RDMA, and use them. | |
673 | * 3. Post a recv message to set up asynch completion, then send | |
674 | * the request (rpcrdma_ep_post). | |
675 | * 4. No partial sends are possible in the RPC-RDMA protocol (as in UDP). | |
676 | */ | |
677 | ||
678 | static int | |
679 | xprt_rdma_send_request(struct rpc_task *task) | |
680 | { | |
681 | struct rpc_rqst *rqst = task->tk_rqstp; | |
682 | struct rpc_xprt *xprt = task->tk_xprt; | |
683 | struct rpcrdma_req *req = rpcr_to_rdmar(rqst); | |
684 | struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt); | |
685 | ||
686 | /* marshal the send itself */ | |
687 | if (req->rl_niovs == 0 && rpcrdma_marshal_req(rqst) != 0) { | |
688 | r_xprt->rx_stats.failed_marshal_count++; | |
689 | dprintk("RPC: %s: rpcrdma_marshal_req failed\n", | |
690 | __func__); | |
691 | return -EIO; | |
692 | } | |
693 | ||
694 | if (req->rl_reply == NULL) /* e.g. reconnection */ | |
695 | rpcrdma_recv_buffer_get(req); | |
696 | ||
697 | if (req->rl_reply) { | |
698 | req->rl_reply->rr_func = rpcrdma_reply_handler; | |
699 | /* this need only be done once, but... */ | |
700 | req->rl_reply->rr_xprt = xprt; | |
701 | } | |
702 | ||
575448bd TT |
703 | /* Must suppress retransmit to maintain credits */ |
704 | if (req->rl_connect_cookie == xprt->connect_cookie) | |
705 | goto drop_connection; | |
706 | req->rl_connect_cookie = xprt->connect_cookie; | |
707 | ||
708 | if (rpcrdma_ep_post(&r_xprt->rx_ia, &r_xprt->rx_ep, req)) | |
709 | goto drop_connection; | |
f58851e6 | 710 | |
ad0e9e01 | 711 | task->tk_bytes_sent += rqst->rq_snd_buf.len; |
f58851e6 TT |
712 | rqst->rq_bytes_sent = 0; |
713 | return 0; | |
575448bd TT |
714 | |
715 | drop_connection: | |
716 | xprt_disconnect_done(xprt); | |
717 | return -ENOTCONN; /* implies disconnect */ | |
f58851e6 TT |
718 | } |
719 | ||
720 | static void xprt_rdma_print_stats(struct rpc_xprt *xprt, struct seq_file *seq) | |
721 | { | |
722 | struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt); | |
723 | long idle_time = 0; | |
724 | ||
725 | if (xprt_connected(xprt)) | |
726 | idle_time = (long)(jiffies - xprt->last_used) / HZ; | |
727 | ||
728 | seq_printf(seq, | |
729 | "\txprt:\trdma %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu " | |
730 | "%lu %lu %lu %Lu %Lu %Lu %Lu %lu %lu %lu\n", | |
731 | ||
732 | 0, /* need a local port? */ | |
733 | xprt->stat.bind_count, | |
734 | xprt->stat.connect_count, | |
735 | xprt->stat.connect_time, | |
736 | idle_time, | |
737 | xprt->stat.sends, | |
738 | xprt->stat.recvs, | |
739 | xprt->stat.bad_xids, | |
740 | xprt->stat.req_u, | |
741 | xprt->stat.bklog_u, | |
742 | ||
743 | r_xprt->rx_stats.read_chunk_count, | |
744 | r_xprt->rx_stats.write_chunk_count, | |
745 | r_xprt->rx_stats.reply_chunk_count, | |
746 | r_xprt->rx_stats.total_rdma_request, | |
747 | r_xprt->rx_stats.total_rdma_reply, | |
748 | r_xprt->rx_stats.pullup_copy_count, | |
749 | r_xprt->rx_stats.fixup_copy_count, | |
750 | r_xprt->rx_stats.hardway_register_count, | |
751 | r_xprt->rx_stats.failed_marshal_count, | |
752 | r_xprt->rx_stats.bad_reply_count); | |
753 | } | |
754 | ||
755 | /* | |
756 | * Plumbing for rpc transport switch and kernel module | |
757 | */ | |
758 | ||
759 | static struct rpc_xprt_ops xprt_rdma_procs = { | |
760 | .reserve_xprt = xprt_rdma_reserve_xprt, | |
761 | .release_xprt = xprt_release_xprt_cong, /* sunrpc/xprt.c */ | |
762 | .release_request = xprt_release_rqst_cong, /* ditto */ | |
763 | .set_retrans_timeout = xprt_set_retrans_timeout_def, /* ditto */ | |
764 | .rpcbind = rpcb_getport_async, /* sunrpc/rpcb_clnt.c */ | |
765 | .set_port = xprt_rdma_set_port, | |
766 | .connect = xprt_rdma_connect, | |
767 | .buf_alloc = xprt_rdma_allocate, | |
768 | .buf_free = xprt_rdma_free, | |
769 | .send_request = xprt_rdma_send_request, | |
770 | .close = xprt_rdma_close, | |
771 | .destroy = xprt_rdma_destroy, | |
772 | .print_stats = xprt_rdma_print_stats | |
773 | }; | |
774 | ||
775 | static struct xprt_class xprt_rdma = { | |
776 | .list = LIST_HEAD_INIT(xprt_rdma.list), | |
777 | .name = "rdma", | |
778 | .owner = THIS_MODULE, | |
779 | .ident = XPRT_TRANSPORT_RDMA, | |
780 | .setup = xprt_setup_rdma, | |
781 | }; | |
782 | ||
783 | static void __exit xprt_rdma_cleanup(void) | |
784 | { | |
785 | int rc; | |
786 | ||
b3cd8d45 | 787 | dprintk(KERN_INFO "RPCRDMA Module Removed, deregister RPC RDMA transport\n"); |
f58851e6 TT |
788 | #ifdef RPC_DEBUG |
789 | if (sunrpc_table_header) { | |
790 | unregister_sysctl_table(sunrpc_table_header); | |
791 | sunrpc_table_header = NULL; | |
792 | } | |
793 | #endif | |
794 | rc = xprt_unregister_transport(&xprt_rdma); | |
795 | if (rc) | |
796 | dprintk("RPC: %s: xprt_unregister returned %i\n", | |
797 | __func__, rc); | |
798 | } | |
799 | ||
800 | static int __init xprt_rdma_init(void) | |
801 | { | |
802 | int rc; | |
803 | ||
804 | rc = xprt_register_transport(&xprt_rdma); | |
805 | ||
806 | if (rc) | |
807 | return rc; | |
808 | ||
809 | dprintk(KERN_INFO "RPCRDMA Module Init, register RPC RDMA transport\n"); | |
810 | ||
811 | dprintk(KERN_INFO "Defaults:\n"); | |
812 | dprintk(KERN_INFO "\tSlots %d\n" | |
813 | "\tMaxInlineRead %d\n\tMaxInlineWrite %d\n", | |
814 | xprt_rdma_slot_table_entries, | |
815 | xprt_rdma_max_inline_read, xprt_rdma_max_inline_write); | |
816 | dprintk(KERN_INFO "\tPadding %d\n\tMemreg %d\n", | |
817 | xprt_rdma_inline_write_padding, xprt_rdma_memreg_strategy); | |
818 | ||
819 | #ifdef RPC_DEBUG | |
820 | if (!sunrpc_table_header) | |
821 | sunrpc_table_header = register_sysctl_table(sunrpc_table); | |
822 | #endif | |
823 | return 0; | |
824 | } | |
825 | ||
826 | module_init(xprt_rdma_init); | |
827 | module_exit(xprt_rdma_cleanup); |