transport.c 20 KB

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