backchannel.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2015 Oracle. All rights reserved.
  4. *
  5. * Support for backward direction RPCs on RPC/RDMA.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/sunrpc/xprt.h>
  9. #include <linux/sunrpc/svc.h>
  10. #include <linux/sunrpc/svc_xprt.h>
  11. #include <linux/sunrpc/svc_rdma.h>
  12. #include "xprt_rdma.h"
  13. #include <trace/events/rpcrdma.h>
  14. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  15. # define RPCDBG_FACILITY RPCDBG_TRANS
  16. #endif
  17. #undef RPCRDMA_BACKCHANNEL_DEBUG
  18. static void rpcrdma_bc_free_rqst(struct rpcrdma_xprt *r_xprt,
  19. struct rpc_rqst *rqst)
  20. {
  21. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  22. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  23. spin_lock(&buf->rb_reqslock);
  24. list_del(&req->rl_all);
  25. spin_unlock(&buf->rb_reqslock);
  26. rpcrdma_destroy_req(req);
  27. }
  28. static int rpcrdma_bc_setup_reqs(struct rpcrdma_xprt *r_xprt,
  29. unsigned int count)
  30. {
  31. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  32. struct rpc_rqst *rqst;
  33. unsigned int i;
  34. for (i = 0; i < (count << 1); i++) {
  35. struct rpcrdma_regbuf *rb;
  36. struct rpcrdma_req *req;
  37. size_t size;
  38. req = rpcrdma_create_req(r_xprt);
  39. if (IS_ERR(req))
  40. return PTR_ERR(req);
  41. rqst = &req->rl_slot;
  42. rqst->rq_xprt = xprt;
  43. INIT_LIST_HEAD(&rqst->rq_list);
  44. INIT_LIST_HEAD(&rqst->rq_bc_list);
  45. __set_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state);
  46. spin_lock_bh(&xprt->bc_pa_lock);
  47. list_add(&rqst->rq_bc_pa_list, &xprt->bc_pa_list);
  48. spin_unlock_bh(&xprt->bc_pa_lock);
  49. size = r_xprt->rx_data.inline_rsize;
  50. rb = rpcrdma_alloc_regbuf(size, DMA_TO_DEVICE, GFP_KERNEL);
  51. if (IS_ERR(rb))
  52. goto out_fail;
  53. req->rl_sendbuf = rb;
  54. xdr_buf_init(&rqst->rq_snd_buf, rb->rg_base,
  55. min_t(size_t, size, PAGE_SIZE));
  56. }
  57. return 0;
  58. out_fail:
  59. rpcrdma_bc_free_rqst(r_xprt, rqst);
  60. return -ENOMEM;
  61. }
  62. /**
  63. * xprt_rdma_bc_setup - Pre-allocate resources for handling backchannel requests
  64. * @xprt: transport associated with these backchannel resources
  65. * @reqs: number of concurrent incoming requests to expect
  66. *
  67. * Returns 0 on success; otherwise a negative errno
  68. */
  69. int xprt_rdma_bc_setup(struct rpc_xprt *xprt, unsigned int reqs)
  70. {
  71. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  72. int rc;
  73. /* The backchannel reply path returns each rpc_rqst to the
  74. * bc_pa_list _after_ the reply is sent. If the server is
  75. * faster than the client, it can send another backward
  76. * direction request before the rpc_rqst is returned to the
  77. * list. The client rejects the request in this case.
  78. *
  79. * Twice as many rpc_rqsts are prepared to ensure there is
  80. * always an rpc_rqst available as soon as a reply is sent.
  81. */
  82. if (reqs > RPCRDMA_BACKWARD_WRS >> 1)
  83. goto out_err;
  84. rc = rpcrdma_bc_setup_reqs(r_xprt, reqs);
  85. if (rc)
  86. goto out_free;
  87. r_xprt->rx_buf.rb_bc_srv_max_requests = reqs;
  88. request_module("svcrdma");
  89. trace_xprtrdma_cb_setup(r_xprt, reqs);
  90. return 0;
  91. out_free:
  92. xprt_rdma_bc_destroy(xprt, reqs);
  93. out_err:
  94. pr_err("RPC: %s: setup backchannel transport failed\n", __func__);
  95. return -ENOMEM;
  96. }
  97. /**
  98. * xprt_rdma_bc_up - Create transport endpoint for backchannel service
  99. * @serv: server endpoint
  100. * @net: network namespace
  101. *
  102. * The "xprt" is an implied argument: it supplies the name of the
  103. * backchannel transport class.
  104. *
  105. * Returns zero on success, negative errno on failure
  106. */
  107. int xprt_rdma_bc_up(struct svc_serv *serv, struct net *net)
  108. {
  109. int ret;
  110. ret = svc_create_xprt(serv, "rdma-bc", net, PF_INET, 0, 0);
  111. if (ret < 0)
  112. return ret;
  113. return 0;
  114. }
  115. /**
  116. * xprt_rdma_bc_maxpayload - Return maximum backchannel message size
  117. * @xprt: transport
  118. *
  119. * Returns maximum size, in bytes, of a backchannel message
  120. */
  121. size_t xprt_rdma_bc_maxpayload(struct rpc_xprt *xprt)
  122. {
  123. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  124. struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
  125. size_t maxmsg;
  126. maxmsg = min_t(unsigned int, cdata->inline_rsize, cdata->inline_wsize);
  127. maxmsg = min_t(unsigned int, maxmsg, PAGE_SIZE);
  128. return maxmsg - RPCRDMA_HDRLEN_MIN;
  129. }
  130. static int rpcrdma_bc_marshal_reply(struct rpc_rqst *rqst)
  131. {
  132. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
  133. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  134. __be32 *p;
  135. rpcrdma_set_xdrlen(&req->rl_hdrbuf, 0);
  136. xdr_init_encode(&req->rl_stream, &req->rl_hdrbuf,
  137. req->rl_rdmabuf->rg_base);
  138. p = xdr_reserve_space(&req->rl_stream, 28);
  139. if (unlikely(!p))
  140. return -EIO;
  141. *p++ = rqst->rq_xid;
  142. *p++ = rpcrdma_version;
  143. *p++ = cpu_to_be32(r_xprt->rx_buf.rb_bc_srv_max_requests);
  144. *p++ = rdma_msg;
  145. *p++ = xdr_zero;
  146. *p++ = xdr_zero;
  147. *p = xdr_zero;
  148. if (rpcrdma_prepare_send_sges(r_xprt, req, RPCRDMA_HDRLEN_MIN,
  149. &rqst->rq_snd_buf, rpcrdma_noch))
  150. return -EIO;
  151. trace_xprtrdma_cb_reply(rqst);
  152. return 0;
  153. }
  154. /**
  155. * xprt_rdma_bc_send_reply - marshal and send a backchannel reply
  156. * @rqst: RPC rqst with a backchannel RPC reply in rq_snd_buf
  157. *
  158. * Caller holds the transport's write lock.
  159. *
  160. * Returns:
  161. * %0 if the RPC message has been sent
  162. * %-ENOTCONN if the caller should reconnect and call again
  163. * %-EIO if a permanent error occurred and the request was not
  164. * sent. Do not try to send this message again.
  165. */
  166. int xprt_rdma_bc_send_reply(struct rpc_rqst *rqst)
  167. {
  168. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
  169. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  170. int rc;
  171. if (!xprt_connected(rqst->rq_xprt))
  172. goto drop_connection;
  173. rc = rpcrdma_bc_marshal_reply(rqst);
  174. if (rc < 0)
  175. goto failed_marshal;
  176. rpcrdma_post_recvs(r_xprt, true);
  177. if (rpcrdma_ep_post(&r_xprt->rx_ia, &r_xprt->rx_ep, req))
  178. goto drop_connection;
  179. return 0;
  180. failed_marshal:
  181. if (rc != -ENOTCONN)
  182. return rc;
  183. drop_connection:
  184. xprt_disconnect_done(rqst->rq_xprt);
  185. return -ENOTCONN;
  186. }
  187. /**
  188. * xprt_rdma_bc_destroy - Release resources for handling backchannel requests
  189. * @xprt: transport associated with these backchannel resources
  190. * @reqs: number of incoming requests to destroy; ignored
  191. */
  192. void xprt_rdma_bc_destroy(struct rpc_xprt *xprt, unsigned int reqs)
  193. {
  194. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  195. struct rpc_rqst *rqst, *tmp;
  196. spin_lock_bh(&xprt->bc_pa_lock);
  197. list_for_each_entry_safe(rqst, tmp, &xprt->bc_pa_list, rq_bc_pa_list) {
  198. list_del(&rqst->rq_bc_pa_list);
  199. spin_unlock_bh(&xprt->bc_pa_lock);
  200. rpcrdma_bc_free_rqst(r_xprt, rqst);
  201. spin_lock_bh(&xprt->bc_pa_lock);
  202. }
  203. spin_unlock_bh(&xprt->bc_pa_lock);
  204. }
  205. /**
  206. * xprt_rdma_bc_free_rqst - Release a backchannel rqst
  207. * @rqst: request to release
  208. */
  209. void xprt_rdma_bc_free_rqst(struct rpc_rqst *rqst)
  210. {
  211. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  212. struct rpc_xprt *xprt = rqst->rq_xprt;
  213. dprintk("RPC: %s: freeing rqst %p (req %p)\n",
  214. __func__, rqst, req);
  215. rpcrdma_recv_buffer_put(req->rl_reply);
  216. req->rl_reply = NULL;
  217. spin_lock_bh(&xprt->bc_pa_lock);
  218. list_add_tail(&rqst->rq_bc_pa_list, &xprt->bc_pa_list);
  219. spin_unlock_bh(&xprt->bc_pa_lock);
  220. }
  221. /**
  222. * rpcrdma_bc_receive_call - Handle a backward direction call
  223. * @r_xprt: transport receiving the call
  224. * @rep: receive buffer containing the call
  225. *
  226. * Operational assumptions:
  227. * o Backchannel credits are ignored, just as the NFS server
  228. * forechannel currently does
  229. * o The ULP manages a replay cache (eg, NFSv4.1 sessions).
  230. * No replay detection is done at the transport level
  231. */
  232. void rpcrdma_bc_receive_call(struct rpcrdma_xprt *r_xprt,
  233. struct rpcrdma_rep *rep)
  234. {
  235. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  236. struct svc_serv *bc_serv;
  237. struct rpcrdma_req *req;
  238. struct rpc_rqst *rqst;
  239. struct xdr_buf *buf;
  240. size_t size;
  241. __be32 *p;
  242. p = xdr_inline_decode(&rep->rr_stream, 0);
  243. size = xdr_stream_remaining(&rep->rr_stream);
  244. #ifdef RPCRDMA_BACKCHANNEL_DEBUG
  245. pr_info("RPC: %s: callback XID %08x, length=%u\n",
  246. __func__, be32_to_cpup(p), size);
  247. pr_info("RPC: %s: %*ph\n", __func__, size, p);
  248. #endif
  249. /* Grab a free bc rqst */
  250. spin_lock(&xprt->bc_pa_lock);
  251. if (list_empty(&xprt->bc_pa_list)) {
  252. spin_unlock(&xprt->bc_pa_lock);
  253. goto out_overflow;
  254. }
  255. rqst = list_first_entry(&xprt->bc_pa_list,
  256. struct rpc_rqst, rq_bc_pa_list);
  257. list_del(&rqst->rq_bc_pa_list);
  258. spin_unlock(&xprt->bc_pa_lock);
  259. /* Prepare rqst */
  260. rqst->rq_reply_bytes_recvd = 0;
  261. rqst->rq_bytes_sent = 0;
  262. rqst->rq_xid = *p;
  263. rqst->rq_private_buf.len = size;
  264. buf = &rqst->rq_rcv_buf;
  265. memset(buf, 0, sizeof(*buf));
  266. buf->head[0].iov_base = p;
  267. buf->head[0].iov_len = size;
  268. buf->len = size;
  269. /* The receive buffer has to be hooked to the rpcrdma_req
  270. * so that it is not released while the req is pointing
  271. * to its buffer, and so that it can be reposted after
  272. * the Upper Layer is done decoding it.
  273. */
  274. req = rpcr_to_rdmar(rqst);
  275. req->rl_reply = rep;
  276. trace_xprtrdma_cb_call(rqst);
  277. /* Queue rqst for ULP's callback service */
  278. bc_serv = xprt->bc_serv;
  279. spin_lock(&bc_serv->sv_cb_lock);
  280. list_add(&rqst->rq_bc_list, &bc_serv->sv_cb_list);
  281. spin_unlock(&bc_serv->sv_cb_lock);
  282. wake_up(&bc_serv->sv_cb_waitq);
  283. r_xprt->rx_stats.bcall_count++;
  284. return;
  285. out_overflow:
  286. pr_warn("RPC/RDMA backchannel overflow\n");
  287. xprt_disconnect_done(xprt);
  288. /* This receive buffer gets reposted automatically
  289. * when the connection is re-established.
  290. */
  291. return;
  292. }