transport.c 22 KB

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