transport.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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. 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 max_padding = PAGE_SIZE;
  75. static unsigned int min_memreg = RPCRDMA_BOUNCEBUFFERS;
  76. static unsigned int max_memreg = RPCRDMA_LAST - 1;
  77. static unsigned int dummy;
  78. static struct ctl_table_header *sunrpc_table_header;
  79. static struct ctl_table xr_tunables_table[] = {
  80. {
  81. .procname = "rdma_slot_table_entries",
  82. .data = &xprt_rdma_slot_table_entries,
  83. .maxlen = sizeof(unsigned int),
  84. .mode = 0644,
  85. .proc_handler = proc_dointvec_minmax,
  86. .extra1 = &min_slot_table_size,
  87. .extra2 = &max_slot_table_size
  88. },
  89. {
  90. .procname = "rdma_max_inline_read",
  91. .data = &xprt_rdma_max_inline_read,
  92. .maxlen = sizeof(unsigned int),
  93. .mode = 0644,
  94. .proc_handler = proc_dointvec_minmax,
  95. .extra1 = &min_inline_size,
  96. .extra2 = &max_inline_size,
  97. },
  98. {
  99. .procname = "rdma_max_inline_write",
  100. .data = &xprt_rdma_max_inline_write,
  101. .maxlen = sizeof(unsigned int),
  102. .mode = 0644,
  103. .proc_handler = proc_dointvec_minmax,
  104. .extra1 = &min_inline_size,
  105. .extra2 = &max_inline_size,
  106. },
  107. {
  108. .procname = "rdma_inline_write_padding",
  109. .data = &dummy,
  110. .maxlen = sizeof(unsigned int),
  111. .mode = 0644,
  112. .proc_handler = proc_dointvec_minmax,
  113. .extra1 = SYSCTL_ZERO,
  114. .extra2 = &max_padding,
  115. },
  116. {
  117. .procname = "rdma_memreg_strategy",
  118. .data = &xprt_rdma_memreg_strategy,
  119. .maxlen = sizeof(unsigned int),
  120. .mode = 0644,
  121. .proc_handler = proc_dointvec_minmax,
  122. .extra1 = &min_memreg,
  123. .extra2 = &max_memreg,
  124. },
  125. {
  126. .procname = "rdma_pad_optimize",
  127. .data = &xprt_rdma_pad_optimize,
  128. .maxlen = sizeof(unsigned int),
  129. .mode = 0644,
  130. .proc_handler = proc_dointvec,
  131. },
  132. { },
  133. };
  134. static struct ctl_table sunrpc_table[] = {
  135. {
  136. .procname = "sunrpc",
  137. .mode = 0555,
  138. .child = xr_tunables_table
  139. },
  140. { },
  141. };
  142. #endif
  143. static const struct rpc_xprt_ops xprt_rdma_procs;
  144. static void
  145. xprt_rdma_format_addresses4(struct rpc_xprt *xprt, struct sockaddr *sap)
  146. {
  147. struct sockaddr_in *sin = (struct sockaddr_in *)sap;
  148. char buf[20];
  149. snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
  150. xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
  151. xprt->address_strings[RPC_DISPLAY_NETID] = RPCBIND_NETID_RDMA;
  152. }
  153. static void
  154. xprt_rdma_format_addresses6(struct rpc_xprt *xprt, struct sockaddr *sap)
  155. {
  156. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
  157. char buf[40];
  158. snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
  159. xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
  160. xprt->address_strings[RPC_DISPLAY_NETID] = RPCBIND_NETID_RDMA6;
  161. }
  162. void
  163. xprt_rdma_format_addresses(struct rpc_xprt *xprt, struct sockaddr *sap)
  164. {
  165. char buf[128];
  166. switch (sap->sa_family) {
  167. case AF_INET:
  168. xprt_rdma_format_addresses4(xprt, sap);
  169. break;
  170. case AF_INET6:
  171. xprt_rdma_format_addresses6(xprt, sap);
  172. break;
  173. default:
  174. pr_err("rpcrdma: Unrecognized address family\n");
  175. return;
  176. }
  177. (void)rpc_ntop(sap, buf, sizeof(buf));
  178. xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
  179. snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
  180. xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
  181. snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
  182. xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
  183. xprt->address_strings[RPC_DISPLAY_PROTO] = "rdma";
  184. }
  185. void
  186. xprt_rdma_free_addresses(struct rpc_xprt *xprt)
  187. {
  188. unsigned int i;
  189. for (i = 0; i < RPC_DISPLAY_MAX; i++)
  190. switch (i) {
  191. case RPC_DISPLAY_PROTO:
  192. case RPC_DISPLAY_NETID:
  193. continue;
  194. default:
  195. kfree(xprt->address_strings[i]);
  196. }
  197. }
  198. /**
  199. * xprt_rdma_connect_worker - establish connection in the background
  200. * @work: worker thread context
  201. *
  202. * Requester holds the xprt's send lock to prevent activity on this
  203. * transport while a fresh connection is being established. RPC tasks
  204. * sleep on the xprt's pending queue waiting for connect to complete.
  205. */
  206. static void
  207. xprt_rdma_connect_worker(struct work_struct *work)
  208. {
  209. struct rpcrdma_xprt *r_xprt = container_of(work, struct rpcrdma_xprt,
  210. rx_connect_worker.work);
  211. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  212. int rc;
  213. rc = rpcrdma_xprt_connect(r_xprt);
  214. xprt_clear_connecting(xprt);
  215. if (!rc) {
  216. xprt->connect_cookie++;
  217. xprt->stat.connect_count++;
  218. xprt->stat.connect_time += (long)jiffies -
  219. xprt->stat.connect_start;
  220. xprt_set_connected(xprt);
  221. rc = -EAGAIN;
  222. } else
  223. rpcrdma_xprt_disconnect(r_xprt);
  224. xprt_unlock_connect(xprt, r_xprt);
  225. xprt_wake_pending_tasks(xprt, rc);
  226. }
  227. /**
  228. * xprt_rdma_inject_disconnect - inject a connection fault
  229. * @xprt: transport context
  230. *
  231. * If @xprt is connected, disconnect it to simulate spurious
  232. * connection loss. Caller must hold @xprt's send lock to
  233. * ensure that data structures and hardware resources are
  234. * stable during the rdma_disconnect() call.
  235. */
  236. static void
  237. xprt_rdma_inject_disconnect(struct rpc_xprt *xprt)
  238. {
  239. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  240. trace_xprtrdma_op_inject_dsc(r_xprt);
  241. rdma_disconnect(r_xprt->rx_ep->re_id);
  242. }
  243. /**
  244. * xprt_rdma_destroy - Full tear down of transport
  245. * @xprt: doomed transport context
  246. *
  247. * Caller guarantees there will be no more calls to us with
  248. * this @xprt.
  249. */
  250. static void
  251. xprt_rdma_destroy(struct rpc_xprt *xprt)
  252. {
  253. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  254. cancel_delayed_work_sync(&r_xprt->rx_connect_worker);
  255. rpcrdma_xprt_disconnect(r_xprt);
  256. rpcrdma_buffer_destroy(&r_xprt->rx_buf);
  257. xprt_rdma_free_addresses(xprt);
  258. xprt_free(xprt);
  259. module_put(THIS_MODULE);
  260. }
  261. /* 60 second timeout, no retries */
  262. static const struct rpc_timeout xprt_rdma_default_timeout = {
  263. .to_initval = 60 * HZ,
  264. .to_maxval = 60 * HZ,
  265. };
  266. /**
  267. * xprt_setup_rdma - Set up transport to use RDMA
  268. *
  269. * @args: rpc transport arguments
  270. */
  271. static struct rpc_xprt *
  272. xprt_setup_rdma(struct xprt_create *args)
  273. {
  274. struct rpc_xprt *xprt;
  275. struct rpcrdma_xprt *new_xprt;
  276. struct sockaddr *sap;
  277. int rc;
  278. if (args->addrlen > sizeof(xprt->addr))
  279. return ERR_PTR(-EBADF);
  280. if (!try_module_get(THIS_MODULE))
  281. return ERR_PTR(-EIO);
  282. xprt = xprt_alloc(args->net, sizeof(struct rpcrdma_xprt), 0,
  283. xprt_rdma_slot_table_entries);
  284. if (!xprt) {
  285. module_put(THIS_MODULE);
  286. return ERR_PTR(-ENOMEM);
  287. }
  288. xprt->timeout = &xprt_rdma_default_timeout;
  289. xprt->connect_timeout = xprt->timeout->to_initval;
  290. xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
  291. xprt->bind_timeout = RPCRDMA_BIND_TO;
  292. xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
  293. xprt->idle_timeout = RPCRDMA_IDLE_DISC_TO;
  294. xprt->resvport = 0; /* privileged port not needed */
  295. xprt->ops = &xprt_rdma_procs;
  296. /*
  297. * Set up RDMA-specific connect data.
  298. */
  299. sap = args->dstaddr;
  300. /* Ensure xprt->addr holds valid server TCP (not RDMA)
  301. * address, for any side protocols which peek at it */
  302. xprt->prot = IPPROTO_TCP;
  303. xprt->addrlen = args->addrlen;
  304. memcpy(&xprt->addr, sap, xprt->addrlen);
  305. if (rpc_get_port(sap))
  306. xprt_set_bound(xprt);
  307. xprt_rdma_format_addresses(xprt, sap);
  308. new_xprt = rpcx_to_rdmax(xprt);
  309. rc = rpcrdma_buffer_create(new_xprt);
  310. if (rc) {
  311. xprt_rdma_free_addresses(xprt);
  312. xprt_free(xprt);
  313. module_put(THIS_MODULE);
  314. return ERR_PTR(rc);
  315. }
  316. INIT_DELAYED_WORK(&new_xprt->rx_connect_worker,
  317. xprt_rdma_connect_worker);
  318. xprt->max_payload = RPCRDMA_MAX_DATA_SEGS << PAGE_SHIFT;
  319. return xprt;
  320. }
  321. /**
  322. * xprt_rdma_close - close a transport connection
  323. * @xprt: transport context
  324. *
  325. * Called during autoclose or device removal.
  326. *
  327. * Caller holds @xprt's send lock to prevent activity on this
  328. * transport while the connection is torn down.
  329. */
  330. void xprt_rdma_close(struct rpc_xprt *xprt)
  331. {
  332. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  333. rpcrdma_xprt_disconnect(r_xprt);
  334. xprt->reestablish_timeout = 0;
  335. ++xprt->connect_cookie;
  336. xprt_disconnect_done(xprt);
  337. }
  338. /**
  339. * xprt_rdma_set_port - update server port with rpcbind result
  340. * @xprt: controlling RPC transport
  341. * @port: new port value
  342. *
  343. * Transport connect status is unchanged.
  344. */
  345. static void
  346. xprt_rdma_set_port(struct rpc_xprt *xprt, u16 port)
  347. {
  348. struct sockaddr *sap = (struct sockaddr *)&xprt->addr;
  349. char buf[8];
  350. rpc_set_port(sap, port);
  351. kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
  352. snprintf(buf, sizeof(buf), "%u", port);
  353. xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
  354. kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
  355. snprintf(buf, sizeof(buf), "%4hx", port);
  356. xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
  357. }
  358. /**
  359. * xprt_rdma_timer - invoked when an RPC times out
  360. * @xprt: controlling RPC transport
  361. * @task: RPC task that timed out
  362. *
  363. * Invoked when the transport is still connected, but an RPC
  364. * retransmit timeout occurs.
  365. *
  366. * Since RDMA connections don't have a keep-alive, forcibly
  367. * disconnect and retry to connect. This drives full
  368. * detection of the network path, and retransmissions of
  369. * all pending RPCs.
  370. */
  371. static void
  372. xprt_rdma_timer(struct rpc_xprt *xprt, struct rpc_task *task)
  373. {
  374. xprt_force_disconnect(xprt);
  375. }
  376. /**
  377. * xprt_rdma_set_connect_timeout - set timeouts for establishing a connection
  378. * @xprt: controlling transport instance
  379. * @connect_timeout: reconnect timeout after client disconnects
  380. * @reconnect_timeout: reconnect timeout after server disconnects
  381. *
  382. */
  383. static void xprt_rdma_set_connect_timeout(struct rpc_xprt *xprt,
  384. unsigned long connect_timeout,
  385. unsigned long reconnect_timeout)
  386. {
  387. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  388. trace_xprtrdma_op_set_cto(r_xprt, connect_timeout, reconnect_timeout);
  389. spin_lock(&xprt->transport_lock);
  390. if (connect_timeout < xprt->connect_timeout) {
  391. struct rpc_timeout to;
  392. unsigned long initval;
  393. to = *xprt->timeout;
  394. initval = connect_timeout;
  395. if (initval < RPCRDMA_INIT_REEST_TO << 1)
  396. initval = RPCRDMA_INIT_REEST_TO << 1;
  397. to.to_initval = initval;
  398. to.to_maxval = initval;
  399. r_xprt->rx_timeout = to;
  400. xprt->timeout = &r_xprt->rx_timeout;
  401. xprt->connect_timeout = connect_timeout;
  402. }
  403. if (reconnect_timeout < xprt->max_reconnect_timeout)
  404. xprt->max_reconnect_timeout = reconnect_timeout;
  405. spin_unlock(&xprt->transport_lock);
  406. }
  407. /**
  408. * xprt_rdma_connect - schedule an attempt to reconnect
  409. * @xprt: transport state
  410. * @task: RPC scheduler context (unused)
  411. *
  412. */
  413. static void
  414. xprt_rdma_connect(struct rpc_xprt *xprt, struct rpc_task *task)
  415. {
  416. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  417. struct rpcrdma_ep *ep = r_xprt->rx_ep;
  418. unsigned long delay;
  419. WARN_ON_ONCE(!xprt_lock_connect(xprt, task, r_xprt));
  420. delay = 0;
  421. if (ep && ep->re_connect_status != 0) {
  422. delay = xprt_reconnect_delay(xprt);
  423. xprt_reconnect_backoff(xprt, RPCRDMA_INIT_REEST_TO);
  424. }
  425. trace_xprtrdma_op_connect(r_xprt, delay);
  426. queue_delayed_work(xprtiod_workqueue, &r_xprt->rx_connect_worker,
  427. delay);
  428. }
  429. /**
  430. * xprt_rdma_alloc_slot - allocate an rpc_rqst
  431. * @xprt: controlling RPC transport
  432. * @task: RPC task requesting a fresh rpc_rqst
  433. *
  434. * tk_status values:
  435. * %0 if task->tk_rqstp points to a fresh rpc_rqst
  436. * %-EAGAIN if no rpc_rqst is available; queued on backlog
  437. */
  438. static void
  439. xprt_rdma_alloc_slot(struct rpc_xprt *xprt, struct rpc_task *task)
  440. {
  441. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  442. struct rpcrdma_req *req;
  443. req = rpcrdma_buffer_get(&r_xprt->rx_buf);
  444. if (!req)
  445. goto out_sleep;
  446. task->tk_rqstp = &req->rl_slot;
  447. task->tk_status = 0;
  448. return;
  449. out_sleep:
  450. task->tk_status = -ENOMEM;
  451. xprt_add_backlog(xprt, task);
  452. }
  453. /**
  454. * xprt_rdma_free_slot - release an rpc_rqst
  455. * @xprt: controlling RPC transport
  456. * @rqst: rpc_rqst to release
  457. *
  458. */
  459. static void
  460. xprt_rdma_free_slot(struct rpc_xprt *xprt, struct rpc_rqst *rqst)
  461. {
  462. struct rpcrdma_xprt *r_xprt =
  463. container_of(xprt, struct rpcrdma_xprt, rx_xprt);
  464. rpcrdma_reply_put(&r_xprt->rx_buf, rpcr_to_rdmar(rqst));
  465. if (!xprt_wake_up_backlog(xprt, rqst)) {
  466. memset(rqst, 0, sizeof(*rqst));
  467. rpcrdma_buffer_put(&r_xprt->rx_buf, rpcr_to_rdmar(rqst));
  468. }
  469. }
  470. static bool rpcrdma_check_regbuf(struct rpcrdma_xprt *r_xprt,
  471. struct rpcrdma_regbuf *rb, size_t size,
  472. gfp_t flags)
  473. {
  474. if (unlikely(rdmab_length(rb) < size)) {
  475. if (!rpcrdma_regbuf_realloc(rb, size, flags))
  476. return false;
  477. r_xprt->rx_stats.hardway_register_count += size;
  478. }
  479. return true;
  480. }
  481. /**
  482. * xprt_rdma_allocate - allocate transport resources for an RPC
  483. * @task: RPC task
  484. *
  485. * Return values:
  486. * 0: Success; rq_buffer points to RPC buffer to use
  487. * ENOMEM: Out of memory, call again later
  488. * EIO: A permanent error occurred, do not retry
  489. */
  490. static int
  491. xprt_rdma_allocate(struct rpc_task *task)
  492. {
  493. struct rpc_rqst *rqst = task->tk_rqstp;
  494. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
  495. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  496. gfp_t flags;
  497. flags = RPCRDMA_DEF_GFP;
  498. if (RPC_IS_ASYNC(task))
  499. flags = GFP_NOWAIT | __GFP_NOWARN;
  500. if (RPC_IS_SWAPPER(task))
  501. flags |= __GFP_MEMALLOC;
  502. if (!rpcrdma_check_regbuf(r_xprt, req->rl_sendbuf, rqst->rq_callsize,
  503. flags))
  504. goto out_fail;
  505. if (!rpcrdma_check_regbuf(r_xprt, req->rl_recvbuf, rqst->rq_rcvsize,
  506. flags))
  507. goto out_fail;
  508. rqst->rq_buffer = rdmab_data(req->rl_sendbuf);
  509. rqst->rq_rbuffer = rdmab_data(req->rl_recvbuf);
  510. return 0;
  511. out_fail:
  512. return -ENOMEM;
  513. }
  514. /**
  515. * xprt_rdma_free - release resources allocated by xprt_rdma_allocate
  516. * @task: RPC task
  517. *
  518. * Caller guarantees rqst->rq_buffer is non-NULL.
  519. */
  520. static void
  521. xprt_rdma_free(struct rpc_task *task)
  522. {
  523. struct rpc_rqst *rqst = task->tk_rqstp;
  524. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
  525. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  526. if (!list_empty(&req->rl_registered))
  527. frwr_unmap_sync(r_xprt, req);
  528. /* XXX: If the RPC is completing because of a signal and
  529. * not because a reply was received, we ought to ensure
  530. * that the Send completion has fired, so that memory
  531. * involved with the Send is not still visible to the NIC.
  532. */
  533. }
  534. /**
  535. * xprt_rdma_send_request - marshal and send an RPC request
  536. * @rqst: RPC message in rq_snd_buf
  537. *
  538. * Caller holds the transport's write lock.
  539. *
  540. * Returns:
  541. * %0 if the RPC message has been sent
  542. * %-ENOTCONN if the caller should reconnect and call again
  543. * %-EAGAIN if the caller should call again
  544. * %-ENOBUFS if the caller should call again after a delay
  545. * %-EMSGSIZE if encoding ran out of buffer space. The request
  546. * was not sent. Do not try to send this message again.
  547. * %-EIO if an I/O error occurred. The request was not sent.
  548. * Do not try to send this message again.
  549. */
  550. static int
  551. xprt_rdma_send_request(struct rpc_rqst *rqst)
  552. {
  553. struct rpc_xprt *xprt = rqst->rq_xprt;
  554. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  555. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  556. int rc = 0;
  557. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  558. if (unlikely(!rqst->rq_buffer))
  559. return xprt_rdma_bc_send_reply(rqst);
  560. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  561. if (!xprt_connected(xprt))
  562. return -ENOTCONN;
  563. if (!xprt_request_get_cong(xprt, rqst))
  564. return -EBADSLT;
  565. rc = rpcrdma_marshal_req(r_xprt, rqst);
  566. if (rc < 0)
  567. goto failed_marshal;
  568. /* Must suppress retransmit to maintain credits */
  569. if (rqst->rq_connect_cookie == xprt->connect_cookie)
  570. goto drop_connection;
  571. rqst->rq_xtime = ktime_get();
  572. if (rpcrdma_post_sends(r_xprt, req))
  573. goto drop_connection;
  574. rqst->rq_xmit_bytes_sent += rqst->rq_snd_buf.len;
  575. /* An RPC with no reply will throw off credit accounting,
  576. * so drop the connection to reset the credit grant.
  577. */
  578. if (!rpc_reply_expected(rqst->rq_task))
  579. goto drop_connection;
  580. return 0;
  581. failed_marshal:
  582. if (rc != -ENOTCONN)
  583. return rc;
  584. drop_connection:
  585. xprt_rdma_close(xprt);
  586. return -ENOTCONN;
  587. }
  588. void xprt_rdma_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
  589. {
  590. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  591. long idle_time = 0;
  592. if (xprt_connected(xprt))
  593. idle_time = (long)(jiffies - xprt->last_used) / HZ;
  594. seq_puts(seq, "\txprt:\trdma ");
  595. seq_printf(seq, "%u %lu %lu %lu %ld %lu %lu %lu %llu %llu ",
  596. 0, /* need a local port? */
  597. xprt->stat.bind_count,
  598. xprt->stat.connect_count,
  599. xprt->stat.connect_time / HZ,
  600. idle_time,
  601. xprt->stat.sends,
  602. xprt->stat.recvs,
  603. xprt->stat.bad_xids,
  604. xprt->stat.req_u,
  605. xprt->stat.bklog_u);
  606. seq_printf(seq, "%lu %lu %lu %llu %llu %llu %llu %lu %lu %lu %lu ",
  607. r_xprt->rx_stats.read_chunk_count,
  608. r_xprt->rx_stats.write_chunk_count,
  609. r_xprt->rx_stats.reply_chunk_count,
  610. r_xprt->rx_stats.total_rdma_request,
  611. r_xprt->rx_stats.total_rdma_reply,
  612. r_xprt->rx_stats.pullup_copy_count,
  613. r_xprt->rx_stats.fixup_copy_count,
  614. r_xprt->rx_stats.hardway_register_count,
  615. r_xprt->rx_stats.failed_marshal_count,
  616. r_xprt->rx_stats.bad_reply_count,
  617. r_xprt->rx_stats.nomsg_call_count);
  618. seq_printf(seq, "%lu %lu %lu %lu %lu %lu\n",
  619. r_xprt->rx_stats.mrs_recycled,
  620. r_xprt->rx_stats.mrs_orphaned,
  621. r_xprt->rx_stats.mrs_allocated,
  622. r_xprt->rx_stats.local_inv_needed,
  623. r_xprt->rx_stats.empty_sendctx_q,
  624. r_xprt->rx_stats.reply_waits_for_send);
  625. }
  626. static int
  627. xprt_rdma_enable_swap(struct rpc_xprt *xprt)
  628. {
  629. return 0;
  630. }
  631. static void
  632. xprt_rdma_disable_swap(struct rpc_xprt *xprt)
  633. {
  634. }
  635. /*
  636. * Plumbing for rpc transport switch and kernel module
  637. */
  638. static const struct rpc_xprt_ops xprt_rdma_procs = {
  639. .reserve_xprt = xprt_reserve_xprt_cong,
  640. .release_xprt = xprt_release_xprt_cong, /* sunrpc/xprt.c */
  641. .alloc_slot = xprt_rdma_alloc_slot,
  642. .free_slot = xprt_rdma_free_slot,
  643. .release_request = xprt_release_rqst_cong, /* ditto */
  644. .wait_for_reply_request = xprt_wait_for_reply_request_def, /* ditto */
  645. .timer = xprt_rdma_timer,
  646. .rpcbind = rpcb_getport_async, /* sunrpc/rpcb_clnt.c */
  647. .set_port = xprt_rdma_set_port,
  648. .connect = xprt_rdma_connect,
  649. .buf_alloc = xprt_rdma_allocate,
  650. .buf_free = xprt_rdma_free,
  651. .send_request = xprt_rdma_send_request,
  652. .close = xprt_rdma_close,
  653. .destroy = xprt_rdma_destroy,
  654. .set_connect_timeout = xprt_rdma_set_connect_timeout,
  655. .print_stats = xprt_rdma_print_stats,
  656. .enable_swap = xprt_rdma_enable_swap,
  657. .disable_swap = xprt_rdma_disable_swap,
  658. .inject_disconnect = xprt_rdma_inject_disconnect,
  659. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  660. .bc_setup = xprt_rdma_bc_setup,
  661. .bc_maxpayload = xprt_rdma_bc_maxpayload,
  662. .bc_num_slots = xprt_rdma_bc_max_slots,
  663. .bc_free_rqst = xprt_rdma_bc_free_rqst,
  664. .bc_destroy = xprt_rdma_bc_destroy,
  665. #endif
  666. };
  667. static struct xprt_class xprt_rdma = {
  668. .list = LIST_HEAD_INIT(xprt_rdma.list),
  669. .name = "rdma",
  670. .owner = THIS_MODULE,
  671. .ident = XPRT_TRANSPORT_RDMA,
  672. .setup = xprt_setup_rdma,
  673. .netid = { "rdma", "rdma6", "" },
  674. };
  675. void xprt_rdma_cleanup(void)
  676. {
  677. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  678. if (sunrpc_table_header) {
  679. unregister_sysctl_table(sunrpc_table_header);
  680. sunrpc_table_header = NULL;
  681. }
  682. #endif
  683. xprt_unregister_transport(&xprt_rdma);
  684. xprt_unregister_transport(&xprt_rdma_bc);
  685. }
  686. int xprt_rdma_init(void)
  687. {
  688. int rc;
  689. rc = xprt_register_transport(&xprt_rdma);
  690. if (rc)
  691. return rc;
  692. rc = xprt_register_transport(&xprt_rdma_bc);
  693. if (rc) {
  694. xprt_unregister_transport(&xprt_rdma);
  695. return rc;
  696. }
  697. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  698. if (!sunrpc_table_header)
  699. sunrpc_table_header = register_sysctl_table(sunrpc_table);
  700. #endif
  701. return 0;
  702. }