transport.c 22 KB

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