transport.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. /*
  3. * Copyright (c) 2014-2017 Oracle. All rights reserved.
  4. * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the BSD-type
  10. * license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * Neither the name of the Network Appliance, Inc. nor the names of
  25. * its contributors may be used to endorse or promote products
  26. * derived from this software without specific prior written
  27. * permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. */
  41. /*
  42. * transport.c
  43. *
  44. * This file contains the top-level implementation of an RPC RDMA
  45. * transport.
  46. *
  47. * Naming convention: functions beginning with xprt_ are part of the
  48. * transport switch. All others are RPC RDMA internal.
  49. */
  50. #include <linux/module.h>
  51. #include <linux/slab.h>
  52. #include <linux/seq_file.h>
  53. #include <linux/smp.h>
  54. #include <linux/sunrpc/addr.h>
  55. #include <linux/sunrpc/svc_rdma.h>
  56. #include "xprt_rdma.h"
  57. #include <trace/events/rpcrdma.h>
  58. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  59. # define RPCDBG_FACILITY RPCDBG_TRANS
  60. #endif
  61. /*
  62. * tunables
  63. */
  64. static unsigned int xprt_rdma_slot_table_entries = RPCRDMA_DEF_SLOT_TABLE;
  65. unsigned int xprt_rdma_max_inline_read = RPCRDMA_DEF_INLINE;
  66. static unsigned int xprt_rdma_max_inline_write = RPCRDMA_DEF_INLINE;
  67. unsigned int xprt_rdma_memreg_strategy = RPCRDMA_FRWR;
  68. int xprt_rdma_pad_optimize;
  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 min_inline_size = RPCRDMA_MIN_INLINE;
  73. static unsigned int max_inline_size = RPCRDMA_MAX_INLINE;
  74. static unsigned int zero;
  75. static unsigned int max_padding = PAGE_SIZE;
  76. static unsigned int min_memreg = RPCRDMA_BOUNCEBUFFERS;
  77. static unsigned int max_memreg = RPCRDMA_LAST - 1;
  78. static unsigned int dummy;
  79. static struct ctl_table_header *sunrpc_table_header;
  80. static struct ctl_table xr_tunables_table[] = {
  81. {
  82. .procname = "rdma_slot_table_entries",
  83. .data = &xprt_rdma_slot_table_entries,
  84. .maxlen = sizeof(unsigned int),
  85. .mode = 0644,
  86. .proc_handler = proc_dointvec_minmax,
  87. .extra1 = &min_slot_table_size,
  88. .extra2 = &max_slot_table_size
  89. },
  90. {
  91. .procname = "rdma_max_inline_read",
  92. .data = &xprt_rdma_max_inline_read,
  93. .maxlen = sizeof(unsigned int),
  94. .mode = 0644,
  95. .proc_handler = proc_dointvec_minmax,
  96. .extra1 = &min_inline_size,
  97. .extra2 = &max_inline_size,
  98. },
  99. {
  100. .procname = "rdma_max_inline_write",
  101. .data = &xprt_rdma_max_inline_write,
  102. .maxlen = sizeof(unsigned int),
  103. .mode = 0644,
  104. .proc_handler = proc_dointvec_minmax,
  105. .extra1 = &min_inline_size,
  106. .extra2 = &max_inline_size,
  107. },
  108. {
  109. .procname = "rdma_inline_write_padding",
  110. .data = &dummy,
  111. .maxlen = sizeof(unsigned int),
  112. .mode = 0644,
  113. .proc_handler = proc_dointvec_minmax,
  114. .extra1 = &zero,
  115. .extra2 = &max_padding,
  116. },
  117. {
  118. .procname = "rdma_memreg_strategy",
  119. .data = &xprt_rdma_memreg_strategy,
  120. .maxlen = sizeof(unsigned int),
  121. .mode = 0644,
  122. .proc_handler = proc_dointvec_minmax,
  123. .extra1 = &min_memreg,
  124. .extra2 = &max_memreg,
  125. },
  126. {
  127. .procname = "rdma_pad_optimize",
  128. .data = &xprt_rdma_pad_optimize,
  129. .maxlen = sizeof(unsigned int),
  130. .mode = 0644,
  131. .proc_handler = proc_dointvec,
  132. },
  133. { },
  134. };
  135. static struct ctl_table sunrpc_table[] = {
  136. {
  137. .procname = "sunrpc",
  138. .mode = 0555,
  139. .child = xr_tunables_table
  140. },
  141. { },
  142. };
  143. #endif
  144. static const struct rpc_xprt_ops xprt_rdma_procs;
  145. static void
  146. xprt_rdma_format_addresses4(struct rpc_xprt *xprt, struct sockaddr *sap)
  147. {
  148. struct sockaddr_in *sin = (struct sockaddr_in *)sap;
  149. char buf[20];
  150. snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
  151. xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
  152. xprt->address_strings[RPC_DISPLAY_NETID] = RPCBIND_NETID_RDMA;
  153. }
  154. static void
  155. xprt_rdma_format_addresses6(struct rpc_xprt *xprt, struct sockaddr *sap)
  156. {
  157. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
  158. char buf[40];
  159. snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
  160. xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
  161. xprt->address_strings[RPC_DISPLAY_NETID] = RPCBIND_NETID_RDMA6;
  162. }
  163. void
  164. xprt_rdma_format_addresses(struct rpc_xprt *xprt, struct sockaddr *sap)
  165. {
  166. char buf[128];
  167. switch (sap->sa_family) {
  168. case AF_INET:
  169. xprt_rdma_format_addresses4(xprt, sap);
  170. break;
  171. case AF_INET6:
  172. xprt_rdma_format_addresses6(xprt, sap);
  173. break;
  174. default:
  175. pr_err("rpcrdma: Unrecognized address family\n");
  176. return;
  177. }
  178. (void)rpc_ntop(sap, buf, sizeof(buf));
  179. xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
  180. snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
  181. xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
  182. snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
  183. xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
  184. xprt->address_strings[RPC_DISPLAY_PROTO] = "rdma";
  185. }
  186. void
  187. xprt_rdma_free_addresses(struct rpc_xprt *xprt)
  188. {
  189. unsigned int i;
  190. for (i = 0; i < RPC_DISPLAY_MAX; i++)
  191. switch (i) {
  192. case RPC_DISPLAY_PROTO:
  193. case RPC_DISPLAY_NETID:
  194. continue;
  195. default:
  196. kfree(xprt->address_strings[i]);
  197. }
  198. }
  199. void
  200. rpcrdma_conn_func(struct rpcrdma_ep *ep)
  201. {
  202. schedule_delayed_work(&ep->rep_connect_worker, 0);
  203. }
  204. void
  205. rpcrdma_connect_worker(struct work_struct *work)
  206. {
  207. struct rpcrdma_ep *ep =
  208. container_of(work, struct rpcrdma_ep, rep_connect_worker.work);
  209. struct rpcrdma_xprt *r_xprt =
  210. container_of(ep, struct rpcrdma_xprt, rx_ep);
  211. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  212. spin_lock_bh(&xprt->transport_lock);
  213. if (ep->rep_connected > 0) {
  214. if (!xprt_test_and_set_connected(xprt)) {
  215. xprt->stat.connect_count++;
  216. xprt->stat.connect_time += (long)jiffies -
  217. xprt->stat.connect_start;
  218. xprt_wake_pending_tasks(xprt, 0);
  219. }
  220. } else {
  221. if (xprt_test_and_clear_connected(xprt))
  222. xprt_wake_pending_tasks(xprt, -ENOTCONN);
  223. }
  224. spin_unlock_bh(&xprt->transport_lock);
  225. }
  226. static void
  227. xprt_rdma_connect_worker(struct work_struct *work)
  228. {
  229. struct rpcrdma_xprt *r_xprt = container_of(work, struct rpcrdma_xprt,
  230. rx_connect_worker.work);
  231. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  232. int rc = 0;
  233. xprt_clear_connected(xprt);
  234. rc = rpcrdma_ep_connect(&r_xprt->rx_ep, &r_xprt->rx_ia);
  235. if (rc)
  236. xprt_wake_pending_tasks(xprt, rc);
  237. xprt_clear_connecting(xprt);
  238. }
  239. static void
  240. xprt_rdma_inject_disconnect(struct rpc_xprt *xprt)
  241. {
  242. struct rpcrdma_xprt *r_xprt = container_of(xprt, struct rpcrdma_xprt,
  243. rx_xprt);
  244. trace_xprtrdma_inject_dsc(r_xprt);
  245. rdma_disconnect(r_xprt->rx_ia.ri_id);
  246. }
  247. /*
  248. * xprt_rdma_destroy
  249. *
  250. * Destroy the xprt.
  251. * Free all memory associated with the object, including its own.
  252. * NOTE: none of the *destroy methods free memory for their top-level
  253. * objects, even though they may have allocated it (they do free
  254. * private memory). It's up to the caller to handle it. In this
  255. * case (RDMA transport), all structure memory is inlined with the
  256. * struct rpcrdma_xprt.
  257. */
  258. static void
  259. xprt_rdma_destroy(struct rpc_xprt *xprt)
  260. {
  261. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  262. trace_xprtrdma_destroy(r_xprt);
  263. cancel_delayed_work_sync(&r_xprt->rx_connect_worker);
  264. xprt_clear_connected(xprt);
  265. rpcrdma_ep_destroy(&r_xprt->rx_ep, &r_xprt->rx_ia);
  266. rpcrdma_buffer_destroy(&r_xprt->rx_buf);
  267. rpcrdma_ia_close(&r_xprt->rx_ia);
  268. xprt_rdma_free_addresses(xprt);
  269. xprt_free(xprt);
  270. module_put(THIS_MODULE);
  271. }
  272. static const struct rpc_timeout xprt_rdma_default_timeout = {
  273. .to_initval = 60 * HZ,
  274. .to_maxval = 60 * HZ,
  275. };
  276. /**
  277. * xprt_setup_rdma - Set up transport to use RDMA
  278. *
  279. * @args: rpc transport arguments
  280. */
  281. static struct rpc_xprt *
  282. xprt_setup_rdma(struct xprt_create *args)
  283. {
  284. struct rpcrdma_create_data_internal cdata;
  285. struct rpc_xprt *xprt;
  286. struct rpcrdma_xprt *new_xprt;
  287. struct rpcrdma_ep *new_ep;
  288. struct sockaddr *sap;
  289. int rc;
  290. if (args->addrlen > sizeof(xprt->addr)) {
  291. dprintk("RPC: %s: address too large\n", __func__);
  292. return ERR_PTR(-EBADF);
  293. }
  294. xprt = xprt_alloc(args->net, sizeof(struct rpcrdma_xprt), 0, 0);
  295. if (xprt == NULL) {
  296. dprintk("RPC: %s: couldn't allocate rpcrdma_xprt\n",
  297. __func__);
  298. return ERR_PTR(-ENOMEM);
  299. }
  300. /* 60 second timeout, no retries */
  301. xprt->timeout = &xprt_rdma_default_timeout;
  302. xprt->bind_timeout = RPCRDMA_BIND_TO;
  303. xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
  304. xprt->idle_timeout = RPCRDMA_IDLE_DISC_TO;
  305. xprt->resvport = 0; /* privileged port not needed */
  306. xprt->tsh_size = 0; /* RPC-RDMA handles framing */
  307. xprt->ops = &xprt_rdma_procs;
  308. /*
  309. * Set up RDMA-specific connect data.
  310. */
  311. sap = args->dstaddr;
  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. xprt_rdma_format_addresses(xprt, sap);
  320. cdata.max_requests = xprt_rdma_slot_table_entries;
  321. cdata.rsize = RPCRDMA_MAX_SEGS * PAGE_SIZE; /* RDMA write max */
  322. cdata.wsize = RPCRDMA_MAX_SEGS * PAGE_SIZE; /* RDMA read max */
  323. cdata.inline_wsize = xprt_rdma_max_inline_write;
  324. if (cdata.inline_wsize > cdata.wsize)
  325. cdata.inline_wsize = cdata.wsize;
  326. cdata.inline_rsize = xprt_rdma_max_inline_read;
  327. if (cdata.inline_rsize > cdata.rsize)
  328. cdata.inline_rsize = cdata.rsize;
  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);
  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. rc = rpcrdma_ep_create(&new_xprt->rx_ep,
  345. &new_xprt->rx_ia, &new_xprt->rx_data);
  346. if (rc)
  347. goto out2;
  348. rc = rpcrdma_buffer_create(new_xprt);
  349. if (rc)
  350. goto out3;
  351. INIT_DELAYED_WORK(&new_xprt->rx_connect_worker,
  352. xprt_rdma_connect_worker);
  353. xprt->max_payload = new_xprt->rx_ia.ri_ops->ro_maxpages(new_xprt);
  354. if (xprt->max_payload == 0)
  355. goto out4;
  356. xprt->max_payload <<= PAGE_SHIFT;
  357. dprintk("RPC: %s: transport data payload maximum: %zu bytes\n",
  358. __func__, xprt->max_payload);
  359. if (!try_module_get(THIS_MODULE))
  360. goto out4;
  361. dprintk("RPC: %s: %s:%s\n", __func__,
  362. xprt->address_strings[RPC_DISPLAY_ADDR],
  363. xprt->address_strings[RPC_DISPLAY_PORT]);
  364. trace_xprtrdma_create(new_xprt);
  365. return xprt;
  366. out4:
  367. rpcrdma_buffer_destroy(&new_xprt->rx_buf);
  368. rc = -ENODEV;
  369. out3:
  370. rpcrdma_ep_destroy(new_ep, &new_xprt->rx_ia);
  371. out2:
  372. rpcrdma_ia_close(&new_xprt->rx_ia);
  373. out1:
  374. trace_xprtrdma_destroy(new_xprt);
  375. xprt_rdma_free_addresses(xprt);
  376. xprt_free(xprt);
  377. return ERR_PTR(rc);
  378. }
  379. /**
  380. * xprt_rdma_close - Close down RDMA connection
  381. * @xprt: generic transport to be closed
  382. *
  383. * Called during transport shutdown reconnect, or device
  384. * removal. Caller holds the transport's write lock.
  385. */
  386. static void
  387. xprt_rdma_close(struct rpc_xprt *xprt)
  388. {
  389. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  390. struct rpcrdma_ep *ep = &r_xprt->rx_ep;
  391. struct rpcrdma_ia *ia = &r_xprt->rx_ia;
  392. dprintk("RPC: %s: closing xprt %p\n", __func__, xprt);
  393. if (test_and_clear_bit(RPCRDMA_IAF_REMOVING, &ia->ri_flags)) {
  394. xprt_clear_connected(xprt);
  395. rpcrdma_ia_remove(ia);
  396. return;
  397. }
  398. if (ep->rep_connected == -ENODEV)
  399. return;
  400. if (ep->rep_connected > 0)
  401. xprt->reestablish_timeout = 0;
  402. xprt_disconnect_done(xprt);
  403. rpcrdma_ep_disconnect(ep, ia);
  404. /* Prepare @xprt for the next connection by reinitializing
  405. * its credit grant to one (see RFC 8166, Section 3.3.3).
  406. */
  407. r_xprt->rx_buf.rb_credits = 1;
  408. xprt->cwnd = RPC_CWNDSHIFT;
  409. }
  410. /**
  411. * xprt_rdma_set_port - update server port with rpcbind result
  412. * @xprt: controlling RPC transport
  413. * @port: new port value
  414. *
  415. * Transport connect status is unchanged.
  416. */
  417. static void
  418. xprt_rdma_set_port(struct rpc_xprt *xprt, u16 port)
  419. {
  420. struct sockaddr *sap = (struct sockaddr *)&xprt->addr;
  421. char buf[8];
  422. dprintk("RPC: %s: setting port for xprt %p (%s:%s) to %u\n",
  423. __func__, xprt,
  424. xprt->address_strings[RPC_DISPLAY_ADDR],
  425. xprt->address_strings[RPC_DISPLAY_PORT],
  426. port);
  427. rpc_set_port(sap, port);
  428. kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
  429. snprintf(buf, sizeof(buf), "%u", port);
  430. xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
  431. kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
  432. snprintf(buf, sizeof(buf), "%4hx", port);
  433. xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
  434. }
  435. /**
  436. * xprt_rdma_timer - invoked when an RPC times out
  437. * @xprt: controlling RPC transport
  438. * @task: RPC task that timed out
  439. *
  440. * Invoked when the transport is still connected, but an RPC
  441. * retransmit timeout occurs.
  442. *
  443. * Since RDMA connections don't have a keep-alive, forcibly
  444. * disconnect and retry to connect. This drives full
  445. * detection of the network path, and retransmissions of
  446. * all pending RPCs.
  447. */
  448. static void
  449. xprt_rdma_timer(struct rpc_xprt *xprt, struct rpc_task *task)
  450. {
  451. xprt_force_disconnect(xprt);
  452. }
  453. static void
  454. xprt_rdma_connect(struct rpc_xprt *xprt, struct rpc_task *task)
  455. {
  456. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  457. if (r_xprt->rx_ep.rep_connected != 0) {
  458. /* Reconnect */
  459. schedule_delayed_work(&r_xprt->rx_connect_worker,
  460. xprt->reestablish_timeout);
  461. xprt->reestablish_timeout <<= 1;
  462. if (xprt->reestablish_timeout > RPCRDMA_MAX_REEST_TO)
  463. xprt->reestablish_timeout = RPCRDMA_MAX_REEST_TO;
  464. else if (xprt->reestablish_timeout < RPCRDMA_INIT_REEST_TO)
  465. xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
  466. } else {
  467. schedule_delayed_work(&r_xprt->rx_connect_worker, 0);
  468. if (!RPC_IS_ASYNC(task))
  469. flush_delayed_work(&r_xprt->rx_connect_worker);
  470. }
  471. }
  472. /**
  473. * xprt_rdma_alloc_slot - allocate an rpc_rqst
  474. * @xprt: controlling RPC transport
  475. * @task: RPC task requesting a fresh rpc_rqst
  476. *
  477. * tk_status values:
  478. * %0 if task->tk_rqstp points to a fresh rpc_rqst
  479. * %-EAGAIN if no rpc_rqst is available; queued on backlog
  480. */
  481. static void
  482. xprt_rdma_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
  483. {
  484. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  485. struct rpcrdma_req *req;
  486. req = rpcrdma_buffer_get(&r_xprt->rx_buf);
  487. if (!req)
  488. goto out_sleep;
  489. task->tk_rqstp = &req->rl_slot;
  490. task->tk_status = 0;
  491. return;
  492. out_sleep:
  493. rpc_sleep_on(&xprt->backlog, task, NULL);
  494. task->tk_status = -EAGAIN;
  495. }
  496. /**
  497. * xprt_rdma_free_slot - release an rpc_rqst
  498. * @xprt: controlling RPC transport
  499. * @rqst: rpc_rqst to release
  500. *
  501. */
  502. static void
  503. xprt_rdma_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *rqst)
  504. {
  505. memset(rqst, 0, sizeof(*rqst));
  506. rpcrdma_buffer_put(rpcr_to_rdmar(rqst));
  507. rpc_wake_up_next(&xprt->backlog);
  508. }
  509. static bool
  510. rpcrdma_get_sendbuf(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req,
  511. size_t size, gfp_t flags)
  512. {
  513. struct rpcrdma_regbuf *rb;
  514. if (req->rl_sendbuf && rdmab_length(req->rl_sendbuf) >= size)
  515. return true;
  516. rb = rpcrdma_alloc_regbuf(size, DMA_TO_DEVICE, flags);
  517. if (IS_ERR(rb))
  518. return false;
  519. rpcrdma_free_regbuf(req->rl_sendbuf);
  520. r_xprt->rx_stats.hardway_register_count += size;
  521. req->rl_sendbuf = rb;
  522. return true;
  523. }
  524. /* The rq_rcv_buf is used only if a Reply chunk is necessary.
  525. * The decision to use a Reply chunk is made later in
  526. * rpcrdma_marshal_req. This buffer is registered at that time.
  527. *
  528. * Otherwise, the associated RPC Reply arrives in a separate
  529. * Receive buffer, arbitrarily chosen by the HCA. The buffer
  530. * allocated here for the RPC Reply is not utilized in that
  531. * case. See rpcrdma_inline_fixup.
  532. *
  533. * A regbuf is used here to remember the buffer size.
  534. */
  535. static bool
  536. rpcrdma_get_recvbuf(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req,
  537. size_t size, gfp_t flags)
  538. {
  539. struct rpcrdma_regbuf *rb;
  540. if (req->rl_recvbuf && rdmab_length(req->rl_recvbuf) >= size)
  541. return true;
  542. rb = rpcrdma_alloc_regbuf(size, DMA_NONE, flags);
  543. if (IS_ERR(rb))
  544. return false;
  545. rpcrdma_free_regbuf(req->rl_recvbuf);
  546. r_xprt->rx_stats.hardway_register_count += size;
  547. req->rl_recvbuf = rb;
  548. return true;
  549. }
  550. /**
  551. * xprt_rdma_allocate - allocate transport resources for an RPC
  552. * @task: RPC task
  553. *
  554. * Return values:
  555. * 0: Success; rq_buffer points to RPC buffer to use
  556. * ENOMEM: Out of memory, call again later
  557. * EIO: A permanent error occurred, do not retry
  558. *
  559. * The RDMA allocate/free functions need the task structure as a place
  560. * to hide the struct rpcrdma_req, which is necessary for the actual
  561. * send/recv sequence.
  562. *
  563. * xprt_rdma_allocate provides buffers that are already mapped for
  564. * DMA, and a local DMA lkey is provided for each.
  565. */
  566. static int
  567. xprt_rdma_allocate(struct rpc_task *task)
  568. {
  569. struct rpc_rqst *rqst = task->tk_rqstp;
  570. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
  571. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  572. gfp_t flags;
  573. flags = RPCRDMA_DEF_GFP;
  574. if (RPC_IS_SWAPPER(task))
  575. flags = __GFP_MEMALLOC | GFP_NOWAIT | __GFP_NOWARN;
  576. if (!rpcrdma_get_sendbuf(r_xprt, req, rqst->rq_callsize, flags))
  577. goto out_fail;
  578. if (!rpcrdma_get_recvbuf(r_xprt, req, rqst->rq_rcvsize, flags))
  579. goto out_fail;
  580. rqst->rq_buffer = req->rl_sendbuf->rg_base;
  581. rqst->rq_rbuffer = req->rl_recvbuf->rg_base;
  582. trace_xprtrdma_allocate(task, req);
  583. return 0;
  584. out_fail:
  585. trace_xprtrdma_allocate(task, NULL);
  586. return -ENOMEM;
  587. }
  588. /**
  589. * xprt_rdma_free - release resources allocated by xprt_rdma_allocate
  590. * @task: RPC task
  591. *
  592. * Caller guarantees rqst->rq_buffer is non-NULL.
  593. */
  594. static void
  595. xprt_rdma_free(struct rpc_task *task)
  596. {
  597. struct rpc_rqst *rqst = task->tk_rqstp;
  598. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
  599. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  600. if (test_bit(RPCRDMA_REQ_F_PENDING, &req->rl_flags))
  601. rpcrdma_release_rqst(r_xprt, req);
  602. trace_xprtrdma_rpc_done(task, req);
  603. }
  604. /**
  605. * xprt_rdma_send_request - marshal and send an RPC request
  606. * @task: RPC task with an RPC message in rq_snd_buf
  607. *
  608. * Caller holds the transport's write lock.
  609. *
  610. * Returns:
  611. * %0 if the RPC message has been sent
  612. * %-ENOTCONN if the caller should reconnect and call again
  613. * %-EAGAIN if the caller should call again
  614. * %-ENOBUFS if the caller should call again after a delay
  615. * %-EIO if a permanent error occurred and the request was not
  616. * sent. Do not try to send this message again.
  617. */
  618. static int
  619. xprt_rdma_send_request(struct rpc_task *task)
  620. {
  621. struct rpc_rqst *rqst = task->tk_rqstp;
  622. struct rpc_xprt *xprt = rqst->rq_xprt;
  623. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  624. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  625. int rc = 0;
  626. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  627. if (unlikely(!rqst->rq_buffer))
  628. return xprt_rdma_bc_send_reply(rqst);
  629. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  630. if (!xprt_connected(xprt))
  631. goto drop_connection;
  632. rc = rpcrdma_marshal_req(r_xprt, rqst);
  633. if (rc < 0)
  634. goto failed_marshal;
  635. /* Must suppress retransmit to maintain credits */
  636. if (rqst->rq_connect_cookie == xprt->connect_cookie)
  637. goto drop_connection;
  638. rqst->rq_xtime = ktime_get();
  639. __set_bit(RPCRDMA_REQ_F_PENDING, &req->rl_flags);
  640. if (rpcrdma_ep_post(&r_xprt->rx_ia, &r_xprt->rx_ep, req))
  641. goto drop_connection;
  642. rqst->rq_xmit_bytes_sent += rqst->rq_snd_buf.len;
  643. rqst->rq_bytes_sent = 0;
  644. /* An RPC with no reply will throw off credit accounting,
  645. * so drop the connection to reset the credit grant.
  646. */
  647. if (!rpc_reply_expected(task))
  648. goto drop_connection;
  649. return 0;
  650. failed_marshal:
  651. if (rc != -ENOTCONN)
  652. return rc;
  653. drop_connection:
  654. xprt_disconnect_done(xprt);
  655. return -ENOTCONN; /* implies disconnect */
  656. }
  657. void xprt_rdma_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
  658. {
  659. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  660. long idle_time = 0;
  661. if (xprt_connected(xprt))
  662. idle_time = (long)(jiffies - xprt->last_used) / HZ;
  663. seq_puts(seq, "\txprt:\trdma ");
  664. seq_printf(seq, "%u %lu %lu %lu %ld %lu %lu %lu %llu %llu ",
  665. 0, /* need a local port? */
  666. xprt->stat.bind_count,
  667. xprt->stat.connect_count,
  668. xprt->stat.connect_time,
  669. idle_time,
  670. xprt->stat.sends,
  671. xprt->stat.recvs,
  672. xprt->stat.bad_xids,
  673. xprt->stat.req_u,
  674. xprt->stat.bklog_u);
  675. seq_printf(seq, "%lu %lu %lu %llu %llu %llu %llu %lu %lu %lu %lu ",
  676. r_xprt->rx_stats.read_chunk_count,
  677. r_xprt->rx_stats.write_chunk_count,
  678. r_xprt->rx_stats.reply_chunk_count,
  679. r_xprt->rx_stats.total_rdma_request,
  680. r_xprt->rx_stats.total_rdma_reply,
  681. r_xprt->rx_stats.pullup_copy_count,
  682. r_xprt->rx_stats.fixup_copy_count,
  683. r_xprt->rx_stats.hardway_register_count,
  684. r_xprt->rx_stats.failed_marshal_count,
  685. r_xprt->rx_stats.bad_reply_count,
  686. r_xprt->rx_stats.nomsg_call_count);
  687. seq_printf(seq, "%lu %lu %lu %lu %lu %lu\n",
  688. r_xprt->rx_stats.mrs_recovered,
  689. r_xprt->rx_stats.mrs_orphaned,
  690. r_xprt->rx_stats.mrs_allocated,
  691. r_xprt->rx_stats.local_inv_needed,
  692. r_xprt->rx_stats.empty_sendctx_q,
  693. r_xprt->rx_stats.reply_waits_for_send);
  694. }
  695. static int
  696. xprt_rdma_enable_swap(struct rpc_xprt *xprt)
  697. {
  698. return 0;
  699. }
  700. static void
  701. xprt_rdma_disable_swap(struct rpc_xprt *xprt)
  702. {
  703. }
  704. /*
  705. * Plumbing for rpc transport switch and kernel module
  706. */
  707. static const struct rpc_xprt_ops xprt_rdma_procs = {
  708. .reserve_xprt = xprt_reserve_xprt_cong,
  709. .release_xprt = xprt_release_xprt_cong, /* sunrpc/xprt.c */
  710. .alloc_slot = xprt_rdma_alloc_slot,
  711. .free_slot = xprt_rdma_free_slot,
  712. .release_request = xprt_release_rqst_cong, /* ditto */
  713. .set_retrans_timeout = xprt_set_retrans_timeout_def, /* ditto */
  714. .timer = xprt_rdma_timer,
  715. .rpcbind = rpcb_getport_async, /* sunrpc/rpcb_clnt.c */
  716. .set_port = xprt_rdma_set_port,
  717. .connect = xprt_rdma_connect,
  718. .buf_alloc = xprt_rdma_allocate,
  719. .buf_free = xprt_rdma_free,
  720. .send_request = xprt_rdma_send_request,
  721. .close = xprt_rdma_close,
  722. .destroy = xprt_rdma_destroy,
  723. .print_stats = xprt_rdma_print_stats,
  724. .enable_swap = xprt_rdma_enable_swap,
  725. .disable_swap = xprt_rdma_disable_swap,
  726. .inject_disconnect = xprt_rdma_inject_disconnect,
  727. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  728. .bc_setup = xprt_rdma_bc_setup,
  729. .bc_up = xprt_rdma_bc_up,
  730. .bc_maxpayload = xprt_rdma_bc_maxpayload,
  731. .bc_free_rqst = xprt_rdma_bc_free_rqst,
  732. .bc_destroy = xprt_rdma_bc_destroy,
  733. #endif
  734. };
  735. static struct xprt_class xprt_rdma = {
  736. .list = LIST_HEAD_INIT(xprt_rdma.list),
  737. .name = "rdma",
  738. .owner = THIS_MODULE,
  739. .ident = XPRT_TRANSPORT_RDMA,
  740. .setup = xprt_setup_rdma,
  741. .netid = { "rdma", "rdma6", "" },
  742. };
  743. void xprt_rdma_cleanup(void)
  744. {
  745. int rc;
  746. dprintk("RPCRDMA Module Removed, deregister RPC RDMA transport\n");
  747. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  748. if (sunrpc_table_header) {
  749. unregister_sysctl_table(sunrpc_table_header);
  750. sunrpc_table_header = NULL;
  751. }
  752. #endif
  753. rc = xprt_unregister_transport(&xprt_rdma);
  754. if (rc)
  755. dprintk("RPC: %s: xprt_unregister returned %i\n",
  756. __func__, rc);
  757. rpcrdma_destroy_wq();
  758. rc = xprt_unregister_transport(&xprt_rdma_bc);
  759. if (rc)
  760. dprintk("RPC: %s: xprt_unregister(bc) returned %i\n",
  761. __func__, rc);
  762. }
  763. int xprt_rdma_init(void)
  764. {
  765. int rc;
  766. rc = rpcrdma_alloc_wq();
  767. if (rc)
  768. return rc;
  769. rc = xprt_register_transport(&xprt_rdma);
  770. if (rc) {
  771. rpcrdma_destroy_wq();
  772. return rc;
  773. }
  774. rc = xprt_register_transport(&xprt_rdma_bc);
  775. if (rc) {
  776. xprt_unregister_transport(&xprt_rdma);
  777. rpcrdma_destroy_wq();
  778. return rc;
  779. }
  780. dprintk("RPCRDMA Module Init, register RPC RDMA transport\n");
  781. dprintk("Defaults:\n");
  782. dprintk("\tSlots %d\n"
  783. "\tMaxInlineRead %d\n\tMaxInlineWrite %d\n",
  784. xprt_rdma_slot_table_entries,
  785. xprt_rdma_max_inline_read, xprt_rdma_max_inline_write);
  786. dprintk("\tPadding 0\n\tMemreg %d\n", xprt_rdma_memreg_strategy);
  787. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  788. if (!sunrpc_table_header)
  789. sunrpc_table_header = register_sysctl_table(sunrpc_table);
  790. #endif
  791. return 0;
  792. }