transport.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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 = 1;
  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. void
  195. rpcrdma_conn_func(struct rpcrdma_ep *ep)
  196. {
  197. schedule_delayed_work(&ep->rep_connect_worker, 0);
  198. }
  199. void
  200. rpcrdma_connect_worker(struct work_struct *work)
  201. {
  202. struct rpcrdma_ep *ep =
  203. container_of(work, struct rpcrdma_ep, rep_connect_worker.work);
  204. struct rpcrdma_xprt *r_xprt =
  205. container_of(ep, struct rpcrdma_xprt, rx_ep);
  206. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  207. spin_lock_bh(&xprt->transport_lock);
  208. if (++xprt->connect_cookie == 0) /* maintain a reserved value */
  209. ++xprt->connect_cookie;
  210. if (ep->rep_connected > 0) {
  211. if (!xprt_test_and_set_connected(xprt))
  212. xprt_wake_pending_tasks(xprt, 0);
  213. } else {
  214. if (xprt_test_and_clear_connected(xprt))
  215. xprt_wake_pending_tasks(xprt, -ENOTCONN);
  216. }
  217. spin_unlock_bh(&xprt->transport_lock);
  218. }
  219. static void
  220. xprt_rdma_connect_worker(struct work_struct *work)
  221. {
  222. struct rpcrdma_xprt *r_xprt = container_of(work, struct rpcrdma_xprt,
  223. rx_connect_worker.work);
  224. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  225. int rc = 0;
  226. xprt_clear_connected(xprt);
  227. dprintk("RPC: %s: %sconnect\n", __func__,
  228. r_xprt->rx_ep.rep_connected != 0 ? "re" : "");
  229. rc = rpcrdma_ep_connect(&r_xprt->rx_ep, &r_xprt->rx_ia);
  230. if (rc)
  231. xprt_wake_pending_tasks(xprt, rc);
  232. dprintk("RPC: %s: exit\n", __func__);
  233. xprt_clear_connecting(xprt);
  234. }
  235. static void
  236. xprt_rdma_inject_disconnect(struct rpc_xprt *xprt)
  237. {
  238. struct rpcrdma_xprt *r_xprt = container_of(xprt, struct rpcrdma_xprt,
  239. rx_xprt);
  240. pr_info("rpcrdma: injecting transport disconnect on xprt=%p\n", xprt);
  241. rdma_disconnect(r_xprt->rx_ia.ri_id);
  242. }
  243. /*
  244. * xprt_rdma_destroy
  245. *
  246. * Destroy the xprt.
  247. * Free all memory associated with the object, including its own.
  248. * NOTE: none of the *destroy methods free memory for their top-level
  249. * objects, even though they may have allocated it (they do free
  250. * private memory). It's up to the caller to handle it. In this
  251. * case (RDMA transport), all structure memory is inlined with the
  252. * struct rpcrdma_xprt.
  253. */
  254. static void
  255. xprt_rdma_destroy(struct rpc_xprt *xprt)
  256. {
  257. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  258. dprintk("RPC: %s: called\n", __func__);
  259. cancel_delayed_work_sync(&r_xprt->rx_connect_worker);
  260. xprt_clear_connected(xprt);
  261. rpcrdma_ep_destroy(&r_xprt->rx_ep, &r_xprt->rx_ia);
  262. rpcrdma_buffer_destroy(&r_xprt->rx_buf);
  263. rpcrdma_ia_close(&r_xprt->rx_ia);
  264. xprt_rdma_free_addresses(xprt);
  265. xprt_free(xprt);
  266. dprintk("RPC: %s: returning\n", __func__);
  267. module_put(THIS_MODULE);
  268. }
  269. static const struct rpc_timeout xprt_rdma_default_timeout = {
  270. .to_initval = 60 * HZ,
  271. .to_maxval = 60 * HZ,
  272. };
  273. /**
  274. * xprt_setup_rdma - Set up transport to use RDMA
  275. *
  276. * @args: rpc transport arguments
  277. */
  278. static struct rpc_xprt *
  279. xprt_setup_rdma(struct xprt_create *args)
  280. {
  281. struct rpcrdma_create_data_internal cdata;
  282. struct rpc_xprt *xprt;
  283. struct rpcrdma_xprt *new_xprt;
  284. struct rpcrdma_ep *new_ep;
  285. struct sockaddr *sap;
  286. int rc;
  287. if (args->addrlen > sizeof(xprt->addr)) {
  288. dprintk("RPC: %s: address too large\n", __func__);
  289. return ERR_PTR(-EBADF);
  290. }
  291. xprt = xprt_alloc(args->net, sizeof(struct rpcrdma_xprt),
  292. xprt_rdma_slot_table_entries,
  293. xprt_rdma_slot_table_entries);
  294. if (xprt == NULL) {
  295. dprintk("RPC: %s: couldn't allocate rpcrdma_xprt\n",
  296. __func__);
  297. return ERR_PTR(-ENOMEM);
  298. }
  299. /* 60 second timeout, no retries */
  300. xprt->timeout = &xprt_rdma_default_timeout;
  301. xprt->bind_timeout = RPCRDMA_BIND_TO;
  302. xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
  303. xprt->idle_timeout = RPCRDMA_IDLE_DISC_TO;
  304. xprt->resvport = 0; /* privileged port not needed */
  305. xprt->tsh_size = 0; /* RPC-RDMA handles framing */
  306. xprt->ops = &xprt_rdma_procs;
  307. /*
  308. * Set up RDMA-specific connect data.
  309. */
  310. sap = (struct sockaddr *)&cdata.addr;
  311. memcpy(sap, 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, sap, xprt->addrlen);
  317. if (rpc_get_port(sap))
  318. xprt_set_bound(xprt);
  319. cdata.max_requests = xprt->max_reqs;
  320. cdata.rsize = RPCRDMA_MAX_SEGS * PAGE_SIZE; /* RDMA write max */
  321. cdata.wsize = RPCRDMA_MAX_SEGS * PAGE_SIZE; /* RDMA read max */
  322. cdata.inline_wsize = xprt_rdma_max_inline_write;
  323. if (cdata.inline_wsize > cdata.wsize)
  324. cdata.inline_wsize = cdata.wsize;
  325. cdata.inline_rsize = xprt_rdma_max_inline_read;
  326. if (cdata.inline_rsize > cdata.rsize)
  327. cdata.inline_rsize = cdata.rsize;
  328. cdata.padding = xprt_rdma_inline_write_padding;
  329. /*
  330. * Create new transport instance, which includes initialized
  331. * o ia
  332. * o endpoint
  333. * o buffers
  334. */
  335. new_xprt = rpcx_to_rdmax(xprt);
  336. rc = rpcrdma_ia_open(new_xprt, sap, xprt_rdma_memreg_strategy);
  337. if (rc)
  338. goto out1;
  339. /*
  340. * initialize and create ep
  341. */
  342. new_xprt->rx_data = cdata;
  343. new_ep = &new_xprt->rx_ep;
  344. new_ep->rep_remote_addr = cdata.addr;
  345. rc = rpcrdma_ep_create(&new_xprt->rx_ep,
  346. &new_xprt->rx_ia, &new_xprt->rx_data);
  347. if (rc)
  348. goto out2;
  349. /*
  350. * Allocate pre-registered send and receive buffers for headers and
  351. * any inline data. Also specify any padding which will be provided
  352. * from a preregistered zero buffer.
  353. */
  354. rc = rpcrdma_buffer_create(new_xprt);
  355. if (rc)
  356. goto out3;
  357. /*
  358. * Register a callback for connection events. This is necessary because
  359. * connection loss notification is async. We also catch connection loss
  360. * when reaping receives.
  361. */
  362. INIT_DELAYED_WORK(&new_xprt->rx_connect_worker,
  363. xprt_rdma_connect_worker);
  364. xprt_rdma_format_addresses(xprt, sap);
  365. xprt->max_payload = new_xprt->rx_ia.ri_ops->ro_maxpages(new_xprt);
  366. if (xprt->max_payload == 0)
  367. goto out4;
  368. xprt->max_payload <<= PAGE_SHIFT;
  369. dprintk("RPC: %s: transport data payload maximum: %zu bytes\n",
  370. __func__, xprt->max_payload);
  371. if (!try_module_get(THIS_MODULE))
  372. goto out4;
  373. dprintk("RPC: %s: %s:%s\n", __func__,
  374. xprt->address_strings[RPC_DISPLAY_ADDR],
  375. xprt->address_strings[RPC_DISPLAY_PORT]);
  376. return xprt;
  377. out4:
  378. xprt_rdma_free_addresses(xprt);
  379. rc = -EINVAL;
  380. out3:
  381. rpcrdma_ep_destroy(new_ep, &new_xprt->rx_ia);
  382. out2:
  383. rpcrdma_ia_close(&new_xprt->rx_ia);
  384. out1:
  385. xprt_free(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. 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_xprt *xprt, struct rpc_task *task)
  413. {
  414. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  415. if (r_xprt->rx_ep.rep_connected != 0) {
  416. /* Reconnect */
  417. schedule_delayed_work(&r_xprt->rx_connect_worker,
  418. xprt->reestablish_timeout);
  419. xprt->reestablish_timeout <<= 1;
  420. if (xprt->reestablish_timeout > RPCRDMA_MAX_REEST_TO)
  421. xprt->reestablish_timeout = RPCRDMA_MAX_REEST_TO;
  422. else if (xprt->reestablish_timeout < RPCRDMA_INIT_REEST_TO)
  423. xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
  424. } else {
  425. schedule_delayed_work(&r_xprt->rx_connect_worker, 0);
  426. if (!RPC_IS_ASYNC(task))
  427. flush_delayed_work(&r_xprt->rx_connect_worker);
  428. }
  429. }
  430. /* Allocate a fixed-size buffer in which to construct and send the
  431. * RPC-over-RDMA header for this request.
  432. */
  433. static bool
  434. rpcrdma_get_rdmabuf(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req,
  435. gfp_t flags)
  436. {
  437. size_t size = RPCRDMA_HDRBUF_SIZE;
  438. struct rpcrdma_regbuf *rb;
  439. if (req->rl_rdmabuf)
  440. return true;
  441. rb = rpcrdma_alloc_regbuf(size, DMA_TO_DEVICE, flags);
  442. if (IS_ERR(rb))
  443. return false;
  444. r_xprt->rx_stats.hardway_register_count += size;
  445. req->rl_rdmabuf = rb;
  446. return true;
  447. }
  448. static bool
  449. rpcrdma_get_sendbuf(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req,
  450. size_t size, gfp_t flags)
  451. {
  452. struct rpcrdma_regbuf *rb;
  453. if (req->rl_sendbuf && rdmab_length(req->rl_sendbuf) >= size)
  454. return true;
  455. rb = rpcrdma_alloc_regbuf(size, DMA_TO_DEVICE, flags);
  456. if (IS_ERR(rb))
  457. return false;
  458. rpcrdma_free_regbuf(req->rl_sendbuf);
  459. r_xprt->rx_stats.hardway_register_count += size;
  460. req->rl_sendbuf = rb;
  461. return true;
  462. }
  463. /* The rq_rcv_buf is used only if a Reply chunk is necessary.
  464. * The decision to use a Reply chunk is made later in
  465. * rpcrdma_marshal_req. This buffer is registered at that time.
  466. *
  467. * Otherwise, the associated RPC Reply arrives in a separate
  468. * Receive buffer, arbitrarily chosen by the HCA. The buffer
  469. * allocated here for the RPC Reply is not utilized in that
  470. * case. See rpcrdma_inline_fixup.
  471. *
  472. * A regbuf is used here to remember the buffer size.
  473. */
  474. static bool
  475. rpcrdma_get_recvbuf(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req,
  476. size_t size, gfp_t flags)
  477. {
  478. struct rpcrdma_regbuf *rb;
  479. if (req->rl_recvbuf && rdmab_length(req->rl_recvbuf) >= size)
  480. return true;
  481. rb = rpcrdma_alloc_regbuf(size, DMA_NONE, flags);
  482. if (IS_ERR(rb))
  483. return false;
  484. rpcrdma_free_regbuf(req->rl_recvbuf);
  485. r_xprt->rx_stats.hardway_register_count += size;
  486. req->rl_recvbuf = rb;
  487. return true;
  488. }
  489. /**
  490. * xprt_rdma_allocate - allocate transport resources for an RPC
  491. * @task: RPC task
  492. *
  493. * Return values:
  494. * 0: Success; rq_buffer points to RPC buffer to use
  495. * ENOMEM: Out of memory, call again later
  496. * EIO: A permanent error occurred, do not retry
  497. *
  498. * The RDMA allocate/free functions need the task structure as a place
  499. * to hide the struct rpcrdma_req, which is necessary for the actual
  500. * send/recv sequence.
  501. *
  502. * xprt_rdma_allocate provides buffers that are already mapped for
  503. * DMA, and a local DMA lkey is provided for each.
  504. */
  505. static int
  506. xprt_rdma_allocate(struct rpc_task *task)
  507. {
  508. struct rpc_rqst *rqst = task->tk_rqstp;
  509. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
  510. struct rpcrdma_req *req;
  511. gfp_t flags;
  512. req = rpcrdma_buffer_get(&r_xprt->rx_buf);
  513. if (req == NULL)
  514. return -ENOMEM;
  515. flags = RPCRDMA_DEF_GFP;
  516. if (RPC_IS_SWAPPER(task))
  517. flags = __GFP_MEMALLOC | GFP_NOWAIT | __GFP_NOWARN;
  518. if (!rpcrdma_get_rdmabuf(r_xprt, req, flags))
  519. goto out_fail;
  520. if (!rpcrdma_get_sendbuf(r_xprt, req, rqst->rq_callsize, flags))
  521. goto out_fail;
  522. if (!rpcrdma_get_recvbuf(r_xprt, req, rqst->rq_rcvsize, flags))
  523. goto out_fail;
  524. dprintk("RPC: %5u %s: send size = %zd, recv size = %zd, req = %p\n",
  525. task->tk_pid, __func__, rqst->rq_callsize,
  526. rqst->rq_rcvsize, req);
  527. req->rl_connect_cookie = 0; /* our reserved value */
  528. rpcrdma_set_xprtdata(rqst, req);
  529. rqst->rq_buffer = req->rl_sendbuf->rg_base;
  530. rqst->rq_rbuffer = req->rl_recvbuf->rg_base;
  531. return 0;
  532. out_fail:
  533. rpcrdma_buffer_put(req);
  534. return -ENOMEM;
  535. }
  536. /**
  537. * xprt_rdma_free - release resources allocated by xprt_rdma_allocate
  538. * @task: RPC task
  539. *
  540. * Caller guarantees rqst->rq_buffer is non-NULL.
  541. */
  542. static void
  543. xprt_rdma_free(struct rpc_task *task)
  544. {
  545. struct rpc_rqst *rqst = task->tk_rqstp;
  546. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
  547. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  548. struct rpcrdma_ia *ia = &r_xprt->rx_ia;
  549. if (req->rl_backchannel)
  550. return;
  551. dprintk("RPC: %s: called on 0x%p\n", __func__, req->rl_reply);
  552. if (unlikely(!list_empty(&req->rl_registered)))
  553. ia->ri_ops->ro_unmap_safe(r_xprt, req, !RPC_IS_ASYNC(task));
  554. rpcrdma_unmap_sges(ia, req);
  555. rpcrdma_buffer_put(req);
  556. }
  557. /**
  558. * xprt_rdma_send_request - marshal and send an RPC request
  559. * @task: RPC task with an RPC message in rq_snd_buf
  560. *
  561. * Return values:
  562. * 0: The request has been sent
  563. * ENOTCONN: Caller needs to invoke connect logic then call again
  564. * ENOBUFS: Call again later to send the request
  565. * EIO: A permanent error occurred. The request was not sent,
  566. * and don't try it again
  567. *
  568. * send_request invokes the meat of RPC RDMA. It must do the following:
  569. *
  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 = rqst->rq_xprt;
  583. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  584. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  585. int rc = 0;
  586. /* On retransmit, remove any previously registered chunks */
  587. if (unlikely(!list_empty(&req->rl_registered)))
  588. r_xprt->rx_ia.ri_ops->ro_unmap_safe(r_xprt, req, false);
  589. rc = rpcrdma_marshal_req(rqst);
  590. if (rc < 0)
  591. goto failed_marshal;
  592. if (req->rl_reply == NULL) /* e.g. reconnection */
  593. rpcrdma_recv_buffer_get(req);
  594. /* Must suppress retransmit to maintain credits */
  595. if (req->rl_connect_cookie == xprt->connect_cookie)
  596. goto drop_connection;
  597. req->rl_connect_cookie = xprt->connect_cookie;
  598. if (rpcrdma_ep_post(&r_xprt->rx_ia, &r_xprt->rx_ep, req))
  599. goto drop_connection;
  600. rqst->rq_xmit_bytes_sent += rqst->rq_snd_buf.len;
  601. rqst->rq_bytes_sent = 0;
  602. return 0;
  603. failed_marshal:
  604. dprintk("RPC: %s: rpcrdma_marshal_req failed, status %i\n",
  605. __func__, rc);
  606. if (rc == -EIO)
  607. r_xprt->rx_stats.failed_marshal_count++;
  608. if (rc != -ENOTCONN)
  609. return rc;
  610. drop_connection:
  611. xprt_disconnect_done(xprt);
  612. return -ENOTCONN; /* implies disconnect */
  613. }
  614. void xprt_rdma_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
  615. {
  616. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  617. long idle_time = 0;
  618. if (xprt_connected(xprt))
  619. idle_time = (long)(jiffies - xprt->last_used) / HZ;
  620. seq_puts(seq, "\txprt:\trdma ");
  621. seq_printf(seq, "%u %lu %lu %lu %ld %lu %lu %lu %llu %llu ",
  622. 0, /* need a local port? */
  623. xprt->stat.bind_count,
  624. xprt->stat.connect_count,
  625. xprt->stat.connect_time,
  626. idle_time,
  627. xprt->stat.sends,
  628. xprt->stat.recvs,
  629. xprt->stat.bad_xids,
  630. xprt->stat.req_u,
  631. xprt->stat.bklog_u);
  632. seq_printf(seq, "%lu %lu %lu %llu %llu %llu %llu %lu %lu %lu %lu ",
  633. r_xprt->rx_stats.read_chunk_count,
  634. r_xprt->rx_stats.write_chunk_count,
  635. r_xprt->rx_stats.reply_chunk_count,
  636. r_xprt->rx_stats.total_rdma_request,
  637. r_xprt->rx_stats.total_rdma_reply,
  638. r_xprt->rx_stats.pullup_copy_count,
  639. r_xprt->rx_stats.fixup_copy_count,
  640. r_xprt->rx_stats.hardway_register_count,
  641. r_xprt->rx_stats.failed_marshal_count,
  642. r_xprt->rx_stats.bad_reply_count,
  643. r_xprt->rx_stats.nomsg_call_count);
  644. seq_printf(seq, "%lu %lu %lu %lu\n",
  645. r_xprt->rx_stats.mrs_recovered,
  646. r_xprt->rx_stats.mrs_orphaned,
  647. r_xprt->rx_stats.mrs_allocated,
  648. r_xprt->rx_stats.local_inv_needed);
  649. }
  650. static int
  651. xprt_rdma_enable_swap(struct rpc_xprt *xprt)
  652. {
  653. return 0;
  654. }
  655. static void
  656. xprt_rdma_disable_swap(struct rpc_xprt *xprt)
  657. {
  658. }
  659. /*
  660. * Plumbing for rpc transport switch and kernel module
  661. */
  662. static struct rpc_xprt_ops xprt_rdma_procs = {
  663. .reserve_xprt = xprt_reserve_xprt_cong,
  664. .release_xprt = xprt_release_xprt_cong, /* sunrpc/xprt.c */
  665. .alloc_slot = xprt_alloc_slot,
  666. .release_request = xprt_release_rqst_cong, /* ditto */
  667. .set_retrans_timeout = xprt_set_retrans_timeout_def, /* ditto */
  668. .rpcbind = rpcb_getport_async, /* sunrpc/rpcb_clnt.c */
  669. .set_port = xprt_rdma_set_port,
  670. .connect = xprt_rdma_connect,
  671. .buf_alloc = xprt_rdma_allocate,
  672. .buf_free = xprt_rdma_free,
  673. .send_request = xprt_rdma_send_request,
  674. .close = xprt_rdma_close,
  675. .destroy = xprt_rdma_destroy,
  676. .print_stats = xprt_rdma_print_stats,
  677. .enable_swap = xprt_rdma_enable_swap,
  678. .disable_swap = xprt_rdma_disable_swap,
  679. .inject_disconnect = xprt_rdma_inject_disconnect,
  680. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  681. .bc_setup = xprt_rdma_bc_setup,
  682. .bc_up = xprt_rdma_bc_up,
  683. .bc_maxpayload = xprt_rdma_bc_maxpayload,
  684. .bc_free_rqst = xprt_rdma_bc_free_rqst,
  685. .bc_destroy = xprt_rdma_bc_destroy,
  686. #endif
  687. };
  688. static struct xprt_class xprt_rdma = {
  689. .list = LIST_HEAD_INIT(xprt_rdma.list),
  690. .name = "rdma",
  691. .owner = THIS_MODULE,
  692. .ident = XPRT_TRANSPORT_RDMA,
  693. .setup = xprt_setup_rdma,
  694. };
  695. void xprt_rdma_cleanup(void)
  696. {
  697. int rc;
  698. dprintk("RPCRDMA Module Removed, deregister RPC RDMA transport\n");
  699. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  700. if (sunrpc_table_header) {
  701. unregister_sysctl_table(sunrpc_table_header);
  702. sunrpc_table_header = NULL;
  703. }
  704. #endif
  705. rc = xprt_unregister_transport(&xprt_rdma);
  706. if (rc)
  707. dprintk("RPC: %s: xprt_unregister returned %i\n",
  708. __func__, rc);
  709. rpcrdma_destroy_wq();
  710. rc = xprt_unregister_transport(&xprt_rdma_bc);
  711. if (rc)
  712. dprintk("RPC: %s: xprt_unregister(bc) returned %i\n",
  713. __func__, rc);
  714. }
  715. int xprt_rdma_init(void)
  716. {
  717. int rc;
  718. rc = rpcrdma_alloc_wq();
  719. if (rc)
  720. return rc;
  721. rc = xprt_register_transport(&xprt_rdma);
  722. if (rc) {
  723. rpcrdma_destroy_wq();
  724. return rc;
  725. }
  726. rc = xprt_register_transport(&xprt_rdma_bc);
  727. if (rc) {
  728. xprt_unregister_transport(&xprt_rdma);
  729. rpcrdma_destroy_wq();
  730. return rc;
  731. }
  732. dprintk("RPCRDMA Module Init, register RPC RDMA transport\n");
  733. dprintk("Defaults:\n");
  734. dprintk("\tSlots %d\n"
  735. "\tMaxInlineRead %d\n\tMaxInlineWrite %d\n",
  736. xprt_rdma_slot_table_entries,
  737. xprt_rdma_max_inline_read, xprt_rdma_max_inline_write);
  738. dprintk("\tPadding %d\n\tMemreg %d\n",
  739. xprt_rdma_inline_write_padding, xprt_rdma_memreg_strategy);
  740. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  741. if (!sunrpc_table_header)
  742. sunrpc_table_header = register_sysctl_table(sunrpc_table);
  743. #endif
  744. return 0;
  745. }