transport.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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. static 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 zero;
  69. static unsigned int max_padding = PAGE_SIZE;
  70. static unsigned int min_memreg = RPCRDMA_BOUNCEBUFFERS;
  71. static unsigned int max_memreg = RPCRDMA_LAST - 1;
  72. static struct ctl_table_header *sunrpc_table_header;
  73. static struct ctl_table xr_tunables_table[] = {
  74. {
  75. .procname = "rdma_slot_table_entries",
  76. .data = &xprt_rdma_slot_table_entries,
  77. .maxlen = sizeof(unsigned int),
  78. .mode = 0644,
  79. .proc_handler = proc_dointvec_minmax,
  80. .extra1 = &min_slot_table_size,
  81. .extra2 = &max_slot_table_size
  82. },
  83. {
  84. .procname = "rdma_max_inline_read",
  85. .data = &xprt_rdma_max_inline_read,
  86. .maxlen = sizeof(unsigned int),
  87. .mode = 0644,
  88. .proc_handler = proc_dointvec,
  89. },
  90. {
  91. .procname = "rdma_max_inline_write",
  92. .data = &xprt_rdma_max_inline_write,
  93. .maxlen = sizeof(unsigned int),
  94. .mode = 0644,
  95. .proc_handler = proc_dointvec,
  96. },
  97. {
  98. .procname = "rdma_inline_write_padding",
  99. .data = &xprt_rdma_inline_write_padding,
  100. .maxlen = sizeof(unsigned int),
  101. .mode = 0644,
  102. .proc_handler = proc_dointvec_minmax,
  103. .extra1 = &zero,
  104. .extra2 = &max_padding,
  105. },
  106. {
  107. .procname = "rdma_memreg_strategy",
  108. .data = &xprt_rdma_memreg_strategy,
  109. .maxlen = sizeof(unsigned int),
  110. .mode = 0644,
  111. .proc_handler = proc_dointvec_minmax,
  112. .extra1 = &min_memreg,
  113. .extra2 = &max_memreg,
  114. },
  115. {
  116. .procname = "rdma_pad_optimize",
  117. .data = &xprt_rdma_pad_optimize,
  118. .maxlen = sizeof(unsigned int),
  119. .mode = 0644,
  120. .proc_handler = proc_dointvec,
  121. },
  122. { },
  123. };
  124. static struct ctl_table sunrpc_table[] = {
  125. {
  126. .procname = "sunrpc",
  127. .mode = 0555,
  128. .child = xr_tunables_table
  129. },
  130. { },
  131. };
  132. #endif
  133. #define RPCRDMA_BIND_TO (60U * HZ)
  134. #define RPCRDMA_INIT_REEST_TO (5U * HZ)
  135. #define RPCRDMA_MAX_REEST_TO (30U * HZ)
  136. #define RPCRDMA_IDLE_DISC_TO (5U * 60 * HZ)
  137. static struct rpc_xprt_ops xprt_rdma_procs; /* forward reference */
  138. static void
  139. xprt_rdma_format_addresses4(struct rpc_xprt *xprt, struct sockaddr *sap)
  140. {
  141. struct sockaddr_in *sin = (struct sockaddr_in *)sap;
  142. char buf[20];
  143. snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
  144. xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
  145. xprt->address_strings[RPC_DISPLAY_NETID] = RPCBIND_NETID_RDMA;
  146. }
  147. static void
  148. xprt_rdma_format_addresses6(struct rpc_xprt *xprt, struct sockaddr *sap)
  149. {
  150. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
  151. char buf[40];
  152. snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
  153. xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
  154. xprt->address_strings[RPC_DISPLAY_NETID] = RPCBIND_NETID_RDMA6;
  155. }
  156. static void
  157. xprt_rdma_format_addresses(struct rpc_xprt *xprt, struct sockaddr *sap)
  158. {
  159. char buf[128];
  160. switch (sap->sa_family) {
  161. case AF_INET:
  162. xprt_rdma_format_addresses4(xprt, sap);
  163. break;
  164. case AF_INET6:
  165. xprt_rdma_format_addresses6(xprt, sap);
  166. break;
  167. default:
  168. pr_err("rpcrdma: Unrecognized address family\n");
  169. return;
  170. }
  171. (void)rpc_ntop(sap, buf, sizeof(buf));
  172. xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
  173. snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
  174. xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
  175. snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
  176. xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
  177. xprt->address_strings[RPC_DISPLAY_PROTO] = "rdma";
  178. }
  179. static void
  180. xprt_rdma_free_addresses(struct rpc_xprt *xprt)
  181. {
  182. unsigned int i;
  183. for (i = 0; i < RPC_DISPLAY_MAX; i++)
  184. switch (i) {
  185. case RPC_DISPLAY_PROTO:
  186. case RPC_DISPLAY_NETID:
  187. continue;
  188. default:
  189. kfree(xprt->address_strings[i]);
  190. }
  191. }
  192. static void
  193. xprt_rdma_connect_worker(struct work_struct *work)
  194. {
  195. struct rpcrdma_xprt *r_xprt = container_of(work, struct rpcrdma_xprt,
  196. rx_connect_worker.work);
  197. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  198. int rc = 0;
  199. xprt_clear_connected(xprt);
  200. dprintk("RPC: %s: %sconnect\n", __func__,
  201. r_xprt->rx_ep.rep_connected != 0 ? "re" : "");
  202. rc = rpcrdma_ep_connect(&r_xprt->rx_ep, &r_xprt->rx_ia);
  203. if (rc)
  204. xprt_wake_pending_tasks(xprt, rc);
  205. dprintk("RPC: %s: exit\n", __func__);
  206. xprt_clear_connecting(xprt);
  207. }
  208. static void
  209. xprt_rdma_inject_disconnect(struct rpc_xprt *xprt)
  210. {
  211. struct rpcrdma_xprt *r_xprt = container_of(xprt, struct rpcrdma_xprt,
  212. rx_xprt);
  213. pr_info("rpcrdma: injecting transport disconnect on xprt=%p\n", xprt);
  214. rdma_disconnect(r_xprt->rx_ia.ri_id);
  215. }
  216. /*
  217. * xprt_rdma_destroy
  218. *
  219. * Destroy the xprt.
  220. * Free all memory associated with the object, including its own.
  221. * NOTE: none of the *destroy methods free memory for their top-level
  222. * objects, even though they may have allocated it (they do free
  223. * private memory). It's up to the caller to handle it. In this
  224. * case (RDMA transport), all structure memory is inlined with the
  225. * struct rpcrdma_xprt.
  226. */
  227. static void
  228. xprt_rdma_destroy(struct rpc_xprt *xprt)
  229. {
  230. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  231. dprintk("RPC: %s: called\n", __func__);
  232. cancel_delayed_work_sync(&r_xprt->rx_connect_worker);
  233. xprt_clear_connected(xprt);
  234. rpcrdma_ep_destroy(&r_xprt->rx_ep, &r_xprt->rx_ia);
  235. rpcrdma_buffer_destroy(&r_xprt->rx_buf);
  236. rpcrdma_ia_close(&r_xprt->rx_ia);
  237. xprt_rdma_free_addresses(xprt);
  238. xprt_free(xprt);
  239. dprintk("RPC: %s: returning\n", __func__);
  240. module_put(THIS_MODULE);
  241. }
  242. static const struct rpc_timeout xprt_rdma_default_timeout = {
  243. .to_initval = 60 * HZ,
  244. .to_maxval = 60 * HZ,
  245. };
  246. /**
  247. * xprt_setup_rdma - Set up transport to use RDMA
  248. *
  249. * @args: rpc transport arguments
  250. */
  251. static struct rpc_xprt *
  252. xprt_setup_rdma(struct xprt_create *args)
  253. {
  254. struct rpcrdma_create_data_internal cdata;
  255. struct rpc_xprt *xprt;
  256. struct rpcrdma_xprt *new_xprt;
  257. struct rpcrdma_ep *new_ep;
  258. struct sockaddr *sap;
  259. int rc;
  260. if (args->addrlen > sizeof(xprt->addr)) {
  261. dprintk("RPC: %s: address too large\n", __func__);
  262. return ERR_PTR(-EBADF);
  263. }
  264. xprt = xprt_alloc(args->net, sizeof(struct rpcrdma_xprt),
  265. xprt_rdma_slot_table_entries,
  266. xprt_rdma_slot_table_entries);
  267. if (xprt == NULL) {
  268. dprintk("RPC: %s: couldn't allocate rpcrdma_xprt\n",
  269. __func__);
  270. return ERR_PTR(-ENOMEM);
  271. }
  272. /* 60 second timeout, no retries */
  273. xprt->timeout = &xprt_rdma_default_timeout;
  274. xprt->bind_timeout = RPCRDMA_BIND_TO;
  275. xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
  276. xprt->idle_timeout = RPCRDMA_IDLE_DISC_TO;
  277. xprt->resvport = 0; /* privileged port not needed */
  278. xprt->tsh_size = 0; /* RPC-RDMA handles framing */
  279. xprt->ops = &xprt_rdma_procs;
  280. /*
  281. * Set up RDMA-specific connect data.
  282. */
  283. sap = (struct sockaddr *)&cdata.addr;
  284. memcpy(sap, args->dstaddr, args->addrlen);
  285. /* Ensure xprt->addr holds valid server TCP (not RDMA)
  286. * address, for any side protocols which peek at it */
  287. xprt->prot = IPPROTO_TCP;
  288. xprt->addrlen = args->addrlen;
  289. memcpy(&xprt->addr, sap, xprt->addrlen);
  290. if (rpc_get_port(sap))
  291. xprt_set_bound(xprt);
  292. cdata.max_requests = xprt->max_reqs;
  293. cdata.rsize = RPCRDMA_MAX_SEGS * PAGE_SIZE; /* RDMA write max */
  294. cdata.wsize = RPCRDMA_MAX_SEGS * PAGE_SIZE; /* RDMA read max */
  295. cdata.inline_wsize = xprt_rdma_max_inline_write;
  296. if (cdata.inline_wsize > cdata.wsize)
  297. cdata.inline_wsize = cdata.wsize;
  298. cdata.inline_rsize = xprt_rdma_max_inline_read;
  299. if (cdata.inline_rsize > cdata.rsize)
  300. cdata.inline_rsize = cdata.rsize;
  301. cdata.padding = xprt_rdma_inline_write_padding;
  302. /*
  303. * Create new transport instance, which includes initialized
  304. * o ia
  305. * o endpoint
  306. * o buffers
  307. */
  308. new_xprt = rpcx_to_rdmax(xprt);
  309. rc = rpcrdma_ia_open(new_xprt, sap, xprt_rdma_memreg_strategy);
  310. if (rc)
  311. goto out1;
  312. /*
  313. * initialize and create ep
  314. */
  315. new_xprt->rx_data = cdata;
  316. new_ep = &new_xprt->rx_ep;
  317. new_ep->rep_remote_addr = cdata.addr;
  318. rc = rpcrdma_ep_create(&new_xprt->rx_ep,
  319. &new_xprt->rx_ia, &new_xprt->rx_data);
  320. if (rc)
  321. goto out2;
  322. /*
  323. * Allocate pre-registered send and receive buffers for headers and
  324. * any inline data. Also specify any padding which will be provided
  325. * from a preregistered zero buffer.
  326. */
  327. rc = rpcrdma_buffer_create(new_xprt);
  328. if (rc)
  329. goto out3;
  330. /*
  331. * Register a callback for connection events. This is necessary because
  332. * connection loss notification is async. We also catch connection loss
  333. * when reaping receives.
  334. */
  335. INIT_DELAYED_WORK(&new_xprt->rx_connect_worker,
  336. xprt_rdma_connect_worker);
  337. xprt_rdma_format_addresses(xprt, sap);
  338. xprt->max_payload = new_xprt->rx_ia.ri_ops->ro_maxpages(new_xprt);
  339. if (xprt->max_payload == 0)
  340. goto out4;
  341. xprt->max_payload <<= PAGE_SHIFT;
  342. dprintk("RPC: %s: transport data payload maximum: %zu bytes\n",
  343. __func__, xprt->max_payload);
  344. if (!try_module_get(THIS_MODULE))
  345. goto out4;
  346. dprintk("RPC: %s: %s:%s\n", __func__,
  347. xprt->address_strings[RPC_DISPLAY_ADDR],
  348. xprt->address_strings[RPC_DISPLAY_PORT]);
  349. return xprt;
  350. out4:
  351. xprt_rdma_free_addresses(xprt);
  352. rc = -EINVAL;
  353. out3:
  354. rpcrdma_ep_destroy(new_ep, &new_xprt->rx_ia);
  355. out2:
  356. rpcrdma_ia_close(&new_xprt->rx_ia);
  357. out1:
  358. xprt_free(xprt);
  359. return ERR_PTR(rc);
  360. }
  361. /*
  362. * Close a connection, during shutdown or timeout/reconnect
  363. */
  364. static void
  365. xprt_rdma_close(struct rpc_xprt *xprt)
  366. {
  367. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  368. dprintk("RPC: %s: closing\n", __func__);
  369. if (r_xprt->rx_ep.rep_connected > 0)
  370. xprt->reestablish_timeout = 0;
  371. xprt_disconnect_done(xprt);
  372. rpcrdma_ep_disconnect(&r_xprt->rx_ep, &r_xprt->rx_ia);
  373. }
  374. static void
  375. xprt_rdma_set_port(struct rpc_xprt *xprt, u16 port)
  376. {
  377. struct sockaddr_in *sap;
  378. sap = (struct sockaddr_in *)&xprt->addr;
  379. sap->sin_port = htons(port);
  380. sap = (struct sockaddr_in *)&rpcx_to_rdmad(xprt).addr;
  381. sap->sin_port = htons(port);
  382. dprintk("RPC: %s: %u\n", __func__, port);
  383. }
  384. static void
  385. xprt_rdma_connect(struct rpc_xprt *xprt, struct rpc_task *task)
  386. {
  387. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  388. if (r_xprt->rx_ep.rep_connected != 0) {
  389. /* Reconnect */
  390. schedule_delayed_work(&r_xprt->rx_connect_worker,
  391. xprt->reestablish_timeout);
  392. xprt->reestablish_timeout <<= 1;
  393. if (xprt->reestablish_timeout > RPCRDMA_MAX_REEST_TO)
  394. xprt->reestablish_timeout = RPCRDMA_MAX_REEST_TO;
  395. else if (xprt->reestablish_timeout < RPCRDMA_INIT_REEST_TO)
  396. xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
  397. } else {
  398. schedule_delayed_work(&r_xprt->rx_connect_worker, 0);
  399. if (!RPC_IS_ASYNC(task))
  400. flush_delayed_work(&r_xprt->rx_connect_worker);
  401. }
  402. }
  403. /*
  404. * The RDMA allocate/free functions need the task structure as a place
  405. * to hide the struct rpcrdma_req, which is necessary for the actual send/recv
  406. * sequence.
  407. *
  408. * The RPC layer allocates both send and receive buffers in the same call
  409. * (rq_send_buf and rq_rcv_buf are both part of a single contiguous buffer).
  410. * We may register rq_rcv_buf when using reply chunks.
  411. */
  412. static void *
  413. xprt_rdma_allocate(struct rpc_task *task, size_t size)
  414. {
  415. struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
  416. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  417. struct rpcrdma_regbuf *rb;
  418. struct rpcrdma_req *req;
  419. size_t min_size;
  420. gfp_t flags;
  421. req = rpcrdma_buffer_get(&r_xprt->rx_buf);
  422. if (req == NULL)
  423. return NULL;
  424. flags = GFP_NOIO | __GFP_NOWARN;
  425. if (RPC_IS_SWAPPER(task))
  426. flags = __GFP_MEMALLOC | GFP_NOWAIT | __GFP_NOWARN;
  427. if (req->rl_rdmabuf == NULL)
  428. goto out_rdmabuf;
  429. if (req->rl_sendbuf == NULL)
  430. goto out_sendbuf;
  431. if (size > req->rl_sendbuf->rg_size)
  432. goto out_sendbuf;
  433. out:
  434. dprintk("RPC: %s: size %zd, request 0x%p\n", __func__, size, req);
  435. req->rl_connect_cookie = 0; /* our reserved value */
  436. return req->rl_sendbuf->rg_base;
  437. out_rdmabuf:
  438. min_size = RPCRDMA_INLINE_WRITE_THRESHOLD(task->tk_rqstp);
  439. rb = rpcrdma_alloc_regbuf(&r_xprt->rx_ia, min_size, flags);
  440. if (IS_ERR(rb))
  441. goto out_fail;
  442. req->rl_rdmabuf = rb;
  443. out_sendbuf:
  444. /* XDR encoding and RPC/RDMA marshaling of this request has not
  445. * yet occurred. Thus a lower bound is needed to prevent buffer
  446. * overrun during marshaling.
  447. *
  448. * RPC/RDMA marshaling may choose to send payload bearing ops
  449. * inline, if the result is smaller than the inline threshold.
  450. * The value of the "size" argument accounts for header
  451. * requirements but not for the payload in these cases.
  452. *
  453. * Likewise, allocate enough space to receive a reply up to the
  454. * size of the inline threshold.
  455. *
  456. * It's unlikely that both the send header and the received
  457. * reply will be large, but slush is provided here to allow
  458. * flexibility when marshaling.
  459. */
  460. min_size = RPCRDMA_INLINE_READ_THRESHOLD(task->tk_rqstp);
  461. min_size += RPCRDMA_INLINE_WRITE_THRESHOLD(task->tk_rqstp);
  462. if (size < min_size)
  463. size = min_size;
  464. rb = rpcrdma_alloc_regbuf(&r_xprt->rx_ia, size, flags);
  465. if (IS_ERR(rb))
  466. goto out_fail;
  467. rb->rg_owner = req;
  468. r_xprt->rx_stats.hardway_register_count += size;
  469. rpcrdma_free_regbuf(&r_xprt->rx_ia, req->rl_sendbuf);
  470. req->rl_sendbuf = rb;
  471. goto out;
  472. out_fail:
  473. rpcrdma_buffer_put(req);
  474. r_xprt->rx_stats.failed_marshal_count++;
  475. return NULL;
  476. }
  477. /*
  478. * This function returns all RDMA resources to the pool.
  479. */
  480. static void
  481. xprt_rdma_free(void *buffer)
  482. {
  483. struct rpcrdma_req *req;
  484. struct rpcrdma_xprt *r_xprt;
  485. struct rpcrdma_regbuf *rb;
  486. int i;
  487. if (buffer == NULL)
  488. return;
  489. rb = container_of(buffer, struct rpcrdma_regbuf, rg_base[0]);
  490. req = rb->rg_owner;
  491. r_xprt = container_of(req->rl_buffer, struct rpcrdma_xprt, rx_buf);
  492. dprintk("RPC: %s: called on 0x%p\n", __func__, req->rl_reply);
  493. for (i = 0; req->rl_nchunks;) {
  494. --req->rl_nchunks;
  495. i += r_xprt->rx_ia.ri_ops->ro_unmap(r_xprt,
  496. &req->rl_segments[i]);
  497. }
  498. rpcrdma_buffer_put(req);
  499. }
  500. /*
  501. * send_request invokes the meat of RPC RDMA. It must do the following:
  502. * 1. Marshal the RPC request into an RPC RDMA request, which means
  503. * putting a header in front of data, and creating IOVs for RDMA
  504. * from those in the request.
  505. * 2. In marshaling, detect opportunities for RDMA, and use them.
  506. * 3. Post a recv message to set up asynch completion, then send
  507. * the request (rpcrdma_ep_post).
  508. * 4. No partial sends are possible in the RPC-RDMA protocol (as in UDP).
  509. */
  510. static int
  511. xprt_rdma_send_request(struct rpc_task *task)
  512. {
  513. struct rpc_rqst *rqst = task->tk_rqstp;
  514. struct rpc_xprt *xprt = rqst->rq_xprt;
  515. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  516. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  517. int rc = 0;
  518. rc = rpcrdma_marshal_req(rqst);
  519. if (rc < 0)
  520. goto failed_marshal;
  521. if (req->rl_reply == NULL) /* e.g. reconnection */
  522. rpcrdma_recv_buffer_get(req);
  523. /* Must suppress retransmit to maintain credits */
  524. if (req->rl_connect_cookie == xprt->connect_cookie)
  525. goto drop_connection;
  526. req->rl_connect_cookie = xprt->connect_cookie;
  527. if (rpcrdma_ep_post(&r_xprt->rx_ia, &r_xprt->rx_ep, req))
  528. goto drop_connection;
  529. rqst->rq_xmit_bytes_sent += rqst->rq_snd_buf.len;
  530. rqst->rq_bytes_sent = 0;
  531. return 0;
  532. failed_marshal:
  533. r_xprt->rx_stats.failed_marshal_count++;
  534. dprintk("RPC: %s: rpcrdma_marshal_req failed, status %i\n",
  535. __func__, rc);
  536. if (rc == -EIO)
  537. return -EIO;
  538. drop_connection:
  539. xprt_disconnect_done(xprt);
  540. return -ENOTCONN; /* implies disconnect */
  541. }
  542. static void xprt_rdma_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
  543. {
  544. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  545. long idle_time = 0;
  546. if (xprt_connected(xprt))
  547. idle_time = (long)(jiffies - xprt->last_used) / HZ;
  548. seq_puts(seq, "\txprt:\trdma ");
  549. seq_printf(seq, "%u %lu %lu %lu %ld %lu %lu %lu %llu %llu ",
  550. 0, /* need a local port? */
  551. xprt->stat.bind_count,
  552. xprt->stat.connect_count,
  553. xprt->stat.connect_time,
  554. idle_time,
  555. xprt->stat.sends,
  556. xprt->stat.recvs,
  557. xprt->stat.bad_xids,
  558. xprt->stat.req_u,
  559. xprt->stat.bklog_u);
  560. seq_printf(seq, "%lu %lu %lu %llu %llu %llu %llu %lu %lu %lu %lu\n",
  561. r_xprt->rx_stats.read_chunk_count,
  562. r_xprt->rx_stats.write_chunk_count,
  563. r_xprt->rx_stats.reply_chunk_count,
  564. r_xprt->rx_stats.total_rdma_request,
  565. r_xprt->rx_stats.total_rdma_reply,
  566. r_xprt->rx_stats.pullup_copy_count,
  567. r_xprt->rx_stats.fixup_copy_count,
  568. r_xprt->rx_stats.hardway_register_count,
  569. r_xprt->rx_stats.failed_marshal_count,
  570. r_xprt->rx_stats.bad_reply_count,
  571. r_xprt->rx_stats.nomsg_call_count);
  572. }
  573. static int
  574. xprt_rdma_enable_swap(struct rpc_xprt *xprt)
  575. {
  576. return 0;
  577. }
  578. static void
  579. xprt_rdma_disable_swap(struct rpc_xprt *xprt)
  580. {
  581. }
  582. /*
  583. * Plumbing for rpc transport switch and kernel module
  584. */
  585. static struct rpc_xprt_ops xprt_rdma_procs = {
  586. .reserve_xprt = xprt_reserve_xprt_cong,
  587. .release_xprt = xprt_release_xprt_cong, /* sunrpc/xprt.c */
  588. .alloc_slot = xprt_alloc_slot,
  589. .release_request = xprt_release_rqst_cong, /* ditto */
  590. .set_retrans_timeout = xprt_set_retrans_timeout_def, /* ditto */
  591. .rpcbind = rpcb_getport_async, /* sunrpc/rpcb_clnt.c */
  592. .set_port = xprt_rdma_set_port,
  593. .connect = xprt_rdma_connect,
  594. .buf_alloc = xprt_rdma_allocate,
  595. .buf_free = xprt_rdma_free,
  596. .send_request = xprt_rdma_send_request,
  597. .close = xprt_rdma_close,
  598. .destroy = xprt_rdma_destroy,
  599. .print_stats = xprt_rdma_print_stats,
  600. .enable_swap = xprt_rdma_enable_swap,
  601. .disable_swap = xprt_rdma_disable_swap,
  602. .inject_disconnect = xprt_rdma_inject_disconnect,
  603. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  604. .bc_setup = xprt_rdma_bc_setup,
  605. .bc_up = xprt_rdma_bc_up,
  606. .bc_free_rqst = xprt_rdma_bc_free_rqst,
  607. .bc_destroy = xprt_rdma_bc_destroy,
  608. #endif
  609. };
  610. static struct xprt_class xprt_rdma = {
  611. .list = LIST_HEAD_INIT(xprt_rdma.list),
  612. .name = "rdma",
  613. .owner = THIS_MODULE,
  614. .ident = XPRT_TRANSPORT_RDMA,
  615. .setup = xprt_setup_rdma,
  616. };
  617. void xprt_rdma_cleanup(void)
  618. {
  619. int rc;
  620. dprintk("RPCRDMA Module Removed, deregister RPC RDMA transport\n");
  621. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  622. if (sunrpc_table_header) {
  623. unregister_sysctl_table(sunrpc_table_header);
  624. sunrpc_table_header = NULL;
  625. }
  626. #endif
  627. rc = xprt_unregister_transport(&xprt_rdma);
  628. if (rc)
  629. dprintk("RPC: %s: xprt_unregister returned %i\n",
  630. __func__, rc);
  631. rpcrdma_destroy_wq();
  632. frwr_destroy_recovery_wq();
  633. }
  634. int xprt_rdma_init(void)
  635. {
  636. int rc;
  637. rc = frwr_alloc_recovery_wq();
  638. if (rc)
  639. return rc;
  640. rc = rpcrdma_alloc_wq();
  641. if (rc) {
  642. frwr_destroy_recovery_wq();
  643. return rc;
  644. }
  645. rc = xprt_register_transport(&xprt_rdma);
  646. if (rc) {
  647. rpcrdma_destroy_wq();
  648. frwr_destroy_recovery_wq();
  649. return rc;
  650. }
  651. dprintk("RPCRDMA Module Init, register RPC RDMA transport\n");
  652. dprintk("Defaults:\n");
  653. dprintk("\tSlots %d\n"
  654. "\tMaxInlineRead %d\n\tMaxInlineWrite %d\n",
  655. xprt_rdma_slot_table_entries,
  656. xprt_rdma_max_inline_read, xprt_rdma_max_inline_write);
  657. dprintk("\tPadding %d\n\tMemreg %d\n",
  658. xprt_rdma_inline_write_padding, xprt_rdma_memreg_strategy);
  659. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  660. if (!sunrpc_table_header)
  661. sunrpc_table_header = register_sysctl_table(sunrpc_table);
  662. #endif
  663. return 0;
  664. }