transport.c 22 KB

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