backchannel.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 "xprt_rdma.h"
  12. #include <trace/events/rpcrdma.h>
  13. #undef RPCRDMA_BACKCHANNEL_DEBUG
  14. /**
  15. * xprt_rdma_bc_setup - Pre-allocate resources for handling backchannel requests
  16. * @xprt: transport associated with these backchannel resources
  17. * @reqs: number of concurrent incoming requests to expect
  18. *
  19. * Returns 0 on success; otherwise a negative errno
  20. */
  21. int xprt_rdma_bc_setup(struct rpc_xprt *xprt, unsigned int reqs)
  22. {
  23. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  24. r_xprt->rx_buf.rb_bc_srv_max_requests = RPCRDMA_BACKWARD_WRS >> 1;
  25. trace_xprtrdma_cb_setup(r_xprt, reqs);
  26. return 0;
  27. }
  28. /**
  29. * xprt_rdma_bc_maxpayload - Return maximum backchannel message size
  30. * @xprt: transport
  31. *
  32. * Returns maximum size, in bytes, of a backchannel message
  33. */
  34. size_t xprt_rdma_bc_maxpayload(struct rpc_xprt *xprt)
  35. {
  36. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  37. struct rpcrdma_ep *ep = r_xprt->rx_ep;
  38. size_t maxmsg;
  39. maxmsg = min_t(unsigned int, ep->re_inline_send, ep->re_inline_recv);
  40. maxmsg = min_t(unsigned int, maxmsg, PAGE_SIZE);
  41. return maxmsg - RPCRDMA_HDRLEN_MIN;
  42. }
  43. unsigned int xprt_rdma_bc_max_slots(struct rpc_xprt *xprt)
  44. {
  45. return RPCRDMA_BACKWARD_WRS >> 1;
  46. }
  47. static int rpcrdma_bc_marshal_reply(struct rpc_rqst *rqst)
  48. {
  49. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
  50. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  51. __be32 *p;
  52. rpcrdma_set_xdrlen(&req->rl_hdrbuf, 0);
  53. xdr_init_encode(&req->rl_stream, &req->rl_hdrbuf,
  54. rdmab_data(req->rl_rdmabuf), rqst);
  55. p = xdr_reserve_space(&req->rl_stream, 28);
  56. if (unlikely(!p))
  57. return -EIO;
  58. *p++ = rqst->rq_xid;
  59. *p++ = rpcrdma_version;
  60. *p++ = cpu_to_be32(r_xprt->rx_buf.rb_bc_srv_max_requests);
  61. *p++ = rdma_msg;
  62. *p++ = xdr_zero;
  63. *p++ = xdr_zero;
  64. *p = xdr_zero;
  65. if (rpcrdma_prepare_send_sges(r_xprt, req, RPCRDMA_HDRLEN_MIN,
  66. &rqst->rq_snd_buf, rpcrdma_noch_pullup))
  67. return -EIO;
  68. trace_xprtrdma_cb_reply(r_xprt, rqst);
  69. return 0;
  70. }
  71. /**
  72. * xprt_rdma_bc_send_reply - marshal and send a backchannel reply
  73. * @rqst: RPC rqst with a backchannel RPC reply in rq_snd_buf
  74. *
  75. * Caller holds the transport's write lock.
  76. *
  77. * Returns:
  78. * %0 if the RPC message has been sent
  79. * %-ENOTCONN if the caller should reconnect and call again
  80. * %-EIO if a permanent error occurred and the request was not
  81. * sent. Do not try to send this message again.
  82. */
  83. int xprt_rdma_bc_send_reply(struct rpc_rqst *rqst)
  84. {
  85. struct rpc_xprt *xprt = rqst->rq_xprt;
  86. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  87. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  88. int rc;
  89. if (!xprt_connected(xprt))
  90. return -ENOTCONN;
  91. if (!xprt_request_get_cong(xprt, rqst))
  92. return -EBADSLT;
  93. rc = rpcrdma_bc_marshal_reply(rqst);
  94. if (rc < 0)
  95. goto failed_marshal;
  96. if (frwr_send(r_xprt, req))
  97. goto drop_connection;
  98. return 0;
  99. failed_marshal:
  100. if (rc != -ENOTCONN)
  101. return rc;
  102. drop_connection:
  103. xprt_rdma_close(xprt);
  104. return -ENOTCONN;
  105. }
  106. /**
  107. * xprt_rdma_bc_destroy - Release resources for handling backchannel requests
  108. * @xprt: transport associated with these backchannel resources
  109. * @reqs: number of incoming requests to destroy; ignored
  110. */
  111. void xprt_rdma_bc_destroy(struct rpc_xprt *xprt, unsigned int reqs)
  112. {
  113. struct rpc_rqst *rqst, *tmp;
  114. spin_lock(&xprt->bc_pa_lock);
  115. list_for_each_entry_safe(rqst, tmp, &xprt->bc_pa_list, rq_bc_pa_list) {
  116. list_del(&rqst->rq_bc_pa_list);
  117. spin_unlock(&xprt->bc_pa_lock);
  118. rpcrdma_req_destroy(rpcr_to_rdmar(rqst));
  119. spin_lock(&xprt->bc_pa_lock);
  120. }
  121. spin_unlock(&xprt->bc_pa_lock);
  122. }
  123. /**
  124. * xprt_rdma_bc_free_rqst - Release a backchannel rqst
  125. * @rqst: request to release
  126. */
  127. void xprt_rdma_bc_free_rqst(struct rpc_rqst *rqst)
  128. {
  129. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  130. struct rpcrdma_rep *rep = req->rl_reply;
  131. struct rpc_xprt *xprt = rqst->rq_xprt;
  132. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  133. rpcrdma_rep_put(&r_xprt->rx_buf, rep);
  134. req->rl_reply = NULL;
  135. spin_lock(&xprt->bc_pa_lock);
  136. list_add_tail(&rqst->rq_bc_pa_list, &xprt->bc_pa_list);
  137. spin_unlock(&xprt->bc_pa_lock);
  138. xprt_put(xprt);
  139. }
  140. static struct rpc_rqst *rpcrdma_bc_rqst_get(struct rpcrdma_xprt *r_xprt)
  141. {
  142. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  143. struct rpcrdma_req *req;
  144. struct rpc_rqst *rqst;
  145. size_t size;
  146. spin_lock(&xprt->bc_pa_lock);
  147. rqst = list_first_entry_or_null(&xprt->bc_pa_list, struct rpc_rqst,
  148. rq_bc_pa_list);
  149. if (!rqst)
  150. goto create_req;
  151. list_del(&rqst->rq_bc_pa_list);
  152. spin_unlock(&xprt->bc_pa_lock);
  153. return rqst;
  154. create_req:
  155. spin_unlock(&xprt->bc_pa_lock);
  156. /* Set a limit to prevent a remote from overrunning our resources.
  157. */
  158. if (xprt->bc_alloc_count >= RPCRDMA_BACKWARD_WRS)
  159. return NULL;
  160. size = min_t(size_t, r_xprt->rx_ep->re_inline_recv, PAGE_SIZE);
  161. req = rpcrdma_req_create(r_xprt, size);
  162. if (!req)
  163. return NULL;
  164. if (rpcrdma_req_setup(r_xprt, req)) {
  165. rpcrdma_req_destroy(req);
  166. return NULL;
  167. }
  168. xprt->bc_alloc_count++;
  169. rqst = &req->rl_slot;
  170. rqst->rq_xprt = xprt;
  171. __set_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state);
  172. xdr_buf_init(&rqst->rq_snd_buf, rdmab_data(req->rl_sendbuf), size);
  173. return rqst;
  174. }
  175. /**
  176. * rpcrdma_bc_receive_call - Handle a reverse-direction Call
  177. * @r_xprt: transport receiving the call
  178. * @rep: receive buffer containing the call
  179. *
  180. * Operational assumptions:
  181. * o Backchannel credits are ignored, just as the NFS server
  182. * forechannel currently does
  183. * o The ULP manages a replay cache (eg, NFSv4.1 sessions).
  184. * No replay detection is done at the transport level
  185. */
  186. void rpcrdma_bc_receive_call(struct rpcrdma_xprt *r_xprt,
  187. struct rpcrdma_rep *rep)
  188. {
  189. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  190. struct svc_serv *bc_serv;
  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. bc_serv = xprt->bc_serv;
  224. xprt_get(xprt);
  225. spin_lock(&bc_serv->sv_cb_lock);
  226. list_add(&rqst->rq_bc_list, &bc_serv->sv_cb_list);
  227. spin_unlock(&bc_serv->sv_cb_lock);
  228. wake_up(&bc_serv->sv_cb_waitq);
  229. r_xprt->rx_stats.bcall_count++;
  230. return;
  231. out_overflow:
  232. pr_warn("RPC/RDMA backchannel overflow\n");
  233. xprt_force_disconnect(xprt);
  234. /* This receive buffer gets reposted automatically
  235. * when the connection is re-established.
  236. */
  237. return;
  238. }