transport.c 23 KB

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