backchannel.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Copyright (c) 2015 Oracle. All rights reserved.
  3. *
  4. * Support for backward direction RPCs on RPC/RDMA.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/sunrpc/xprt.h>
  8. #include <linux/sunrpc/svc.h>
  9. #include <linux/sunrpc/svc_xprt.h>
  10. #include "xprt_rdma.h"
  11. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  12. # define RPCDBG_FACILITY RPCDBG_TRANS
  13. #endif
  14. #undef RPCRDMA_BACKCHANNEL_DEBUG
  15. static void rpcrdma_bc_free_rqst(struct rpcrdma_xprt *r_xprt,
  16. struct rpc_rqst *rqst)
  17. {
  18. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  19. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  20. spin_lock(&buf->rb_reqslock);
  21. list_del(&req->rl_all);
  22. spin_unlock(&buf->rb_reqslock);
  23. rpcrdma_destroy_req(req);
  24. kfree(rqst);
  25. }
  26. static int rpcrdma_bc_setup_rqst(struct rpcrdma_xprt *r_xprt,
  27. struct rpc_rqst *rqst)
  28. {
  29. struct rpcrdma_regbuf *rb;
  30. struct rpcrdma_req *req;
  31. size_t size;
  32. req = rpcrdma_create_req(r_xprt);
  33. if (IS_ERR(req))
  34. return PTR_ERR(req);
  35. req->rl_backchannel = true;
  36. rb = rpcrdma_alloc_regbuf(RPCRDMA_HDRBUF_SIZE,
  37. DMA_TO_DEVICE, GFP_KERNEL);
  38. if (IS_ERR(rb))
  39. goto out_fail;
  40. req->rl_rdmabuf = rb;
  41. size = r_xprt->rx_data.inline_rsize;
  42. rb = rpcrdma_alloc_regbuf(size, DMA_TO_DEVICE, GFP_KERNEL);
  43. if (IS_ERR(rb))
  44. goto out_fail;
  45. req->rl_sendbuf = rb;
  46. xdr_buf_init(&rqst->rq_snd_buf, rb->rg_base, size);
  47. rpcrdma_set_xprtdata(rqst, req);
  48. return 0;
  49. out_fail:
  50. rpcrdma_bc_free_rqst(r_xprt, rqst);
  51. return -ENOMEM;
  52. }
  53. /* Allocate and add receive buffers to the rpcrdma_buffer's
  54. * existing list of rep's. These are released when the
  55. * transport is destroyed.
  56. */
  57. static int rpcrdma_bc_setup_reps(struct rpcrdma_xprt *r_xprt,
  58. unsigned int count)
  59. {
  60. struct rpcrdma_rep *rep;
  61. int rc = 0;
  62. while (count--) {
  63. rep = rpcrdma_create_rep(r_xprt);
  64. if (IS_ERR(rep)) {
  65. pr_err("RPC: %s: reply buffer alloc failed\n",
  66. __func__);
  67. rc = PTR_ERR(rep);
  68. break;
  69. }
  70. rpcrdma_recv_buffer_put(rep);
  71. }
  72. return rc;
  73. }
  74. /**
  75. * xprt_rdma_bc_setup - Pre-allocate resources for handling backchannel requests
  76. * @xprt: transport associated with these backchannel resources
  77. * @reqs: number of concurrent incoming requests to expect
  78. *
  79. * Returns 0 on success; otherwise a negative errno
  80. */
  81. int xprt_rdma_bc_setup(struct rpc_xprt *xprt, unsigned int reqs)
  82. {
  83. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  84. struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
  85. struct rpc_rqst *rqst;
  86. unsigned int i;
  87. int rc;
  88. /* The backchannel reply path returns each rpc_rqst to the
  89. * bc_pa_list _after_ the reply is sent. If the server is
  90. * faster than the client, it can send another backward
  91. * direction request before the rpc_rqst is returned to the
  92. * list. The client rejects the request in this case.
  93. *
  94. * Twice as many rpc_rqsts are prepared to ensure there is
  95. * always an rpc_rqst available as soon as a reply is sent.
  96. */
  97. if (reqs > RPCRDMA_BACKWARD_WRS >> 1)
  98. goto out_err;
  99. for (i = 0; i < (reqs << 1); i++) {
  100. rqst = kzalloc(sizeof(*rqst), GFP_KERNEL);
  101. if (!rqst) {
  102. pr_err("RPC: %s: Failed to create bc rpc_rqst\n",
  103. __func__);
  104. goto out_free;
  105. }
  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. return maxmsg - RPCRDMA_HDRLEN_MIN;
  162. }
  163. /**
  164. * rpcrdma_bc_marshal_reply - Send backwards direction reply
  165. * @rqst: buffer containing RPC reply data
  166. *
  167. * Returns zero on success.
  168. */
  169. int rpcrdma_bc_marshal_reply(struct rpc_rqst *rqst)
  170. {
  171. struct rpc_xprt *xprt = rqst->rq_xprt;
  172. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  173. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  174. struct rpcrdma_msg *headerp;
  175. headerp = rdmab_to_msg(req->rl_rdmabuf);
  176. headerp->rm_xid = rqst->rq_xid;
  177. headerp->rm_vers = rpcrdma_version;
  178. headerp->rm_credit =
  179. cpu_to_be32(r_xprt->rx_buf.rb_bc_srv_max_requests);
  180. headerp->rm_type = rdma_msg;
  181. headerp->rm_body.rm_chunks[0] = xdr_zero;
  182. headerp->rm_body.rm_chunks[1] = xdr_zero;
  183. headerp->rm_body.rm_chunks[2] = xdr_zero;
  184. if (!rpcrdma_prepare_send_sges(&r_xprt->rx_ia, req, RPCRDMA_HDRLEN_MIN,
  185. &rqst->rq_snd_buf, rpcrdma_noch))
  186. return -EIO;
  187. return 0;
  188. }
  189. /**
  190. * xprt_rdma_bc_destroy - Release resources for handling backchannel requests
  191. * @xprt: transport associated with these backchannel resources
  192. * @reqs: number of incoming requests to destroy; ignored
  193. */
  194. void xprt_rdma_bc_destroy(struct rpc_xprt *xprt, unsigned int reqs)
  195. {
  196. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  197. struct rpc_rqst *rqst, *tmp;
  198. spin_lock_bh(&xprt->bc_pa_lock);
  199. list_for_each_entry_safe(rqst, tmp, &xprt->bc_pa_list, rq_bc_pa_list) {
  200. list_del(&rqst->rq_bc_pa_list);
  201. spin_unlock_bh(&xprt->bc_pa_lock);
  202. rpcrdma_bc_free_rqst(r_xprt, rqst);
  203. spin_lock_bh(&xprt->bc_pa_lock);
  204. }
  205. spin_unlock_bh(&xprt->bc_pa_lock);
  206. }
  207. /**
  208. * xprt_rdma_bc_free_rqst - Release a backchannel rqst
  209. * @rqst: request to release
  210. */
  211. void xprt_rdma_bc_free_rqst(struct rpc_rqst *rqst)
  212. {
  213. struct rpc_xprt *xprt = rqst->rq_xprt;
  214. dprintk("RPC: %s: freeing rqst %p (req %p)\n",
  215. __func__, rqst, rpcr_to_rdmar(rqst));
  216. smp_mb__before_atomic();
  217. WARN_ON_ONCE(!test_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state));
  218. clear_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state);
  219. smp_mb__after_atomic();
  220. spin_lock_bh(&xprt->bc_pa_lock);
  221. list_add_tail(&rqst->rq_bc_pa_list, &xprt->bc_pa_list);
  222. spin_unlock_bh(&xprt->bc_pa_lock);
  223. }
  224. /**
  225. * rpcrdma_bc_receive_call - Handle a backward direction call
  226. * @xprt: transport receiving the call
  227. * @rep: receive buffer containing the call
  228. *
  229. * Called in the RPC reply handler, which runs in a tasklet.
  230. * Be quick about it.
  231. *
  232. * Operational assumptions:
  233. * o Backchannel credits are ignored, just as the NFS server
  234. * forechannel currently does
  235. * o The ULP manages a replay cache (eg, NFSv4.1 sessions).
  236. * No replay detection is done at the transport level
  237. */
  238. void rpcrdma_bc_receive_call(struct rpcrdma_xprt *r_xprt,
  239. struct rpcrdma_rep *rep)
  240. {
  241. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  242. struct rpcrdma_msg *headerp;
  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. headerp = rdmab_to_msg(rep->rr_rdmabuf);
  250. #ifdef RPCRDMA_BACKCHANNEL_DEBUG
  251. pr_info("RPC: %s: callback XID %08x, length=%u\n",
  252. __func__, be32_to_cpu(headerp->rm_xid), rep->rr_len);
  253. pr_info("RPC: %s: %*ph\n", __func__, rep->rr_len, headerp);
  254. #endif
  255. /* Sanity check:
  256. * Need at least enough bytes for RPC/RDMA header, as code
  257. * here references the header fields by array offset. Also,
  258. * backward calls are always inline, so ensure there
  259. * are some bytes beyond the RPC/RDMA header.
  260. */
  261. if (rep->rr_len < RPCRDMA_HDRLEN_MIN + 24)
  262. goto out_short;
  263. p = (__be32 *)((unsigned char *)headerp + RPCRDMA_HDRLEN_MIN);
  264. size = rep->rr_len - RPCRDMA_HDRLEN_MIN;
  265. /* Grab a free bc rqst */
  266. spin_lock(&xprt->bc_pa_lock);
  267. if (list_empty(&xprt->bc_pa_list)) {
  268. spin_unlock(&xprt->bc_pa_lock);
  269. goto out_overflow;
  270. }
  271. rqst = list_first_entry(&xprt->bc_pa_list,
  272. struct rpc_rqst, rq_bc_pa_list);
  273. list_del(&rqst->rq_bc_pa_list);
  274. spin_unlock(&xprt->bc_pa_lock);
  275. dprintk("RPC: %s: using rqst %p\n", __func__, rqst);
  276. /* Prepare rqst */
  277. rqst->rq_reply_bytes_recvd = 0;
  278. rqst->rq_bytes_sent = 0;
  279. rqst->rq_xid = headerp->rm_xid;
  280. rqst->rq_private_buf.len = size;
  281. set_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state);
  282. buf = &rqst->rq_rcv_buf;
  283. memset(buf, 0, sizeof(*buf));
  284. buf->head[0].iov_base = p;
  285. buf->head[0].iov_len = size;
  286. buf->len = size;
  287. /* The receive buffer has to be hooked to the rpcrdma_req
  288. * so that it can be reposted after the server is done
  289. * parsing it but just before sending the backward
  290. * direction reply.
  291. */
  292. req = rpcr_to_rdmar(rqst);
  293. dprintk("RPC: %s: attaching rep %p to req %p\n",
  294. __func__, rep, req);
  295. req->rl_reply = rep;
  296. /* Defeat the retransmit detection logic in send_request */
  297. req->rl_connect_cookie = 0;
  298. /* Queue rqst for ULP's callback service */
  299. bc_serv = xprt->bc_serv;
  300. spin_lock(&bc_serv->sv_cb_lock);
  301. list_add(&rqst->rq_bc_list, &bc_serv->sv_cb_list);
  302. spin_unlock(&bc_serv->sv_cb_lock);
  303. wake_up(&bc_serv->sv_cb_waitq);
  304. r_xprt->rx_stats.bcall_count++;
  305. return;
  306. out_overflow:
  307. pr_warn("RPC/RDMA backchannel overflow\n");
  308. xprt_disconnect_done(xprt);
  309. /* This receive buffer gets reposted automatically
  310. * when the connection is re-established.
  311. */
  312. return;
  313. out_short:
  314. pr_warn("RPC/RDMA short backward direction call\n");
  315. if (rpcrdma_ep_post_recv(&r_xprt->rx_ia, rep))
  316. xprt_disconnect_done(xprt);
  317. else
  318. pr_warn("RPC: %s: reposting rep %p\n",
  319. __func__, rep);
  320. }