backchannel.c 9.1 KB

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