backchannel.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2015-2020, Oracle and/or its affiliates.
  4. *
  5. * Support for reverse-direction RPCs on RPC/RDMA.
  6. */
  7. #include <linux/sunrpc/xprt.h>
  8. #include <linux/sunrpc/svc.h>
  9. #include <linux/sunrpc/svc_xprt.h>
  10. #include <linux/sunrpc/svc_rdma.h>
  11. #include <linux/sunrpc/bc_xprt.h>
  12. #include "xprt_rdma.h"
  13. #include <trace/events/rpcrdma.h>
  14. #undef RPCRDMA_BACKCHANNEL_DEBUG
  15. /**
  16. * xprt_rdma_bc_setup - Pre-allocate resources for handling backchannel requests
  17. * @xprt: transport associated with these backchannel resources
  18. * @reqs: number of concurrent incoming requests to expect
  19. *
  20. * Returns 0 on success; otherwise a negative errno
  21. */
  22. int xprt_rdma_bc_setup(struct rpc_xprt *xprt, unsigned int reqs)
  23. {
  24. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  25. r_xprt->rx_buf.rb_bc_srv_max_requests = RPCRDMA_BACKWARD_WRS >> 1;
  26. trace_xprtrdma_cb_setup(r_xprt, reqs);
  27. return 0;
  28. }
  29. /**
  30. * xprt_rdma_bc_maxpayload - Return maximum backchannel message size
  31. * @xprt: transport
  32. *
  33. * Returns maximum size, in bytes, of a backchannel message
  34. */
  35. size_t xprt_rdma_bc_maxpayload(struct rpc_xprt *xprt)
  36. {
  37. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  38. struct rpcrdma_ep *ep = r_xprt->rx_ep;
  39. size_t maxmsg;
  40. maxmsg = min_t(unsigned int, ep->re_inline_send, ep->re_inline_recv);
  41. maxmsg = min_t(unsigned int, maxmsg, PAGE_SIZE);
  42. return maxmsg - RPCRDMA_HDRLEN_MIN;
  43. }
  44. unsigned int xprt_rdma_bc_max_slots(struct rpc_xprt *xprt)
  45. {
  46. return RPCRDMA_BACKWARD_WRS >> 1;
  47. }
  48. static int rpcrdma_bc_marshal_reply(struct rpc_rqst *rqst)
  49. {
  50. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
  51. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  52. __be32 *p;
  53. rpcrdma_set_xdrlen(&req->rl_hdrbuf, 0);
  54. xdr_init_encode(&req->rl_stream, &req->rl_hdrbuf,
  55. rdmab_data(req->rl_rdmabuf), rqst);
  56. p = xdr_reserve_space(&req->rl_stream, 28);
  57. if (unlikely(!p))
  58. return -EIO;
  59. *p++ = rqst->rq_xid;
  60. *p++ = rpcrdma_version;
  61. *p++ = cpu_to_be32(r_xprt->rx_buf.rb_bc_srv_max_requests);
  62. *p++ = rdma_msg;
  63. *p++ = xdr_zero;
  64. *p++ = xdr_zero;
  65. *p = xdr_zero;
  66. if (rpcrdma_prepare_send_sges(r_xprt, req, RPCRDMA_HDRLEN_MIN,
  67. &rqst->rq_snd_buf, rpcrdma_noch_pullup))
  68. return -EIO;
  69. trace_xprtrdma_cb_reply(r_xprt, rqst);
  70. return 0;
  71. }
  72. /**
  73. * xprt_rdma_bc_send_reply - marshal and send a backchannel reply
  74. * @rqst: RPC rqst with a backchannel RPC reply in rq_snd_buf
  75. *
  76. * Caller holds the transport's write lock.
  77. *
  78. * Returns:
  79. * %0 if the RPC message has been sent
  80. * %-ENOTCONN if the caller should reconnect and call again
  81. * %-EIO if a permanent error occurred and the request was not
  82. * sent. Do not try to send this message again.
  83. */
  84. int xprt_rdma_bc_send_reply(struct rpc_rqst *rqst)
  85. {
  86. struct rpc_xprt *xprt = rqst->rq_xprt;
  87. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  88. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  89. int rc;
  90. if (!xprt_connected(xprt))
  91. return -ENOTCONN;
  92. if (!xprt_request_get_cong(xprt, rqst))
  93. return -EBADSLT;
  94. rc = rpcrdma_bc_marshal_reply(rqst);
  95. if (rc < 0)
  96. goto failed_marshal;
  97. if (frwr_send(r_xprt, req))
  98. goto drop_connection;
  99. return 0;
  100. failed_marshal:
  101. if (rc != -ENOTCONN)
  102. return rc;
  103. drop_connection:
  104. xprt_rdma_close(xprt);
  105. return -ENOTCONN;
  106. }
  107. /**
  108. * xprt_rdma_bc_destroy - Release resources for handling backchannel requests
  109. * @xprt: transport associated with these backchannel resources
  110. * @reqs: number of incoming requests to destroy; ignored
  111. */
  112. void xprt_rdma_bc_destroy(struct rpc_xprt *xprt, unsigned int reqs)
  113. {
  114. struct rpc_rqst *rqst, *tmp;
  115. spin_lock(&xprt->bc_pa_lock);
  116. list_for_each_entry_safe(rqst, tmp, &xprt->bc_pa_list, rq_bc_pa_list) {
  117. list_del(&rqst->rq_bc_pa_list);
  118. spin_unlock(&xprt->bc_pa_lock);
  119. rpcrdma_req_destroy(rpcr_to_rdmar(rqst));
  120. spin_lock(&xprt->bc_pa_lock);
  121. }
  122. spin_unlock(&xprt->bc_pa_lock);
  123. }
  124. /**
  125. * xprt_rdma_bc_free_rqst - Release a backchannel rqst
  126. * @rqst: request to release
  127. */
  128. void xprt_rdma_bc_free_rqst(struct rpc_rqst *rqst)
  129. {
  130. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  131. struct rpcrdma_rep *rep = req->rl_reply;
  132. struct rpc_xprt *xprt = rqst->rq_xprt;
  133. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  134. rpcrdma_rep_put(&r_xprt->rx_buf, rep);
  135. req->rl_reply = NULL;
  136. spin_lock(&xprt->bc_pa_lock);
  137. list_add_tail(&rqst->rq_bc_pa_list, &xprt->bc_pa_list);
  138. spin_unlock(&xprt->bc_pa_lock);
  139. xprt_put(xprt);
  140. }
  141. static struct rpc_rqst *rpcrdma_bc_rqst_get(struct rpcrdma_xprt *r_xprt)
  142. {
  143. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  144. struct rpcrdma_req *req;
  145. struct rpc_rqst *rqst;
  146. size_t size;
  147. spin_lock(&xprt->bc_pa_lock);
  148. rqst = list_first_entry_or_null(&xprt->bc_pa_list, struct rpc_rqst,
  149. rq_bc_pa_list);
  150. if (!rqst)
  151. goto create_req;
  152. list_del(&rqst->rq_bc_pa_list);
  153. spin_unlock(&xprt->bc_pa_lock);
  154. return rqst;
  155. create_req:
  156. spin_unlock(&xprt->bc_pa_lock);
  157. /* Set a limit to prevent a remote from overrunning our resources.
  158. */
  159. if (xprt->bc_alloc_count >= RPCRDMA_BACKWARD_WRS)
  160. return NULL;
  161. size = min_t(size_t, r_xprt->rx_ep->re_inline_recv, PAGE_SIZE);
  162. req = rpcrdma_req_create(r_xprt, size);
  163. if (!req)
  164. return NULL;
  165. if (rpcrdma_req_setup(r_xprt, req)) {
  166. rpcrdma_req_destroy(req);
  167. return NULL;
  168. }
  169. xprt->bc_alloc_count++;
  170. rqst = &req->rl_slot;
  171. rqst->rq_xprt = xprt;
  172. __set_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state);
  173. xdr_buf_init(&rqst->rq_snd_buf, rdmab_data(req->rl_sendbuf), size);
  174. return rqst;
  175. }
  176. /**
  177. * rpcrdma_bc_receive_call - Handle a reverse-direction Call
  178. * @r_xprt: transport receiving the call
  179. * @rep: receive buffer containing the call
  180. *
  181. * Operational assumptions:
  182. * o Backchannel credits are ignored, just as the NFS server
  183. * forechannel currently does
  184. * o The ULP manages a replay cache (eg, NFSv4.1 sessions).
  185. * No replay detection is done at the transport level
  186. */
  187. void rpcrdma_bc_receive_call(struct rpcrdma_xprt *r_xprt,
  188. struct rpcrdma_rep *rep)
  189. {
  190. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  191. struct rpcrdma_req *req;
  192. struct rpc_rqst *rqst;
  193. struct xdr_buf *buf;
  194. size_t size;
  195. __be32 *p;
  196. p = xdr_inline_decode(&rep->rr_stream, 0);
  197. size = xdr_stream_remaining(&rep->rr_stream);
  198. #ifdef RPCRDMA_BACKCHANNEL_DEBUG
  199. pr_info("RPC: %s: callback XID %08x, length=%u\n",
  200. __func__, be32_to_cpup(p), size);
  201. pr_info("RPC: %s: %*ph\n", __func__, size, p);
  202. #endif
  203. rqst = rpcrdma_bc_rqst_get(r_xprt);
  204. if (!rqst)
  205. goto out_overflow;
  206. rqst->rq_reply_bytes_recvd = 0;
  207. rqst->rq_xid = *p;
  208. rqst->rq_private_buf.len = size;
  209. buf = &rqst->rq_rcv_buf;
  210. memset(buf, 0, sizeof(*buf));
  211. buf->head[0].iov_base = p;
  212. buf->head[0].iov_len = size;
  213. buf->len = size;
  214. /* The receive buffer has to be hooked to the rpcrdma_req
  215. * so that it is not released while the req is pointing
  216. * to its buffer, and so that it can be reposted after
  217. * the Upper Layer is done decoding it.
  218. */
  219. req = rpcr_to_rdmar(rqst);
  220. req->rl_reply = rep;
  221. trace_xprtrdma_cb_call(r_xprt, rqst);
  222. /* Queue rqst for ULP's callback service */
  223. xprt_enqueue_bc_request(rqst);
  224. r_xprt->rx_stats.bcall_count++;
  225. return;
  226. out_overflow:
  227. pr_warn("RPC/RDMA backchannel overflow\n");
  228. xprt_force_disconnect(xprt);
  229. /* This receive buffer gets reposted automatically
  230. * when the connection is re-established.
  231. */
  232. return;
  233. }