transport.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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/slab.h>
  50. #include <linux/seq_file.h>
  51. #include <linux/sunrpc/addr.h>
  52. #include "xprt_rdma.h"
  53. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  54. # define RPCDBG_FACILITY RPCDBG_TRANS
  55. #endif
  56. /*
  57. * tunables
  58. */
  59. static unsigned int xprt_rdma_slot_table_entries = RPCRDMA_DEF_SLOT_TABLE;
  60. unsigned int xprt_rdma_max_inline_read = RPCRDMA_DEF_INLINE;
  61. static unsigned int xprt_rdma_max_inline_write = RPCRDMA_DEF_INLINE;
  62. static unsigned int xprt_rdma_inline_write_padding;
  63. static unsigned int xprt_rdma_memreg_strategy = RPCRDMA_FRMR;
  64. int xprt_rdma_pad_optimize = 0;
  65. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  66. static unsigned int min_slot_table_size = RPCRDMA_MIN_SLOT_TABLE;
  67. static unsigned int max_slot_table_size = RPCRDMA_MAX_SLOT_TABLE;
  68. static unsigned int min_inline_size = RPCRDMA_MIN_INLINE;
  69. static unsigned int max_inline_size = RPCRDMA_MAX_INLINE;
  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 struct 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_minmax,
  91. .extra1 = &min_inline_size,
  92. .extra2 = &max_inline_size,
  93. },
  94. {
  95. .procname = "rdma_max_inline_write",
  96. .data = &xprt_rdma_max_inline_write,
  97. .maxlen = sizeof(unsigned int),
  98. .mode = 0644,
  99. .proc_handler = proc_dointvec_minmax,
  100. .extra1 = &min_inline_size,
  101. .extra2 = &max_inline_size,
  102. },
  103. {
  104. .procname = "rdma_inline_write_padding",
  105. .data = &xprt_rdma_inline_write_padding,
  106. .maxlen = sizeof(unsigned int),
  107. .mode = 0644,
  108. .proc_handler = proc_dointvec_minmax,
  109. .extra1 = &zero,
  110. .extra2 = &max_padding,
  111. },
  112. {
  113. .procname = "rdma_memreg_strategy",
  114. .data = &xprt_rdma_memreg_strategy,
  115. .maxlen = sizeof(unsigned int),
  116. .mode = 0644,
  117. .proc_handler = proc_dointvec_minmax,
  118. .extra1 = &min_memreg,
  119. .extra2 = &max_memreg,
  120. },
  121. {
  122. .procname = "rdma_pad_optimize",
  123. .data = &xprt_rdma_pad_optimize,
  124. .maxlen = sizeof(unsigned int),
  125. .mode = 0644,
  126. .proc_handler = proc_dointvec,
  127. },
  128. { },
  129. };
  130. static struct ctl_table sunrpc_table[] = {
  131. {
  132. .procname = "sunrpc",
  133. .mode = 0555,
  134. .child = xr_tunables_table
  135. },
  136. { },
  137. };
  138. #endif
  139. static struct rpc_xprt_ops xprt_rdma_procs; /*forward reference */
  140. static void
  141. xprt_rdma_format_addresses4(struct rpc_xprt *xprt, struct sockaddr *sap)
  142. {
  143. struct sockaddr_in *sin = (struct sockaddr_in *)sap;
  144. char buf[20];
  145. snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
  146. xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
  147. xprt->address_strings[RPC_DISPLAY_NETID] = RPCBIND_NETID_RDMA;
  148. }
  149. static void
  150. xprt_rdma_format_addresses6(struct rpc_xprt *xprt, struct sockaddr *sap)
  151. {
  152. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
  153. char buf[40];
  154. snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
  155. xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
  156. xprt->address_strings[RPC_DISPLAY_NETID] = RPCBIND_NETID_RDMA6;
  157. }
  158. void
  159. xprt_rdma_format_addresses(struct rpc_xprt *xprt, struct sockaddr *sap)
  160. {
  161. char buf[128];
  162. switch (sap->sa_family) {
  163. case AF_INET:
  164. xprt_rdma_format_addresses4(xprt, sap);
  165. break;
  166. case AF_INET6:
  167. xprt_rdma_format_addresses6(xprt, sap);
  168. break;
  169. default:
  170. pr_err("rpcrdma: Unrecognized address family\n");
  171. return;
  172. }
  173. (void)rpc_ntop(sap, buf, sizeof(buf));
  174. xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
  175. snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
  176. xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
  177. snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
  178. xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
  179. xprt->address_strings[RPC_DISPLAY_PROTO] = "rdma";
  180. }
  181. void
  182. xprt_rdma_free_addresses(struct rpc_xprt *xprt)
  183. {
  184. unsigned int i;
  185. for (i = 0; i < RPC_DISPLAY_MAX; i++)
  186. switch (i) {
  187. case RPC_DISPLAY_PROTO:
  188. case RPC_DISPLAY_NETID:
  189. continue;
  190. default:
  191. kfree(xprt->address_strings[i]);
  192. }
  193. }
  194. static void
  195. xprt_rdma_connect_worker(struct work_struct *work)
  196. {
  197. struct rpcrdma_xprt *r_xprt = container_of(work, struct rpcrdma_xprt,
  198. rx_connect_worker.work);
  199. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  200. int rc = 0;
  201. xprt_clear_connected(xprt);
  202. dprintk("RPC: %s: %sconnect\n", __func__,
  203. r_xprt->rx_ep.rep_connected != 0 ? "re" : "");
  204. rc = rpcrdma_ep_connect(&r_xprt->rx_ep, &r_xprt->rx_ia);
  205. if (rc)
  206. xprt_wake_pending_tasks(xprt, rc);
  207. dprintk("RPC: %s: exit\n", __func__);
  208. xprt_clear_connecting(xprt);
  209. }
  210. static void
  211. xprt_rdma_inject_disconnect(struct rpc_xprt *xprt)
  212. {
  213. struct rpcrdma_xprt *r_xprt = container_of(xprt, struct rpcrdma_xprt,
  214. rx_xprt);
  215. pr_info("rpcrdma: injecting transport disconnect on xprt=%p\n", xprt);
  216. rdma_disconnect(r_xprt->rx_ia.ri_id);
  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. dprintk("RPC: %s: called\n", __func__);
  234. cancel_delayed_work_sync(&r_xprt->rx_connect_worker);
  235. xprt_clear_connected(xprt);
  236. rpcrdma_ep_destroy(&r_xprt->rx_ep, &r_xprt->rx_ia);
  237. rpcrdma_buffer_destroy(&r_xprt->rx_buf);
  238. rpcrdma_ia_close(&r_xprt->rx_ia);
  239. xprt_rdma_free_addresses(xprt);
  240. xprt_free(xprt);
  241. dprintk("RPC: %s: returning\n", __func__);
  242. module_put(THIS_MODULE);
  243. }
  244. static const struct rpc_timeout xprt_rdma_default_timeout = {
  245. .to_initval = 60 * HZ,
  246. .to_maxval = 60 * HZ,
  247. };
  248. /**
  249. * xprt_setup_rdma - Set up transport to use RDMA
  250. *
  251. * @args: rpc transport arguments
  252. */
  253. static struct rpc_xprt *
  254. xprt_setup_rdma(struct xprt_create *args)
  255. {
  256. struct rpcrdma_create_data_internal cdata;
  257. struct rpc_xprt *xprt;
  258. struct rpcrdma_xprt *new_xprt;
  259. struct rpcrdma_ep *new_ep;
  260. struct sockaddr *sap;
  261. int rc;
  262. if (args->addrlen > sizeof(xprt->addr)) {
  263. dprintk("RPC: %s: address too large\n", __func__);
  264. return ERR_PTR(-EBADF);
  265. }
  266. xprt = xprt_alloc(args->net, sizeof(struct rpcrdma_xprt),
  267. xprt_rdma_slot_table_entries,
  268. xprt_rdma_slot_table_entries);
  269. if (xprt == NULL) {
  270. dprintk("RPC: %s: couldn't allocate rpcrdma_xprt\n",
  271. __func__);
  272. return ERR_PTR(-ENOMEM);
  273. }
  274. /* 60 second timeout, no retries */
  275. xprt->timeout = &xprt_rdma_default_timeout;
  276. xprt->bind_timeout = RPCRDMA_BIND_TO;
  277. xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
  278. xprt->idle_timeout = RPCRDMA_IDLE_DISC_TO;
  279. xprt->resvport = 0; /* privileged port not needed */
  280. xprt->tsh_size = 0; /* RPC-RDMA handles framing */
  281. xprt->ops = &xprt_rdma_procs;
  282. /*
  283. * Set up RDMA-specific connect data.
  284. */
  285. sap = (struct sockaddr *)&cdata.addr;
  286. memcpy(sap, args->dstaddr, args->addrlen);
  287. /* Ensure xprt->addr holds valid server TCP (not RDMA)
  288. * address, for any side protocols which peek at it */
  289. xprt->prot = IPPROTO_TCP;
  290. xprt->addrlen = args->addrlen;
  291. memcpy(&xprt->addr, sap, xprt->addrlen);
  292. if (rpc_get_port(sap))
  293. xprt_set_bound(xprt);
  294. cdata.max_requests = xprt->max_reqs;
  295. cdata.rsize = RPCRDMA_MAX_SEGS * PAGE_SIZE; /* RDMA write max */
  296. cdata.wsize = RPCRDMA_MAX_SEGS * PAGE_SIZE; /* RDMA read max */
  297. cdata.inline_wsize = xprt_rdma_max_inline_write;
  298. if (cdata.inline_wsize > cdata.wsize)
  299. cdata.inline_wsize = cdata.wsize;
  300. cdata.inline_rsize = xprt_rdma_max_inline_read;
  301. if (cdata.inline_rsize > cdata.rsize)
  302. cdata.inline_rsize = cdata.rsize;
  303. cdata.padding = xprt_rdma_inline_write_padding;
  304. /*
  305. * Create new transport instance, which includes initialized
  306. * o ia
  307. * o endpoint
  308. * o buffers
  309. */
  310. new_xprt = rpcx_to_rdmax(xprt);
  311. rc = rpcrdma_ia_open(new_xprt, sap, xprt_rdma_memreg_strategy);
  312. if (rc)
  313. goto out1;
  314. /*
  315. * initialize and create ep
  316. */
  317. new_xprt->rx_data = cdata;
  318. new_ep = &new_xprt->rx_ep;
  319. new_ep->rep_remote_addr = cdata.addr;
  320. rc = rpcrdma_ep_create(&new_xprt->rx_ep,
  321. &new_xprt->rx_ia, &new_xprt->rx_data);
  322. if (rc)
  323. goto out2;
  324. /*
  325. * Allocate pre-registered send and receive buffers for headers and
  326. * any inline data. Also specify any padding which will be provided
  327. * from a preregistered zero buffer.
  328. */
  329. rc = rpcrdma_buffer_create(new_xprt);
  330. if (rc)
  331. goto out3;
  332. /*
  333. * Register a callback for connection events. This is necessary because
  334. * connection loss notification is async. We also catch connection loss
  335. * when reaping receives.
  336. */
  337. INIT_DELAYED_WORK(&new_xprt->rx_connect_worker,
  338. xprt_rdma_connect_worker);
  339. xprt_rdma_format_addresses(xprt, sap);
  340. xprt->max_payload = new_xprt->rx_ia.ri_ops->ro_maxpages(new_xprt);
  341. if (xprt->max_payload == 0)
  342. goto out4;
  343. xprt->max_payload <<= PAGE_SHIFT;
  344. dprintk("RPC: %s: transport data payload maximum: %zu bytes\n",
  345. __func__, xprt->max_payload);
  346. if (!try_module_get(THIS_MODULE))
  347. goto out4;
  348. dprintk("RPC: %s: %s:%s\n", __func__,
  349. xprt->address_strings[RPC_DISPLAY_ADDR],
  350. xprt->address_strings[RPC_DISPLAY_PORT]);
  351. return xprt;
  352. out4:
  353. xprt_rdma_free_addresses(xprt);
  354. rc = -EINVAL;
  355. out3:
  356. rpcrdma_ep_destroy(new_ep, &new_xprt->rx_ia);
  357. out2:
  358. rpcrdma_ia_close(&new_xprt->rx_ia);
  359. out1:
  360. xprt_free(xprt);
  361. return ERR_PTR(rc);
  362. }
  363. /*
  364. * Close a connection, during shutdown or timeout/reconnect
  365. */
  366. static void
  367. xprt_rdma_close(struct rpc_xprt *xprt)
  368. {
  369. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  370. dprintk("RPC: %s: closing\n", __func__);
  371. if (r_xprt->rx_ep.rep_connected > 0)
  372. xprt->reestablish_timeout = 0;
  373. xprt_disconnect_done(xprt);
  374. rpcrdma_ep_disconnect(&r_xprt->rx_ep, &r_xprt->rx_ia);
  375. }
  376. static void
  377. xprt_rdma_set_port(struct rpc_xprt *xprt, u16 port)
  378. {
  379. struct sockaddr_in *sap;
  380. sap = (struct sockaddr_in *)&xprt->addr;
  381. sap->sin_port = htons(port);
  382. sap = (struct sockaddr_in *)&rpcx_to_rdmad(xprt).addr;
  383. sap->sin_port = htons(port);
  384. dprintk("RPC: %s: %u\n", __func__, port);
  385. }
  386. static void
  387. xprt_rdma_connect(struct rpc_xprt *xprt, struct rpc_task *task)
  388. {
  389. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  390. if (r_xprt->rx_ep.rep_connected != 0) {
  391. /* Reconnect */
  392. schedule_delayed_work(&r_xprt->rx_connect_worker,
  393. xprt->reestablish_timeout);
  394. xprt->reestablish_timeout <<= 1;
  395. if (xprt->reestablish_timeout > RPCRDMA_MAX_REEST_TO)
  396. xprt->reestablish_timeout = RPCRDMA_MAX_REEST_TO;
  397. else if (xprt->reestablish_timeout < RPCRDMA_INIT_REEST_TO)
  398. xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
  399. } else {
  400. schedule_delayed_work(&r_xprt->rx_connect_worker, 0);
  401. if (!RPC_IS_ASYNC(task))
  402. flush_delayed_work(&r_xprt->rx_connect_worker);
  403. }
  404. }
  405. /* Allocate a fixed-size buffer in which to construct and send the
  406. * RPC-over-RDMA header for this request.
  407. */
  408. static bool
  409. rpcrdma_get_rdmabuf(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req,
  410. gfp_t flags)
  411. {
  412. size_t size = RPCRDMA_HDRBUF_SIZE;
  413. struct rpcrdma_regbuf *rb;
  414. if (req->rl_rdmabuf)
  415. return true;
  416. rb = rpcrdma_alloc_regbuf(size, DMA_TO_DEVICE, flags);
  417. if (IS_ERR(rb))
  418. return false;
  419. r_xprt->rx_stats.hardway_register_count += size;
  420. req->rl_rdmabuf = rb;
  421. return true;
  422. }
  423. static bool
  424. rpcrdma_get_sendbuf(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req,
  425. size_t size, gfp_t flags)
  426. {
  427. struct rpcrdma_regbuf *rb;
  428. if (req->rl_sendbuf && rdmab_length(req->rl_sendbuf) >= size)
  429. return true;
  430. rb = rpcrdma_alloc_regbuf(size, DMA_TO_DEVICE, flags);
  431. if (IS_ERR(rb))
  432. return false;
  433. rpcrdma_free_regbuf(req->rl_sendbuf);
  434. r_xprt->rx_stats.hardway_register_count += size;
  435. req->rl_sendbuf = rb;
  436. return true;
  437. }
  438. /* The rq_rcv_buf is used only if a Reply chunk is necessary.
  439. * The decision to use a Reply chunk is made later in
  440. * rpcrdma_marshal_req. This buffer is registered at that time.
  441. *
  442. * Otherwise, the associated RPC Reply arrives in a separate
  443. * Receive buffer, arbitrarily chosen by the HCA. The buffer
  444. * allocated here for the RPC Reply is not utilized in that
  445. * case. See rpcrdma_inline_fixup.
  446. *
  447. * A regbuf is used here to remember the buffer size.
  448. */
  449. static bool
  450. rpcrdma_get_recvbuf(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req,
  451. size_t size, gfp_t flags)
  452. {
  453. struct rpcrdma_regbuf *rb;
  454. if (req->rl_recvbuf && rdmab_length(req->rl_recvbuf) >= size)
  455. return true;
  456. rb = rpcrdma_alloc_regbuf(size, DMA_NONE, flags);
  457. if (IS_ERR(rb))
  458. return false;
  459. rpcrdma_free_regbuf(req->rl_recvbuf);
  460. r_xprt->rx_stats.hardway_register_count += size;
  461. req->rl_recvbuf = rb;
  462. return true;
  463. }
  464. /**
  465. * xprt_rdma_allocate - allocate transport resources for an RPC
  466. * @task: RPC task
  467. *
  468. * Return values:
  469. * 0: Success; rq_buffer points to RPC buffer to use
  470. * ENOMEM: Out of memory, call again later
  471. * EIO: A permanent error occurred, do not retry
  472. *
  473. * The RDMA allocate/free functions need the task structure as a place
  474. * to hide the struct rpcrdma_req, which is necessary for the actual
  475. * send/recv sequence.
  476. *
  477. * xprt_rdma_allocate provides buffers that are already mapped for
  478. * DMA, and a local DMA lkey is provided for each.
  479. */
  480. static int
  481. xprt_rdma_allocate(struct rpc_task *task)
  482. {
  483. struct rpc_rqst *rqst = task->tk_rqstp;
  484. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
  485. struct rpcrdma_req *req;
  486. gfp_t flags;
  487. req = rpcrdma_buffer_get(&r_xprt->rx_buf);
  488. if (req == NULL)
  489. return -ENOMEM;
  490. flags = RPCRDMA_DEF_GFP;
  491. if (RPC_IS_SWAPPER(task))
  492. flags = __GFP_MEMALLOC | GFP_NOWAIT | __GFP_NOWARN;
  493. if (!rpcrdma_get_rdmabuf(r_xprt, req, flags))
  494. goto out_fail;
  495. if (!rpcrdma_get_sendbuf(r_xprt, req, rqst->rq_callsize, flags))
  496. goto out_fail;
  497. if (!rpcrdma_get_recvbuf(r_xprt, req, rqst->rq_rcvsize, flags))
  498. goto out_fail;
  499. dprintk("RPC: %5u %s: send size = %zd, recv size = %zd, req = %p\n",
  500. task->tk_pid, __func__, rqst->rq_callsize,
  501. rqst->rq_rcvsize, req);
  502. req->rl_connect_cookie = 0; /* our reserved value */
  503. rpcrdma_set_xprtdata(rqst, req);
  504. rqst->rq_buffer = req->rl_sendbuf->rg_base;
  505. rqst->rq_rbuffer = req->rl_recvbuf->rg_base;
  506. return 0;
  507. out_fail:
  508. rpcrdma_buffer_put(req);
  509. return -ENOMEM;
  510. }
  511. /**
  512. * xprt_rdma_free - release resources allocated by xprt_rdma_allocate
  513. * @task: RPC task
  514. *
  515. * Caller guarantees rqst->rq_buffer is non-NULL.
  516. */
  517. static void
  518. xprt_rdma_free(struct rpc_task *task)
  519. {
  520. struct rpc_rqst *rqst = task->tk_rqstp;
  521. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
  522. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  523. struct rpcrdma_ia *ia = &r_xprt->rx_ia;
  524. if (req->rl_backchannel)
  525. return;
  526. dprintk("RPC: %s: called on 0x%p\n", __func__, req->rl_reply);
  527. ia->ri_ops->ro_unmap_safe(r_xprt, req, !RPC_IS_ASYNC(task));
  528. rpcrdma_unmap_sges(ia, req);
  529. rpcrdma_buffer_put(req);
  530. }
  531. /**
  532. * xprt_rdma_send_request - marshal and send an RPC request
  533. * @task: RPC task with an RPC message in rq_snd_buf
  534. *
  535. * Return values:
  536. * 0: The request has been sent
  537. * ENOTCONN: Caller needs to invoke connect logic then call again
  538. * ENOBUFS: Call again later to send the request
  539. * EIO: A permanent error occurred. The request was not sent,
  540. * and don't try it again
  541. *
  542. * send_request invokes the meat of RPC RDMA. It must do the following:
  543. *
  544. * 1. Marshal the RPC request into an RPC RDMA request, which means
  545. * putting a header in front of data, and creating IOVs for RDMA
  546. * from those in the request.
  547. * 2. In marshaling, detect opportunities for RDMA, and use them.
  548. * 3. Post a recv message to set up asynch completion, then send
  549. * the request (rpcrdma_ep_post).
  550. * 4. No partial sends are possible in the RPC-RDMA protocol (as in UDP).
  551. */
  552. static int
  553. xprt_rdma_send_request(struct rpc_task *task)
  554. {
  555. struct rpc_rqst *rqst = task->tk_rqstp;
  556. struct rpc_xprt *xprt = rqst->rq_xprt;
  557. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  558. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  559. int rc = 0;
  560. /* On retransmit, remove any previously registered chunks */
  561. r_xprt->rx_ia.ri_ops->ro_unmap_safe(r_xprt, req, false);
  562. rc = rpcrdma_marshal_req(rqst);
  563. if (rc < 0)
  564. goto failed_marshal;
  565. if (req->rl_reply == NULL) /* e.g. reconnection */
  566. rpcrdma_recv_buffer_get(req);
  567. /* Must suppress retransmit to maintain credits */
  568. if (req->rl_connect_cookie == xprt->connect_cookie)
  569. goto drop_connection;
  570. req->rl_connect_cookie = xprt->connect_cookie;
  571. if (rpcrdma_ep_post(&r_xprt->rx_ia, &r_xprt->rx_ep, req))
  572. goto drop_connection;
  573. rqst->rq_xmit_bytes_sent += rqst->rq_snd_buf.len;
  574. rqst->rq_bytes_sent = 0;
  575. return 0;
  576. failed_marshal:
  577. dprintk("RPC: %s: rpcrdma_marshal_req failed, status %i\n",
  578. __func__, rc);
  579. if (rc == -EIO)
  580. r_xprt->rx_stats.failed_marshal_count++;
  581. if (rc != -ENOTCONN)
  582. return rc;
  583. drop_connection:
  584. xprt_disconnect_done(xprt);
  585. return -ENOTCONN; /* implies disconnect */
  586. }
  587. void xprt_rdma_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
  588. {
  589. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  590. long idle_time = 0;
  591. if (xprt_connected(xprt))
  592. idle_time = (long)(jiffies - xprt->last_used) / HZ;
  593. seq_puts(seq, "\txprt:\trdma ");
  594. seq_printf(seq, "%u %lu %lu %lu %ld %lu %lu %lu %llu %llu ",
  595. 0, /* need a local port? */
  596. xprt->stat.bind_count,
  597. xprt->stat.connect_count,
  598. xprt->stat.connect_time,
  599. idle_time,
  600. xprt->stat.sends,
  601. xprt->stat.recvs,
  602. xprt->stat.bad_xids,
  603. xprt->stat.req_u,
  604. xprt->stat.bklog_u);
  605. seq_printf(seq, "%lu %lu %lu %llu %llu %llu %llu %lu %lu %lu %lu ",
  606. r_xprt->rx_stats.read_chunk_count,
  607. r_xprt->rx_stats.write_chunk_count,
  608. r_xprt->rx_stats.reply_chunk_count,
  609. r_xprt->rx_stats.total_rdma_request,
  610. r_xprt->rx_stats.total_rdma_reply,
  611. r_xprt->rx_stats.pullup_copy_count,
  612. r_xprt->rx_stats.fixup_copy_count,
  613. r_xprt->rx_stats.hardway_register_count,
  614. r_xprt->rx_stats.failed_marshal_count,
  615. r_xprt->rx_stats.bad_reply_count,
  616. r_xprt->rx_stats.nomsg_call_count);
  617. seq_printf(seq, "%lu %lu %lu %lu\n",
  618. r_xprt->rx_stats.mrs_recovered,
  619. r_xprt->rx_stats.mrs_orphaned,
  620. r_xprt->rx_stats.mrs_allocated,
  621. r_xprt->rx_stats.local_inv_needed);
  622. }
  623. static int
  624. xprt_rdma_enable_swap(struct rpc_xprt *xprt)
  625. {
  626. return 0;
  627. }
  628. static void
  629. xprt_rdma_disable_swap(struct rpc_xprt *xprt)
  630. {
  631. }
  632. /*
  633. * Plumbing for rpc transport switch and kernel module
  634. */
  635. static struct rpc_xprt_ops xprt_rdma_procs = {
  636. .reserve_xprt = xprt_reserve_xprt_cong,
  637. .release_xprt = xprt_release_xprt_cong, /* sunrpc/xprt.c */
  638. .alloc_slot = xprt_alloc_slot,
  639. .release_request = xprt_release_rqst_cong, /* ditto */
  640. .set_retrans_timeout = xprt_set_retrans_timeout_def, /* ditto */
  641. .rpcbind = rpcb_getport_async, /* sunrpc/rpcb_clnt.c */
  642. .set_port = xprt_rdma_set_port,
  643. .connect = xprt_rdma_connect,
  644. .buf_alloc = xprt_rdma_allocate,
  645. .buf_free = xprt_rdma_free,
  646. .send_request = xprt_rdma_send_request,
  647. .close = xprt_rdma_close,
  648. .destroy = xprt_rdma_destroy,
  649. .print_stats = xprt_rdma_print_stats,
  650. .enable_swap = xprt_rdma_enable_swap,
  651. .disable_swap = xprt_rdma_disable_swap,
  652. .inject_disconnect = xprt_rdma_inject_disconnect,
  653. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  654. .bc_setup = xprt_rdma_bc_setup,
  655. .bc_up = xprt_rdma_bc_up,
  656. .bc_maxpayload = xprt_rdma_bc_maxpayload,
  657. .bc_free_rqst = xprt_rdma_bc_free_rqst,
  658. .bc_destroy = xprt_rdma_bc_destroy,
  659. #endif
  660. };
  661. static struct xprt_class xprt_rdma = {
  662. .list = LIST_HEAD_INIT(xprt_rdma.list),
  663. .name = "rdma",
  664. .owner = THIS_MODULE,
  665. .ident = XPRT_TRANSPORT_RDMA,
  666. .setup = xprt_setup_rdma,
  667. };
  668. void xprt_rdma_cleanup(void)
  669. {
  670. int rc;
  671. dprintk("RPCRDMA Module Removed, deregister RPC RDMA transport\n");
  672. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  673. if (sunrpc_table_header) {
  674. unregister_sysctl_table(sunrpc_table_header);
  675. sunrpc_table_header = NULL;
  676. }
  677. #endif
  678. rc = xprt_unregister_transport(&xprt_rdma);
  679. if (rc)
  680. dprintk("RPC: %s: xprt_unregister returned %i\n",
  681. __func__, rc);
  682. rpcrdma_destroy_wq();
  683. rc = xprt_unregister_transport(&xprt_rdma_bc);
  684. if (rc)
  685. dprintk("RPC: %s: xprt_unregister(bc) returned %i\n",
  686. __func__, rc);
  687. }
  688. int xprt_rdma_init(void)
  689. {
  690. int rc;
  691. rc = rpcrdma_alloc_wq();
  692. if (rc)
  693. return rc;
  694. rc = xprt_register_transport(&xprt_rdma);
  695. if (rc) {
  696. rpcrdma_destroy_wq();
  697. return rc;
  698. }
  699. rc = xprt_register_transport(&xprt_rdma_bc);
  700. if (rc) {
  701. xprt_unregister_transport(&xprt_rdma);
  702. rpcrdma_destroy_wq();
  703. return rc;
  704. }
  705. dprintk("RPCRDMA Module Init, register RPC RDMA transport\n");
  706. dprintk("Defaults:\n");
  707. dprintk("\tSlots %d\n"
  708. "\tMaxInlineRead %d\n\tMaxInlineWrite %d\n",
  709. xprt_rdma_slot_table_entries,
  710. xprt_rdma_max_inline_read, xprt_rdma_max_inline_write);
  711. dprintk("\tPadding %d\n\tMemreg %d\n",
  712. xprt_rdma_inline_write_padding, xprt_rdma_memreg_strategy);
  713. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  714. if (!sunrpc_table_header)
  715. sunrpc_table_header = register_sysctl_table(sunrpc_table);
  716. #endif
  717. return 0;
  718. }