backchannel.c 9.3 KB

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