transport.c 22 KB

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