transport.c 23 KB

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